DXGL r102 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r101‎ | r102 | r103 >
Date:01:15, 18 February 2012
Author:admin
Status:new
Tags:
Comment:
Begin implementing DrawIndexedPrimitive in render thread
Hopefully finalize D3D shader ID specification
Remove debug-only restriction on D3D interfaces
Modified paths:
  • /ddraw/glDirect3DDevice.cpp (modified) (history)
  • /ddraw/glDirect3DDevice.h (modified) (history)
  • /ddraw/glDirectDraw.cpp (modified) (history)
  • /ddraw/glRenderer.cpp (modified) (history)
  • /ddraw/glRenderer.h (modified) (history)
  • /ddraw/shadergen.cpp (modified) (history)
  • /ddraw/shadergen.h (modified) (history)
  • /dxgltest/dxgltest.cpp (modified) (history)

Diff [purge]

Index: ddraw/glDirect3DDevice.cpp
@@ -22,6 +22,7 @@
2323 #include "glDirect3DDevice.h"
2424 #include "glDirect3DLight.h"
2525 #include "glRenderer.h"
 26+#include "shadergen.h"
2627 #include "glutil.h"
2728
2829 const DWORD renderstate_default[153] = {0, // 0
@@ -240,26 +241,6 @@
241242 FIXME("glDirect3DDevice7::DeleteStateBlock: stub");
242243 ERR(DDERR_GENERIC);
243244 }
244 -int setdrawmode(D3DPRIMITIVETYPE d3dptPrimitiveType)
245 -{
246 - switch(d3dptPrimitiveType)
247 - {
248 - case D3DPT_POINTLIST:
249 - return GL_POINTS;
250 - case D3DPT_LINELIST:
251 - return GL_LINES;
252 - case D3DPT_LINESTRIP:
253 - return GL_LINE_STRIP;
254 - case D3DPT_TRIANGLELIST:
255 - return GL_TRIANGLES;
256 - case D3DPT_TRIANGLESTRIP:
257 - return GL_TRIANGLE_STRIP;
258 - case D3DPT_TRIANGLEFAN:
259 - return GL_TRIANGLE_FAN;
260 - default:
261 - return -1;
262 - }
263 -}
264245
265246 void glDirect3DDevice7::SetArraySize(DWORD size, DWORD vertex, DWORD texcoord)
266247 {
@@ -307,10 +288,9 @@
308289 int numtextures = (VertexType & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
309290 shader |= (__int64)numtextures << 31;
310291 if(VertexType & D3DFVF_XYZRHW) shader |= (1i64 << 34);
311 - if((VertexType & D3DFVF_DIFFUSE) && (VertexType & D3DFVF_SPECULAR)) shader |= (1i64<<35);
312 - for(i = 0; i < numtextures; i++)
313 - shader |= (__int64)((VertexType >> (16+(2*i))) & 3) << (36 + (2*i));
314 - if(VertexType & D3DFVF_NORMAL) shader |= (1i64 << 52);
 292+ if(VertexType & D3DFVF_DIFFUSE) shader |= (1i64<<35);
 293+ if(VertexType & D3DFVF_SPECULAR) shader |= (1i64<<36);
 294+ if(VertexType & D3DFVF_NORMAL) shader |= (1i64 << 37);
315295 int lightindex = 0;
316296 for(i = 0; i < 8; i++)
317297 {
@@ -317,11 +297,12 @@
318298 if(gllights[i] != -1)
319299 {
320300 if(lights[gllights[i]]->light.dltType != D3DLIGHT_DIRECTIONAL)
321 - shader |= (1i64 << (53+lightindex));
 301+ shader |= (1i64 << (38+lightindex));
322302 lightindex++;
323303 }
324304 }
325 - if(((VertexType >> 1) & 7) >= 3) shader |= (__int64)(((VertexType >> 1) & 7) - 2) << 61;
 305+ if(((VertexType >> 1) & 7) >= 3) shader |= (__int64)(((VertexType >> 1) & 7) - 2) << 46;
 306+
326307 return shader;
327308 }
328309
@@ -330,13 +311,8 @@
331312 {
332313 if(!this) return DDERR_INVALIDPARAMS;
333314 if(!inscene) return D3DERR_SCENE_NOT_IN_SCENE;
334 - int drawmode = setdrawmode(d3dptPrimitiveType);
335 - if(drawmode == -1) return DDERR_INVALIDPARAMS;
336 - if(dwVertexTypeDesc & D3DFVF_XYZB1) ERR(DDERR_GENERIC);
337 -
338 - //for(int i = 0; i <
339 - FIXME("glDirect3DDevice7::DrawIndexedPrimitive: stub");
340 - ERR(DDERR_GENERIC);
 315+ return glD3D7->glDD7->renderer->DrawIndexedPrimitive(this,d3dptPrimitiveType,dwVertexTypeDesc,lpvVertices,
 316+ dwVertexCount,lpwIndices,dwIndexCount,dwFlags);
341317 }
342318 HRESULT WINAPI glDirect3DDevice7::DrawIndexedPrimitiveStrided(D3DPRIMITIVETYPE d3dptPrimitiveType, DWORD dwVertexTypeDesc,
343319 LPD3DDRAWPRIMITIVESTRIDEDDATA lpvVerticexArray, DWORD dwVertexCount, LPWORD lpwIndices, DWORD dwIndexCount, DWORD dwFlags)
Index: ddraw/glDirect3DDevice.h
@@ -86,9 +86,9 @@
8787 HRESULT WINAPI SetViewport(LPD3DVIEWPORT7 lpViewport);
8888 HRESULT WINAPI ValidateDevice(LPDWORD lpdwPasses);
8989 void SetArraySize(DWORD size, DWORD vertex, DWORD texcoord);
 90+ __int64 SelectShader(DWORD VertexType);
9091
9192 private:
92 - __int64 SelectShader(DWORD VertexType);
9393 D3DMATRIX matWorld,matView,matProjection;
9494 glDirect3D7 *glD3D7;
9595 glDirectDrawSurface7 *glDDS7;
Index: ddraw/glDirectDraw.cpp
@@ -649,14 +649,9 @@
650650 }
651651 if(riid == IID_IDirect3D7)
652652 {
653 - #ifdef _DEBUG
654653 this->AddRef();
655654 *ppvObj = new glDirect3D7(this);
656655 return DD_OK;
657 - #else
658 - FIXME("Add IDirect3D Interfaces\n");
659 - ERR(DDERR_GENERIC);
660 - #endif
661656 }
662657 if(riid == IID_IDirectDrawGammaControl)
663658 {
Index: ddraw/glRenderer.cpp
@@ -19,6 +19,7 @@
2020 #include "glDirectDraw.h"
2121 #include "glDirectDrawSurface.h"
2222 #include "glDirectDrawPalette.h"
 23+#include "glDirect3DDevice.h"
2324 #include "glRenderer.h"
2425 #include "glutil.h"
2526 #include "ddraw.h"
@@ -43,6 +44,28 @@
4445 return (number<<2)+(number>>4);
4546 }
4647
 48+int setdrawmode(D3DPRIMITIVETYPE d3dptPrimitiveType)
 49+{
 50+ switch(d3dptPrimitiveType)
 51+ {
 52+ case D3DPT_POINTLIST:
 53+ return GL_POINTS;
 54+ case D3DPT_LINELIST:
 55+ return GL_LINES;
 56+ case D3DPT_LINESTRIP:
 57+ return GL_LINE_STRIP;
 58+ case D3DPT_TRIANGLELIST:
 59+ return GL_TRIANGLES;
 60+ case D3DPT_TRIANGLESTRIP:
 61+ return GL_TRIANGLE_STRIP;
 62+ case D3DPT_TRIANGLEFAN:
 63+ return GL_TRIANGLE_FAN;
 64+ default:
 65+ return -1;
 66+ }
 67+}
 68+
 69+
