DXGL r101 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r100‎ | r101 | r102 >
Date:00:49, 16 February 2012
Author:admin
Status:new
Tags:
Comment:
Add code to convert render state to shader ID
Add texture state ID array to SetShader function
Modified paths:
  • /ddraw/glDirect3DDevice.cpp (modified) (history)
  • /ddraw/glDirect3DDevice.h (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/shadergen.cpp (modified) (history)
  • /ddraw/shadergen.h (modified) (history)

Diff [purge]

Index: ddraw/glDirect3DDevice.cpp
@@ -146,6 +146,7 @@
147147 lights = (glDirect3DLight**) malloc(16*sizeof(glDirect3DLight*));
148148 ZeroMemory(lights,16*sizeof(glDirect3DLight*));
149149 memset(gllights,0xff,8*sizeof(int));
 150+ memset(gltextures,0,8*sizeof(GLuint));
150151 glD3D7->glDD7->renderer->InitD3D(zbuffer);
151152 }
152153 glDirect3DDevice7::~glDirect3DDevice7()
@@ -268,6 +269,62 @@
269270 else if(size > maxarray) normals = (GLfloat*)realloc(normals,size*4*sizeof(GLfloat));
270271 }
271272
 273+__int64 glDirect3DDevice7::SelectShader(DWORD VertexType)
 274+{
 275+ int i;
 276+ __int64 shader = 0;
 277+ switch(renderstate[D3DRENDERSTATE_SHADEMODE])
 278+ {
 279+ case D3DSHADE_FLAT:
 280+ default:
 281+ break;
 282+ case D3DSHADE_GOURAUD:
 283+ shader |= 1;
 284+ break;
 285+ case D3DSHADE_PHONG:
 286+ shader |= 3;
 287+ break;
 288+ }
 289+ if(renderstate[D3DRENDERSTATE_ALPHATESTENABLE]) shader |= 4;
 290+ shader |= (((renderstate[D3DRENDERSTATE_ALPHAFUNC]-1) & 7) << 3);
 291+ shader |= ((renderstate[D3DRENDERSTATE_FOGTABLEMODE] & 3) << 6);
 292+ shader |= ((renderstate[D3DRENDERSTATE_FOGVERTEXMODE] & 3) << 8);
 293+ if(renderstate[D3DRENDERSTATE_RANGEFOGENABLE]) shader |= (1 << 10);
 294+ if(renderstate[D3DRENDERSTATE_SPECULARENABLE]) shader |= (1 << 11);
 295+ if(renderstate[D3DRENDERSTATE_STIPPLEDALPHA]) shader |= (1 << 12);
 296+ if(renderstate[D3DRENDERSTATE_COLORKEYENABLE]) shader |= (1 << 13);
 297+ shader |= ((renderstate[D3DRENDERSTATE_ZBIAS] & 15) << 14);
 298+ int numlights = 0;
 299+ for(i = 0; i < 8; i++)
 300+ if(gllights[i] != -1) numlights++;
 301+ shader |= numlights << 18;
 302+ if(renderstate[D3DRENDERSTATE_LOCALVIEWER]) shader |= (1 << 21);
 303+ if(renderstate[D3DRENDERSTATE_COLORKEYBLENDENABLE]) shader |= (1 << 22);
 304+ shader |= ((renderstate[D3DRENDERSTATE_DIFFUSEMATERIALSOURCE] & 3) << 23);
 305+ shader |= ((renderstate[D3DRENDERSTATE_SPECULARMATERIALSOURCE] & 3) << 25);
 306+ shader |= ((renderstate[D3DRENDERSTATE_AMBIENTMATERIALSOURCE] & 3) << 27);
 307+ shader |= ((renderstate[D3DRENDERSTATE_EMISSIVEMATERIALSOURCE] & 3) << 29);
 308+ int numtextures = (VertexType & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
 309+ shader |= (__int64)numtextures << 31;
 310+ if(VertexType & D3DFVF_XYZRHW) shader |= (1i64 << 34);
 311+ if((VertexType & D3DFVF_DIFFUSE) && (VertexType & D3DFVF_SPECULAR)) shader |= (1i64<<35);
 312+ for(i = 0; i < numtextures; i++)
 313+ shader |= (__int64)((VertexType >> (16+(2*i))) & 3) << (36 + (2*i));
 314+ if(VertexType & D3DFVF_NORMAL) shader |= (1i64 << 52);
 315+ int lightindex = 0;
 316+ for(i = 0; i < 8; i++)
 317+ {
 318+ if(gllights[i] != -1)
 319+ {
 320+ if(lights[gllights[i]]->light.dltType != D3DLIGHT_DIRECTIONAL)
 321+ shader |= (1i64 << (53+lightindex));
 322+ lightindex++;
 323+ }
 324+ }
 325+ if(((VertexType >> 1) & 7) >= 3) shader |= (__int64)(((VertexType >> 1) & 7) - 2) << 61;
 326+ return shader;
 327+}
 328+
272329 HRESULT WINAPI glDirect3DDevice7::DrawIndexedPrimitive(D3DPRIMITIVETYPE d3dptPrimitiveType, DWORD dwVertexTypeDesc,
273330 LPVOID lpvVertices, DWORD dwVertexCount, LPWORD lpwIndices, DWORD dwIndexCount, DWORD dwFlags)
274331 {
Index: ddraw/glDirect3DDevice.h
@@ -88,6 +88,7 @@
8989 void SetArraySize(DWORD size, DWORD vertex, DWORD texcoord);
9090
9191 private:
 92+ __int64 SelectShader(DWORD VertexType);
9293 D3DMATRIX matWorld,matView,matProjection;
9394 glDirect3D7 *glD3D7;
9495 glDirectDrawSurface7 *glDDS7;
@@ -97,6 +98,7 @@
9899 D3DMATERIAL7 material;
99100 glDirect3DLight **lights;
100101 int gllights[8];
 102+ GLuint gltextures[8];
101103 DWORD lightsmax;
102104 bool inscene;
103105 DWORD maxarray;
Index: ddraw/glRenderer.cpp
@@ -592,7 +592,7 @@
593593 glClear(GL_DEPTH_BUFFER_BIT);
594594 if(dwFlags & DDBLT_COLORFILL)
595595 {
596 - SetShader(PROG_FILL,1);
 596+ SetShader(PROG_FILL,NULL,true);
597597 glDisable(GL_TEXTURE_2D);
598598 glDisable(GL_ALPHA_TEST);
599599 switch(ddInterface->GetBPP())
@@ -638,7 +638,7 @@
639639 if(src) glBindTexture(GL_TEXTURE_2D,src->GetTexture());
640640 if((dwFlags & DDBLT_KEYSRC) && (src && src->colorkey[0].enabled) && !(dwFlags & DDBLT_COLORFILL))
641641 {
642 - SetShader(PROG_CKEY,1);
 642+ SetShader(PROG_CKEY,NULL,true);
643643 GLint keyloc = glGetUniformLocation(shaders[PROG_CKEY].prog,"keyIn");
644644 switch(ddInterface->GetBPP())
645645 {
@@ -669,7 +669,7 @@
670670 }
671671 else if(!(dwFlags & DDBLT_COLORFILL))
672672 {
673 - SetShader(PROG_TEXTURE,1);
 673+ SetShader(PROG_TEXTURE,NULL,true);
674674 GLint texloc = glGetUniformLocation(shaders[PROG_TEXTURE].prog,"Texture");
675675 glUniform1i(texloc,0);
676676 }
@@ -820,7 +820,7 @@
821821 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
822822 if(ddInterface->GetBPP() == 8)
823823 {
824 - SetShader(PROG_PAL256,true);
 824+ SetShader(PROG_PAL256,NULL,true);
825825 glBindTexture(GL_TEXTURE_2D,paltex);
826826 glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,1,0,GL_RGBA,GL_UNSIGNED_BYTE,dest->palette->GetPalette(NULL));
827827 GLint palloc = glGetUniformLocation(shaders[PROG_PAL256].prog,"ColorTable");
@@ -835,7 +835,7 @@
836836 if(dxglcfg.scalingfilter)
837837 {
838838 _DrawBackbuffer(&texture,dest->fakex,dest->fakey);
839 - SetShader(PROG_TEXTURE,true);
 839+ SetShader(PROG_TEXTURE,NULL,true);
840840 glEnable(GL_TEXTURE_2D);
841841 glBindTexture(GL_TEXTURE_2D,texture);
842842 GLuint prog = GetProgram() & 0xFFFFFFFF;
@@ -845,7 +845,7 @@
846846 }
847847 else
848848 {
849 - SetShader(PROG_TEXTURE,true);
 849+ SetShader(PROG_TEXTURE,NULL,true);
850850 glEnable(GL_TEXTURE_2D);
851851 glBindTexture(GL_TEXTURE_2D,texture);
852852 GLuint prog = GetProgram() & 0xFFFFFFFF;
Index: ddraw/shadergen.cpp
@@ -55,21 +55,27 @@
5656 Bit 11 - Specular highlights
5757 Bit 12 - Stippled alpha
5858 Bit 13 - Color key transparency
59 -Bit 14 - Enable Z bias
60 -Bits 15-17 - Number of lights
61 -Bit 18 - Camera relative specular highlights
62 -Bit 19 - Alpha blended color key
63 -Bits 20-21 - Diffuse material source
64 -Bits 22-23 - Specular material source
65 -Bits 24-25 - Ambient material source
66 -Bits 26-27 - Emissive material source
67 -Bits 28-30 - Number of textures
68 -Bit 31 - RGB or RGBA color
69 -Bits 32-33 - Vertex format
70 -Bits 34-49 - Texture coordinate format
71 -Bit 50 - Enable normals
72 -Bits 51-58 - Light types
 59+Bit 14-17 - Z bias
 60+Bits 18-20 - Number of lights
 61+Bit 21 - Camera relative specular highlights
 62+Bit 22 - Alpha blended color key
 63+Bits 23-24 - Diffuse material source
 64+Bits 25-26 - Specular material source
 65+Bits 27-28 - Ambient material source
 66+Bits 29-30 - Emissive material source
 67+Bits 31-33 - Number of textures
 68+Bit 34 - Use transformed vertices
 69+Bit 35 - Use secondary color
 70+Bits 36-51 - Texture coordinate format:
 71+00=2dim 01=3dim 10=4dim 11=1dim
 72+Bit 52 - Enable normals
 73+Bits 53-60 - Light types
 74+Bits 61-63 - Number of blending weights
7375 */
 76+
 77+/* Bits in Texture Stage ID:
 78+
 79+*/
7480 void ZeroShaderArray()
7581 {
7682 ZeroMemory(genshaders,256*sizeof(GenShader));
@@ -77,7 +83,7 @@
7884 isbuiltin = true;
7985 }
8086
81 -void SetShader(__int64 id, bool builtin)
 87+void SetShader(__int64 id, TexState *texstate, bool builtin)
8288 {
8389 int shaderindex = -1;
8490 if(builtin)
Index: ddraw/shadergen.h
@@ -18,8 +18,19 @@
1919 #ifndef _SHADERGEN_H
2020 #define _SHADERGEN_H
2121
 22+typedef struct
 23+{
 24+ __int64 TEX0;
 25+ __int64 TEX1;
 26+ __int64 TEX2;
 27+ __int64 TEX3;
 28+ __int64 TEX4;
 29+ __int64 TEX5;
 30+ __int64 TEX6;
 31+ __int64 TEX7;
 32+} TexState;
2233
23 -void SetShader(__int64 id, bool builtin);
 34+void SetShader(__int64 id, TexState *texstate, bool builtin);
2435 GLuint GetProgram();
2536 void ZeroShaderArray();
2637 void CreateShader(int index, __int64 id);