DXGL r452 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r451‎ | r452 | r453 >
Date:13:49, 29 June 2014
Author:admin
Status:new
Tags:
Comment:
Implement 2D blitting shader generator and fix related bugs.
Modified paths:
  • /ddraw/ShaderGen2D.cpp (modified) (history)
  • /ddraw/ShaderGen2D.h (modified) (history)
  • /ddraw/ShaderGen3D.cpp (modified) (history)
  • /ddraw/ShaderManager.cpp (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)

Diff [purge]

Index: ddraw/ShaderGen2D.cpp
@@ -158,9 +158,9 @@
159159
160160 // Uniforms
161161 static const char unif_view[] = "uniform vec4 view;\n";
162 -static const char unif_srctex[] = "uniform sampler2d srctex;\n";
163 -static const char unif_desttex[] = "uniform sampler2d desttex;\n";
164 -static const char unif_patterntex[] = "uniform sampler2d patterntex;\n";
 162+static const char unif_srctex[] = "uniform sampler2D srctex;\n";
 163+static const char unif_desttex[] = "uniform sampler2D desttex;\n";
 164+static const char unif_patterntex[] = "uniform sampler2D patterntex;\n";
165165 static const char unif_ckeysrc[] = "uniform ivec3 ckeysrc;\n";
166166 static const char unif_ckeydest[] = "uniform ivec3 ckeydest;\n";
167167
@@ -171,11 +171,11 @@
172172 static const char var_pixel[] = "ivec4 pixel;\n";
173173
174174 // Operations
175 -static const char op_src[] = "pixel = ivec4(texture2D(src,gl_TexCoord[0].st)*255.5);\n";
176 -static const char op_color[] = "pixel = ivec4(gl_FragColor*255.5);\n";
177 -static const char op_dest[] = "dest = ivec4(texture2D(dest,gl_TexCoord[1].st)*255.5);\n";
178 -static const char op_pattern[] = "pattern = ivec4(texture2D(pattern,gl_TexCoord[2].st)*255.5);\n";
179 -static const char op_destout[] = "gl_FragColor = vec4(pixel)/255.5;\n";
 175+static const char op_src[] = "pixel = ivec4(texture2D(srctex,gl_TexCoord[0].st)*255.0);\n";
 176+static const char op_color[] = "pixel = ivec4(gl_FragColor*255.0);\n";
 177+static const char op_dest[] = "dest = ivec4(texture2D(desttex,gl_TexCoord[1].st)*255.0);\n";
 178+static const char op_pattern[] = "pattern = ivec4(texture2D(patterntex,gl_TexCoord[2].st)*255.0);\n";
 179+static const char op_destout[] = "gl_FragColor = vec4(pixel)/255.0;\n";
180180 static const char op_vertex[] = "vec4 xyzw = vec4(xy[0],xy[1],0,1);\n\
181181 mat4 proj = mat4(\n\
182182 vec4(2.0 / (view[1] - view[0]), 0, 0, 0),\n\
@@ -185,8 +185,8 @@
186186 -(view[2] + view[3]) / (view[2] - view[3]), -1 , 1));\n\
187187 gl_Position = proj * xyzw;\n";
188188 static const char op_vertcolorrgb[] = "gl_FrontColor = vec4(rgb,1.0);\n";
189 -static const char op_texcoord0[] = "gl_TexCoord[0] = vec4(st,0.0,1.0);\n";
190 -static const char op_ckeysrc[] = "if(pixel == ckey) discard;\n";
 189+static const char op_texcoord0[] = "gl_TexCoord[0] = vec4(srcst,0.0,1.0);\n";
 190+static const char op_ckeysrc[] = "if(pixel.rgb == ckeysrc) discard;\n";
191191
192192
193193 // Functions
@@ -710,17 +710,29 @@
711711 "gl_FragColor = vec4(1.0);\n",//FF WHITENESS
712712 };
713713
 714+DWORD PackROPBits(DWORD rop, DWORD flags)
 715+{
 716+ DWORD out = rop & 0xF2FAADFF;
 717+ if (rop & 0x10000) out |= 1 << 9;
 718+ if (rop & 0x20000) out |= 1 << 12;
 719+ if (rop & 0x40000) out |= 1 << 14;
 720+ if (rop & 0x80000) out |= 1 << 16;
 721+ if (rop & 0x100000) out |= 1 << 18;
 722+ if (rop & 0x200000) out |= 1 << 24;
 723+ if (rop & 0x400000) out |= 1 << 26;
 724+ if (rop & 0x800000) out |= 1 << 27;
 725+ return out;
 726+}
 727+
