DXGL r137 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r136‎ | r137 | r138 >
Date:21:56, 15 April 2012
Author:admin
Status:new
Tags:
Comment:
Fill in texture stages in fragment shader.
Set texture stage uniforms.
Modified paths:
  • /ddraw/glDirect3DDevice.cpp (modified) (history)
  • /ddraw/glDirect3DDevice.h (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/glutil.cpp (modified) (history)
  • /ddraw/glutil.h (modified) (history)
  • /ddraw/shadergen.cpp (modified) (history)
  • /dxgltest/Tests3D.cpp (modified) (history)

Diff [purge]

Index: ddraw/glDirect3DDevice.cpp
@@ -417,6 +417,7 @@
418418 }
419419 texstages[i].shaderid |= (__int64)(texstages[i].texcoordindex&7) << 54;
420420 texstages[i].shaderid |= (__int64)((texstages[i].texcoordindex>>16)&3) << 57;
 421+ if(texstages[i].texture) texstages[i].shaderid |= 1i64 << 59;
421422 return shader;
422423 }
423424
@@ -904,6 +905,7 @@
905906 if(dwStage > 7) return DDERR_INVALIDPARAMS;
906907 if(texstages[dwStage].texture) texstages[dwStage].texture->Release();
907908 texstages[dwStage].texture = (glDirectDrawSurface7*)lpTexture;
 909+ texstages[dwStage].dirty = true;
908910 if(lpTexture) lpTexture->AddRef();
909911 return D3D_OK;
910912 }
Index: ddraw/glDirect3DDevice.h
@@ -133,10 +133,10 @@
134134 glDirectDrawSurface7 *glDDS7;
135135 DWORD renderstate[153];
136136 TEXTURESTAGE texstages[8];
 137+ glDirect3D7 *glD3D7;
137138
138139 private:
139140 HRESULT fvftoglvertex(DWORD dwVertexTypeDesc,LPDWORD vertptr);
140 - glDirect3D7 *glD3D7;
141141 ULONG refcount;
142142 GLuint gltextures[8];
143143 DWORD lightsmax;
Index: ddraw/glRenderer.cpp
@@ -1142,6 +1142,28 @@
11431143 glUniform4f(prog.uniforms[136],RGBA_GETRED(ambient),RGBA_GETGREEN(ambient),
11441144 RGBA_GETBLUE(ambient),RGBA_GETALPHA(ambient));
11451145
 1146+ for(i = 0; i < 8; i++)
 1147+ {
 1148+ if(device->texstages[i].colorop == D3DTOP_DISABLE) break;
 1149+ if(device->texstages[i].texture)
 1150+ {
 1151+ if(device->texstages[i].texture->dirty & 1)
 1152+ {
 1153+ _UploadTexture(device->texstages[i].texture->buffer,device->texstages[i].texture->bigbuffer,
 1154+ device->texstages[i].texture->texture,device->texstages[i].texture->ddsd.dwWidth,
 1155+ device->texstages[i].texture->ddsd.dwHeight,device->texstages[i].texture->fakex,
 1156+ device->texstages[i].texture->fakey,device->texstages[i].texture->ddsd.lPitch,
 1157+ (device->texstages[i].texture->ddsd.ddpfPixelFormat.dwRGBBitCount/8*device->texstages[i].texture->fakex),
 1158+ device->texstages[i].texture->ddsd.ddpfPixelFormat.dwRGBBitCount,device->texstages[i].texture->texformat,
 1159+ device->texstages[i].texture->texformat2,device->texstages[i].texture->texformat3);
 1160+ device->texstages[i].texture->dirty &= ~1;
 1161+ }
 1162+ SetTexture(i,device->texstages[i].texture->texture);
 1163+ }
 1164+ else SetTexture(i,0);
 1165+ glUniform1i(prog.uniforms[128+i],i);
 1166+ }
 1167+