4770 int oldswap = 0;
4871 int swapinterval = 0;
4972 inline void SetSwap(int swap)
@@ -387,6 +410,35 @@
388411 LeaveCriticalSection(&cs);
389412 }
390413
 414+HRESULT glRenderer::DrawIndexedPrimitive(glDirect3DDevice7 *device, D3DPRIMITIVETYPE d3dptPrimitiveType, DWORD dwVertexTypeDesc,
 415+ LPVOID lpvVertices, DWORD dwVertexCount, LPWORD lpwIndices, DWORD dwIndexCount, DWORD dwFlags)
 416+{
 417+ MSG Msg;
 418+ EnterCriticalSection(&cs);
 419+ wndbusy = true;
 420+ inputs[0] = device;
 421+ inputs[1] = (void*)d3dptPrimitiveType;
 422+ inputs[2] = (void*)dwVertexTypeDesc;
 423+ inputs[3] = lpvVertices;
 424+ inputs[4] = (void*)dwVertexCount;
 425+ inputs[5] = lpwIndices;
 426+ inputs[6] = (void*)dwIndexCount;
 427+ inputs[7] = (void*)dwFlags;
 428+ SendMessage(hRenderWnd,GLEVENT_DRAWINDEXEDPRIMITIVE,0,0);
 429+ while(wndbusy)
 430+ {
 431+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
 432+ {
 433+ TranslateMessage(&Msg);
 434+ DispatchMessage(&Msg);
 435+ }
 436+ Sleep(0);
 437+ }
 438+ LeaveCriticalSection(&cs);
 439+ return (HRESULT)outputs[0];
 440+}
 441+
 442+