714728 void ShaderGen2D_Init(ShaderGen2D *gen, glExtensions *ext, ShaderManager *shaderman)
715729 {
716730 gen->ext = ext;
717731 gen->shaders = shaderman;
718 - gen->current_genshader2D = 0;
719732 gen->shadercount = 0;
720733 gen->genindex = 0;
721734 gen->maxshaders = 256;
722735 gen->genshaders2D = (GenShader2D *)malloc(256 * sizeof(GenShader2D));
723736 ZeroMemory(gen->genshaders2D, 256 * sizeof(GenShader2D));
724 - gen->current_genshader2D = 0;
725737 }
726738
727739 void ShaderGen2D_Delete(ShaderGen2D *gen)
@@ -737,7 +749,6 @@
738750 }
739751 if (gen->genshaders2D) free(gen->genshaders2D);
740752 gen->genshaders2D = NULL;
741 - gen->current_genshader2D = 0;
742753 gen->shadercount = 0;
743754 gen->genindex = 0;
744755 }
@@ -754,7 +765,7 @@
755766 idstring[21] = 0;
756767 // Create vertex shader
757768 // Header
758 - STRING *vsrc = &gen->genshaders2D->shader.vsrc;
 769+ STRING *vsrc = &gen->genshaders2D[index].shader.vsrc;
