DXGL r151 - Code Review

Jump to navigation Jump to search
Repository:DXGL
Revision:r150‎ | r151 | r152 >
Date:21:28, 29 May 2012
Author:admin
Status:new
Tags:
Comment:
Revert revision 199, was too buggy for release.
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)

Diff [purge]

Index: ddraw/glDirect3DDevice.cpp
@@ -210,14 +210,16 @@
211211 diffuse = specular = NULL;
212212 ZeroMemory(texcoords,8*sizeof(GLfloat*));
213213 memcpy(renderstate,renderstate_default,153*sizeof(DWORD));
214 - __gluMakeIdentityf((GLfloat*)&matrices[0]);
215 - for(int i = 1; i < 24; i++)
216 - memcpy(&matrices[i],&matrices[0],sizeof(D3DMATRIX));
 214+ __gluMakeIdentityf(matWorld);
 215+ __gluMakeIdentityf(matView);
 216+ __gluMakeIdentityf(matProjection);
 217+ __gluMakeIdentityf(matNormal);
217218 texstages[0] = texstagedefault0;
218219 texstages[1] = texstages[2] = texstages[3] = texstages[4] =
219220 texstages[5] = texstages[6] = texstages[7] = texstagedefault1;
220221 refcount = 1;
221222 inscene = false;
 223+ normal_dirty = false;