391443 DWORD glRenderer::_Entry()
392444 {
393445 MSG Msg;
@@ -969,11 +1021,37 @@
9701022 glFlush();
9711023 }
9721024
 1025+void glRenderer::_DrawIndexedPrimitive(glDirect3DDevice7 *device, D3DPRIMITIVETYPE d3dptPrimitiveType, DWORD dwVertexTypeDesc,
 1026+ LPVOID lpvVertices, DWORD dwVertexCount, LPWORD lpwIndices, DWORD dwIndexCount, DWORD dwFlags)
 1027+{
 1028+ int drawmode = setdrawmode(d3dptPrimitiveType);
 1029+ if(drawmode == -1)
 1030+ {
 1031+ outputs[0] = (void*)DDERR_INVALIDPARAMS;
 1032+ wndbusy = false;
 1033+ return;
 1034+ }
 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+ }
 1042+ SetShader(device->SelectShader(dwVertexTypeDesc),NULL,0);
 1043+
 1044+ FIXME("glDirect3DDevice::DrawIndexedPrimitive: stub");
 1045+ outputs[0] = (void*)DDERR_GENERIC;
 1046+ wndbusy = false;
 1047+ return;
 1048+}
 1049+
9731050 LRESULT glRenderer::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
9741051 {
9751052 int oldx,oldy;
9761053 float mulx, muly;
9771054 float tmpfloats[16];
 1055+ D3DPRIMITIVETYPE d3dpt;
9781056 int translatex, translatey;
9791057 LPARAM newpos;
9801058 HWND hParent;
@@ -1099,6 +1177,11 @@
11001178 case GLEVENT_FLUSH:
11011179 _Flush();
11021180 return 0;
 1181+ case GLEVENT_DRAWINDEXEDPRIMITIVE:
 1182+ memcpy(&d3dpt,&inputs[1],4);
 1183+ _DrawIndexedPrimitive((glDirect3DDevice7*)inputs[0],d3dpt,(DWORD)inputs[2],(LPVOID)inputs[3],
 1184+ (DWORD)inputs[4],(LPWORD)inputs[5],(DWORD)inputs[6],(DWORD)inputs[7]);
 1185+ return 0;
11031186 }
11041187 return DefWindowProc(hwnd,msg,wParam,lParam);
11051188 }
Index: ddraw/glRenderer.h
@@ -62,10 +62,19 @@
6363 #define GLEVENT_INITD3D WM_USER+8
6464 #define GLEVENT_CLEAR WM_USER+9
6565 #define GLEVENT_FLUSH WM_USER+10
 66+#define GLEVENT_DRAWPRIMITIVE WM_USER+11
 67+#define GLEVENT_DRAWPRIMITIVESTRIDED WM_USER+12
 68+#define GLEVENT_DRAWPRIMITIVEVB WM_USER+13
 69+#define GLEVENT_DRAWINDEXEDPRIMITIVE WM_USER+14
 70+#define GLEVENT_DRAWINDEXEDPRIMITIVESTRIDED WM_USER+15
 71+#define GLEVENT_DRAWINDEXEDPRIMITIVEVB WM_USER+16
