DXGL r237 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r236‎ | r237 | r238 >
Date:17:40, 12 August 2012
Author:admin
Status:new
Tags:
Comment:
Cache 2D shader vertex attributes
Modified paths:
  • /ddraw/glExtensions.cpp (modified) (history)
  • /ddraw/glExtensions.h (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/glRenderer.h (modified) (history)
  • /ddraw/glutil.cpp (modified) (history)
  • /ddraw/glutil.h (modified) (history)
  • /ddraw/shaders.cpp (modified) (history)
  • /ddraw/shaders.h (modified) (history)

Diff [purge]

Index: ddraw/glExtensions.cpp
@@ -36,6 +36,7 @@
3737 GLint (APIENTRY *glGetAttribLocation) (GLuint program, const GLchar* name) = NULL;
3838 void (APIENTRY *glVertexAttribPointer) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* pointer) = NULL;
3939 void (APIENTRY *glEnableVertexAttribArray) (GLuint index) = NULL;
 40+void (APIENTRY *glDisableVertexAttribArray) (GLuint index) = NULL;
4041
4142 void (APIENTRY *glGenFramebuffers) (GLsizei n, GLuint* ids) = NULL;
4243 void (APIENTRY *glBindFramebuffer) (GLenum target, GLuint framebuffer) = NULL;
@@ -142,6 +143,7 @@
143144 glGetAttribLocation = (PFNGLGETATTRIBLOCATIONPROC)wglGetProcAddress("glGetAttribLocation");
144145 glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)wglGetProcAddress("glVertexAttribPointer");
145146 glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)wglGetProcAddress("glEnableVertexAttribArray");
 147+ glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)wglGetProcAddress("glDisableVertexAttribArray");
