DXGL r77 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r76‎ | r77 | r78 >
Date:17:25, 15 January 2012
Author:admin
Status:new
Tags:
Comment:
Use shaders for all drawing operations.
Use version.h for GetDeviceIdentifier
Modified paths:
  • /common/version.h (modified) (history)
  • /ddraw/ddraw.vcxproj (modified) (history)
  • /ddraw/ddraw.vcxproj.filters (modified) (history)
  • /ddraw/glDirectDraw.cpp (modified) (history)
  • /ddraw/glDirectDrawSurface.cpp (modified) (history)
  • /ddraw/glExtensions.cpp (modified) (history)
  • /ddraw/glExtensions.h (modified) (history)
  • /ddraw/shadergen.cpp (added) (history)
  • /ddraw/shadergen.h (added) (history)
  • /ddraw/shaders.cpp (modified) (history)
  • /ddraw/shaders.h (modified) (history)

Diff [purge]

Index: common/version.h
@@ -8,6 +8,7 @@
99 #define DXGLBUILD 0
1010
1111 #define DXGLVERNUMBER DXGLMAJOR,DXGLMINOR,DXGLPOINT,DXGLBUILD
 12+#define DXGLVERQWORD (((unsigned __int64)DXGLMAJOR<<48)+((unsigned __int64)DXGLMINOR<<32)+((unsigned __int64)DXGLPOINT<<16)+(unsigned __int64)DXGLBUILD)
1213 #define DXGLVERSTRING "0.1.3.0"
1314
1415
Index: ddraw/ddraw.vcxproj
@@ -179,6 +179,7 @@
180180 <ClInclude Include="include\GL\wglext.h" />
181181 <ClInclude Include="include\winedef.h" />
182182 <ClInclude Include="scalers.h" />
 183+ <ClInclude Include="shadergen.h" />
183184 <ClInclude Include="shaders.h" />
184185 </ItemGroup>
185186 <ItemGroup>
@@ -220,6 +221,7 @@
221222 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
222223 </ClCompile>
223224 <ClCompile Include="scalers.cpp" />
 225+ <ClCompile Include="shadergen.cpp" />
224226 <ClCompile Include="shaders.cpp" />
225227 </ItemGroup>
226228 <ItemGroup>
Index: ddraw/ddraw.vcxproj.filters
@@ -89,6 +89,9 @@
9090 <ClInclude Include="glutil.h">
9191 <Filter>Header Files</Filter>
9292 </ClInclude>
 93+ <ClInclude Include="shadergen.h">
 94+ <Filter>Header Files</Filter>
 95+ </ClInclude>
9396 </ItemGroup>
9497 <ItemGroup>
9598 <ClCompile Include="ddraw.cpp">
@@ -139,6 +142,9 @@
140143 <ClCompile Include="glutil.cpp">
141144 <Filter>Source Files</Filter>
142145 </ClCompile>
 146+ <ClCompile Include="shadergen.cpp">
 147+ <Filter>Source Files</Filter>
 148+ </ClCompile>
143149 </ItemGroup>
144150 <ItemGroup>
145151 <ResourceCompile Include="ddraw.rc">
Index: ddraw/glDirectDraw.cpp
@@ -24,6 +24,7 @@
2525 #include "glDirectDrawSurface.h"
2626 #include "glDirectDrawPalette.h"
2727 #include "glutil.h"
 28+#include "../common/version.h"
2829
2930 bool directdraw_created = false; // emulate only one ddraw device
3031 bool wndclasscreated = false;
@@ -31,7 +32,7 @@
3233 DDDEVICEIDENTIFIER2 devid = {
3334 "ddraw.dll",
3435 "DXGL DDraw Wrapper",
35 - 0x0000000000070000,
 36+ 0,
3637 0,0,0,0,
3738 0,0};
3839
@@ -548,6 +549,7 @@
549550 glDirectDraw7::glDirectDraw7()
550551 {
551552 initialized = false;
 553+ devid.liDriverVersion.QuadPart = DXGLVERQWORD;
552554 refcount = 1;
553555 }
554556
@@ -559,6 +561,7 @@
560562 error = DDERR_INVALIDPARAMS ;
561563 return;
562564 }
 565+ devid.liDriverVersion.QuadPart = DXGLVERQWORD;
