DXGL r103 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r102‎ | r103 | r104 >
Date:18:11, 18 February 2012
Author:admin
Status:new
Tags:
Comment:
Vertex shader: Generate directional lights and basic transform
Modified paths:
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/shadergen.cpp (modified) (history)

Diff [purge]

Index: ddraw/glRenderer.cpp
@@ -1031,13 +1031,6 @@
10321032 wndbusy = false;
10331033 return;
10341034 }
1035 - if(dwVertexTypeDesc & D3DFVF_XYZB1)
1036 - {
1037 - outputs[0] = (void*)DDERR_GENERIC;
1038 - wndbusy = false;
1039 - FIXME("glDirect3DDevice::DrawIndexedPrimitive: D3DFVF_XYZB1 stub");
1040 - return;
1041 - }
10421035 SetShader(device->SelectShader(dwVertexTypeDesc),NULL,0);
10431036
10441037 FIXME("glDirect3DDevice::DrawIndexedPrimitive: stub");
Index: ddraw/shadergen.cpp
@@ -174,12 +174,9 @@
175175 static const char mainend[] = "} ";
176176 // Attributes
177177 static const char attr_xyz[] = "attribute vec3 xyz;\n";
178 -static const char conv_xyz[] = "vec4 xyzw = vec4(xyz[0],xyz[1],xyz[2],1);\n";
179178 static const char attr_xyzw[] = "attribute vec4 xyzw;\n";
180179 static const char attr_nxyz[] = "attribute vec3 nxyz;\n";
181180 static const char attr_blend[] = "attribute float blendX;\n";
182 -static const char attr_rgb[] = "attribute vec3 rgbX;\n";
183 -static const char conv_rgb[] = "vec4 rgbaX = vec4(rgbX[0],rgbX[1],rgbX[2],1);\n";
184181 static const char attr_rgba[] = "attribute vec4 rgbaX;\n";
185182 static const char attr_s[] = "attribute float sX;\n";
186183 static const char conv_s[] = "vec4 strqX = vec4(sX,0,0,1);\n";
@@ -189,16 +186,16 @@
190187 static const char conv_str[] = "vec4 strqX = vec4(strX[0],strX[1],strX[2],1);\n";
191188 static const char attr_strq[] = "attribute vec4 strqX;\n";
192189 // Uniforms
193 -static const char unif_mats[] = "uniform mat4 world;\n\
 190+static const char unif_matrices[] = "uniform mat4 world;\n\
194191 uniform mat4 view;\n\
195 -uniform mat4 projection;\n";
196 -static const char modelview[] = "mat4 modelview = world * view;\n";
 192+uniform mat4 projection;\n\
 193+uniform mat4 normalmat;\n";
197194 static const char unif_material[] = "struct Material\n\
198195 {\n\
199196 vec4 diffuse;\n\
200197 vec4 ambient;\n\
201198 vec4 specular;\n\
202 -vec4 emussive;\n\
 199+vec4 emissive;\n\
203200 int power;\n\
204201 };\n\
205202 uniform Material material;\n";
@@ -218,13 +215,49 @@
219216 float phi;\n\
220217 };\n";
221218 static const char unif_light[] = "uniform Light lightX;\n";
 219+// Variables
 220+static const char var_colors[] = "vec4 diffuse;\n\
 221+vec4 specular;\n\
 222+vec4 ambient;\n";
 223+static const char var_xyzw[] = "vec4 xyzw;\n";
222224 // Operations
223 -static const char normalize[] = "vec3 N = normalize(vec3(modelview*vec4(nxyz,0.0)));\n";
 225+static const char op_transform[] = "xyzw = vec4(xyz[0],xyz[1],xyz[2],1);\n\
 226+gl_Position = ((world*view)*projection)*xyzw;\n";
 227+static const char op_passthru[] = "gl_Position = xyzw;\n";
 228+static const char op_resetcolor[] = "diffuse = specular = ambient = vec4(0.0);\n";
 229+static const char op_dirlight[] = "DirLight(lightX);\n";
 230+static const char op_spotlight[] = "SpotLight(lightX);\n";
 231+static const char op_colorout[] = "vec4 color = (material.diffuse * diffuse) + (material.ambient * ambient) + \n\
 232+(material.specular * specular) + material.emissive;\n";