146148 }
147149 else
148150 {
Index: ddraw/glExtensions.h
@@ -56,6 +56,7 @@
5757 GLAPI GLint (APIENTRY *glGetAttribLocation) (GLuint program, const GLchar* name);
5858 GLAPI void (APIENTRY *glVertexAttribPointer) (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid* pointer);
5959 GLAPI void (APIENTRY *glEnableVertexAttribArray) (GLuint index);
 60+GLAPI void (APIENTRY *glDisableVertexAttribArray) (GLuint index);
6061
6162 GLAPI void (APIENTRY *glGenFramebuffers) (GLsizei n, GLuint* ids);
6263 GLAPI void (APIENTRY *glBindFramebuffer) (GLenum target, GLuint framebuffer);
Index: ddraw/glRenderer.cpp
@@ -811,6 +811,7 @@
812812 ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
813813 ClearDepth(1.0);
814814 ClearStencil(0);
 815+ EnableArray(-1,false);
815816 glClear(GL_COLOR_BUFFER_BIT);
816817 glFlush();
817818 SetScissor(false,0,0,0,0);
@@ -844,6 +845,7 @@
845846 void glRenderer::_Blt(LPRECT lpDestRect, glDirectDrawSurface7 *src,
846847 glDirectDrawSurface7 *dest, LPRECT lpSrcRect, DWORD dwFlags, LPDDBLTFX lpDDBltFx)
847848 {
 849+ int progtype;
848850 LONG sizes[6];
849851 ddInterface->GetSizes(sizes);
850852 int error;
@@ -886,6 +888,7 @@
887889 if(dwFlags & DDBLT_COLORFILL)
888890 {
889891 SetShader(PROG_FILL,NULL,NULL,true);
 892+ progtype = PROG_FILL;
890893 switch(ddInterface->GetBPP())
891894 {
892895 case 8:
@@ -925,6 +928,7 @@
926929 if((dwFlags & DDBLT_KEYSRC) && (src && src->colorkey[0].enabled) && !(dwFlags & DDBLT_COLORFILL))
927930 {
928931 SetShader(PROG_CKEY,NULL,NULL,true);
 932+ progtype = PROG_CKEY;
929933 GLint keyloc = glGetUniformLocation(shaders[PROG_CKEY].prog,"keyIn");
930934 switch(ddInterface->GetBPP())
931935 {
@@ -956,6 +960,7 @@
957961 else if(!(dwFlags & DDBLT_COLORFILL))
958962 {
959963 SetShader(PROG_TEXTURE,NULL,NULL,true);
 964+ progtype = PROG_TEXTURE;
960965 GLint texloc = glGetUniformLocation(shaders[PROG_TEXTURE].prog,"Texture");
961966 glUniform1i(texloc,0);
962967 }
@@ -965,20 +970,17 @@
966971 GLint viewloc = glGetUniformLocation(prog,"view");
967972 glUniform4f(viewloc,0,(GLfloat)dest->fakex,0,(GLfloat)dest->fakey);
968973 dest->dirty |= 2;
969 - GLint xyloc = glGetAttribLocation(prog,"xy");
970 - glEnableVertexAttribArray(xyloc);
971 - glVertexAttribPointer(xyloc,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].x);
972 - GLint rgbloc = glGetAttribLocation(prog,"rgb");
973 - if(rgbloc != -1)
 974+ EnableArray(shaders[progtype].pos,true);
 975+ glVertexAttribPointer(shaders[progtype].pos,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].x);
 976+ if(shaders[progtype].rgb != -1)
974977 {
975 - glEnableVertexAttribArray(rgbloc);
976 - glVertexAttribPointer(rgbloc,3,GL_UNSIGNED_BYTE,true,sizeof(BltVertex),&bltvertices[0].r);
 978+ EnableArray(shaders[progtype].rgb,true);
 979+ glVertexAttribPointer(shaders[progtype].rgb,3,GL_UNSIGNED_BYTE,true,sizeof(BltVertex),&bltvertices[0].r);
977980 }
978981 if(!(dwFlags & DDBLT_COLORFILL))
979982 {
980 - GLint stloc = glGetAttribLocation(prog,"st");
981 - glEnableVertexAttribArray(stloc);
982 - glVertexAttribPointer(stloc,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].s);
 983+ EnableArray(shaders[progtype].texcoord,true);
 984+ glVertexAttribPointer(shaders[progtype].texcoord,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].s);
983985 }
984986 glDrawRangeElements(GL_TRIANGLE_STRIP,0,3,4,GL_UNSIGNED_SHORT,bltindices);
985987 SetFBO(0,0,false);
@@ -1003,7 +1005,7 @@
10041006 return texture;
10051007 }
10061008
1007 -void glRenderer::_DrawBackbuffer(GLuint *texture, int x, int y)
 1009+void glRenderer::_DrawBackbuffer(GLuint *texture, int x, int y, int progtype)
10081010 {
10091011 GLfloat view[4];
10101012 SetActiveTexture(0);
@@ -1037,12 +1039,10 @@
10381040 bltvertices[0].y = bltvertices[1].y = bltvertices[1].x = bltvertices[3].x = 0.;
10391041 bltvertices[0].x = bltvertices[2].x = (float)x;
10401042 bltvertices[2].y = bltvertices[3].y = (float)y;
1041 - GLint xyloc = glGetAttribLocation(prog,"xy");
1042 - glEnableVertexAttribArray(xyloc);
1043 - glVertexAttribPointer(xyloc,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].x);
1044 - GLint stloc = glGetAttribLocation(prog,"st");
1045 - glEnableVertexAttribArray(stloc);
1046 - glVertexAttribPointer(stloc,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].s);
 1043+ EnableArray(shaders[progtype].pos,true);
 1044+ glVertexAttribPointer(shaders[progtype].pos,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].x);
 1045+ EnableArray(shaders[progtype].texcoord,true);
 1046+ glVertexAttribPointer(shaders[progtype].texcoord,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].s);