11461168 if(device->glDDS7->zbuffer) SetFBO(device->glDDS7->texture,device->glDDS7->zbuffer->texture,device->glDDS7->zbuffer->hasstencil);
11471169 else SetFBO(device->glDDS7->texture,0,false);
11481170 glViewport(device->viewport.dwX,device->viewport.dwY,device->viewport.dwWidth,device->viewport.dwHeight);
Index: ddraw/glutil.cpp
@@ -114,6 +114,12 @@
115115 }
116116 }
117117
 118+void SetTexture(int level,GLuint texture)
 119+{
 120+ SetActiveTexture(level);
 121+ glBindTexture(GL_TEXTURE_2D,texture);
 122+}
 123+
118124 void SetWrap(int level, DWORD coord, DWORD address)
119125 {
120126 if(level == -1)
Index: ddraw/glutil.h
@@ -25,6 +25,7 @@
2626
2727 void InitFBO();
2828 void DeleteFBO();
 29+void SetTexture(int level,GLuint texture);
2930 void SetActiveTexture(int level);
3031 GLenum SetFBO(GLint color, GLint z, bool stencil);
3132
Index: ddraw/shadergen.cpp
@@ -88,6 +88,7 @@
8989 Bits in flags:
9090 00=passthru 01=cameraspacenormal
9191 10=cameraspaceposition 11=cameraspacereflectionvector
 92+Bit 59: Texture image enabled
9293 */
9394 void ZeroShaderArray()
9495 {
@@ -236,13 +237,14 @@
237238 };\n";
238239 static const char unif_light[] = "uniform Light lightX;\n";
239240 static const char unif_ambient[] = "uniform vec4 ambientcolor;\n";
240 -static const char unif_tex[] = "uniform sampler2d texX;\n";
 241+static const char unif_tex[] = "uniform sampler2D texX;\n";
241242 // Variables
242243 static const char var_common[] = "vec4 diffuse;\n\
243244 vec4 specular;\n\
244245 vec4 ambient;\n\
245246 vec3 N;";
246 -static const char var_color[] = "vec4 color;\n";
 247+static const char var_color[] = "vec3 color;\n\
 248+float alpha;\n";
247249 static const char var_xyzw[] = "vec4 xyzw;\n";
248250 // Operations
249251 static const char op_transform[] = "xyzw = vec4(xyz,1);\n\
@@ -256,11 +258,11 @@
257259 static const char op_dirlight[] = "DirLight(lightX);\n";
258260 static const char op_dirlightnospecular[] = "DirLightNoSpecular(lightX);\n";
259261 static const char op_spotlight[] = "SpotLight(lightX);\n";
260 -static const char op_colorout[] = "vec4 color = (material.diffuse * diffuse) + (material.ambient * ambient) + \n\
261 -(material.specular * specular) + material.emissive;\n\
262 -gl_FrontColor = color;\n";
263 -static const char op_colorfragout[] = "gl_FragColor = color;\n";
264 -static const char op_colorfragin[] = "color = gl_Color;\n";
 262+static const char op_colorout[] = "gl_FrontColor = (material.diffuse * diffuse) + (material.ambient * ambient) + material.emissive;\n\
 263+gl_FrontSecondaryColor = (material.specular * specular);\n";
 264+static const char op_colorfragout[] = "gl_FragColor = vec4(color,alpha);\n";
 265+static const char op_colorfragin[] = "color = gl_Color.rgb;\n\
 266+alpha = gl_Color.a;\n";
265267 static const char op_texpassthru1[] = "gl_TexCoord[x] = ";
266268 static const char op_texpassthru2s[] = "vec4(sX,0,0,1);\n";
267269 static const char op_texpassthru2st[] = "vec4(stX,0,1);\n";
@@ -514,6 +516,13 @@
515517 fsrc->append(idheader);
516518 fsrc->append(idstring);
517519 // Uniforms
 520+ for(i = 0; i < 8; i++)
 521+ {
 522+ if((texstate[i].shaderid & 31) == D3DTOP_DISABLE)break;
 523+ tmp = unif_tex;
 524+ tmp.replace(21,1,_itoa(i,idstring,10));
 525+ fsrc->append(tmp);
 526+ }
518527 // Variables
519528 fsrc->append(var_color);
520529 // Functions
@@ -520,9 +529,84 @@
521530 // Main
522531 fsrc->append(mainstart);
523532 fsrc->append(op_colorfragin);
524 - for(int i = 0; i < 8; i++)
 533+ string arg1,arg2;
 534+ int args[4];
 535+ const string blendargs[] = {"color","gl_Color","texture2DProj(texX,gl_TexCoord[Y]).rgb",
 536+ "texture2DProj(texX,gl_TexCoord[Y]).a","texfactor","gl_SecondaryColor","vec3(1,1,1)","1"};
 537+ for(i = 0; i < 8; i++)
525538 {
526 -
 539+ if((texstate[i].shaderid & 31) == D3DTOP_DISABLE)break;
 540+ // Color stage
 541+ args[0] = (texstate[i].shaderid>>5)&63;
 542+ switch(args[0]&7) //arg1
 543+ {
 544+ case D3DTA_CURRENT:
 545+ default:
 546+ arg1 = blendargs[0];
 547+ break;
 548+ case D3DTA_DIFFUSE:
 549+ arg1 = blendargs[1];
 550+ break;
 551+ case D3DTA_TEXTURE:
 552+ arg1 = blendargs[2];
 553+ arg1.replace(17,1,_itoa(i,idstring,10));
 554+ arg1.replace(31,1,_itoa((texstate[i].shaderid>>54)&7,idstring,10));
 555+ break;
 556+ case D3DTA_TFACTOR:
 557+ FIXME("Support texture factor value");
 558+ arg1 = blendargs[4];
 559+ break;
 560+ case D3DTA_SPECULAR:
 561+ arg1 = blendargs[5];
 562+ break;
 563+ }
 564+ args[1] = (texstate[i].shaderid>>11)&63;
 565+ switch(args[1]&7) //arg2
 566+ {
 567+ case D3DTA_CURRENT:
 568+ default:
 569+ arg2 = blendargs[0];
 570+ break;
 571+ case D3DTA_DIFFUSE:
 572+ arg2 = blendargs[1];
 573+ break;
 574+ case D3DTA_TEXTURE:
 575+ arg2 = blendargs[3];
 576+ arg2.replace(17,1,_itoa(i,idstring,10));
 577+ arg2.replace(31,1,_itoa((texstate[i].shaderid>>54)&7,idstring,10));
 578+ break;
 579+ case D3DTA_TFACTOR:
 580+ FIXME("Support texture factor value");
 581+ arg2 = blendargs[4];
 582+ break;
 583+ case D3DTA_SPECULAR:
 584+ arg2 = blendargs[5];
 585+ break;
 586+ }
 587+ switch(texstate[i].shaderid & 31)
 588+ {
 589+ case D3DTOP_DISABLE:
 590+ default:
 591+ break;
 592+ case D3DTOP_SELECTARG1:
 593+ fsrc->append("color = " + arg1 + ";\n");
 594+ break;
 595+ case D3DTOP_SELECTARG2:
 596+ fsrc->append("color = " + arg2 + ";\n");
 597+ break;
 598+ case D3DTOP_MODULATE:
 599+ fsrc->append("color = " + arg1 + " * " + arg2 + ";\n");
 600+ break;
 601+ case D3DTOP_MODULATE2X:
 602+ fsrc->append("color = (" + arg1 + " * " + arg2 + ") * 2;\n");
 603+ break;
 604+ case D3DTOP_MODULATE4X:
 605+ fsrc->append("color = (" + arg1 + " * " + arg2 + ") * 4;\n");
 606+ break;
 607+ case D3DTOP_ADD:
 608+ fsrc->append("color = " + arg1 + " + " + arg2 + ";\n");
 609+ break;
 610+ }
527611 }
528612 fsrc->append(op_colorfragout);
529613 fsrc->append(mainend);
Index: dxgltest/Tests3D.cpp
@@ -68,6 +68,7 @@
6969 break;
7070 case WM_DESTROY:
7171 StopTimer();
 72+ Sleep(100);
7273 for(int i = 0; i < 8; i++)
7374 {
7475 if(textures[i])