222224 this->glD3D7 = glD3D7;
223225 glD3D7->AddRef();
224226 this->glDDS7 = glDDS7;
@@ -233,8 +235,6 @@
234236 d3ddesc.dwMaxTextureWidth = d3ddesc.dwMaxTextureHeight =
235237 d3ddesc.dwMaxTextureRepeat = d3ddesc.dwMaxTextureAspectRatio = glD3D7->glDD7->renderer->gl_caps.TextureMax;
236238 glD3D7->glDD7->renderer->InitD3D(zbuffer);
237 - glD3D7->glDD7->renderer->SetRenderState(0,153,renderstate);
238 - glD3D7->glDD7->renderer->SetMatrix(0,24,matrices);
239239 }
240240 glDirect3DDevice7::~glDirect3DDevice7()
241241 {
@@ -513,8 +513,8 @@
514514 if(!inscene) return D3DERR_SCENE_NOT_IN_SCENE;
515515 HRESULT err = fvftoglvertex(dwVertexTypeDesc,(LPDWORD)lpvVertices);
516516 if(err != D3D_OK) return err;
517 - return glD3D7->glDD7->renderer->DrawPrimitives(this,setdrawmode(d3dptPrimitiveType),
518 - vertdata,true,texformats,dwVertexCount,lpwIndices,dwIndexCount,dwFlags);
 517+ return glD3D7->glDD7->renderer->DrawPrimitives(this,setdrawmode(d3dptPrimitiveType),vertdata,texformats,
 518+ dwVertexCount,lpwIndices,dwIndexCount,dwFlags);
519519 }
520520 HRESULT WINAPI glDirect3DDevice7::DrawIndexedPrimitiveStrided(D3DPRIMITIVETYPE d3dptPrimitiveType, DWORD dwVertexTypeDesc,
521521 LPD3DDRAWPRIMITIVESTRIDEDDATA lpvVerticexArray, DWORD dwVertexCount, LPWORD lpwIndices, DWORD dwIndexCount, DWORD dwFlags)
@@ -772,9 +772,20 @@
773773 HRESULT WINAPI glDirect3DDevice7::GetTransform(D3DTRANSFORMSTATETYPE dtstTransformStateType, LPD3DMATRIX lpD3DMatrix)
774774 {
775775 if(!this) return DDERR_INVALIDPARAMS;
776 - if(dtstTransformStateType > D3DTRANSFORMSTATE_TEXTURE7) return DDERR_INVALIDPARAMS;
777 - memcpy(lpD3DMatrix,&matrices[dtstTransformStateType],sizeof(D3DMATRIX));
778 - return D3D_OK;
 776+ switch(dtstTransformStateType)
 777+ {
 778+ case D3DTRANSFORMSTATE_WORLD:
 779+ memcpy(lpD3DMatrix,&matWorld,sizeof(D3DMATRIX));
 780+ return D3D_OK;
 781+ case D3DTRANSFORMSTATE_VIEW:
 782+ memcpy(lpD3DMatrix,&matView,sizeof(D3DMATRIX));
 783+ return D3D_OK;
 784+ case D3DTRANSFORMSTATE_PROJECTION:
 785+ memcpy(lpD3DMatrix,&matProjection,sizeof(D3DMATRIX));
 786+ return D3D_OK;
 787+ default:
 788+ ERR(DDERR_INVALIDPARAMS);
 789+ }
779790 }
780791 HRESULT WINAPI glDirect3DDevice7::GetViewport(LPD3DVIEWPORT7 lpViewport)
781792 {
@@ -1127,10 +1138,22 @@
11281139 HRESULT WINAPI glDirect3DDevice7::SetTransform(D3DTRANSFORMSTATETYPE dtstTransformStateType, LPD3DMATRIX lpD3DMatrix)
11291140 {
11301141 if(!this) return DDERR_INVALIDPARAMS;
1131 - if(dtstTransformStateType > D3DTRANSFORMSTATE_TEXTURE7) return DDERR_INVALIDPARAMS;
1132 - memcpy(&matrices[dtstTransformStateType],lpD3DMatrix,sizeof(D3DMATRIX));
1133 - glD3D7->glDD7->renderer->SetMatrix(dtstTransformStateType,1,lpD3DMatrix);
1134 - return D3D_OK;
 1142+ switch(dtstTransformStateType)
 1143+ {
 1144+ case D3DTRANSFORMSTATE_WORLD:
 1145+ memcpy(&matWorld,lpD3DMatrix,sizeof(D3DMATRIX));
 1146+ normal_dirty = true;
 1147+ return D3D_OK;
 1148+ case D3DTRANSFORMSTATE_VIEW:
 1149+ memcpy(&matView,lpD3DMatrix,sizeof(D3DMATRIX));
 1150+ normal_dirty = true;
 1151+ return D3D_OK;
 1152+ case D3DTRANSFORMSTATE_PROJECTION:
 1153+ memcpy(&matProjection,lpD3DMatrix,sizeof(D3DMATRIX));
 1154+ return D3D_OK;
 1155+ default:
 1156+ ERR(DDERR_INVALIDPARAMS);
 1157+ }
11351158 }
11361159 HRESULT WINAPI glDirect3DDevice7::SetViewport(LPD3DVIEWPORT7 lpViewport)
11371160 {
@@ -1164,3 +1187,26 @@
11651188 return D3D_OK;
11661189 }
11671190
 1191+void glDirect3DDevice7::UpdateNormalMatrix()
 1192+{
 1193+ GLfloat worldview[16];
 1194+ GLfloat tmp[16];
 1195+
 1196+ ZeroMemory(&worldview,sizeof(D3DMATRIX));
 1197+ ZeroMemory(&tmp,sizeof(D3DMATRIX));
 1198+ __gluMultMatricesf(matWorld,matView,worldview); // Get worldview
 1199+ if(__gluInvertMatrixf(worldview,tmp)) // Invert
 1200+ {
 1201+ memcpy(matNormal,tmp,3*sizeof(GLfloat));
 1202+ memcpy(matNormal+3,tmp+4,3*sizeof(GLfloat));
 1203+ memcpy(matNormal+6,tmp+8,3*sizeof(GLfloat));
 1204+ }
 1205+ else
 1206+ {
 1207+ memcpy(matNormal,worldview,3*sizeof(GLfloat));
 1208+ memcpy(matNormal+3,worldview+4,3*sizeof(GLfloat));
 1209+ memcpy(matNormal+6,worldview+8,3*sizeof(GLfloat));
 1210+ }
 1211+
 1212+ normal_dirty = false;
 1213+}
\ No newline at end of file
Index: ddraw/glDirect3DDevice.h
@@ -122,7 +122,12 @@
123123 HRESULT WINAPI ValidateDevice(LPDWORD lpdwPasses);
124124 void SetArraySize(DWORD size, DWORD vertex, DWORD texcoord);
125125 __int64 SelectShader(GLVERTEX *VertexType);
126 - D3DMATRIX matrices[24];
 126+ void UpdateNormalMatrix();
 127+ GLfloat matWorld[16];
 128+ GLfloat matView[16];
 129+ GLfloat matProjection[16];
 130+ GLfloat matNormal[9];
 131+ bool normal_dirty;
127132 D3DMATERIAL7 material;
128133 D3DVIEWPORT7 viewport;
129134 glDirect3DLight **lights;
@@ -148,7 +153,7 @@
149154 GLubyte *ambient;
150155 GLfloat *texcoords[8];
151156 GLVERTEX vertdata[18];
152 - DWORD texformats[8];
 157+ int texformats[8];
153158 };
154159
155160 #endif //__GLDIRECT3DDEVICE_H
\ No newline at end of file
Index: ddraw/glDirectDraw.cpp
@@ -1380,7 +1380,7 @@
13811381
13821382 void glDirectDraw7::DeleteGL()
13831383 {
1384 - if(renderer) delete renderer;
 1384+ delete renderer;
13851385 renderer = NULL;
13861386 }
13871387
Index: ddraw/glRenderer.cpp
@@ -15,11 +15,6 @@
1616 // License along with this library; if not, write to the Free Software
1717 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1818
19 -/**
20 - * @file glRenderer.cpp
21 - * @brief Contains the functions that control the DXGL rendering pipeline.
22 - */
23 -
2419 #include "common.h"
2520 #include "glDirectDraw.h"
2621 #include "glDirectDrawSurface.h"
@@ -31,7 +26,6 @@
3227 #include "ddraw.h"
3328 #include "scalers.h"
3429 #include <string>
35 -#include <cstdarg>
3630 using namespace std;
3731 #include "shadergen.h"
3832 #include "matrix.h"
@@ -43,37 +37,8 @@
4438 int backy = 0;
4539 BltVertex bltvertices[4];
4640 const GLushort bltindices[4] = {0,1,2,3};
47 -const RECT nullrect = {0,0,0,0};
4841
4942 /**
50 - * Waits for an object to be signaled, while processing window messages received
51 - * by the calling thread.
52 - * @param object
53 - * Win32 handle to the object to wait for
54 - */
55 -void WaitForMessageAndObject(HANDLE object)
56 -{
57 - bool loop = true;
58 - DWORD wake;
59 - MSG msg;
60 - DWORD error;
61 - while(loop)
62 - {
63 - wake = MsgWaitForMultipleObjects(1,&object,FALSE,INFINITE,QS_ALLEVENTS);
64 - if(wake == (WAIT_OBJECT_0+1))
65 - {
66 - while(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
67 - {
68 - TranslateMessage(&msg);
69 - DispatchMessage(&msg);
70 - }
71 - }
72 - else loop = false;
73 - error = GetLastError();
74 - }
75 -}
76 -
77 -/**
7843 * Expands a 5-bit value to 8 bits.
7944 * @param number
8045 * 5-bit value to convert to 8 bits.
@@ -116,122 +81,6 @@
11782 }
11883
11984 /**
120 - * Adds a command to the renderer queue.\n
121 - * If the command requires more space than the queue buffer, the buffer will be
122 - * expanded. If there is no free space for the command, execution will pause
123 - * until the queue has been sufficiently emptied.
124 - * @param opcode
125 - * Code that describes the command to be added to the queue.
126 - * @param mode
127 - * Method to use for synchronization:
128 - * - 0: Do not fail the call
129 - * - 1: Fail if queue is full
130 - * - 2: Fail if queue is not empty
131 - * @param size
132 - * Size of the command in DWORDs
133 - * @param paramcount
134 - * Number of parameters to add to the queue command.
135 - * @param ...
136 - * Parameters for the command, when required. This is given in pairs of two
137 - * arguments:
138 - * - size: The size of the parameter, in bytes. Note that parameters are DWORD
139 - * aligned. The size cannot exceed the size of the command, minus data already
140 - * written to the queue.
141 - * - pointer: A pointer to the data to be added to the command.\n
142 - * If no parameters are used for the opcode, then supply 0 and NULL for the ...
143 - * parameter.
144 - * @return
145 - * Zero if the call succeeds, nonzero otherwise.
146 - */
147 -int glRenderer::AddQueue(DWORD opcode, int mode, DWORD size, int paramcount, ...)
148 -{
149 - EnterCriticalSection(&queuecs);
150 - if((mode == 2) && queuelength)
151 - {
152 - LeaveCriticalSection(&queuecs);
153 - return 1;
154 - }
155 - va_list params;
156 - // Check queue size
157 - va_start(params,paramcount);
158 - int argsize;
159 - void *argptr;
160 - if(size > queuesize)
161 - {
162 - queue = (LPDWORD)realloc(queue,(queuesize+size)*sizeof(DWORD));
163 - queuesize += size;
164 - }
165 - if(queuesize - queue_write < size)
166 - {
167 - if(queue_read < size)
168 - {
169 - if(mode == 1)
170 - {
171 - LeaveCriticalSection(&queuecs);
172 - return 1;
173 - }
174 - if(queue_write < queuesize)
175 - {
176 - queue[queue_write] = OP_RESETQUEUE;
177 - queuelength++;
178 - }
179 - LeaveCriticalSection(&queuecs);
180 - Sync(size);
181 - EnterCriticalSection(&queuecs);
182 - }
183 - }
184 - if(queue_write < queue_read)
185 - {
186 - if(queue_read - queue_write < size)
187 - {
188 - LeaveCriticalSection(&queuecs);
189 - Sync(size);
190 - EnterCriticalSection(&queuecs);
191 - }
192 - }
193 - queue[queue_write++] = opcode;
194 - queue[queue_write++] = size;
195 - size -= 2;
196 - for(int i = 0; i < paramcount; i++)
197 - {
198 - argsize = va_arg(params,int);
199 - argptr = va_arg(params,void*);
200 - if(!argsize) continue;
201 - if((NextMultipleOf4(argsize)/4) > size) break;
202 - queue[queue_write++] = argsize;
203 - if(argptr) memcpy(queue+queue_write,argptr,argsize);
204 - queue_write += (NextMultipleOf4(argsize)/4);
205 - size -= (NextMultipleOf4(argsize)/4);
206 - }
207 - va_end(params);
208 - queuelength++;
209 - if(!running) SetEvent(start);
210 - LeaveCriticalSection(&queuecs);
211 - return 0;
212 -}
213 -
214 -/**
215 - * Waits until the specified amount of queue space is free
216 - * @param size
217 - * If nonzero, the number of DWORDs that must be available within the queue.
218 - * If zero, waits until the queue is empty.
219 - */
220 -void glRenderer::Sync(int size)
221 -{
222 - EnterCriticalSection(&queuecs);
223 - if(!queuelength && !running)
224 - {
225 - LeaveCriticalSection(&queuecs);
226 - return;
227 - }
228 - ResetEvent(sync);
229 - syncsize = size;
230 - if(!running) SetEvent(start);
231 - LeaveCriticalSection(&queuecs);
232 - WaitForMessageAndObject(sync);
233 -}
234 -
235 -/**
23685 * Internal function for uploading surface content to an OpenGL texture
23786 * @param buffer
23887 * Contains the contents of the surface
@@ -286,9 +135,9 @@
287136 }
288137 glTexImage2D(GL_TEXTURE_2D,0,texformat3,bigx,bigy,0,texformat,texformat2,bigbuffer);
289138 }
 139+ return 0;
290140 }
291141
292 -
293142 /**
294143 * Internal function for downloading surface content from an OpenGL texture
295144 * @param buffer
@@ -342,6 +191,7 @@
343192 break;
344193 }
345194 }
 195+ return 0;
346196 }
347197
348198 /**
@@ -358,18 +208,16 @@
359209 */
360210 glRenderer::glRenderer(int width, int height, int bpp, bool fullscreen, HWND hwnd, glDirectDraw7 *glDD7)
361211 {
 212+ MSG Msg;
 213+ wndbusy = false;
362214 hDC = NULL;
363215 hRC = NULL;
364216 PBO = 0;
365217 hasHWnd = false;
366218 dib.enabled = false;
367 - normal_dirty = false;
368 - running = false;
369219 hWnd = hwnd;
370220 hRenderWnd = NULL;
371 - busy = CreateEvent(NULL,FALSE,FALSE,NULL);
372 - InitializeCriticalSection(&commandcs);
373 - InitializeCriticalSection(&queuecs);
 221+ InitializeCriticalSection(&cs);
374222 if(fullscreen)
375223 {
376224 SetWindowLongPtrA(hWnd,GWL_EXSTYLE,WS_EX_APPWINDOW);
@@ -380,7 +228,6 @@
381229 {
382230 // TODO: Adjust window rect
383231 }
384 - EnterCriticalSection(&commandcs);
385232 SetWindowPos(hWnd,HWND_TOP,0,0,0,0,SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
386233 inputs[0] = (void*)width;
387234 inputs[1] = (void*)height;
@@ -389,9 +236,17 @@
390237 inputs[4] = (void*)hWnd;
391238 inputs[5] = glDD7;
392239 inputs[6] = this;
 240+ wndbusy = true;
393241 hThread = CreateThread(NULL,0,ThreadEntry,inputs,0,NULL);
394 - WaitForMessageAndObject(busy);
395 - LeaveCriticalSection(&commandcs);
 242+ while(wndbusy)
 243+ {
 244+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
 245+ {
 246+ TranslateMessage(&Msg);
 247+ DispatchMessage(&Msg);
 248+ }
 249+ Sleep(0);
 250+ }
396251 }
397252
398253 /**
@@ -399,14 +254,21 @@
400255 */
401256 glRenderer::~glRenderer()
402257 {
403 - EnterCriticalSection(&commandcs);
404 - AddQueue(OP_DELETE,0,2,0,NULL);
405 - Sync(0);
406 - LeaveCriticalSection(&commandcs);
407 - DeleteCriticalSection(&commandcs);
408 - CloseHandle(busy);
409 - CloseHandle(sync);
410 - CloseHandle(start);
 258+ MSG Msg;
 259+ EnterCriticalSection(&cs);
 260+ wndbusy = true;
 261+ SendMessage(hRenderWnd,GLEVENT_DELETE,0,0);
 262+ while(wndbusy)
 263+ {
 264+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
 265+ {
 266+ TranslateMessage(&Msg);
 267+ DispatchMessage(&Msg);
 268+ }
 269+ Sleep(0);
 270+ }
 271+ LeaveCriticalSection(&cs);
 272+ DeleteCriticalSection(&cs);
411273 }
412274
413275 /**
@@ -440,15 +302,21 @@
441303 */
442304 GLuint glRenderer::MakeTexture(GLint min, GLint mag, GLint wraps, GLint wrapt, DWORD width, DWORD height, GLint texformat1, GLint texformat2, GLint texformat3)
443305 {
444 - EnterCriticalSection(&commandcs);
445 - AddQueue(OP_CREATE,0,20,9,4,&min,4,&mag,4,&wraps,4,&wrapt,4,&width,
446 - 4,&height,4,&texformat1,4,&texformat2,4,&texformat3);
447 - Sync(0);
448 - LeaveCriticalSection(&commandcs);
449 - return (GLuint)output;
 306+ EnterCriticalSection(&cs);
 307+ inputs[0] = (void*)min;
 308+ inputs[1] = (void*)mag;
 309+ inputs[2] = (void*)wraps;
 310+ inputs[3] = (void*)wrapt;
 311+ inputs[4] = (void*)width;
 312+ inputs[5] = (void*)height;
 313+ inputs[6] = (void*)texformat1;
 314+ inputs[7] = (void*)texformat2;
 315+ inputs[8] = (void*)texformat3;
 316+ SendMessage(hRenderWnd,GLEVENT_CREATE,0,0);
 317+ LeaveCriticalSection(&cs);
 318+ return (GLuint)outputs[0];
450319 }
451320
452 -
453321 /**
454322 * Uploads the content of a surface to an OpenGL texture.
455323 * @param buffer
@@ -475,14 +343,37 @@
476344 * @param texformat3
477345 * OpenGL internalformat parameter for glTexImage2D
478346 */
479 -void glRenderer::UploadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y,
 347+int glRenderer::UploadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y,
480348 int bigx, int bigy, int pitch, int bigpitch, int bpp, int texformat, int texformat2, int texformat3)
481349 {
482 - EnterCriticalSection(&commandcs);
483 - AddQueue(OP_UPLOAD,0,28,13,4,&buffer,4,&bigbuffer,4,&texture,4,&x,4,&y,4,&bigx,
484 - 4,&bigy,4,&pitch,4,&bigpitch,4,&bpp,4,&texformat,4,&texformat2,4,&texformat3);
485 - Sync(0);
486 - LeaveCriticalSection(&commandcs);
 350+ EnterCriticalSection(&cs);
 351+ MSG Msg;
 352+ inputs[0] = buffer;
 353+ inputs[1] = bigbuffer;
 354+ inputs[2] = (void*)texture;
 355+ inputs[3] = (void*)x;
 356+ inputs[4] = (void*)y;
 357+ inputs[5] = (void*)bigx;
 358+ inputs[6] = (void*)bigy;
 359+ inputs[7] = (void*)pitch;
 360+ inputs[8] = (void*)bigpitch;
 361+ inputs[9] = (void*)bpp;
 362+ inputs[10] = (void*)texformat;
 363+ inputs[11] = (void*)texformat2;
 364+ inputs[12] = (void*)texformat3;
 365+ wndbusy = true;
 366+ SendMessage(hRenderWnd,GLEVENT_UPLOAD,0,0);
 367+ while(wndbusy)
 368+ {
 369+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
 370+ {
 371+ TranslateMessage(&Msg);
 372+ DispatchMessage(&Msg);
 373+ }
 374+ Sleep(0);
 375+ }
 376+ LeaveCriticalSection(&cs);
 377+ return (int)outputs[0];
487378 }
488379
489380 /**
@@ -509,14 +400,36 @@
510401 * @param texformat2
511402 * OpenGL type parameter for glGetTexImage
512403 */
513 -void glRenderer::DownloadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y,
 404+int glRenderer::DownloadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y,
514405 int bigx, int bigy, int pitch, int bigpitch, int bpp, int texformat, int texformat2)
515406 {
516 - EnterCriticalSection(&commandcs);
517 - AddQueue(OP_DOWNLOAD,0,26,12,4,&buffer,4,&bigbuffer,4,&texture,4,&x,4,&y,4,&bigx,
518 - 4,&bigy,4,&pitch,4,&bigpitch,4,&bpp,4,&texformat,4,&texformat2);
519 - Sync(0);
520 - LeaveCriticalSection(&commandcs);
 407+ EnterCriticalSection(&cs);
 408+ MSG Msg;
 409+ inputs[0] = buffer;
 410+ inputs[1] = bigbuffer;
 411+ inputs[2] = (void*)texture;
 412+ inputs[3] = (void*)x;
 413+ inputs[4] = (void*)y;
 414+ inputs[5] = (void*)bigx;
 415+ inputs[6] = (void*)bigy;
 416+ inputs[7] = (void*)pitch;
 417+ inputs[8] = (void*)bigpitch;
 418+ inputs[9] = (void*)bpp;
 419+ inputs[10] = (void*)texformat;
 420+ inputs[11] = (void*)texformat2;
 421+ wndbusy = true;
 422+ SendMessage(hRenderWnd,GLEVENT_DOWNLOAD,0,0);
 423+ while(wndbusy)
 424+ {
 425+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
 426+ {
 427+ TranslateMessage(&Msg);
 428+ DispatchMessage(&Msg);
 429+ }
 430+ Sleep(0);
 431+ }
 432+ LeaveCriticalSection(&cs);
 433+ return (int)outputs[0];
521434 }
522435
523436 /**
@@ -526,9 +439,10 @@
527440 */
528441 void glRenderer::DeleteTexture(GLuint texture)
529442 {
530 - EnterCriticalSection(&commandcs);
531 - AddQueue(OP_DELETETEX,0,4,1,4,&texture);
532 - LeaveCriticalSection(&commandcs);
 443+ EnterCriticalSection(&cs);
 444+ inputs[0] = (void*)texture;
 445+ SendMessage(hRenderWnd,GLEVENT_DELETETEX,0,0);
 446+ LeaveCriticalSection(&cs);
533447 }
534448
535449 /**
@@ -544,7 +458,7 @@
545459 * entire surface will be used.
546460 * @param dwFlags
547461 * Flags to determine the behavior of the blitter. Certain flags control the
548 - * synchronization of the operation:
 462+ * synchronization of the operation: (not yet implemented)
549463 * - DDBLT_ASYNC: Adds the command to the queue. If the queue is full, returns
550464 * DDERR_WASSTILLDRAWING.
551465 * - DDBLT_DONOTWAIT: Fails and returns DDERR_WASSTILLDRAWING if the queue is full.
@@ -557,26 +471,27 @@
558472 HRESULT glRenderer::Blt(LPRECT lpDestRect, glDirectDrawSurface7 *src,
559473 glDirectDrawSurface7 *dest, LPRECT lpSrcRect, DWORD dwFlags, LPDDBLTFX lpDDBltFx)
560474 {
561 - DWORD nullfx = 0xFFFFFFFF;
562 - RECT emptyrect = nullrect;
563 - EnterCriticalSection(&commandcs);
564 - int syncmode = 0;
565 - if(dwFlags & DDBLT_ASYNC) syncmode = 1;
566 - if(dwFlags & DDBLT_DONOTWAIT) syncmode = 2;
567 - if(!lpSrcRect) lpSrcRect = &emptyrect;
568 - if(!lpDestRect) lpDestRect = &emptyrect;
569 - int fxsize = 4;
570 - if(lpDDBltFx) fxsize = sizeof(DDBLTFX);
571 - else lpDDBltFx = (LPDDBLTFX)&nullfx;
572 - if(AddQueue(OP_BLT,syncmode,5+(sizeof(RECT)/2)+(fxsize/4),6,sizeof(RECT),lpDestRect,4,&src,
573 - 4,&dest,sizeof(RECT),lpSrcRect,4,&dwFlags,fxsize,lpDDBltFx))
 475+ MSG Msg;
 476+ EnterCriticalSection(&cs);
 477+ inputs[0] = lpDestRect;
 478+ inputs[1] = src;
 479+ inputs[2] = dest;
 480+ inputs[3] = lpSrcRect;
 481+ inputs[4] = (void*)dwFlags;
 482+ inputs[5] = lpDDBltFx;
 483+ wndbusy = true;
 484+ SendMessage(hRenderWnd,GLEVENT_BLT,0,0);
 485+ while(wndbusy)
574486 {
575 - LeaveCriticalSection(&commandcs);
576 - return DDERR_WASSTILLDRAWING;
 487+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
 488+ {
 489+ TranslateMessage(&Msg);
 490+ DispatchMessage(&Msg);
 491+ }
 492+ Sleep(0);
577493 }
578 - if(dwFlags & DDBLT_WAIT) Sync(0);
579 - LeaveCriticalSection(&commandcs);
580 - return DD_OK;
 494+ LeaveCriticalSection(&cs);
 495+ return (HRESULT)outputs[0];
581496 }
582497
583498 /**
@@ -592,10 +507,24 @@
593508 */
594509 void glRenderer::DrawScreen(GLuint texture, GLuint paltex, glDirectDrawSurface7 *dest, glDirectDrawSurface7 *src)
595510 {
596 - EnterCriticalSection(&commandcs);
597 - AddQueue(OP_DRAWSCREEN,0,10,4,4,&texture,4,&paltex,4,&dest,4,&src);
598 - Sync(0);
599 - LeaveCriticalSection(&commandcs);
 511+ MSG Msg;
 512+ EnterCriticalSection(&cs);
 513+ inputs[0] = (void*)texture;
 514+ inputs[1] = (void*)paltex;
 515+ inputs[2] = dest;
 516+ inputs[3] = src;
 517+ wndbusy = true;
 518+ SendMessage(hRenderWnd,GLEVENT_DRAWSCREEN,0,0);
 519+ while(wndbusy)
 520+ {
 521+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
 522+ {
 523+ TranslateMessage(&Msg);
 524+ DispatchMessage(&Msg);
 525+ }
 526+ Sleep(0);
 527+ }
 528+ LeaveCriticalSection(&cs);
600529 }
601530
602531 /**
@@ -605,9 +534,21 @@
606535 */
607536 void glRenderer::InitD3D(int zbuffer)
608537 {
609 - EnterCriticalSection(&commandcs);
610 - AddQueue(OP_INITD3D,0,4,1,4,zbuffer);
611 - LeaveCriticalSection(&commandcs);
 538+ MSG Msg;
 539+ EnterCriticalSection(&cs);
 540+ wndbusy = true;
 541+ inputs[0] = (void*)zbuffer;
 542+ SendMessage(hRenderWnd,GLEVENT_INITD3D,0,0);
 543+ while(wndbusy)
 544+ {
 545+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
 546+ {
 547+ TranslateMessage(&Msg);
 548+ DispatchMessage(&Msg);
 549+ }
 550+ Sleep(0);
 551+ }
 552+ LeaveCriticalSection(&cs);
612553 }
613554
614555 /**
@@ -631,23 +572,49 @@
632573 */
633574 HRESULT glRenderer::Clear(glDirectDrawSurface7 *target, DWORD dwCount, LPD3DRECT lpRects, DWORD dwFlags, DWORD dwColor, D3DVALUE dvZ, DWORD dwStencil)
634575 {
635 - EnterCriticalSection(&commandcs);
636 - int rectsize = dwCount * sizeof(D3DRECT);
637 - AddQueue(OP_CLEAR,0,15+(rectsize/4),7,4,&target,4,&dwCount,rectsize,lpRects,
638 - 4,dwFlags,4,dwColor,4,dvZ,4,dwStencil);
639 - LeaveCriticalSection(&commandcs);
640 - return D3D_OK;
 576+ MSG Msg;
 577+ EnterCriticalSection(&cs);
 578+ wndbusy = true;
 579+ inputs[0] = target;
 580+ inputs[1] = (void*)dwCount;
 581+ inputs[2] = lpRects;
 582+ inputs[3] = (void*)dwFlags;
 583+ inputs[4] = (void*)dwColor;
 584+ memcpy(&inputs[5],&dvZ,4);
 585+ inputs[6] = (void*)dwStencil;
 586+ SendMessage(hRenderWnd,GLEVENT_CLEAR,0,0);
 587+ while(wndbusy)
 588+ {
 589+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
 590+ {
 591+ TranslateMessage(&Msg);
 592+ DispatchMessage(&Msg);
 593+ }
 594+ Sleep(0);
 595+ }
 596+ LeaveCriticalSection(&cs);
 597+ return (HRESULT)outputs[0];
641598 }
642599
643600 /**
644 - * Issues a glFlush command and empties the queue.
 601+ * Flushes queued OpenGL commands to the GPU.
645602 */
646603 void glRenderer::Flush()
647604 {
648 - EnterCriticalSection(&commandcs);
649 - AddQueue(OP_CLEAR,0,2,0,0,NULL);
650 - Sync(0);
651 - LeaveCriticalSection(&commandcs);
 605+ MSG Msg;
 606+ EnterCriticalSection(&cs);
 607+ wndbusy = true;
 608+ SendMessage(hRenderWnd,GLEVENT_FLUSH,0,0);
 609+ while(wndbusy)
 610+ {
 611+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
 612+ {
 613+ TranslateMessage(&Msg);
 614+ DispatchMessage(&Msg);
 615+ }
 616+ Sleep(0);
 617+ }
 618+ LeaveCriticalSection(&cs);
652619 }
653620
654621 /**
@@ -670,89 +637,40 @@
671638 * @param indexcount
672639 * Number of vertex indices. May be 0 for non-indexed mode.
673640 * @param flags
674 - * Set to D3DDP_WAIT to wait until the queue has processed the call.
 641+ * Set to D3DDP_WAIT to wait until the queue has processed the call. (not yet
 642+ * implemented)
675643 * @return
676644 * D3D_OK if the call succeeds, or D3DERR_INVALIDVERTEXTYPE if the vertex format
677645 * has no position coordinates.
678646 */
679 -HRESULT glRenderer::DrawPrimitives(glDirect3DDevice7 *device, GLenum mode, GLVERTEX *vertices, bool packed,
680 - DWORD *texformats, DWORD count, LPWORD indices, DWORD indexcount, DWORD flags)
 647+HRESULT glRenderer::DrawPrimitives(glDirect3DDevice7 *device, GLenum mode, GLVERTEX *vertices, int *texformats, DWORD count, LPWORD indices,
 648+ DWORD indexcount, DWORD flags)
681649 {
682 - if(!vertices[0].data) return D3DERR_INVALIDVERTEXTYPE;
683 - GLVERTEX vertdata[18];
684 - EnterCriticalSection(&commandcs);
685 - __int64 shader = device->SelectShader(vertices);
686 - int vertsize = 0;
687 - if(packed)
 650+ MSG Msg;
 651+ EnterCriticalSection(&cs);
 652+ wndbusy = true;
 653+ inputs[0] = device;
 654+ inputs[1] = (void*)mode;
 655+ inputs[2] = vertices;
 656+ inputs[3] = texformats;
 657+ inputs[4] = (void*)count;
 658+ inputs[5] = indices;
 659+ inputs[6] = (void*)indexcount;
 660+ inputs[7] = (void*)flags;
 661+ SendMessage(hRenderWnd,GLEVENT_DRAWPRIMITIVES,0,0);
 662+ while(wndbusy)
688663 {
689 - vertsize = vertices[0].stride * count;
690 - AddQueue(OP_DRAWPRIMITIVES,0,NextMultipleOf4(40+(18*sizeof(GLVERTEX))+(8*sizeof(DWORD))+(indexcount*sizeof(WORD))+vertsize)/4,
691 - 10,4,&device,4,&mode,18*sizeof(GLVERTEX),vertices,1,&packed,8*sizeof(DWORD),texformats,4,&count,indexcount*sizeof(WORD),
692 - &indices,4,&indexcount,4,&flags,8,&shader,vertsize,vertices[0].data);
693 - }
694 - else
695 - {
696 - for(int i = 0; i < 18; i++)
 664+ while(PeekMessage(&Msg,hRenderWnd,0,0,PM_REMOVE))
697665 {
698 - vertdata[i].stride = vertices[i].stride;
699 - if(vertices[i].data)
700 - {
701 - vertdata[i].data = (void*)vertsize;
702 - vertsize += (vertices[i].stride * count);
703 - }
704 - else vertdata[i].data = (void*)-1;
 666+ TranslateMessage(&Msg);
 667+ DispatchMessage(&Msg);
705668 }
706 - FIXME("glRenderer::DrawPrimitives: Add Strided Vertex format");
707 - AddQueue(OP_DRAWPRIMITIVES,0,NextMultipleOf4(32+(18*sizeof(GLVERTEX))+(8*sizeof(DWORD))+(indexcount*sizeof(WORD))+vertsize)/4,
708 - 10,4,&device,4,&mode,18*sizeof(GLVERTEX),vertdata,1,packed,8*sizeof(DWORD),texformats,4,count,indexcount*sizeof(WORD),
709 - indices,4,indexcount,4,flags,vertsize,vertdata[0].data);
 669+ Sleep(0);
710670 }
711 - if(flags & D3DDP_WAIT) Sync(0);
712 - LeaveCriticalSection(&commandcs);
713 - return D3D_OK;
 671+ return (HRESULT)outputs[0];
714672 }
715673
716674 /**
717 - * Sets one or more render states in the glRenderer class
718 - * @param index
719 - * Index of the render state(s) to set
720 - * @param count
721 - * Number of states to set at once
722 - * @param data
723 - * Pointer to state data to copy
724 - * @remark
725 - * If the render thread is not currently running, this function will immediately
726 - * copy the data to the renderer. Otherwise, it will add a command to the queue.
727 - */
728 -void glRenderer::SetRenderState(DWORD index, DWORD count, DWORD *data)
729 -{
730 - EnterCriticalSection(&commandcs);
731 - if(!running) memcpy(&renderstate[index],data,count*sizeof(DWORD));
732 - else AddQueue(OP_SETRENDERSTATE,0,4+count,2,4,&index,count*4,data);
733 - LeaveCriticalSection(&commandcs);
734 -}
735 -
736 -/**
737 - * Sets one or more Direct3D matrices in the glRenderer class
738 - * @param index
739 - * Index of the matrix or matrices to set
740 - * @param count
741 - * Number of matrices to set at once
742 - * @param data
743 - * Pointer to matrices to copy
744 - * @remark
745 - * If the render thread is not currently running, this function will immediately
746 - * copy the data to the renderer. Otherwise, it will add a command to the queue.
747 - */
748 -void glRenderer::SetMatrix(DWORD index, DWORD count, D3DMATRIX *data)
749 -{
750 - EnterCriticalSection(&commandcs);
751 - if(!running) memcpy(&renderstate[index],data,count*sizeof(D3DMATRIX));
752 - else AddQueue(OP_SETMATRIX,0,4+((count*sizeof(D3DMATRIX))/4),2,4,&index,count*4,data);
753 - LeaveCriticalSection(&commandcs);
754 -}
755 -
756 -/**
757675 * Main loop for glRenderer class
758676 * @return
759677 * Returns 0 to signal successful thread termination
@@ -759,102 +677,15 @@
760678 */
761679 DWORD glRenderer::_Entry()
762680 {
763 - DWORD size;
764681 MSG Msg;
 682+ EnterCriticalSection(&cs);
765683 _InitGL((int)inputs[0],(int)inputs[1],(int)inputs[2],(int)inputs[3],(HWND)inputs[4],(glDirectDraw7*)inputs[5]);
766 - dead = false;
767 - queue = (LPDWORD)malloc(1048576);
768 - queuesize = 1048576/sizeof(DWORD);
769 - queuelength = queue_read = queue_write = syncsize = 0;
770 - SetEvent(busy);
771 - start = CreateEvent(NULL,TRUE,FALSE,NULL);
772 - ResetEvent(start);
773 - sync = CreateEvent(NULL,TRUE,FALSE,NULL);
774 - queueloop:
775 - MsgWaitForMultipleObjects(1,&start,FALSE,INFINITE,QS_ALLEVENTS);
776 - if(PeekMessage(&Msg,NULL,0,0,PM_REMOVE))
 684+ LeaveCriticalSection(&cs);
 685+ while(GetMessage(&Msg, NULL, 0, 0) > 0)
777686 {
778687 TranslateMessage(&Msg);
779688 DispatchMessage(&Msg);
780689 }
781 - if(queuelength)
782 - {
783 - running = true;
784 - switch(queue[queue_read])
785 - {
786 - case OP_NULL:
787 - default:
788 - break;
789 - case OP_DELETE:
790 - DestroyWindow(hRenderWnd);
791 - break;
792 - case OP_CREATE:
793 - if(queue[queue_read+1] != 20) break;
794 - output = (void*)_MakeTexture(queue[queue_read+3],queue[queue_read+5],queue[queue_read+7],queue[queue_read+9],
795 - queue[queue_read+11],queue[queue_read+13],queue[queue_read+15],queue[queue_read+17],queue[queue_read+19]);
796 - break;
797 - case OP_UPLOAD:
798 - if(queue[queue_read+1] != 28) break;
799 - _UploadTexture((char*)queue[queue_read+3],(char*)queue[queue_read+5],queue[queue_read+7],queue[queue_read+9],
800 - queue[queue_read+11],queue[queue_read+13],queue[queue_read+15],queue[queue_read+17],queue[queue_read+19],
801 - queue[queue_read+21],queue[queue_read+23],queue[queue_read+25],queue[queue_read+27]);
802 - break;
803 - case OP_DOWNLOAD:
804 - if(queue[queue_read+1] != 26) break;
805 - _DownloadTexture((char*)queue[queue_read+3],(char*)queue[queue_read+5],queue[queue_read+7],queue[queue_read+9],
806 - queue[queue_read+11],queue[queue_read+13],queue[queue_read+15],queue[queue_read+17],queue[queue_read+19],
807 - queue[queue_read+21],queue[queue_read+23],queue[queue_read+25]);
808 - break;
809 - case OP_DELETETEX:
810 - if(queue[queue_read+1] != 4) break;
811 - _DeleteTexture(queue[queue_read+3]);
812 - break;
813 - case OP_BLT:
814 - if(queue[queue_read+1] < 5) break;
815 - _Blt((LPRECT)&queue[queue_read+3],(glDirectDrawSurface7*)queue[queue_read+4+(sizeof(RECT)/4)],
816 - (glDirectDrawSurface7*)queue[queue_read+6+(sizeof(RECT)/4)],(LPRECT)&queue[queue_read+8+(sizeof(RECT)/4)],
817 - queue[queue_read+9+(sizeof(RECT)/2)],(LPDDBLTFX)&queue[queue_read+11+(sizeof(RECT)/2)]);
818 - break;
819 - case OP_DRAWSCREEN:
820 - if(queue[queue_read+1] != 10) break;
821 - _DrawScreen(queue[queue_read+3],queue[queue_read+5],(glDirectDrawSurface7*)queue[queue_read+7],
822 - (glDirectDrawSurface7*)queue[queue_read+9]);
823 - break;
824 - case OP_INITD3D:
825 - if(queue[queue_read+1] != 4) break;
826 - _InitD3D(queue[queue_read+3]);
827 - break;
828 - case OP_CLEAR:
829 - if(queue[queue_read+1] < 15) break;
830 - size = queue[queue_read+1] - 15;
831 - if(!size) _Clear((glDirectDrawSurface7*)queue[queue_read+3],queue[queue_read+5],NULL,
832 - queue[queue_read+8],queue[queue_read+10],queue[queue_read+12],queue[queue_read+14]);
833 - else _Clear((glDirectDrawSurface7*)queue[queue_read+3],queue[queue_read+5],(LPD3DRECT)&queue[queue_read+7],
834 - queue[queue_read+8+size],queue[queue_read+10+size],queue[queue_read+12+size],
835 - queue[queue_read+14+size]);
836 - break;
837 - case OP_FLUSH:
838 - _Flush();
839 - break;
840 - }
841 - EnterCriticalSection(&queuecs);
842 - queuelength--;
843 - queue_read+=queue[queue_read+1];
844 - if((queue_read >= syncsize) && syncsize != 0) SetEvent(sync);
845 - }
846 - else EnterCriticalSection(&queuecs);
847 - if(!queuelength)
848 - {
849 - ResetEvent(start);
850 - queue_read = 0;
851 - queue_write = 0;
852 - running = false;
853 - SetEvent(sync);
854 - }
855 - LeaveCriticalSection(&queuecs);
856 - if(!dead) goto queueloop;
857 - free(queue);
858 - queue = NULL;
859690 return 0;
860691 }
861692
@@ -909,13 +740,8 @@
910741 {
911742 width = GetSystemMetrics(SM_CXSCREEN);
912743 height = GetSystemMetrics(SM_CYSCREEN);
913 -#ifdef _DEBUG
914 - hRenderWnd = CreateWindowExA(WS_EX_TOOLWINDOW|WS_EX_LAYERED|WS_EX_TRANSPARENT,
915 - "DXGLRenderWindow","Renderer",WS_POPUP,0,0,width,height,0,0,NULL,this);
916 -#else
917744 hRenderWnd = CreateWindowExA(WS_EX_TOOLWINDOW|WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_TOPMOST,
918745 "DXGLRenderWindow","Renderer",WS_POPUP,0,0,width,height,0,0,NULL,this);
919 -#endif
920746 hasHWnd = false;
921747 SetWindowPos(hRenderWnd,HWND_TOP,0,0,rectRender.right,rectRender.bottom,SWP_SHOWWINDOW|SWP_NOACTIVATE);
922748 }
@@ -971,6 +797,7 @@
972798 gllock = false;
973799 return FALSE;
974800 }
 801+ wndbusy = false;
