DXGL r525 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r524‎ | r525 | r526 >
Date:19:59, 7 September 2014
Author:admin
Status:new
Tags:
Comment:
Add dithering (requires OpenGL 2.1 or above)
Modified paths:
  • /Help/Help.vcxproj (modified) (history)
  • /ddraw/ShaderGen3D.cpp (modified) (history)
  • /ddraw/glDirect3DDevice.cpp (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)

Diff [purge]

Index: Help/Help.vcxproj
@@ -101,6 +101,7 @@
102102 <ItemGroup>
103103 <None Include="configuration.htm">
104104 <DeploymentContent>true</DeploymentContent>
 105+ <SubType>Designer</SubType>
105106 </None>
106107 <None Include="dxgl.css" />
107108 <None Include="dxgl.hhp" />
Index: ddraw/ShaderGen3D.cpp
@@ -81,6 +81,7 @@
8282 Bit 59 - Enable lights VS/FS
8383 Bit 60 - Use vertex colors VS
8484 Bit 61 - Enable fog VS/FS
 85+Bit 62 - Enable dithering FS
8586 */
8687
8788 /* Bits in Texture Stage ID:
@@ -270,8 +271,9 @@
271272
272273
273274 static const char header[] =
274 - "//REV" STR(SHADER3DVERSION) "\n\
275 -#version 110\n";
 275+ "//REV" STR(SHADER3DVERSION) "\n";
 276+static const char ver110[] = "#version 110\n";
 277+static const char ver120[] = "#version 120\n";
276278 static const char vertexshader[] = "//Vertex Shader\n";
277279 static const char fragshader[] = "//Fragment Shader\n";
278280 static const char idheader[] = "//ID: 0x";
@@ -282,7 +284,6 @@
283285 static const char attr_xyz[] = "attribute vec3 xyz;\n";
284286 static const char attr_rhw[] = "attribute float rhw;\n";
285287 static const char attr_nxyz[] = "attribute vec3 nxyz;\n";
286 -static const char const_nxyz[] = "const vec3 nxyz = vec3(0,0,0);\n";
287288 static const char attr_blend[] = "attribute float blendX;\n";
288289 static const char attr_rgba[] = "attribute vec4 rgbaX;\n";
289290 static const char attr_s[] = "attribute float sX;\n";
@@ -318,6 +319,7 @@
319320 static const char unif_alpharef[] = "uniform int alpharef;\n";
320321 static const char unif_key[] = "uniform ivec3 keyX;\n";
321322 static const char unif_world[] = "uniform mat4 matWorld;\n";
 323+static const char unif_ditherbits[] = "uniform ivec4 ditherbits;\n";
322324 // Variables
323325 static const char var_common[] = "vec4 diffuse;\n\
324326 vec4 specular;\n\
@@ -327,6 +329,17 @@
328330 static const char var_xyzw[] = "vec4 xyzw;\n";
329331 static const char var_fogfactorvertex[] = "varying float fogfactor;\n";
330332 static const char var_fogfactorpixel[] = "float fogfactor;\n";
 333+// Constants
 334+static const char const_nxyz[] = "const vec3 nxyz = vec3(0,0,0);\n";
 335+static const char const_threshold[] = "float threshold[64] = float[64](\n\
 336+0, 32, 8, 40, 2, 34, 10, 42,\n\
 337+48, 16, 56, 24, 50, 18, 58, 26,\n\
 338+12, 44, 4, 36, 14, 46, 6, 38,\n\
 339+60, 28, 52, 20, 62, 30, 54, 22,\n\
 340+3, 35, 11, 43, 1, 33, 9, 41,\n\
 341+51, 19, 59, 27, 49, 17, 57, 25,\n\
 342+15, 47, 7, 39, 13, 45, 5, 37,\n\
 343+63, 31, 55, 23, 61, 29, 53, 21);\n";
331344 // Operations
332345 static const char op_transform[] = "xyzw = vec4(xyz,1.0);\n\
333346 vec4 pos = gl_ModelViewProjectionMatrix*xyzw;\n\
@@ -348,6 +361,7 @@
349362 static const char op_color2vert[] = "gl_FrontSecondaryColor = rgba1.bgra;\n";
350363 static const char op_colorwhite[] = "gl_FrontColor = vec4(1.0,1.0,1.0,1.0);\n";
351364 static const char op_colorfragout[] = "gl_FragColor = color;\n";
 365+static const char op_dither[] = "color = dither(color);\n";
352366 static const char op_colorfragin[] = "color = gl_Color;\n";
353367 static const char op_colorkey[] = "if(ivec3(texture2DProj(texX,gl_TexCoord[Y])*255.5).rgb == keyZ) discard;\n";
354368 static const char op_texpassthru1[] = "gl_TexCoord[x] = ";
@@ -432,9 +446,36 @@
433447 ambient += light.ambient;\n\
434448 specular += light.specular*pf*attenuation;\n\
435449 }\n";
 450+static const char func_dither[] = "vec4 dither(vec4 color2)\n\
 451+{\n\
 452+ vec4 color = color2;\n\
 453+ int x = int(mod(gl_FragCoord.x, 8.0));\n\
 454+ int y = int(mod(gl_FragCoord.y, 8.0));\n\
 455+ vec4 limit;\n\
 456+ limit.r = (threshold[x + (y * 8)]) / ((pow(2.0, float(ditherbits.r)) - 1.0)*64.0);\n\
 457+ limit.g = (threshold[x + (y * 8)]) / ((pow(2.0, float(ditherbits.g)) - 1.0)*64.0);\n\
 458+ limit.b = (threshold[x + (y * 8)]) / ((pow(2.0, float(ditherbits.b)) - 1.0)*64.0);\n\
 459+ limit.a = (threshold[x + (y * 8)]) / ((pow(2.0, float(ditherbits.a)) - 1.0)*64.0);\n\
 460+ color.r += limit.r;\n\
 461+ color.g += limit.g;\n\
 462+ color.b += limit.b;\n\
 463+ color.a += limit.a;\n\
 464+ color.r *= pow(2.0, float(ditherbits.r)) - 1.0;\n\
 465+ color.r = floor(color.r);\n\
 466+ color.g *= pow(2.0, float(ditherbits.g)) - 1.0;\n\
 467+ color.g = floor(color.g);\n\
 468+ color.b *= pow(2.0, float(ditherbits.b)) - 1.0;\n\
 469+ color.b = floor(color.b);\n\
 470+ color.a *= pow(2.0, float(ditherbits.a)) - 1.0;\n\
 471+ color.a = floor(color.a);\n\
 472+ color.r /= pow(2.0, float(ditherbits.r)) - 1.0;\n\
 473+ color.g /= pow(2.0, float(ditherbits.g)) - 1.0;\n\
 474+ color.b /= pow(2.0, float(ditherbits.b)) - 1.0;\n\
 475+ color.a /= pow(2.0, float(ditherbits.a)) - 1.0;\n\
 476+ return color;\n\
 477+}\n";
436478
437479
438 -
439480 /**
440481 * Creates an OpenGL shader program
441482 * @param This
@@ -456,6 +497,7 @@
457498 bool hasdir = false;
458499 bool haspoint = false;
459500 bool hasspot = false;
 501+ bool dither = false;
460502 int count;
461503 int numlights;
462504 int vertexfog,pixelfog;
@@ -474,6 +516,7 @@
475517 //Header
476518 STRING *vsrc = &This->genshaders[index].shader.vsrc;
477519 String_Append(vsrc, header);
 520+ String_Append(vsrc, ver110);
478521 String_Append(vsrc, vertexshader);
479522 String_Append(vsrc, idheader);
480523 String_Append(vsrc, idstring);
@@ -733,8 +776,15 @@
734777 }
735778 #endif
736779 // Create fragment shader
 780+ if ((id>>62)&1)
 781+ {
 782+ if ((This->ext->glver_major > 2) || ((This->ext->glver_major == 2) && (This->ext->glver_minor >= 1)))
 783+ dither = true;
 784+ }
737785 STRING *fsrc = &This->genshaders[index].shader.fsrc;
738786 String_Append(fsrc, header);
 787+ if (dither) String_Append(fsrc, ver120);
 788+ else String_Append(fsrc, ver110);
739789 String_Append(fsrc, fragshader);
740790 _snprintf(idstring,21,"%0.16I64X\n",id);
741791 idstring[21] = 0;
@@ -761,11 +811,14 @@
762812 }
763813 }
764814 if((id>>2)&1) String_Append(fsrc, unif_alpharef);
 815+ if (dither) String_Append(fsrc, unif_ditherbits);
765816 // Variables
766817 String_Append(fsrc, var_color);
767818 if(vertexfog && !pixelfog) String_Append(fsrc, var_fogfactorvertex);
768819 if(pixelfog) String_Append(fsrc, var_fogfactorpixel);
 820+ if (dither) String_Append(fsrc, const_threshold);
769821 // Functions
 822+ if (dither) String_Append(fsrc, func_dither);
770823 // Main
771824 String_Append(fsrc, mainstart);
772825 String_Append(fsrc, op_colorfragin);
@@ -1253,6 +1306,7 @@
12541307 String_Append(fsrc, op_fogblend);
12551308 }
12561309 if(((id>>61)&1) && !vertexfog && !pixelfog) String_Append(fsrc, op_fogassign);
 1310+ if (dither) String_Append(fsrc,op_dither);
12571311 String_Append(fsrc, op_colorfragout);
12581312 String_Append(fsrc, mainend);
12591313 String_Free(&tmp);
@@ -1390,6 +1444,7 @@
13911445 unifkey[3] = i + '0';
13921446 This->genshaders[index].shader.uniforms[142 + i] = This->ext->glGetUniformLocation(This->genshaders[index].shader.prog, unifkey);
13931447 }
 1448+ This->genshaders[index].shader.uniforms[150] = This->ext->glGetUniformLocation(This->genshaders[index].shader.prog,"ditherbits");
13941449 }
13951450
13961451 }
\ No newline at end of file
Index: ddraw/glDirect3DDevice.cpp
@@ -772,6 +772,7 @@
773773 if(renderstate[D3DRENDERSTATE_LIGHTING]) shader |= (1i64 << 59);
774774 if(renderstate[D3DRENDERSTATE_COLORVERTEX]) shader |= (1i64 << 60);
775775 if(renderstate[D3DRENDERSTATE_FOGENABLE]) shader |= (1i64 << 61);
 776+ if(renderstate[D3DRENDERSTATE_DITHERENABLE]) shader |= (1i64 << 62);
776777 for(i = 0; i < 8; i++)
777778 {
778779 if(!texstages[i].dirty) continue;
Index: ddraw/glRenderer.cpp
@@ -2013,6 +2013,7 @@
20142014 if(prog.uniforms[139]!= -1) This->ext->glUniform1f(prog.uniforms[139],device->viewport.dwX);
20152015 if(prog.uniforms[140]!= -1) This->ext->glUniform1f(prog.uniforms[140],device->viewport.dwY);
20162016 if(prog.uniforms[141]!= -1) This->ext->glUniform1i(prog.uniforms[141],device->renderstate[D3DRENDERSTATE_ALPHAREF]);
 2017+ if(prog.uniforms[150]!= -1) This->ext->glUniform4iv(prog.uniforms[150],1,(GLint*)device->glDDS7->texture->colorbits);
20172018 do
20182019 {
20192020 if (This->util->SetFBO(device->glDDS7) == GL_FRAMEBUFFER_COMPLETE) break;