6672
 73+
6774 extern int swapinterval;
6875 extern inline void SetSwap(int swap);
6976
 77+class glDirect3DDevice7;
 78+
7079 class glRenderer
7180 {
7281 public:
@@ -82,6 +91,8 @@
8392 void InitD3D(int zbuffer);
8493 void Flush();
8594 HRESULT Clear(glDirectDrawSurface7 *target, DWORD dwCount, LPD3DRECT lpRects, DWORD dwFlags, DWORD dwColor, D3DVALUE dvZ, DWORD dwStencil);
 95+ HRESULT DrawIndexedPrimitive(glDirect3DDevice7 *device, D3DPRIMITIVETYPE d3dptPrimitiveType, DWORD dwVertexTypeDesc,
 96+ LPVOID lpvVertices, DWORD dwVertexCount, LPWORD lpwIndices, DWORD dwIndexCount, DWORD dwFlags);
8697 HGLRC hRC;
8798 LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
8899 private:
@@ -98,6 +109,8 @@
99110 void _DrawBackbuffer(GLuint *texture, int x, int y);
100111 void _InitD3D(int zbuffer);
101112 void _Clear(glDirectDrawSurface7 *target, DWORD dwCount, LPD3DRECT lpRects, DWORD dwFlags, DWORD dwColor, D3DVALUE dvZ, DWORD dwStencil);
 113+ void _DrawIndexedPrimitive(glDirect3DDevice7 *device, D3DPRIMITIVETYPE d3dptPrimitiveType, DWORD dwVertexTypeDesc,
 114+ LPVOID lpvVertices, DWORD dwVertexCount, LPWORD lpwIndices, DWORD dwIndexCount, DWORD dwFlags);
102115 glDirectDraw7 *ddInterface;
103116 void _Flush();
104117 void* inputs[32];
Index: ddraw/shadergen.cpp
@@ -34,9 +34,11 @@
3535 {
3636 _GENSHADER shader;
3737 __int64 id;
 38+ __int64 texids[8];
3839 };
3940 GenShader genshaders[256];
4041 static __int64 current_shader = 0;
 42+static __int64 current_texid[8];
4143 static int shadercount = 0;
4244 static int genindex = 0;
4345 static bool initialized = false;
@@ -65,16 +67,31 @@
6668 Bits 29-30 - Emissive material source
6769 Bits 31-33 - Number of textures
6870 Bit 34 - Use transformed vertices
69 -Bit 35 - Use secondary color
70 -Bits 36-51 - Texture coordinate format:
71 -00=2dim 01=3dim 10=4dim 11=1dim
72 -Bit 52 - Enable normals
73 -Bits 53-60 - Light types
74 -Bits 61-63 - Number of blending weights
 71+Bit 35 - Use diffuse color
 72+Bit 36 - Use specular color
 73+Bit 37 - Enable normals
 74+Bits 38-45 - Light types
 75+Bits 46-48 - Number of blending weights
7576 */
7677
7778 /* Bits in Texture Stage ID:
78 -
 79+Bits 0-4: Texture color operation
 80+Bits 5-10: Texture color argument 1
 81+Bits 11-16: Texture color argument 2
 82+Bits 17-20: Texture alpha operation
 83+Bits 21-26: Texture alpha argument 1
 84+Bits 27-32: Texture alpha argument 2
 85+Bits 33-35: Texture coordinate index
 86+Bits 36-37: Texture coordinate flags
 87+Bits 38-39: U Texture address
 88+Bits 40-41: V Texture address
 89+Bits 42-44: Texture magnification filter
 90+Bits 45-46: Texture minification filter
 91+Bit 47: Enable texture coordinate transform
 92+Bits 48-49: Number of texcoord dimensions
 93+Bit 50: Projected texcoord
 94+Bits 51-52: Texutre coordinate format:
 95+00=2dim 01=3dim 10=4dim 11=1dim
7996 */
8097 void ZeroShaderArray()
8198 {
@@ -95,7 +112,10 @@
96113 }
97114 else
98115 {
99 - if(!isbuiltin && (id == current_shader)) return;
 116+ if(!isbuiltin && (id == current_shader))
 117+ {
 118+ if(!memcmp(current_texid,texstate,8*sizeof(__int64))) return;
 119+ }
100120 current_shader = id;
101121 isbuiltin=false;
102122 for(int i = 0; i < shadercount; i++)
@@ -102,8 +122,11 @@
103123 {
104124 if(genshaders[i].id == id)
105125 {
106 - shaderindex = i;
107 - break;
 126+ if(!memcmp(genshaders[i].texids,texstate,8*sizeof(__int64)))
 127+ {
 128+ shaderindex = i;
 129+ break;
 130+ }
108131 }
109132 }
110133 if(shaderindex == -1)
@@ -120,7 +143,7 @@
121144 delete genshaders[shaderindex].shader.fsrc;
122145 ZeroMemory(&genshaders[shaderindex],sizeof(GenShader));
123146 }
124 - CreateShader(genindex,id);
 147+ CreateShader(genindex,id,texstate);
125148 shaderindex = genindex;
126149 genindex++;
127150 if(genindex == 256) genindex = 0;
@@ -198,7 +221,7 @@
199222 // Operations
200223 static const char normalize[] = "vec3 N = normalize(vec3(modelview*vec4(nxyz,0.0)));\n";
201224
202 -void CreateShader(int index, __int64 id)
 225+void CreateShader(int index, __int64 id, TexState *texstate)
203226 {
204227 string tmp;
205228 int i;
@@ -227,7 +250,7 @@
228251 vsrc->append(tmp);
229252 }
230253 }
231 -
 254+