975802 gllock = false;
976803 InitGLExt();
977804 SetSwap(1);
@@ -1017,7 +844,7 @@
1018845 return TRUE;
1019846 }
1020847
1021 -void glRenderer::_Blt(LPRECT lpDestRect, glDirectDrawSurface7 *src,
 848+HRESULT glRenderer::_Blt(LPRECT lpDestRect, glDirectDrawSurface7 *src,
1022849 glDirectDrawSurface7 *dest, LPRECT lpSrcRect, DWORD dwFlags, LPDDBLTFX lpDDBltFx)
1023850 {
1024851 LONG sizes[6];
@@ -1032,6 +859,7 @@
1033860 if(memcmp(&r2,&r,sizeof(RECT)))
1034861 SetWindowPos(hRenderWnd,NULL,0,0,r.right,r.bottom,SWP_SHOWWINDOW);
1035862 }
 863+ wndbusy = false;
1036864 ddInterface->GetSizes(sizes);
1037865 int error;
1038866 error = SetFBO(dest->texture,0,false);
@@ -1172,7 +1000,7 @@
11731001 (ddsd.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE)) ||
11741002 ((ddsd.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) &&
11751003 !(ddsd.ddsCaps.dwCaps & DDSCAPS_FLIP)))_DrawScreen(dest->texture,dest->paltex,dest,dest);
1176 - SetEvent(busy);
 1004+ return DD_OK;
