DXGL r79 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r78‎ | r79 | r80 >
Date:01:46, 17 January 2012
Author:admin
Status:new
Tags:
Comment:
Draw using glDrawRangeElements. Still using deprecated generic vertex attributes though.
Fix GL texture memory leaks.
Fix compiler warnings.
Modified paths:
  • /ddraw/glDirectDrawSurface.cpp (modified) (history)
  • /ddraw/glDirectDrawSurface.h (modified) (history)
  • /ddraw/glExtensions.cpp (modified) (history)
  • /ddraw/glExtensions.h (modified) (history)
  • /ddraw/shadergen.cpp (modified) (history)
  • /ddraw/shadergen.h (modified) (history)
  • /ddraw/shaders.cpp (modified) (history)

Diff [purge]

Index: ddraw/glDirectDrawSurface.cpp
@@ -26,6 +26,9 @@
2727 #include "glDirectDrawClipper.h"
2828 #include "glutil.h"
2929
 30+BltVertex bltvertices[4];
 31+const GLushort bltindices[4] = {0,1,2,3};
 32+
3033 int swapinterval = 0;
3134 inline void SetSwap(int swap)
3235 {
@@ -128,6 +131,8 @@
129132 ZeroMemory(colorkey,4*sizeof(CKEY));
130133 bitmapinfo = (BITMAPINFO *)malloc(sizeof(BITMAPINFO)+(255*sizeof(RGBQUAD)));
131134 palette = NULL;
 135+ paltex = NULL;
 136+ texture = NULL;
132137 clipper = NULL;
133138 hdc = NULL;
134139 dds1 = NULL;
@@ -451,6 +456,7 @@
452457 if(dds3) dds3->Release();
453458 if(dds4) dds4->Release();
454459 if(paltex)glDeleteTextures(1,&paltex);
 460+ if(texture)glDeleteTextures(1,&texture);
455461 if(bitmapinfo) free(bitmapinfo);
456462 if(palette) palette->Release();
457463 if(backbuffer) backbuffer->Release();
@@ -611,15 +617,14 @@
612618 srcrect.bottom = ddsdSrc.dwHeight;
613619 }
614620 else srcrect = *lpSrcRect;
615 - GLfloat coords[8];
616 - coords[0] = (GLfloat)destrect.left * ((GLfloat)fakex/(GLfloat)ddsd.dwWidth);
617 - coords[1] = (GLfloat)destrect.right * ((GLfloat)fakex/(GLfloat)ddsd.dwWidth);
618 - coords[2] = (GLfloat)fakey-((GLfloat)destrect.top * ((GLfloat)fakey/(GLfloat)ddsd.dwHeight));
619 - coords[3] = (GLfloat)fakey-((GLfloat)destrect.bottom * ((GLfloat)fakey/(GLfloat)ddsd.dwHeight));
620 - coords[4] = (GLfloat)srcrect.left / (GLfloat)ddsdSrc.dwWidth;
621 - coords[5] = (GLfloat)srcrect.right / (GLfloat)ddsdSrc.dwWidth;
622 - coords[6] = (GLfloat)srcrect.top / (GLfloat)ddsdSrc.dwHeight;
623 - coords[7] = (GLfloat)srcrect.bottom / (GLfloat)ddsdSrc.dwHeight;
 621+ bltvertices[1].x = bltvertices[3].x = (GLfloat)destrect.left * ((GLfloat)fakex/(GLfloat)ddsd.dwWidth);
 622+ bltvertices[0].x = bltvertices[2].x = (GLfloat)destrect.right * ((GLfloat)fakex/(GLfloat)ddsd.dwWidth);
 623+ bltvertices[0].y = bltvertices[1].y = (GLfloat)fakey-((GLfloat)destrect.top * ((GLfloat)fakey/(GLfloat)ddsd.dwHeight));
 624+ bltvertices[2].y = bltvertices[3].y = (GLfloat)fakey-((GLfloat)destrect.bottom * ((GLfloat)fakey/(GLfloat)ddsd.dwHeight));
 625+ bltvertices[1].s = bltvertices[3].s = (GLfloat)srcrect.left / (GLfloat)ddsdSrc.dwWidth;
 626+ bltvertices[0].s = bltvertices[2].s = (GLfloat)srcrect.right / (GLfloat)ddsdSrc.dwWidth;
 627+ bltvertices[0].t = bltvertices[1].t = (GLfloat)srcrect.top / (GLfloat)ddsdSrc.dwHeight;
 628+ bltvertices[2].t = bltvertices[3].t = (GLfloat)srcrect.bottom / (GLfloat)ddsdSrc.dwHeight;