10471047 glDrawRangeElements(GL_TRIANGLE_STRIP,0,3,4,GL_UNSIGNED_SHORT,bltindices);
10481048 SetFBO(0,0,false);
10491049 }
@@ -1049,6 +1049,7 @@
10501050
10511051 void glRenderer::_DrawScreen(GLuint texture, GLuint paltex, glDirectDrawSurface7 *dest, glDirectDrawSurface7 *src, bool setsync)
10521052 {
 1053+ int progtype;
10531054 RECT r,r2;
10541055 if((dest->ddsd.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE))
10551056 {
@@ -1109,6 +1110,7 @@
11101111 if(ddInterface->GetBPP() == 8)
11111112 {
11121113 SetShader(PROG_PAL256,NULL,NULL,true);
 1114+ progtype = PROG_PAL256;
11131115 glBindTexture(GL_TEXTURE_2D,paltex);
11141116 glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,1,0,GL_RGBA,GL_UNSIGNED_BYTE,dest->palette->GetPalette(NULL));
11151117 GLint palloc = glGetUniformLocation(shaders[PROG_PAL256].prog,"ColorTable");
@@ -1122,8 +1124,9 @@
11231125 SetActiveTexture(0);
11241126 if(dxglcfg.scalingfilter)
11251127 {
1126 - _DrawBackbuffer(&texture,dest->fakex,dest->fakey);
 1128+ _DrawBackbuffer(&texture,dest->fakex,dest->fakey,progtype);
11271129 SetShader(PROG_TEXTURE,NULL,NULL,true);
 1130+ progtype = PROG_TEXTURE;
11281131 glBindTexture(GL_TEXTURE_2D,texture);
11291132 GLuint prog = GetProgram() & 0xFFFFFFFF;
11301133 GLint texloc = glGetUniformLocation(prog,"Texture");
@@ -1133,6 +1136,7 @@
11341137 else
11351138 {
11361139 SetShader(PROG_TEXTURE,NULL,NULL,true);
 1140+ progtype = PROG_TEXTURE;
11371141 glBindTexture(GL_TEXTURE_2D,texture);
11381142 GLuint prog = GetProgram() & 0xFFFFFFFF;
11391143 GLint texloc = glGetUniformLocation(prog,"Texture");
@@ -1156,17 +1160,14 @@
11571161 }
11581162 bltvertices[0].s = bltvertices[0].t = bltvertices[1].t = bltvertices[2].s = 1.;
11591163 bltvertices[1].s = bltvertices[2].t = bltvertices[3].s = bltvertices[3].t = 0.;
1160 - GLint xyloc = glGetAttribLocation(prog,"xy");
1161 - glEnableVertexAttribArray(xyloc);
1162 - glVertexAttribPointer(xyloc,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].x);
1163 - GLint stloc = glGetAttribLocation(prog,"st");
1164 - glEnableVertexAttribArray(stloc);
1165 - glVertexAttribPointer(stloc,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].s);
1166 - GLint rgbloc = glGetAttribLocation(prog,"rgb");
1167 - if(rgbloc != -1)
 1164+ EnableArray(shaders[progtype].pos,true);
 1165+ glVertexAttribPointer(shaders[progtype].pos,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].x);
 1166+ EnableArray(shaders[progtype].texcoord,true);
 1167+ glVertexAttribPointer(shaders[progtype].texcoord,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].s);
 1168+ if(shaders[progtype].rgb != -1)
11681169 {
1169 - glEnableVertexAttribArray(rgbloc);
1170 - glVertexAttribPointer(rgbloc,3,GL_UNSIGNED_BYTE,true,sizeof(BltVertex),&bltvertices[0].r);
 1170+ EnableArray(shaders[progtype].rgb,true);
 1171+ glVertexAttribPointer(shaders[progtype].rgb,3,GL_UNSIGNED_BYTE,true,sizeof(BltVertex),&bltvertices[0].r);
11711172 }
11721173 glDrawRangeElements(GL_TRIANGLE_STRIP,0,3,4,GL_UNSIGNED_SHORT,bltindices);
11731174 glFlush();
@@ -1333,13 +1334,13 @@
13341335 if(device->renderstate[D3DRENDERSTATE_ZWRITEENABLE]) DepthWrite(true);
13351336 else DepthWrite(false);
13361337 _GENSHADER prog = genshaders[current_genshader].shader;
1337 - glEnableVertexAttribArray(prog.attribs[0]);
 1338+ EnableArray(prog.attribs[0],true);