11771005 }
11781006
11791007 GLuint glRenderer::_MakeTexture(GLint min, GLint mag, GLint wraps, GLint wrapt, DWORD width, DWORD height, GLint texformat1, GLint texformat2, GLint texformat3)
@@ -1242,6 +1070,7 @@
12431071 if(memcmp(&r2,&r,sizeof(RECT)))
12441072 SetWindowPos(hRenderWnd,NULL,0,0,r.right,r.bottom,SWP_SHOWWINDOW);
12451073 }
 1074+ wndbusy = false;
12461075 RECT *viewrect = &r2;
12471076 SetSwap(swapinterval);
12481077 LONG sizes[6];
@@ -1391,15 +1220,14 @@
13921221 void glRenderer::_DeleteTexture(GLuint texture)
13931222 {
13941223 glDeleteTextures(1,&texture);
1395 - SetEvent(busy);
13961224 }
13971225
13981226 void glRenderer::_InitD3D(int zbuffer)
13991227 {
1400 - if(zbuffer) glEnable(GL_DEPTH_TEST);
1401 - SetEvent(busy);
 1228+ wndbusy = false;
14021229 glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
14031230 GLfloat ambient[] = {0.0,0.0,0.0,0.0};
 1231+ if(zbuffer) glEnable(GL_DEPTH_TEST);
14041232 glDepthFunc(GL_LEQUAL);
14051233 glDisable(GL_DITHER);
14061234 }
@@ -1408,9 +1236,13 @@
14091237 {
14101238 if(dwCount)
14111239 {
 1240+ outputs[0] = (void*)DDERR_INVALIDPARAMS;
14121241 FIXME("glDirect3DDevice7::Clear: Cannot clear rects yet.");
 1242+ wndbusy = false;
14131243 return;
14141244 }
 1245+ outputs[0] = (void*)D3D_OK;
 1246+ wndbusy = false;
14151247 GLfloat color[4];
14161248 dwordto4float(dwColor,color);
14171249 if(target->zbuffer) SetFBO(target->texture,target->GetZBuffer()->texture,target->GetZBuffer()->hasstencil);
@@ -1434,13 +1266,12 @@
14351267 glClear(clearbits);
14361268 if(target->zbuffer) target->zbuffer->dirty |= 2;
14371269 target->dirty |= 2;
1438 - SetEvent(busy);
14391270 }
14401271
14411272 void glRenderer::_Flush()
14421273 {
 1274+ wndbusy = false;
14431275 glFlush();
1444 - SetEvent(busy);
14451276 }
14461277
14471278 void glRenderer::_DrawPrimitives(glDirect3DDevice7 *device, GLenum mode, GLVERTEX *vertices, int *texformats, DWORD count, LPWORD indices,
@@ -1454,6 +1285,12 @@
14551286 int i;
14561287 if(vertices[1].data) transformed = true;
14571288 else transformed = false;
 1289+ if(!vertices[0].data)
 1290+ {
 1291+ outputs[0] = (void*)DDERR_INVALIDPARAMS;
 1292+ wndbusy = false;
 1293+ return;
 1294+ }
14581295 __int64 shader = device->SelectShader(vertices);
14591296 SetShader(shader,device->texstages,texformats,0);
14601297 _GENSHADER prog = genshaders[current_genshader].shader;
@@ -1536,11 +1373,11 @@
15371374
15381375 }
15391376 }
1540 - if(normal_dirty) _UpdateNormalMatrix();
1541 - if(prog.uniforms[0] != -1) glUniformMatrix4fv(prog.uniforms[0],1,false,(GLfloat*)&matrices[1]);
1542 - if(prog.uniforms[1] != -1) glUniformMatrix4fv(prog.uniforms[1],1,false,(GLfloat*)&matrices[2]);
1543 - if(prog.uniforms[2] != -1) glUniformMatrix4fv(prog.uniforms[2],1,false,(GLfloat*)&matrices[3]);
1544 - if(prog.uniforms[3] != -1) glUniformMatrix3fv(prog.uniforms[3],1,true,(GLfloat*)&matrices[7]);
 1377+ if(device->normal_dirty) device->UpdateNormalMatrix();
 1378+ if(prog.uniforms[0] != -1) glUniformMatrix4fv(prog.uniforms[0],1,false,device->matWorld);
 1379+ if(prog.uniforms[1] != -1) glUniformMatrix4fv(prog.uniforms[1],1,false,device->matView);
 1380+ if(prog.uniforms[2] != -1) glUniformMatrix4fv(prog.uniforms[2],1,false,device->matProjection);
 1381+ if(prog.uniforms[3] != -1) glUniformMatrix3fv(prog.uniforms[3],1,true,device->matNormal);