563566 error = glDirectDraw7::Initialize(lpGUID);
564567 refcount = 1;
565568 }
@@ -1365,10 +1368,6 @@
13661369 SetSwap(1);
13671370 SetSwap(0);
13681371 glViewport(0,0,width,height);
1369 - glMatrixMode(GL_PROJECTION);
1370 - glLoadIdentity();
1371 - glOrtho(0,width,height,0,0,1);
1372 - glMatrixMode(GL_MODELVIEW);
13731372 glDisable(GL_DEPTH_TEST);
13741373 const GLubyte *glver = glGetString(GL_VERSION);
13751374 gl_caps.Version = (GLfloat)atof((char*)glver);
Index: ddraw/glDirectDrawSurface.cpp
@@ -18,6 +18,7 @@
1919 #include "common.h"
2020 #include "scalers.h"
2121 #include "shaders.h"
 22+#include "shadergen.h"
2223 #include "ddraw.h"
2324 #include "glDirectDraw.h"
2425 #include "glDirectDrawSurface.h"
@@ -620,13 +621,10 @@
621622 coords[5] = (GLfloat)srcrect.right / (GLfloat)ddsdSrc.dwWidth;
622623 coords[6] = (GLfloat)srcrect.top / (GLfloat)ddsdSrc.dwHeight;
623624 coords[7] = (GLfloat)srcrect.bottom / (GLfloat)ddsdSrc.dwHeight;
624 - glMatrixMode(GL_PROJECTION);
625625 glClear(GL_DEPTH_BUFFER_BIT);
626 - glLoadIdentity();
627 - glOrtho(0,fakex,fakey,0,0,1);
628 - glMatrixMode(GL_MODELVIEW);
629626 if(dwFlags & DDBLT_COLORFILL)
630627 {
 628+ SetShader(PROG_FILL,1);
631629 glDisable(GL_TEXTURE_2D);
632630 glDisable(GL_ALPHA_TEST);
633631 switch(ddInterface->GetBPP())
@@ -661,7 +659,7 @@
662660 if(lpDDSrcSurface) glBindTexture(GL_TEXTURE_2D,((glDirectDrawSurface7*)lpDDSrcSurface)->GetTexture());
663661 if((dwFlags & DDBLT_KEYSRC) && (src && src->colorkey[0].enabled) && !(dwFlags & DDBLT_COLORFILL))
664662 {
665 - glUseProgram(shaders[PROG_CKEY].prog);
 663+ SetShader(PROG_CKEY,1);
666664 GLint keyloc = glGetUniformLocation(shaders[PROG_CKEY].prog,"keyIn");
667665 switch(ddInterface->GetBPP())
668666 {
@@ -690,7 +688,14 @@
691689 GLint texloc = glGetUniformLocation(shaders[PROG_CKEY].prog,"myTexture");
692690 glUniform1i(texloc,0);
693691 }
694 - else glUseProgram(0);
 692+ else if(!(dwFlags & DDBLT_COLORFILL))
 693+ {
 694+ SetShader(PROG_TEXTURE,1);
 695+ GLint texloc = glGetUniformLocation(shaders[PROG_TEXTURE].prog,"Texture");
 696+ glUniform1i(texloc,0);
 697+ }
 698+ GLint viewloc = glGetUniformLocation(GetProgram()&0xffffffff,"view");
 699+ glUniform4f(viewloc,0,(GLfloat)fakex,0,(GLfloat)fakey);
695700 this->dirty |= 2;
696701 glBegin(GL_QUADS);
697702 glTexCoord2f(coords[4], coords[6]);
@@ -703,7 +708,6 @@
704709 glVertex2f(coords[0], coords[3]);
705710 glEnd();
706711 glColor3f(1.0,1.0,1.0);
707 - glUseProgram(0);
708712 glDisable(GL_TEXTURE_2D);
709713 SetFBO(0,0,false);
710714 glPopAttrib();
@@ -1099,8 +1103,7 @@
11001104 void glDirectDrawSurface7::RenderScreen(GLuint texture, glDirectDrawSurface7 *surface)
11011105 {
11021106 LONG sizes[6];
1103 - glMatrixMode(GL_PROJECTION);
1104 - glLoadIdentity();
 1107+ GLfloat view[4];
11051108 RECT r,r2;
11061109 if(surface->dirty & 1)
11071110 {
@@ -1115,8 +1118,10 @@
11161119 if(ddInterface->GetFullscreen())
11171120 {
11181121 ddInterface->GetSizes(sizes);
1119 - glOrtho((signed)-(sizes[4]-sizes[0])/2,(sizes[4]-sizes[0])/2+sizes[0],
1120 - (signed)-(sizes[5]-sizes[1])/2,(sizes[5]-sizes[1])/2+sizes[1],0,1);
 1122+ view[0] = (signed)-(sizes[4]-sizes[0])/2;
 1123+ view[1] = (signed)(sizes[4]-sizes[0])/2+sizes[0];
 1124+ view[2] = (signed)(sizes[5]-sizes[1])/2+sizes[1];
 1125+ view[3] = (signed)-(sizes[5]-sizes[1])/2;
11211126 }
11221127 else
11231128 {
@@ -1130,15 +1135,23 @@
11311136 ClientToScreen(hwnd,(LPPOINT)&r2.left);
11321137 ClientToScreen(hwnd,(LPPOINT)&r2.right);
11331138 glViewport(0,0,r.right,r.bottom);
1134 - glOrtho((signed)r2.left,(signed)r2.right,(signed)(fakey-r2.bottom),(signed)(fakey-r2.top),0,1);
 1139+ view[0] = (signed)r2.left;
 1140+ view[1] = (signed)r2.right;
 1141+ view[2] = (signed)(fakey-r2.top);
 1142+ view[3] = (signed)(fakey-r2.bottom);
11351143 }
11361144 }
1137 - else glOrtho(0,fakex,fakey,0,0,1);
1138 - glMatrixMode(GL_MODELVIEW);
 1145+ else
 1146+ {
 1147+ view[0] = 0;
 1148+ view[1] = fakex;
 1149+ view[2] = 0;
 1150+ view[3] = fakey;
 1151+ }
11391152 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
11401153 if(ddInterface->GetBPP() == 8)
11411154 {
1142 - glUseProgram(shaders[PROG_PAL256].prog);
 1155+ SetShader(PROG_PAL256,true);
11431156 glBindTexture(GL_TEXTURE_2D,paltex);
11441157 glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,256,1,0,GL_RGBA,GL_UNSIGNED_BYTE,palette->GetPalette(NULL));
11451158 GLint palloc = glGetUniformLocation(shaders[PROG_PAL256].prog,"ColorTable");
@@ -1153,9 +1166,15 @@
11541167 }
11551168 else
11561169 {
 1170+ SetShader(PROG_TEXTURE,true);
11571171 glEnable(GL_TEXTURE_2D);
11581172 glBindTexture(GL_TEXTURE_2D,texture);
 1173+ int prog = GetProgram();
 1174+ GLint texloc = glGetUniformLocation(prog,"Texture");
11591175 }
 1176+ int prog = GetProgram();
 1177+ GLint viewloc = glGetUniformLocation(prog,"view");
 1178+ glUniform4f(viewloc,view[0],view[1],view[2],view[3]);
11601179 if(ddInterface->GetFullscreen())
11611180 {
11621181 glBegin(GL_QUADS);
Index: ddraw/glExtensions.cpp
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2011 William Feely
 3+// Copyright (C) 2011-2012 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -48,6 +48,11 @@
4949 void (APIENTRY *glUniform2i) (GLint location, GLint v0, GLint v1) = NULL;
5050 void (APIENTRY *glUniform3i) (GLint location, GLint v0, GLint v1, GLint v2) = NULL;
5151 void (APIENTRY *glUniform4i) (GLint location, GLint v0, GLint v1, GLint v2, GLint v3) = NULL;
 52+void (APIENTRY *glUniform1f) (GLint location, GLfloat v0) = NULL;
 53+void (APIENTRY *glUniform2f) (GLint location, GLfloat v0, GLfloat v1) = NULL;
 54+void (APIENTRY *glUniform3f) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2) = NULL;
 55+void (APIENTRY *glUniform4f) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) = NULL;
 56+void (APIENTRY *glUniformMatrix4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value) = NULL;
5257
5358 void (APIENTRY *glActiveTexture)(GLenum texture) = NULL;
5459
@@ -85,6 +90,11 @@
8691 glUniform2i = (PFNGLUNIFORM2IPROC)wglGetProcAddress("glUniform2i");
8792 glUniform3i = (PFNGLUNIFORM3IPROC)wglGetProcAddress("glUniform3i");
8893 glUniform4i = (PFNGLUNIFORM4IPROC)wglGetProcAddress("glUniform4i");
 94+ glUniform1f = (PFNGLUNIFORM1FPROC)wglGetProcAddress("glUniform1f");
 95+ glUniform2f = (PFNGLUNIFORM2FPROC)wglGetProcAddress("glUniform2f");
 96+ glUniform3f = (PFNGLUNIFORM3FPROC)wglGetProcAddress("glUniform3f");
 97+ glUniform4f = (PFNGLUNIFORM4FPROC)wglGetProcAddress("glUniform4f");
 98+ glUniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC)wglGetProcAddress("glUniformMatrix4fv");