624629 glClear(GL_DEPTH_BUFFER_BIT);
625630 if(dwFlags & DDBLT_COLORFILL)
626631 {
@@ -629,23 +634,35 @@
630635 switch(ddInterface->GetBPP())
631636 {
632637 case 8:
633 - glColor3ub((GLubyte)lpDDBltFx->dwFillColor,(GLubyte)lpDDBltFx->dwFillColor,
634 - (GLubyte)lpDDBltFx->dwFillColor);
 638+ bltvertices[0].r = bltvertices[0].g = bltvertices[0].b =
 639+ bltvertices[1].r = bltvertices[1].g = bltvertices[1].b =
 640+ bltvertices[2].r = bltvertices[2].g = bltvertices[2].b =
 641+ bltvertices[3].r = bltvertices[3].g = bltvertices[3].b = (GLubyte)lpDDBltFx->dwFillColor;
635642 break;
636643 case 15:
637 - glColor3ub(_5to8bit((lpDDBltFx->dwFillColor>>10) & 31),
638 - _5to8bit((lpDDBltFx->dwFillColor>>5) & 31),
639 - _5to8bit(lpDDBltFx->dwFillColor & 31));
 644+ bltvertices[0].r = bltvertices[1].r = bltvertices[2].r = bltvertices[3].r =
 645+ _5to8bit((lpDDBltFx->dwFillColor>>10) & 31);
 646+ bltvertices[0].g = bltvertices[1].g = bltvertices[2].g = bltvertices[3].g =
 647+ _5to8bit((lpDDBltFx->dwFillColor>>5) & 31);
 648+ bltvertices[0].b = bltvertices[1].b = bltvertices[2].b = bltvertices[3].b =
 649+ _5to8bit(lpDDBltFx->dwFillColor & 31);
640650 break;
641651 case 16:
642 - glColor3ub(_5to8bit((lpDDBltFx->dwFillColor>>11) & 31),
643 - _6to8bit((lpDDBltFx->dwFillColor>>5) & 63),
644 - _5to8bit(lpDDBltFx->dwFillColor & 31));
 652+ bltvertices[0].r = bltvertices[1].r = bltvertices[2].r = bltvertices[3].r =
 653+ _5to8bit((lpDDBltFx->dwFillColor>>11) & 31);
 654+ bltvertices[0].g = bltvertices[1].g = bltvertices[2].g = bltvertices[3].g =
 655+ _6to8bit((lpDDBltFx->dwFillColor>>5) & 63);
 656+ bltvertices[0].b = bltvertices[1].b = bltvertices[2].b = bltvertices[3].b =
 657+ _5to8bit(lpDDBltFx->dwFillColor & 31);
645658 break;
646659 case 24:
647660 case 32:
648 - glColor3ub(((lpDDBltFx->dwFillColor>>16) & 255),
649 - ((lpDDBltFx->dwFillColor>>8) & 255),(lpDDBltFx->dwFillColor & 255));
 661+ bltvertices[0].r = bltvertices[1].r = bltvertices[2].r = bltvertices[3].r =
 662+ ((lpDDBltFx->dwFillColor>>16) & 255);
 663+ bltvertices[0].g = bltvertices[1].g = bltvertices[2].g = bltvertices[3].g =
 664+ ((lpDDBltFx->dwFillColor>>8) & 255);
 665+ bltvertices[0].b = bltvertices[1].b = bltvertices[2].b = bltvertices[3].b =
 666+ (lpDDBltFx->dwFillColor & 255);
650667 default:
651668 break;
652669 }
@@ -653,7 +670,6 @@
654671 else
655672 {
656673 glEnable(GL_TEXTURE_2D);
657 - glColor3f(1.0,1.0,1.0);
658674 }
659675 if(lpDDSrcSurface) glBindTexture(GL_TEXTURE_2D,((glDirectDrawSurface7*)lpDDSrcSurface)->GetTexture());
660676 if((dwFlags & DDBLT_KEYSRC) && (src && src->colorkey[0].enabled) && !(dwFlags & DDBLT_COLORFILL))
@@ -696,17 +712,14 @@
697713 GLint viewloc = glGetUniformLocation(GetProgram()&0xffffffff,"view");
698714 glUniform4f(viewloc,0,(GLfloat)fakex,0,(GLfloat)fakey);
699715 this->dirty |= 2;
700 - glBegin(GL_QUADS);
701 - glTexCoord2f(coords[4], coords[6]);
702 - glVertex2f(coords[0], coords[2]);
703 - glTexCoord2f(coords[5], coords[6]);
704 - glVertex2f( coords[1], coords[2]);
705 - glTexCoord2f(coords[5], coords[7]);
706 - glVertex2f( coords[1], coords[3]);
707 - glTexCoord2f(coords[4], coords[7]);
708 - glVertex2f(coords[0], coords[3]);
709 - glEnd();
710 - glColor3f(1.0,1.0,1.0);
 716+ glEnableClientState(GL_VERTEX_ARRAY);
 717+ glVertexPointer(2,GL_FLOAT,sizeof(BltVertex),&bltvertices[0].x);
 718+ glEnableClientState(GL_COLOR_ARRAY);
 719+ glColorPointer(3,GL_UNSIGNED_BYTE,sizeof(BltVertex),&bltvertices[0].r);
 720+ glClientActiveTexture(GL_TEXTURE0);
 721+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 722+ glTexCoordPointer(2,GL_FLOAT,sizeof(BltVertex),&bltvertices[0].s);
 723+ glDrawRangeElements(GL_TRIANGLE_STRIP,0,3,4,GL_UNSIGNED_SHORT,bltindices);
711724 glDisable(GL_TEXTURE_2D);
712725 SetFBO(0,0,false);
713726 if(((ddsd.ddsCaps.dwCaps & (DDSCAPS_FRONTBUFFER)) &&
@@ -1117,10 +1130,10 @@
11181131 {
11191132 ddInterface->GetSizes(sizes);
11201133 glViewport(0,0,sizes[4],sizes[5]);
1121 - view[0] = (signed)-(sizes[4]-sizes[0])/2;
1122 - view[1] = (signed)(sizes[4]-sizes[0])/2+sizes[0];
1123 - view[2] = (signed)(sizes[5]-sizes[1])/2+sizes[1];
1124 - view[3] = (signed)-(sizes[5]-sizes[1])/2;
 1134+ view[0] = (GLfloat)-(sizes[4]-sizes[0])/2;
 1135+ view[1] = (GLfloat)(sizes[4]-sizes[0])/2+sizes[0];
 1136+ view[2] = (GLfloat)(sizes[5]-sizes[1])/2+sizes[1];
 1137+ view[3] = (GLfloat)-(sizes[5]-sizes[1])/2;
11251138 }
11261139 else
11271140 {
@@ -1134,18 +1147,18 @@
11351148 ClientToScreen(hwnd,(LPPOINT)&r2.left);
11361149 ClientToScreen(hwnd,(LPPOINT)&r2.right);
11371150 glViewport(0,0,r.right,r.bottom);
1138 - view[0] = (signed)r2.left;
1139 - view[1] = (signed)r2.right;
1140 - view[2] = (signed)(fakey-r2.top);
1141 - view[3] = (signed)(fakey-r2.bottom);
 1151+ view[0] = (GLfloat)r2.left;
 1152+ view[1] = (GLfloat)r2.right;
 1153+ view[2] = (GLfloat)fakey-(GLfloat)r2.top;
 1154+ view[3] = (GLfloat)fakey-(GLfloat)r2.bottom;
11421155 }
11431156 }
11441157 else
11451158 {
11461159 view[0] = 0;
1147 - view[1] = fakex;
 1160+ view[1] = (GLfloat)fakex;
11481161 view[2] = 0;
1149 - view[3] = fakey;
 1162+ view[3] = (GLfloat)fakey;
11501163 }
11511164 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
11521165 if(ddInterface->GetBPP() == 8)
@@ -1168,38 +1181,32 @@
11691182 SetShader(PROG_TEXTURE,true);
11701183 glEnable(GL_TEXTURE_2D);
11711184 glBindTexture(GL_TEXTURE_2D,texture);
1172 - int prog = GetProgram();
 1185+ GLuint prog = GetProgram() & 0xFFFFFFFF;
11731186 GLint texloc = glGetUniformLocation(prog,"Texture");
11741187 }
1175 - int prog = GetProgram();
 1188+ GLuint prog = GetProgram();
11761189 GLint viewloc = glGetUniformLocation(prog,"view");
11771190 glUniform4f(viewloc,view[0],view[1],view[2],view[3]);
11781191 if(ddInterface->GetFullscreen())
11791192 {
1180 - glBegin(GL_QUADS);
1181 - glTexCoord2f(0., 1.);
1182 - glVertex2f(0., 0.);
1183 - glTexCoord2f(1., 1.);
1184 - glVertex2f( (float)sizes[0], 0.);
1185 - glTexCoord2f(1., 0.);
1186 - glVertex2f( (float)sizes[0], (float)sizes[1]);
1187 - glTexCoord2f(0., 0.);
1188 - glVertex2f(0., (float)sizes[1]);
1189 - glEnd();
 1193+ bltvertices[0].x = bltvertices[2].x = (float)sizes[0];
 1194+ bltvertices[0].y = bltvertices[1].y = bltvertices[1].x = bltvertices[3].x = 0.;
 1195+ bltvertices[2].y = bltvertices[3].y = (float)sizes[1];
11901196 }
11911197 else
11921198 {
1193 - glBegin(GL_QUADS);
1194 - glTexCoord2f(0., 1.);
1195 - glVertex2f(0., 0.);
1196 - glTexCoord2f(1., 1.);
1197 - glVertex2f( (float)fakex, 0.);
1198 - glTexCoord2f(1., 0.);
1199 - glVertex2f( (float)fakex, (float)fakey);
1200 - glTexCoord2f(0., 0.);
1201 - glVertex2f(0., (float)fakey);
1202 - glEnd();
 1199+ bltvertices[0].x = bltvertices[2].x = (float)fakex;
 1200+ bltvertices[0].y = bltvertices[1].y = bltvertices[1].x = bltvertices[3].x = 0.;
 1201+ bltvertices[2].y = bltvertices[3].y = (float)fakey;
12031202 }
 1203+ bltvertices[0].s = bltvertices[0].t = bltvertices[1].t = bltvertices[2].s = 1.;
 1204+ bltvertices[1].s = bltvertices[2].t = bltvertices[3].s = bltvertices[3].t = 0.;
 1205+ glEnableClientState(GL_VERTEX_ARRAY);
 1206+ glVertexPointer(2,GL_FLOAT,sizeof(BltVertex),&bltvertices[0].x);
 1207+ glClientActiveTexture(GL_TEXTURE0);
 1208+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 1209+ glTexCoordPointer(2,GL_FLOAT,sizeof(BltVertex),&bltvertices[0].s);
 1210+ glDrawRangeElements(GL_TRIANGLE_STRIP,0,3,4,GL_UNSIGNED_SHORT,bltindices);
12041211 glDisable(GL_TEXTURE_2D);
12051212 glFlush();
12061213 SwapBuffers(ddInterface->hDC);
Index: ddraw/glDirectDrawSurface.h
@@ -28,6 +28,16 @@
2929 DDCOLORKEY key;
3030 } CKEY;
3131
 32+struct BltVertex
 33+{
 34+ GLfloat x,y;
 35+ GLubyte r,g,b,a;
 36+ GLfloat s,t;
 37+ GLfloat padding[3];
 38+};
 39+
 40+extern BltVertex bltvertices[4];
 41+