15451382
15461383 if(prog.uniforms[15] != -1) glUniform4fv(prog.uniforms[15],1,(GLfloat*)&device->material.ambient);
15471384 if(prog.uniforms[16] != -1) glUniform4fv(prog.uniforms[16],1,(GLfloat*)&device->material.diffuse);
@@ -1584,7 +1421,7 @@
15851422 lightindex++;
15861423 }
15871424
1588 - DWORD ambient = renderstate[D3DRENDERSTATE_AMBIENT];
 1425+ DWORD ambient = device->renderstate[D3DRENDERSTATE_AMBIENT];
15891426 if(prog.uniforms[136] != -1)
15901427 glUniform4f(prog.uniforms[136],RGBA_GETRED(ambient),RGBA_GETGREEN(ambient),
15911428 RGBA_GETBLUE(ambient),RGBA_GETALPHA(ambient));
@@ -1621,32 +1458,11 @@
16221459 if(device->glDDS7->zbuffer) device->glDDS7->zbuffer->dirty |= 2;
16231460 device->glDDS7->dirty |= 2;
16241461 if(flags & D3DDP_WAIT) glFlush();
1625 - SetEvent(busy);
 1462+ outputs[0] = (void*)D3D_OK;
 1463+ wndbusy = false;
16261464 return;
16271465 }
1628 -void glRenderer::_UpdateNormalMatrix()
1629 -{
1630 - GLfloat worldview[16];
1631 - GLfloat tmp[16];
16321466
1633 - ZeroMemory(&worldview,sizeof(D3DMATRIX));
1634 - ZeroMemory(&tmp,sizeof(D3DMATRIX));
1635 - __gluMultMatricesf((GLfloat*)&matrices[1],(GLfloat*)&matrices[2],worldview); // Get worldview
1636 - if(__gluInvertMatrixf(worldview,tmp)) // Invert
1637 - {
1638 - memcpy((GLfloat*)&matrices[7],tmp,3*sizeof(GLfloat));
1639 - memcpy((GLfloat*)&matrices[7]+3,tmp+4,3*sizeof(GLfloat));
1640 - memcpy((GLfloat*)&matrices[7]+6,tmp+8,3*sizeof(GLfloat));
1641 - }
1642 - else
1643 - {
1644 - memcpy((GLfloat*)&matrices[7],worldview,3*sizeof(GLfloat));
1645 - memcpy((GLfloat*)&matrices[7]+3,worldview+4,3*sizeof(GLfloat));
1646 - memcpy((GLfloat*)&matrices[7]+6,worldview+8,3*sizeof(GLfloat));
1647 - }
1648 -
1649 - normal_dirty = false;
1650 -}
16511467 LRESULT glRenderer::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
16521468 {
16531469 int oldx,oldy;
@@ -1691,15 +1507,14 @@
16921508 };
16931509 if(hDC) ReleaseDC(hRenderWnd,hDC);
16941510 hDC = NULL;
 1511+ wndbusy = false;