8999 }
90100 const GLubyte *glextensions = glGetString(GL_EXTENSIONS);
91101 if(strstr((char*)glextensions,"GL_ARB_framebuffer_object")) GLEXT_ARB_framebuffer_object = 1;
Index: ddraw/glExtensions.h
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2011 William Feely
 3+// Copyright (C) 2011-2012 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -67,6 +67,11 @@
6868 GLAPI void (APIENTRY *glUniform2i) (GLint location, GLint v0, GLint v1);
6969 GLAPI void (APIENTRY *glUniform3i) (GLint location, GLint v0, GLint v1, GLint v2);
7070 GLAPI void (APIENTRY *glUniform4i) (GLint location, GLint v0, GLint v1, GLint v2, GLint v3);
 71+GLAPI void (APIENTRY *glUniform1f) (GLint location, GLfloat v0);
 72+GLAPI void (APIENTRY *glUniform2f) (GLint location, GLfloat v0, GLfloat v1);
 73+GLAPI void (APIENTRY *glUniform3f) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2);
 74+GLAPI void (APIENTRY *glUniform4f) (GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3);
 75+GLAPI void (APIENTRY *glUniformMatrix4fv) (GLint location, GLsizei count, GLboolean transpose, const GLfloat* value);
7176
7277 GLAPI void (APIENTRY *glActiveTexture)(GLenum texture);
7378
Index: ddraw/shadergen.cpp
@@ -0,0 +1,39 @@
 2+// DXGL
 3+// Copyright (C) 2012 William Feely
 4+
 5+// This library is free software; you can redistribute it and/or
 6+// modify it under the terms of the GNU Lesser General Public
 7+// License as published by the Free Software Foundation; either
 8+// version 2.1 of the License, or (at your option) any later version.
 9+
 10+// This library is distributed in the hope that it will be useful,
 11+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 12+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13+// Lesser General Public License for more details.
 14+
 15+// You should have received a copy of the GNU Lesser General Public
 16+// License along with this library; if not, write to the Free Software
 17+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 18+
 19+#include "common.h"
 20+#include "shadergen.h"
 21+#include "shaders.h"
 22+
 23+SHADER genshaders[256];
 24+static __int64 current_shader = 0;
 25+static int shadercount = 0;
 26+static bool initialized = false;
 27+
 28+void SetShader(__int64 id, bool builtin)
 29+{
 30+ if(builtin)
 31+ {
 32+ glUseProgram(shaders[id].prog);
 33+ current_shader = shaders[id].prog;
 34+ }
 35+}
 36+
 37+__int64 GetProgram()
 38+{
 39+ return current_shader;
 40+}