232255 //Main
233256 vsrc->append(mainstart);
234257
Index: ddraw/shadergen.h
@@ -33,6 +33,6 @@
3434 void SetShader(__int64 id, TexState *texstate, bool builtin);
3535 GLuint GetProgram();
3636 void ZeroShaderArray();
37 -void CreateShader(int index, __int64 id);
 37+void CreateShader(int index, __int64 id, TexState *texstate);
3838
3939 #endif
\ No newline at end of file
Index: dxgltest/dxgltest.cpp
@@ -713,21 +713,15 @@
714714 tab.pszText = _T("2D Graphics");
715715 hTab = GetDlgItem(hWnd,IDC_TABS);
716716 SendDlgItemMessage(hWnd,IDC_TABS,TCM_INSERTITEM,1,(LPARAM)&tab);
717 - #ifdef _DEBUG
718717 tab.pszText = _T("3D Graphics");
719718 SendDlgItemMessage(hWnd,IDC_TABS,TCM_INSERTITEM,2,(LPARAM)&tab);
720 - #endif
721719 tabwnd[0] = CreateDialog(hinstance,MAKEINTRESOURCE(IDD_SYSINFO),hTab,SysTabCallback);
722720 tabwnd[1] = CreateDialog(hinstance,MAKEINTRESOURCE(IDD_TESTGFX),hTab,Test2DCallback);
723 - #ifdef _DEBUG
724721 tabwnd[2] = CreateDialog(hinstance,MAKEINTRESOURCE(IDD_TESTGFX),hTab,Test3DCallback);
725 - #endif
726722 SendDlgItemMessage(hWnd,IDC_TABS,TCM_GETITEMRECT,0,(LPARAM)&tabrect);
727723 SetWindowPos(tabwnd[0],NULL,tabrect.left,tabrect.bottom+3,0,0,SWP_SHOWWINDOW|SWP_NOSIZE);
728724 ShowWindow(tabwnd[1],SW_HIDE);
729 - #ifdef _DEBUG
730725 ShowWindow(tabwnd[2],SW_HIDE);
731 - #endif
732726 tabopen = 0;
733727 ShowWindow(hWnd,SW_SHOWNORMAL);
734728 return TRUE;