16951512 PostQuitMessage(0);
1696 - dead = true;
16971513 return 0;
16981514 case WM_SETCURSOR:
16991515 hParent = GetParent(hwnd);
17001516 cursor = (HCURSOR)GetClassLong(hParent,GCL_HCURSOR);
17011517 SetCursor(cursor);
1702 - PostMessage(hParent,msg,wParam,lParam);
1703 - return 0;
 1518+ return SendMessage(hParent,msg,wParam,lParam);
17041519 case WM_MOUSEMOVE:
17051520 case WM_LBUTTONDOWN:
17061521 case WM_LBUTTONUP:
@@ -1734,10 +1549,53 @@
17351550 if(oldx >= sizes[2]) oldx = sizes[2]-1;
17361551 if(oldy >= sizes[3]) oldy = sizes[3]-1;
17371552 newpos = oldx + (oldy << 16);
1738 - PostMessage(hParent,msg,wParam,newpos);
 1553+ return SendMessage(hParent,msg,wParam,newpos);
17391554 }
1740 - else PostMessage(hParent,msg,wParam,lParam);
 1555+ else return SendMessage(hParent,msg,wParam,lParam);
 1556+ case GLEVENT_DELETE:
 1557+ DestroyWindow(hRenderWnd);
17411558 return 0;
 1559+ case GLEVENT_CREATE:
 1560+ outputs[0] = (void*)_MakeTexture((GLint)inputs[0],(GLint)inputs[1],(GLint)inputs[2],(GLint)inputs[3],
 1561+ (DWORD)inputs[4],(DWORD)inputs[5],(GLint)inputs[6],(GLint)inputs[7],(GLint)inputs[8]);
 1562+ return 0;
 1563+ case GLEVENT_UPLOAD:
 1564+ outputs[0] = (void*)_UploadTexture((char*)inputs[0],(char*)inputs[1],(GLuint)inputs[2],(int)inputs[3],
 1565+ (int)inputs[4],(int)inputs[5],(int)inputs[6],(int)inputs[7],(int)inputs[8],(int)inputs[9],
 1566+ (int)inputs[10],(int)inputs[11],(int)inputs[12]);
 1567+ wndbusy = false;
 1568+ return 0;
 1569+ case GLEVENT_DOWNLOAD:
 1570+ outputs[0] = (void*)_DownloadTexture((char*)inputs[0],(char*)inputs[1],(GLuint)inputs[2],(int)inputs[3],
 1571+ (int)inputs[4],(int)inputs[5],(int)inputs[6],(int)inputs[7],(int)inputs[8],(int)inputs[9],
 1572+ (int)inputs[10],(int)inputs[11]);
 1573+ wndbusy = false;
 1574+ return 0;
 1575+ case GLEVENT_DELETETEX:
 1576+ _DeleteTexture((GLuint)inputs[0]);
 1577+ return 0;
 1578+ case GLEVENT_BLT:
 1579+ outputs[0] = (void*)_Blt((LPRECT)inputs[0],(glDirectDrawSurface7*)inputs[1],(glDirectDrawSurface7*)inputs[2],
 1580+ (LPRECT)inputs[3],(DWORD)inputs[4],(LPDDBLTFX)inputs[5]);
 1581+ return 0;
 1582+ case GLEVENT_DRAWSCREEN:
 1583+ _DrawScreen((GLuint)inputs[0],(GLuint)inputs[1],(glDirectDrawSurface7*)inputs[2],(glDirectDrawSurface7*)inputs[3]);
 1584+ return 0;
 1585+ case GLEVENT_INITD3D:
 1586+ _InitD3D((int)inputs[0]);
 1587+ return 0;
 1588+ case GLEVENT_CLEAR:
 1589+ memcpy(&tmpfloats[0],&inputs[5],4);
 1590+ _Clear((glDirectDrawSurface7*)inputs[0],(DWORD)inputs[1],(LPD3DRECT)inputs[2],(DWORD)inputs[3],(DWORD)inputs[4],
 1591+ tmpfloats[0],(DWORD)inputs[6]);
 1592+ return 0;
 1593+ case GLEVENT_FLUSH:
 1594+ _Flush();
 1595+ return 0;
 1596+ case GLEVENT_DRAWPRIMITIVES:
 1597+ _DrawPrimitives((glDirect3DDevice7*)inputs[0],(GLenum)inputs[1],(GLVERTEX*)inputs[2],(int*)inputs[3],(DWORD)inputs[4],
 1598+ (LPWORD)inputs[5],(DWORD)inputs[6],(DWORD)inputs[7]);
 1599+ return 0;