Index: ddraw/shadergen.h
@@ -0,0 +1,26 @@
 2+// DXGL
 3+// Copyright (C) 2012 William Feely
 4+
 5+// This library is free software; you can redistribute it and/or
 6+// modify it under the terms of the GNU Lesser General Public
 7+// License as published by the Free Software Foundation; either
 8+// version 2.1 of the License, or (at your option) any later version.
 9+
 10+// This library is distributed in the hope that it will be useful,
 11+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 12+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 13+// Lesser General Public License for more details.
 14+
 15+// You should have received a copy of the GNU Lesser General Public
 16+// License along with this library; if not, write to the Free Software
 17+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 18+
 19+#ifndef _SHADERGEN_H
 20+#define _SHADERGEN_H
 21+
 22+
 23+void SetShader(__int64 id, bool builtin);
 24+__int64 GetProgram();
 25+
 26+
 27+#endif
\ No newline at end of file
Index: ddraw/shaders.cpp
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2011 William Feely
 3+// Copyright (C) 2011-2012 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -18,8 +18,23 @@
1919 #include "common.h"
2020 #include "shaders.h"
2121
22 -char frag_Pal256[] = "\
 22+const char frag_Color[] = "\
2323 #version 110\n\
 24+void main() \n\
 25+{ \n\
 26+ gl_FragColor = gl_Color; \n\
 27+} ";
 28+
 29+const char frag_Texture[] = "\
 30+#version 110\n\
 31+uniform sampler2D Texture;\n\
 32+void main() \n\
 33+{ \n\
 34+ gl_FragColor = texture2D( Texture, gl_TexCoord[0].st ); \n\
 35+} ";
 36+
 37+const char frag_Pal256[] = "\
 38+#version 110\n\
2439 uniform sampler2D ColorTable; \n\
2540 uniform sampler2D IndexTexture; \n\
2641 void main() \n\
@@ -29,7 +44,7 @@
3045 gl_FragColor = texel; \n\
3146 } ";
3247
33 -char frag_ColorKey[] = "\
 48+const char frag_ColorKey[] = "\