759770 String_Append(vsrc, revheader);
760771 if (id & DDBLT_ROP)
761772 {
@@ -815,7 +826,7 @@
816827 #endif
817828
818829 // Create fragment shader
819 - STRING *fsrc = &gen->genshaders2D->shader.fsrc;
 830+ STRING *fsrc = &gen->genshaders2D[index].shader.fsrc;
820831 String_Append(fsrc, revheader);
821832 if (id & DDBLT_ROP)
822833 {
Index: ddraw/ShaderGen2D.h
@@ -40,7 +40,6 @@
4141
4242 typedef struct ShaderGen2D
4343 {
44 - int current_genshader2D;
4544 GenShader2D *genshaders2D;
4645 int shadercount;
4746 int maxshaders;
@@ -54,6 +53,8 @@
5554 extern const DWORD supported_rops[8];
5655 extern const DWORD supported_rops_gl2[8];
5756
 57+DWORD PackROPBits(DWORD rop, DWORD flags);
 58+
5859 void ShaderGen2D_Init(ShaderGen2D *gen, glExtensions *ext, ShaderManager *shaderman);
5960 void ShaderGen2D_Delete(ShaderGen2D *gen);
6061 void ShaderGen2D_CreateShader2D(ShaderGen2D *gen, int index, DWORD id);
Index: ddraw/ShaderGen3D.cpp
@@ -165,7 +165,7 @@
166166 if ((current_shadertype == 1) && (id == current_shader)) return;
167167 current_shader = id;
168168 current_shadertype = 1;
169 - for (int i = 0; i < shadercount; i++)
 169+ for (int i = 0; i < gen2d->shadercount; i++)
170170 {
171171 if (shaders->gen2d->genshaders2D[i].id == id)
172172 {
Index: ddraw/ShaderManager.cpp
@@ -174,8 +174,8 @@
175175 }
176176 gen3d = new ShaderGen3D(ext, this);
177177 gen2d = (ShaderGen2D*)malloc(sizeof(ShaderGen2D));
 178+ ZeroMemory(gen2d, sizeof(ShaderGen2D));
178179 ShaderGen2D_Init(gen2d, ext, this);
179 - ZeroMemory(gen2d, sizeof(ShaderGen2D));
180180 }
181181
182182 ShaderManager::~ShaderManager()
Index: ddraw/glRenderer.cpp
@@ -872,9 +872,13 @@
873873 void glRenderer::_Blt(LPRECT lpDestRect, glDirectDrawSurface7 *src,
874874 glDirectDrawSurface7 *dest, LPRECT lpSrcRect, DWORD dwFlags, LPDDBLTFX lpDDBltFx)
875875 {
876 - int progtype;
877876 LONG sizes[6];
878877 ddInterface->GetSizes(sizes);
 878+ DWORD shaderid;
 879+ if (lpDDBltFx) shaderid = PackROPBits(lpDDBltFx->dwROP, dwFlags);
 880+ else shaderid = dwFlags & 0xF2FAADFF;
 881+ shaders->SetShader(shaderid, NULL, NULL, 1);
 882+ GenShader2D *shader = &shaders->gen2d->genshaders2D[shaders->gen3d->current_genshader];
879883 util->BlendEnable(false);
880884 util->SetFBO(dest);
881885 util->SetViewport(0,0,dest->fakex,dest->fakey);
@@ -914,8 +918,6 @@
915919 if(dest->zbuffer) glClear(GL_DEPTH_BUFFER_BIT);
916920 if(dwFlags & DDBLT_COLORFILL)
917921 {
918 - shaders->SetShader(PROG_FILL,NULL,NULL,0);
919 - progtype = PROG_FILL;
920922 switch(ddInterface->GetBPP())
921923 {
922924 case 8:
@@ -954,22 +956,20 @@
955957 }
956958 if((dwFlags & DDBLT_KEYSRC) && (src && src->colorkey[0].enabled) && !(dwFlags & DDBLT_COLORFILL))
957959 {
958 - shaders->SetShader(PROG_CKEY,NULL,NULL,0);
959 - progtype = PROG_CKEY;
960960 switch(ddInterface->GetBPP())
961961 {
962962 case 8:
963 - if(ext->glver_major >= 3) ext->glUniform3i(shaders->shaders[progtype].ckey,src->colorkey[0].key.dwColorSpaceHighValue,0,0);
964 - else ext->glUniform3i(shaders->shaders[progtype].ckey,src->colorkey[0].key.dwColorSpaceHighValue,src->colorkey[0].key.dwColorSpaceHighValue,
 963+ if(ext->glver_major >= 3) ext->glUniform3i(shader->shader.uniforms[4],src->colorkey[0].key.dwColorSpaceHighValue,0,0);
 964+ else ext->glUniform3i(shader->shader.uniforms[4],src->colorkey[0].key.dwColorSpaceHighValue,src->colorkey[0].key.dwColorSpaceHighValue,
965965 src->colorkey[0].key.dwColorSpaceHighValue);
966966 break;
967967 case 15:
968 - ext->glUniform3i(shaders->shaders[progtype].ckey,_5to8bit(src->colorkey[0].key.dwColorSpaceHighValue>>10 & 31),
 968+ ext->glUniform3i(shader->shader.uniforms[4],_5to8bit(src->colorkey[0].key.dwColorSpaceHighValue>>10 & 31),
969969 _5to8bit(src->colorkey[0].key.dwColorSpaceHighValue>>5 & 31),
970970 _5to8bit(src->colorkey[0].key.dwColorSpaceHighValue & 31));
971971 break;
972972 case 16:
973 - ext->glUniform3i(shaders->shaders[progtype].ckey,_5to8bit(src->colorkey[0].key.dwColorSpaceHighValue>>11 & 31),
 973+ ext->glUniform3i(shader->shader.uniforms[4],_5to8bit(src->colorkey[0].key.dwColorSpaceHighValue>>11 & 31),
974974 _6to8bit(src->colorkey[0].key.dwColorSpaceHighValue>>5 & 63),
975975 _5to8bit(src->colorkey[0].key.dwColorSpaceHighValue & 31));
976976 break;
@@ -976,18 +976,16 @@
977977 case 24:
978978 case 32:
979979 default:
980 - ext->glUniform3i(shaders->shaders[progtype].ckey,(src->colorkey[0].key.dwColorSpaceHighValue>>16 & 255),
 980+ ext->glUniform3i(shader->shader.uniforms[4],(src->colorkey[0].key.dwColorSpaceHighValue>>16 & 255),
981981 (src->colorkey[0].key.dwColorSpaceHighValue>>8 & 255),
982982 (src->colorkey[0].key.dwColorSpaceHighValue & 255));
983983 break;
984984 }
985 - ext->glUniform1i(shaders->shaders[progtype].tex0,0);
 985+ ext->glUniform1i(shader->shader.uniforms[1],0);
986986 }
987987 else if(!(dwFlags & DDBLT_COLORFILL))
988988 {
989 - shaders->SetShader(PROG_TEXTURE,NULL,NULL,0);
990 - progtype = PROG_TEXTURE;
991 - ext->glUniform1i(shaders->shaders[progtype].tex0,0);
 989+ ext->glUniform1i(shader->shader.uniforms[1],0);
992990 }
993991 if(src)
994992 {
@@ -999,19 +997,19 @@
1000998 }
1001999 }
10021000 else TextureManager_SetTexture(texman,0,NULL);
1003 - ext->glUniform4f(shaders->shaders[progtype].view,0,(GLfloat)dest->fakex,0,(GLfloat)dest->fakey);
 1001+ ext->glUniform4f(shader->shader.uniforms[0],0,(GLfloat)dest->fakex,0,(GLfloat)dest->fakey);
10041002 dest->dirty |= 2;
1005 - util->EnableArray(shaders->shaders[progtype].pos,true);
1006 - ext->glVertexAttribPointer(shaders->shaders[progtype].pos,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].x);
1007 - if(shaders->shaders[progtype].rgb != -1)
 1003+ util->EnableArray(shader->shader.attribs[0],true);
 1004+ ext->glVertexAttribPointer(shader->shader.attribs[0],2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].x);
 1005+ if(shader->shader.attribs[1] != -1)
10081006 {
1009 - util->EnableArray(shaders->shaders[progtype].rgb,true);
1010 - ext->glVertexAttribPointer(shaders->shaders[progtype].rgb,3,GL_UNSIGNED_BYTE,true,sizeof(BltVertex),&bltvertices[0].r);
 1007+ util->EnableArray(shader->shader.attribs[1],true);
 1008+ ext->glVertexAttribPointer(shader->shader.attribs[0],3,GL_UNSIGNED_BYTE,true,sizeof(BltVertex),&bltvertices[0].r);
10111009 }
10121010 if(!(dwFlags & DDBLT_COLORFILL))
10131011 {
1014 - util->EnableArray(shaders->shaders[progtype].texcoord,true);
1015 - ext->glVertexAttribPointer(shaders->shaders[progtype].texcoord,2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].s);
 1012+ util->EnableArray(shader->shader.attribs[3],true);
 1013+ ext->glVertexAttribPointer(shader->shader.attribs[3],2,GL_FLOAT,false,sizeof(BltVertex),&bltvertices[0].s);
10161014 }
10171015 util->SetCull(D3DCULL_NONE);
10181016 util->SetPolyMode(D3DFILL_SOLID);