17421600 default:
17431601 return DefWindowProc(hwnd,msg,wParam,lParam);
17441602 }
Index: ddraw/glRenderer.h
@@ -52,24 +52,20 @@
5353
5454 extern BltVertex bltvertices[4];
5555
56 -#define OP_NULL 0
57 -#define OP_DELETE 1
58 -#define OP_CREATE 2
59 -#define OP_UPLOAD 3
60 -#define OP_DOWNLOAD 4
61 -#define OP_DELETETEX 5
62 -#define OP_BLT 6
63 -#define OP_DRAWSCREEN 7
64 -#define OP_INITD3D 8
65 -#define OP_CLEAR 9
66 -#define OP_FLUSH 10
67 -#define OP_DRAWPRIMITIVES 11
68 -#define OP_SETRENDERSTATE 12
69 -#define OP_SETMATRIX 13
 56+#define GLEVENT_NULL WM_USER
 57+#define GLEVENT_DELETE WM_USER+1
 58+#define GLEVENT_CREATE WM_USER+2
 59+#define GLEVENT_UPLOAD WM_USER+3
 60+#define GLEVENT_DOWNLOAD WM_USER+4
 61+#define GLEVENT_DELETETEX WM_USER+5
 62+#define GLEVENT_BLT WM_USER+6
 63+#define GLEVENT_DRAWSCREEN WM_USER+7
 64+#define GLEVENT_INITD3D WM_USER+8
 65+#define GLEVENT_CLEAR WM_USER+9
 66+#define GLEVENT_FLUSH WM_USER+10
 67+#define GLEVENT_DRAWPRIMITIVES WM_USER+11