3449 #version 110\n\
3550 uniform sampler2D myTexture;\n\
3651 uniform ivec3 keyIn;\n\
@@ -42,7 +57,7 @@
4358 gl_FragColor = value;\n\
4459 } ";
4560
46 -char frag_ColorKeyMask[] = "\
 61+const char frag_ColorKeyMask[] = "\
4762 #version 110\n\
4863 uniform sampler2D myTexture;\n\
4964 uniform ivec4 keyIn;\n\
@@ -55,7 +70,7 @@
5671 else gl_FragColor[0] = 0.0;\n\
5772 } ";
5873
59 -char frag_2ColorKey[] = "\
 74+const char frag_2ColorKey[] = "\
6075 #version 110\n\
6176 uniform sampler2D myTexture;\n\
6277 uniform sampler2D maskTexture;\n\
@@ -72,19 +87,43 @@
7388 gl_FragColor = value;\n\
7489 } ";
7590
76 -int NumberOfShaders = 4;
 91+const char vert_ortho[] = "\
 92+#version 110\n\
 93+uniform vec4 view;\n\
 94+void main()\n\
 95+{\n\
 96+ mat4 proj = mat4(\n\
 97+ vec4(2.0 / (view[1] - view[0]), 0, 0, 0),\n\
 98+ vec4(0, 2.0 / (view[2] - view[3]), 0, 0),\n\
 99+ vec4(0, 0, -2.0, 0),\n\
 100+ vec4(-(view[1] + view[0]) / (view[1] - view[0]),\n\
 101+ -(view[2] + view[3]) / (view[2] - view[3]), -1 , 1));\n\
 102+ gl_Position = proj * gl_Vertex;\n\
 103+ gl_FrontColor = gl_Color;\n\
 104+ gl_TexCoord[0] = gl_MultiTexCoord0;\n\
 105+} ";
77106
78 -SHADER shaders[] = {
79 - {0,0, NULL, frag_Pal256, 0},
80 - {0,0, NULL, frag_ColorKey, 0},
81 - {0,0, NULL, frag_ColorKeyMask, 0},
82 - {0,0, NULL, frag_2ColorKey, 0}
 107+
 108+
 109+// Use EXACTLY one line per entry. Don't change layout of the list.
 110+const int SHADER_START = __LINE__;
 111+SHADER shaders[] =
 112+{
 113+ {0,0, vert_ortho, frag_Color, 0},
 114+ {0,0, vert_ortho, frag_Texture, 0},
 115+ {0,0, vert_ortho, frag_Pal256, 0},
 116+ {0,0, vert_ortho, frag_ColorKey, 0},
 117+ {0,0, vert_ortho, frag_ColorKeyMask, 0},
 118+ {0,0, vert_ortho, frag_2ColorKey, 0}
83119 };
 120+const int SHADER_END = __LINE__ - 4;
 121+const int NumberOfShaders = SHADER_END - SHADER_START;
84122
85123 void CompileShaders()
86124 {
87125 const GLchar *src;
88126 GLint srclen;
 127+ GLenum error;
89128 for(int i = 0; i < NumberOfShaders; i++)
90129 {
91130 shaders[i].prog = glCreateProgram();
Index: ddraw/shaders.h
@@ -1,5 +1,5 @@
22 // DXGL
3 -// Copyright (C) 2011 William Feely
 3+// Copyright (C) 2011-2012 William Feely
44
55 // This library is free software; you can redistribute it and/or
66 // modify it under the terms of the GNU Lesser General Public
@@ -23,17 +23,19 @@
2424 {
2525 GLint vs;
2626 GLint fs;
27 - char *vsrc;
28 - char *fsrc;
 27+ const char *vsrc;
 28+ const char *fsrc;
2929 GLint prog;
3030 } SHADER;
3131
3232 extern SHADER shaders[];
3333
34 -#define PROG_PAL256 0
35 -#define PROG_CKEY 1
36 -#define PROG_CKEYMASK 2
37 -#define PROG_2CKEY 3
 34+#define PROG_FILL 0
 35+#define PROG_TEXTURE 1
 36+#define PROG_PAL256 2
 37+#define PROG_CKEY 3
 38+#define PROG_CKEYMASK 4
 39+#define PROG_2CKEY 5
3840
3941 void CompileShaders();
4042