13381339 glVertexAttribPointer(prog.attribs[0],3,GL_FLOAT,false,vertices[0].stride,vertices[0].data);
13391340 if(transformed)
13401341 {
13411342 if(prog.attribs[1] != -1)
13421343 {
1343 - glEnableVertexAttribArray(prog.attribs[1]);
 1344+ EnableArray(prog.attribs[1],true);
13441345 glVertexAttribPointer(prog.attribs[1],4,GL_FLOAT,false,vertices[1].stride,vertices[1].data);
13451346 }
13461347 }
@@ -1349,7 +1350,7 @@
13501351 {
13511352 if(prog.attribs[i+2] != -1)
13521353 {
1353 - glEnableVertexAttribArray(prog.attribs[i+2]);
 1354+ EnableArray(prog.attribs[i+2],true);
13541355 glVertexAttribPointer(prog.attribs[i+2],1,GL_FLOAT,false,vertices[i+2].stride,vertices[i+2].data);
13551356 }
13561357 }
@@ -1358,7 +1359,7 @@
13591360 {
13601361 if(prog.attribs[7] != -1)
13611362 {
1362 - glEnableVertexAttribArray(prog.attribs[7]);
 1363+ EnableArray(prog.attribs[7],true);
13631364 glVertexAttribPointer(prog.attribs[7],3,GL_FLOAT,false,vertices[7].stride,vertices[7].data);
13641365 }
13651366 }
@@ -1368,7 +1369,7 @@
13691370 {
13701371 if(prog.attribs[8+i] != -1)
13711372 {
1372 - glEnableVertexAttribArray(prog.attribs[8+i]);
 1373+ EnableArray(prog.attribs[8+i],true);
13731374 glVertexAttribPointer(prog.attribs[8+i],4,GL_UNSIGNED_BYTE,true,vertices[i+8].stride,vertices[i+8].data);
13741375 }
13751376 }
@@ -1383,7 +1384,7 @@
13841385 case 0: // st
13851386 if(prog.attribs[i+18] != -1)
13861387 {
1387 - glEnableVertexAttribArray(prog.attribs[i+18]);
 1388+ EnableArray(prog.attribs[i+18],true);
13881389 glVertexAttribPointer(prog.attribs[i+18],2,GL_FLOAT,false,vertices[i+10].stride,vertices[i+10].data);
13891390 }
13901391 break;
@@ -1390,7 +1391,7 @@
13911392 case 1: // str
13921393 if(prog.attribs[i+26] != -1)
13931394 {
1394 - glEnableVertexAttribArray(prog.attribs[i+26]);
 1395+ EnableArray(prog.attribs[i+26],true);
13951396 glVertexAttribPointer(prog.attribs[i+26],3,GL_FLOAT,false,vertices[i+10].stride,vertices[i+10].data);
13961397 }
13971398 break;
@@ -1397,7 +1398,7 @@
13981399 case 2: // strq
13991400 if(prog.attribs[i+34] != -1)
14001401 {
1401 - glEnableVertexAttribArray(prog.attribs[i+34]);
 1402+ EnableArray(prog.attribs[i+34],true);
14021403 glVertexAttribPointer(prog.attribs[i+34],4,GL_FLOAT,false,vertices[i+10].stride,vertices[i+10].data);
14031404 }
14041405 break;
@@ -1404,7 +1405,7 @@
14051406 case 3: // s
14061407 if(prog.attribs[i+10] != -1)
14071408 {
1408 - glEnableVertexAttribArray(prog.attribs[i+10]);
 1409+ EnableArray(prog.attribs[i+10],true);
14091410 glVertexAttribPointer(prog.attribs[i+10],1,GL_FLOAT,false,vertices[i+10].stride,vertices[i+10].data);
14101411 }
14111412 break;
Index: ddraw/glRenderer.h
@@ -110,7 +110,7 @@
111111 GLuint _MakeTexture(GLint min, GLint mag, GLint wraps, GLint wrapt, DWORD width, DWORD height, GLint texformat1, GLint texformat2, GLint texformat3);
112112 void _DrawScreen(GLuint texture, GLuint paltex, glDirectDrawSurface7 *dest, glDirectDrawSurface7 *src, bool setsync);
113113 void _DeleteTexture(GLuint texture);
114 - void _DrawBackbuffer(GLuint *texture, int x, int y);
 114+ void _DrawBackbuffer(GLuint *texture, int x, int y, int progtype);