3242 class glDirectDrawClipper;
3343 class glDirectDrawPalette;
3444 class glDirectDrawSurface1;
Index: ddraw/glExtensions.cpp
@@ -54,7 +54,10 @@
5555 void (APIENTRY *glUniform4f) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) = NULL;
5656 void (APIENTRY *glUniformMatrix4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) = NULL;
5757
 58+void (APIENTRY *glDrawRangeElements) (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
 59+
5860 void (APIENTRY *glActiveTexture)(GLenum texture) = NULL;
 61+void (APIENTRY *glClientActiveTexture) (GLenum texture) = NULL;
5962
6063 BOOL (APIENTRY *wglSwapIntervalEXT)(int interval) = NULL;
6164 int (APIENTRY *wglGetSwapIntervalEXT)() = NULL;
@@ -71,8 +74,13 @@
7275 {
7376 const GLubyte *glversion = glGetString(GL_VERSION);
7477 sscanf((char*)glversion,"%d.%d",&glver_major,&glver_minor);
 78+ if((glver_major >= 2) || ((glver_major >= 1) && (glver_minor >= 2)))
 79+ glDrawRangeElements = (PFNGLDRAWRANGEELEMENTSPROC)wglGetProcAddress("glDrawRangeElements");
7580 if((glver_major >= 2) || ((glver_major >= 1) && (glver_minor >= 3)))
 81+ {
7682 glActiveTexture = (PFNGLACTIVETEXTUREPROC)wglGetProcAddress("glActiveTexture");
 83+ glClientActiveTexture = (PFNGLCLIENTACTIVETEXTUREPROC)wglGetProcAddress("glClientActiveTexture");
 84+ }
7785 if(glver_major >= 2)
7886 {
7987 glCreateShader = (PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader");
Index: ddraw/glExtensions.h
@@ -37,6 +37,7 @@
3838 #undef GLAPI
3939 #define GLAPI extern
4040 #endif
 41+
4142 GLAPI GLuint (APIENTRY *glCreateShader) (GLenum type);
4243 GLAPI void (APIENTRY *glShaderSource) (GLuint shader, GLsizei count, const GLchar** string, const GLint* length);
4344 GLAPI void (APIENTRY *glCompileShader) (GLuint shader);
@@ -73,7 +74,10 @@
7475 GLAPI void (APIENTRY *glUniform4f) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
7576 GLAPI void (APIENTRY *glUniformMatrix4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
7677
 78+GLAPI void (APIENTRY *glDrawRangeElements) (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);
 79+
7780 GLAPI void (APIENTRY *glActiveTexture)(GLenum texture);
 81+GLAPI void (APIENTRY *glClientActiveTexture) (GLenum texture);
7882
7983 GLAPI BOOL (APIENTRY *wglSwapIntervalEXT)(int interval);
8084 GLAPI int (APIENTRY *wglGetSwapIntervalEXT)();
Index: ddraw/shadergen.cpp
@@ -23,6 +23,7 @@
2424 static __int64 current_shader = 0;
2525 static int shadercount = 0;
2626 static bool initialized = false;
 27+static bool isbuiltin = true;
2728
2829 void SetShader(__int64 id, bool builtin)
2930 {
@@ -33,7 +34,11 @@
3435 }
3536 }
3637
37 -__int64 GetProgram()
 38+GLuint GetProgram()
3839 {
39 - return current_shader;
 40+ if(isbuiltin) return current_shader & 0xFFFFFFFF;
 41+ else
 42+ {
 43+ return 0;
 44+ }
4045 }
Index: ddraw/shadergen.h
@@ -20,7 +20,7 @@
2121
2222
2323 void SetShader(__int64 id, bool builtin);
24 -__int64 GetProgram();
 24+GLuint GetProgram();
2525
2626
2727 #endif
\ No newline at end of file
Index: ddraw/shaders.cpp
@@ -123,7 +123,6 @@
124124 {
125125 const GLchar *src;
126126 GLint srclen;
127 - GLenum error;
128127 for(int i = 0; i < NumberOfShaders; i++)
129128 {
130129 shaders[i].prog = glCreateProgram();