224233
 234+// Functions
 235+static const char func_dirlight[] = "void DirLight(in Light light)\n\
 236+{\n\
 237+float NdotHV = 0.0;\n\
 238+vec3 N = normalize(vec3(normalmat*vec4(nxyz,0.0)));\n\
 239+vec3 dir = normalize(light.direction);\n\
 240+ambient += light.ambient;\n\
 241+float NdotL = max(dot(N,dir),0.0);\n\
 242+diffuse += light.diffuse*NdotL;\n\
 243+if(NdotL > 0.0)\n\
 244+{\n\
 245+vec3 eye = (-view[3].xyz / view[3].w);\n\
 246+vec3 P = vec3((world*view)*xyzw);\n\
 247+vec3 L = normalize(light.position.xyz - P);\n\
 248+vec3 V = normalize(eye - P);\n\
 249+NdotHV = max(dot(N,L+V),0.0);\n\
 250+specular += pow(NdotHV,float(material.power));\n\
 251+}\n\
 252+}\n";
 253+
225254 void CreateShader(int index, __int64 id, TexState *texstate)
226255 {
227256 string tmp;
228257 int i;
 258+ bool hasdir = false;
 259+ bool hasspot = false;
 260+ int count;
 261+ int numlights;
229262 char idstring[22];
230263 _snprintf(idstring,21,"%0.16I64X\n",id);
231264 idstring[21] = 0;
@@ -238,23 +271,89 @@
239272 vsrc->append(idheader);
240273 vsrc->append(idstring);
241274 //Variables
242 - vsrc->append(unif_mats);
 275+ // Attributes
 276+ if((id>>34)&1) vsrc->append(attr_xyzw);
 277+ else vsrc->append(attr_xyz);
 278+ tmp = attr_rgba;
 279+ if((id>>35)&1)
 280+ {
 281+ tmp.replace(19,1,"0");
 282+ vsrc->append(tmp);
 283+ }
 284+ if((id>>36)&1)
 285+ {
 286+ tmp.replace(19,1,"1");
 287+ vsrc->append(tmp);
 288+ }
 289+ if((id>>37)&1) vsrc->append(attr_nxyz);
 290+ count = (id>>46)&7;
 291+ if(count)
 292+ {
 293+ tmp = attr_blend;
 294+ for(i = 0; i < count; i++)
 295+ {
 296+ tmp.replace(21,1,_itoa(i,idstring,10));
 297+ vsrc->append(tmp);
 298+ }
 299+ }
 300+
 301+ // Uniforms
 302+ vsrc->append(unif_matrices); // Material
243303 vsrc->append(unif_material);
244 - if((id>>15)&8) // Lighting
 304+ numlights = (id>>18)&7;
 305+ if(numlights) // Lighting
245306 {
246307 vsrc->append(lightstruct);
247 - for(i = 0; i < ((id>>15)&8); i++)
 308+ tmp = unif_light;
 309+ for(i = 0; i < numlights; i++)
248310 {
249 - tmp = unif_light;
250311 tmp.replace(19,1,_itoa(i,idstring,10));
251312 vsrc->append(tmp);
252313 }
253314 }
254315
 316+ // Variables
 317+ vsrc->append(var_colors);
 318+ if(!((id>>34)&1)) vsrc->append(var_xyzw);
 319+
 320+ // Functions
 321+ if(numlights)
 322+ {
 323+ for(i = 0; i < numlights; i++)
 324+ {
 325+ if(id>>(38+i)&1) hasspot = true;
 326+ else hasdir = true;
 327+ }
 328+ }
 329+ if(hasspot) FIXME("Add spot lights");
 330+ if(hasdir) vsrc->append(func_dirlight);
255331 //Main
256332 vsrc->append(mainstart);
257 -
258 -
 333+ if((id>>34)&1) vsrc->append(op_passthru);
 334+ else vsrc->append(op_transform);
 335+ vsrc->append(op_resetcolor);
 336+ if(numlights)
 337+ {
 338+ for(i = 0; i < numlights; i++)
 339+ {
 340+ if(id>>(38+i)&1)
 341+ {
 342+ tmp = op_spotlight;
 343+ tmp.replace(15,1,_itoa(i,idstring,10));
 344+ vsrc->append(tmp);
 345+ }
 346+ else
 347+ {
 348+ tmp = op_dirlight;
 349+ tmp.replace(14,1,_itoa(i,idstring,10));
 350+ vsrc->append(tmp);
 351+ }
 352+ }
 353+ }
 354+ vsrc->append(op_colorout);
259355 vsrc->append(mainend);
260 -
 356+#ifdef _DEBUG
 357+ OutputDebugStringA("Vertex shader:\n");
 358+ OutputDebugStringA(vsrc->c_str());
 359+#endif
261360 }