7068
71 -#define OP_RESETQUEUE 0xFFFFFFFF
7269
73 -
7470 extern int swapinterval;
7571 extern inline void SetSwap(int swap);
7672
@@ -83,8 +79,8 @@
8480 glRenderer(int width, int height, int bpp, bool fullscreen, HWND hwnd, glDirectDraw7 *glDD7);
8581 ~glRenderer();
8682 static DWORD WINAPI ThreadEntry(void *entry);
87 - void UploadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y, int bigx, int bigy, int pitch, int bigpitch, int bpp, int texformat, int texformat2, int texformat3);
88 - void DownloadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y, int bigx, int bigy, int pitch, int bigpitch, int bpp, int texformat, int texformat2);
 83+ int UploadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y, int bigx, int bigy, int pitch, int bigpitch, int bpp, int texformat, int texformat2, int texformat3);
 84+ int DownloadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y, int bigx, int bigy, int pitch, int bigpitch, int bpp, int texformat, int texformat2);
8985 HRESULT Blt(LPRECT lpDestRect, glDirectDrawSurface7 *src,
9086 glDirectDrawSurface7 *dest, LPRECT lpSrcRect, DWORD dwFlags, LPDDBLTFX lpDDBltFx);
9187 GLuint MakeTexture(GLint min, GLint mag, GLint wraps, GLint wrapt, DWORD width, DWORD height, GLint texformat1, GLint texformat2, GLint texformat3);
@@ -92,12 +88,8 @@
9389 void DeleteTexture(GLuint texture);
9490 void InitD3D(int zbuffer);
9591 void Flush();
96 - void SetRenderState(DWORD index, DWORD count, DWORD *data);
97 - void SetMatrix(DWORD index, DWORD count, D3DMATRIX *data);
98 - void Sync(int size);
99 - int AddQueue(DWORD opcode, int mode, DWORD size, int paramcount, ...);
10092 HRESULT Clear(glDirectDrawSurface7 *target, DWORD dwCount, LPD3DRECT lpRects, DWORD dwFlags, DWORD dwColor, D3DVALUE dvZ, DWORD dwStencil);
101 - HRESULT DrawPrimitives(glDirect3DDevice7 *device, GLenum mode, GLVERTEX *vertices, bool packed, DWORD *texformats, DWORD count, LPWORD indices,
 93+ HRESULT DrawPrimitives(glDirect3DDevice7 *device, GLenum mode, GLVERTEX *vertices, int *texformats, DWORD count, LPWORD indices,
10294 DWORD indexcount, DWORD flags);
10395 HGLRC hRC;
10496 LRESULT WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
@@ -106,9 +98,9 @@
10799 // In-thread APIs
108100 DWORD _Entry();
109101 BOOL _InitGL(int width, int height, int bpp, int fullscreen, HWND hWnd, glDirectDraw7 *glDD7);
110 - void _UploadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y, int bigx, int bigy, int pitch, int bigpitch, int bpp, int texformat, int texformat2, int texformat3);
111 - void _DownloadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y, int bigx, int bigy, int pitch, int bigpitch, int bpp, int texformat, int texformat2);
112 - void _Blt(LPRECT lpDestRect, glDirectDrawSurface7 *src,
 102+ int _UploadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y, int bigx, int bigy, int pitch, int bigpitch, int bpp, int texformat, int texformat2, int texformat3);
 103+ int _DownloadTexture(char *buffer, char *bigbuffer, GLuint texture, int x, int y, int bigx, int bigy, int pitch, int bigpitch, int bpp, int texformat, int texformat2);
 104+ HRESULT _Blt(LPRECT lpDestRect, glDirectDrawSurface7 *src,
113105 glDirectDrawSurface7 *dest, LPRECT lpSrcRect, DWORD dwFlags, LPDDBLTFX lpDDBltFx);
114106 GLuint _MakeTexture(GLint min, GLint mag, GLint wraps, GLint wrapt, DWORD width, DWORD height, GLint texformat1, GLint texformat2, GLint texformat3);
115107 void _DrawScreen(GLuint texture, GLuint paltex, glDirectDrawSurface7 *dest, glDirectDrawSurface7 *src);
@@ -116,16 +108,14 @@
117109 void _DrawBackbuffer(GLuint *texture, int x, int y);
118110 void _InitD3D(int zbuffer);
119111 void _Clear(glDirectDrawSurface7 *target, DWORD dwCount, LPD3DRECT lpRects, DWORD dwFlags, DWORD dwColor, D3DVALUE dvZ, DWORD dwStencil);
120 - void _DrawPrimitives(glDirect3DDevice7 *device, GLenum mode, GLVERTEX *vertices, int *texcormats, DWORD count, LPWORD indices,
 112+ void glRenderer::_DrawPrimitives(glDirect3DDevice7 *device, GLenum mode, GLVERTEX *vertices, int *texcormats, DWORD count, LPWORD indices,
121113 DWORD indexcount, DWORD flags);
122 - void _SetRenderState(DWORD index, DWORD count, DWORD *data);
123 - void _SetMatrix(DWORD index, DWORD count, D3DMATRIX *data);
124 - void _UpdateNormalMatrix();
125114 glDirectDraw7 *ddInterface;
126115 void _Flush();
127 - void* inputs[7];
128 - void* output;
 116+ void* inputs[32];
 117+ void* outputs[32];
129118 HANDLE hThread;
 119+ bool wndbusy;
130120 HDC hDC;
131121 HWND hWnd;
132122 HWND hRenderWnd;
@@ -132,22 +122,7 @@
133123 bool hasHWnd;
134124 DIB dib;
135125 GLuint PBO;
136 - HANDLE busy;
137 - HANDLE start;
138 - HANDLE sync;
139 - bool running;
140 - CRITICAL_SECTION commandcs;
141 - CRITICAL_SECTION queuecs;
142 - LPDWORD queue;
143 - int queuesize;
144 - int queuelength;
145 - int queue_read;
146 - int queue_write;
147 - int syncsize;
148 - bool dead;
149 - DWORD renderstate[153];
150 - D3DMATRIX matrices[24];
151 - bool normal_dirty;
 126+ CRITICAL_SECTION cs;
152127 };
153128
154129 #endif //_GLRENDERER_H
\ No newline at end of file