DXGL r135 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r134‎ | r135 | r136 >
Date:13:18, 14 April 2012
Author:admin
Status:new
Tags:
Comment:
Add basic texcoords to vertex shader
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)

Diff [purge]

Index: ddraw/glDirect3DDevice.cpp
@@ -415,6 +415,8 @@
416416 }
417417 if(texstages[i].textransform & D3DTTFF_PROJECTED) texstages[i].shaderid |= 1i64 << 53;
418418 }
 419+ texstages[i].shaderid |= (__int64)(texstages[i].texcoordindex&7) << 54;
 420+ texstages[i].shaderid |= (__int64)((texstages[i].texcoordindex>>16)&3) << 57;
419421 return shader;
420422 }
421423
@@ -468,10 +470,11 @@
469471 for(i = 0; i < 8; i++)
470472 vertdata[i+10].data = NULL;
471473 int numtex = (dwVertexTypeDesc&D3DFVF_TEXCOUNT_MASK)>>D3DFVF_TEXCOUNT_SHIFT;
472 - for(i = 0; i < numtex; i++)
 474+ for(i = 0; i < 8; i++)
473475 {
474 - vertdata[i+10].data = &vertptr[ptr];
475 - texformats[i] = (dwVertexTypeDesc>>(16+(2*i))&3);
 476+ vertdata[i+9].data = &vertptr[ptr];
 477+ if(i >= numtex) texformats[i] = -1;
 478+ else texformats[i] = (dwVertexTypeDesc>>(16+(2*i))&3);
476479 switch(texformats[i])
477480 {
478481 case 0: // st
Index: ddraw/glDirect3DDevice.h
@@ -150,7 +150,7 @@
151151 GLubyte *specular;
152152 GLubyte *ambient;
153153 GLfloat *texcoords[8];
154 - GLVERTEX vertdata[17];
 154+ GLVERTEX vertdata[18];
155155 int texformats[8];
156156 };
157157
Index: ddraw/glRenderer.cpp
@@ -547,6 +547,7 @@
548548 glClear(GL_COLOR_BUFFER_BIT);
549549 glFlush();
550550 SwapBuffers(hDC);
 551+ SetActiveTexture(0);
551552 if(!hasHWnd)
552553 {
553554 dib.enabled = true;
@@ -700,7 +701,7 @@
701702 GLint texloc = glGetUniformLocation(shaders[PROG_TEXTURE].prog,"Texture");
702703 glUniform1i(texloc,0);
703704 }
704 - glActiveTexture(GL_TEXTURE0);
 705+ SetActiveTexture(0);
705706 if(src) glBindTexture(GL_TEXTURE_2D,src->GetTexture());
706707 GLuint prog = GetProgram()&0xffffffff;
707708 GLint viewloc = glGetUniformLocation(prog,"view");
@@ -746,7 +747,7 @@
747748 void glRenderer::_DrawBackbuffer(GLuint *texture, int x, int y)
748749 {
749750 GLfloat view[4];
750 - glActiveTexture(GL_TEXTURE0);
 751+ SetActiveTexture(0);
751752 if(!backbuffer)
752753 {
753754 backbuffer = _MakeTexture(GL_LINEAR,GL_LINEAR,GL_CLAMP_TO_EDGE,GL_CLAMP_TO_EDGE,x,y,GL_BGRA,GL_UNSIGNED_BYTE,GL_RGBA8);
@@ -854,11 +855,11 @@
855856 GLint texloc = glGetUniformLocation(shaders[PROG_PAL256].prog,"IndexTexture");
856857 glUniform1i(texloc,0);
857858 glUniform1i(palloc,1);
858 - glActiveTexture(GL_TEXTURE0);
 859+ SetActiveTexture(0);
859860 glBindTexture(GL_TEXTURE_2D,texture);
860 - glActiveTexture(GL_TEXTURE1);
 861+ SetActiveTexture(1);
861862 glBindTexture(GL_TEXTURE_2D,paltex);
862 - glActiveTexture(GL_TEXTURE0);
 863+ SetActiveTexture(0);
863864 if(dxglcfg.scalingfilter)
864865 {
865866 _DrawBackbuffer(&texture,dest->fakex,dest->fakey);
Index: ddraw/glutil.cpp
@@ -22,8 +22,9 @@
2323 GLuint fbz = 0;
2424 GLuint fbo = 0;
2525 bool stencil = false;
 26+GLint texlevel = 0;
 27+GLint texwrap[16];
2628
27 -
2829 void InitFBO()
2930 {
3031 if(GLEXT_ARB_framebuffer_object)
@@ -103,3 +104,51 @@
104105 ::stencil = stencil;
105106 return error;
106107 }
 108+
 109+void SetActiveTexture(int level)
 110+{
 111+ if(level != texlevel)
 112+ {
 113+ texlevel = level;
 114+ glActiveTexture(GL_TEXTURE0+level);
 115+ }
 116+}
 117+
 118+void SetWrap(int level, DWORD coord, DWORD address)
 119+{
 120+ if(level == -1)
 121+ {
 122+ for(int i = 0; i < 16; i++)
 123+ texwrap[i] = GL_REPEAT;
 124+ }
 125+ if(coord > 1) return;
 126+ if(level > 8) return;
 127+ if(level < 0) return;
 128+ GLint wrapmode;
 129+ switch(address)
 130+ {
 131+ case D3DTADDRESS_WRAP:
 132+ wrapmode = GL_REPEAT;
 133+ break;
 134+ case D3DTADDRESS_MIRROR:
 135+ wrapmode = GL_MIRRORED_REPEAT;
 136+ break;
 137+ case D3DTADDRESS_CLAMP:
 138+ wrapmode = GL_CLAMP_TO_EDGE;
 139+ break;
 140+ case D3DTADDRESS_BORDER:
 141+ wrapmode = GL_CLAMP_TO_BORDER;
 142+ break;
 143+ default:
 144+ return;
 145+ }
 146+ if(texwrap[level*2+coord] == wrapmode) return;
 147+ else
 148+ {
 149+ int currtexture = texlevel;
 150+ SetActiveTexture(level);
 151+ if(coord) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,wrapmode);
 152+ else glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,wrapmode);
 153+ SetActiveTexture(currtexture);
 154+ }
 155+}
\ No newline at end of file
Index: ddraw/glutil.h
@@ -25,6 +25,7 @@
2626
2727 void InitFBO();
2828 void DeleteFBO();
 29+void SetActiveTexture(int level);
2930 GLenum SetFBO(GLint color, GLint z, bool stencil);
3031
3132 #endif //_GLUTIL_H
\ No newline at end of file
Index: ddraw/shadergen.cpp
@@ -34,55 +34,60 @@
3535 int current_genshader;
3636
3737 /* Bits in Shader ID:
38 -Bits 0-1 - Shading mode: 00=flat 01=gouraud 11=phong 10=flat per-pixel
39 -Bit 2 - Alpha test enable
40 -Bits 3-5 - Alpha test function:
 38+Bits 0-1 - Shading mode: 00=flat 01=gouraud 11=phong 10=flat per-pixel VS/FS
 39+Bit 2 - Alpha test enable FS
 40+Bits 3-5 - Alpha test function: FS
4141 000=never 001=less 010=equal 011=lessequal
4242 100=greater 101=notequal 110=lessequal 111=always
43 -Bits 6-7 - Table fog:
 43+Bits 6-7 - Table fog: FS
4444 00 = none 01=exp 10=exp2 11=linear
45 -Bits 8-9 - Vertex fog: same as table
46 -Bit 10 - Range based fog
47 -Bit 11 - Specular highlights
48 -Bit 12 - Stippled alpha
49 -Bit 13 - Color key transparency
50 -Bit 14-17 - Z bias
51 -Bits 18-20 - Number of lights
52 -Bit 21 - Camera relative specular highlights
53 -Bit 22 - Alpha blended color key
54 -Bits 23-24 - Diffuse material source
55 -Bits 25-26 - Specular material source
56 -Bits 27-28 - Ambient material source
57 -Bits 29-30 - Emissive material source
58 -Bits 31-33 - Number of textures
59 -Bit 34 - Use transformed vertices
60 -Bit 35 - Use diffuse color
61 -Bit 36 - Use specular color
62 -Bit 37 - Enable normals
63 -Bits 38-45 - Light types
64 -Bits 46-48 - Number of blending weights
65 -Bit 49 - Normalize normals
 45+Bits 8-9 - Vertex fog: same as table VS
 46+Bit 10 - Range based fog VS/FS
 47+Bit 11 - Specular highlights VS/FS
 48+Bit 12 - Stippled alpha FS
 49+Bit 13 - Color key transparency FS
 50+Bit 14-17 - Z bias FS
 51+Bits 18-20 - Number of lights VS/FS
 52+Bit 21 - Camera relative specular highlights VS/FS
 53+Bit 22 - Alpha blended color key FS
 54+Bits 23-24 - Diffuse material source VS
 55+Bits 25-26 - Specular material source VS
 56+Bits 27-28 - Ambient material source VS
 57+Bits 29-30 - Emissive material source VS
 58+Bits 31-33 - Number of textures VS/FS
 59+Bit 34 - Use transformed vertices VS
 60+Bit 35 - Use diffuse color VS
 61+Bit 36 - Use specular color VS
 62+Bit 37 - Enable normals VS
 63+Bits 38-45 - Light types VS/FS
 64+Bits 46-48 - Number of blending weights VS
 65+Bit 49 - Normalize normals VS
6666 */
6767
6868 /* Bits in Texture Stage ID:
69 -Bits 0-4: Texture color operation
70 -Bits 5-10: Texture color argument 1
71 -Bits 11-16: Texture color argument 2
72 -Bits 17-21: Texture alpha operation
73 -Bits 22-27: Texture alpha argument 1
74 -Bits 28-33: Texture alpha argument 2
75 -Bits 34-36: Texture coordinate index
76 -Bits 37-38: Texture coordinate flags
77 -Bits 39-40: U Texture address
78 -Bits 41-42: V Texture address
79 -Bits 43-45: Texture magnification filter
80 -Bits 46-47: Texture minification filter
81 -Bits 48-49: Texture mip filter
82 -Bit 50: Enable texture coordinate transform
83 -Bits 51-52: Number of texcoord dimensions
84 -Bit 53: Projected texcoord
 69+Bits 0-4: Texture color operation FS
 70+Bits 5-10: Texture color argument 1 FS
 71+Bits 11-16: Texture color argument 2 FS
 72+Bits 17-21: Texture alpha operation FS
 73+Bits 22-27: Texture alpha argument 1 FS
 74+Bits 28-33: Texture alpha argument 2 FS
 75+Bits 34-36: Texture coordinate index VS
 76+Bits 37-38: Texture coordinate flags VS
 77+Bits 39-40: U Texture address GL
 78+Bits 41-42: V Texture address GL
 79+Bits 43-45: Texture magnification filter GL/FS
 80+Bits 46-47: Texture minification filter GL/FS
 81+Bits 48-49: Texture mip filter GL/FS
 82+Bit 50: Enable texture coordinate transform VS
 83+Bits 51-52: Number of texcoord dimensions VS
 84+Bit 53: Projected texcoord VS
8585 Bits in texcoord ID:
8686 00=2dim 01=3dim 10=4dim 11=1dim
 87+Bits 54-56: Texture coordinate index VS
 88+Bits 57-58: Texture coordinate flags VS
 89+Bits in flags:
 90+00=passthru 01=cameraspacenormal
 91+10=cameraspaceposition 11=cameraspacereflectionvector
8792 */
8893 void ZeroShaderArray()
8994 {
@@ -256,6 +261,12 @@
257262 gl_FrontColor = color;\n";
258263 static const char op_colorfragout[] = "gl_FragColor = color;\n";
259264 static const char op_colorfragin[] = "color = gl_Color;\n";
 265+static const char op_texpassthru1[] = "gl_TexCoord[x] = ";
 266+static const char op_texpassthru2s[] = "vec4(sX,0,0,1);\n";
 267+static const char op_texpassthru2st[] = "vec4(stX,0,1);\n";
 268+static const char op_texpassthru2str[] = "vec4(strX,1);\n";
 269+static const char op_texpassthru2strq[] = "strqX;\n";
 270+static const char op_texpassthru2null[] = "vec4(0,0,0,1);\n";
260271
261272 // Functions
262273 static const char func_dirlight[] = "void DirLight(in Light light)\n\
@@ -330,6 +341,31 @@
331342 vsrc->append(tmp);
332343 }
333344 }
 345+ for(i = 0; i < 8; i++)
 346+ {
 347+ switch(texcoords[i])
 348+ {
 349+ case -1:
 350+ continue;
 351+ case 3:
 352+ tmp = attr_s;
 353+ tmp.replace(16,1,_itoa(i,idstring,10));
 354+ break;
 355+ case 0:
 356+ tmp = attr_st;
 357+ tmp.replace(17,1,_itoa(i,idstring,10));
 358+ break;
 359+ case 1:
 360+ tmp = attr_str;
 361+ tmp.replace(18,1,_itoa(i,idstring,10));
 362+ break;
 363+ case 2:
 364+ tmp = attr_strq;
 365+ tmp.replace(19,1,_itoa(i,idstring,10));
 366+ break;
 367+ }
 368+ vsrc->append(tmp);
 369+ }
334370
335371 // Uniforms
336372 vsrc->append(unif_matrices); // Material
@@ -402,6 +438,48 @@
403439 }
404440 }
405441 vsrc->append(op_colorout);
 442+ int texindex;
 443+ for(i = 0; i < 8; i++)
 444+ {
 445+ if((texstate[i].shaderid>>50)&1)
 446+ {
 447+ FIXME("Support texture coordinate transform");
 448+ }
 449+ else
 450+ {
 451+ tmp = op_texpassthru1;
 452+ tmp.replace(12,1,_itoa(i,idstring,10));
 453+ vsrc->append(tmp);
 454+ texindex = (texstate[i].shaderid>>54)&3;
 455+ switch(texcoords[texindex])
 456+ {
 457+ case -1: // No texcoords
 458+ vsrc->append(op_texpassthru2null);
 459+ break;
 460+ case 0: // st
 461+ tmp = op_texpassthru2st;
 462+ tmp.replace(7,1,_itoa(texindex,idstring,10));
 463+ vsrc->append(tmp);
 464+ default:
 465+ break;
 466+ case 1: // str
 467+ tmp = op_texpassthru2str;
 468+ tmp.replace(8,1,_itoa(texindex,idstring,10));
 469+ vsrc->append(tmp);
 470+ break;
 471+ case 2: // strq
 472+ tmp = op_texpassthru2strq;
 473+ tmp.replace(4,1,_itoa(texindex,idstring,10));
 474+ vsrc->append(tmp);
 475+ break;
 476+ case 3: // s
 477+ tmp = op_texpassthru2s;
 478+ tmp.replace(6,1,_itoa(texindex,idstring,10));
 479+ vsrc->append(tmp);
 480+ break;
 481+ }
 482+ }
 483+ }
406484 vsrc->append(mainend);
407485 #ifdef _DEBUG
408486 OutputDebugStringA("Vertex shader:\n");