115115 void _InitD3D(int zbuffer);
116116 void _Clear(glDirectDrawSurface7 *target, DWORD dwCount, LPD3DRECT lpRects, DWORD dwFlags, DWORD dwColor, D3DVALUE dvZ, DWORD dwStencil);
117117 void glRenderer::_DrawPrimitives(glDirect3DDevice7 *device, GLenum mode, GLVERTEX *vertices, int *texcormats, DWORD count, LPWORD indices,
Index: ddraw/glutil.cpp
@@ -51,6 +51,7 @@
5252 GLclampf cleara = 0.0;
5353 GLclampd cleardepth = 1.0;
5454 GLint clearstencil = 0;
 55+bool arrays[42];
5556
5657 void InitFBO()
5758 {
@@ -327,4 +328,21 @@
328329 clearstencil = stencil;
329330 glClearStencil(stencil);
330331 }
 332+}
 333+
 334+void EnableArray(int index, bool enabled)
 335+{
 336+ if(index == -1)
 337+ {
 338+ for(int i = 0; i < 42; i++)
 339+ arrays[i] = false;
 340+ return;
 341+ }
 342+ if(index >= 42) return;
 343+ if(arrays[index] != enabled)
 344+ {
 345+ arrays[index] = enabled;
 346+ if(enabled) glEnableVertexAttribArray(index);
 347+ else glDisableVertexAttribArray(index);
 348+ }
331349 }
\ No newline at end of file
Index: ddraw/glutil.h
@@ -41,5 +41,6 @@
4242 void ClearColor(GLclampf r, GLclampf g, GLclampf b, GLclampf a);
4343 void ClearDepth(GLclampd depth);
4444 void ClearStencil(GLint stencil);
 45+void EnableArray(int index, bool enabled);
4546
4647 #endif //_GLUTIL_H
\ No newline at end of file
Index: ddraw/shaders.cpp
@@ -120,12 +120,12 @@
121121 const int SHADER_START = __LINE__;
122122 SHADER shaders[] =
123123 {
124 - {0,0, vert_ortho, frag_Color, 0},
125 - {0,0, vert_ortho, frag_Texture, 0},
126 - {0,0, vert_ortho, frag_Pal256, 0},
127 - {0,0, vert_ortho, frag_ColorKey, 0},
128 - {0,0, vert_ortho, frag_ColorKeyMask, 0},
129 - {0,0, vert_ortho, frag_2ColorKey, 0}
 124+ {0,0, vert_ortho, frag_Color, 0,-1,-1,-1},
 125+ {0,0, vert_ortho, frag_Texture, 0,-1,-1,-1},
 126+ {0,0, vert_ortho, frag_Pal256, 0,-1,-1,-1},
 127+ {0,0, vert_ortho, frag_ColorKey, 0,-1,-1,-1},
 128+ {0,0, vert_ortho, frag_ColorKeyMask, 0,-1,-1,-1},
 129+ {0,0, vert_ortho, frag_2ColorKey, 0,-1,-1,-1}
130130 };
131131 const int SHADER_END = __LINE__ - 4;
132132 const int NumberOfShaders = SHADER_END - SHADER_START;
@@ -157,6 +157,9 @@
158158 glAttachShader(shaders[i].prog,shaders[i].fs);
159159 }
160160 glLinkProgram(shaders[i].prog);
 161+ shaders[i].pos = glGetAttribLocation(shaders[i].prog,"xy");
 162+ shaders[i].rgb = glGetAttribLocation(shaders[i].prog,"rgb");
 163+ shaders[i].texcoord = glGetAttribLocation(shaders[i].prog,"st");
161164 }
162165 }
163166
Index: ddraw/shaders.h
@@ -26,6 +26,9 @@
2727 const char *vsrc;
2828 const char *fsrc;
2929 GLint prog;
 30+ GLint pos;
 31+ GLint rgb;
 32+ GLint texcoord;
3033 } SHADER;
3134
3235 extern SHADER shaders[];