wined3d: Optimize render states in the stateblock.
[wine/wine-gecko.git] / dlls / wined3d / stateblock.c
blob79bfe561c1946dfb6a9cbfc9bc36d0ddac93d059
1 /*
2 * state block implementation
4 * Copyright 2002 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
29 /***************************************
30 * Stateblock helper functions follow
31 **************************************/
33 /** Allocates the correct amount of space for pixel and vertex shader constants,
34 * along with their set/changed flags on the given stateblock object
36 HRESULT allocate_shader_constants(IWineD3DStateBlockImpl* object) {
38 IWineD3DStateBlockImpl *This = object;
40 #define WINED3D_MEMCHECK(_object) if (NULL == _object) { FIXME("Out of memory!\n"); return E_OUTOFMEMORY; }
42 /* Allocate space for floating point constants */
43 object->pixelShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
44 WINED3D_MEMCHECK(object->pixelShaderConstantF);
45 object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(pshader_constantsF));
46 WINED3D_MEMCHECK(object->changed.pixelShaderConstantsF);
47 object->vertexShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
48 WINED3D_MEMCHECK(object->vertexShaderConstantF);
49 object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(vshader_constantsF));
50 WINED3D_MEMCHECK(object->changed.vertexShaderConstantsF);
52 list_init(&object->set_vconstantsF);
53 list_init(&object->set_pconstantsF);
55 #undef WINED3D_MEMCHECK
57 return WINED3D_OK;
60 /** Copy all members of one stateblock to another */
61 void stateblock_savedstates_copy(
62 IWineD3DStateBlock* iface,
63 SAVEDSTATES* dest,
64 SAVEDSTATES* source) {
66 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
67 unsigned bsize = sizeof(BOOL);
69 /* Single values */
70 dest->indices = source->indices;
71 dest->material = source->material;
72 dest->fvf = source->fvf;
73 dest->viewport = source->viewport;
74 dest->vertexDecl = source->vertexDecl;
75 dest->pixelShader = source->pixelShader;
76 dest->vertexShader = source->vertexShader;
77 dest->scissorRect = dest->scissorRect;
79 /* Fixed size arrays */
80 memcpy(dest->streamSource, source->streamSource, bsize * MAX_STREAMS);
81 memcpy(dest->streamFreq, source->streamFreq, bsize * MAX_STREAMS);
82 memcpy(dest->textures, source->textures, bsize * MAX_COMBINED_SAMPLERS);
83 memcpy(dest->transform, source->transform, bsize * (HIGHEST_TRANSFORMSTATE + 1));
84 memcpy(dest->renderState, source->renderState, bsize * (WINEHIGHEST_RENDER_STATE + 1));
85 memcpy(dest->textureState, source->textureState, bsize * MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1));
86 memcpy(dest->samplerState, source->samplerState, bsize * MAX_COMBINED_SAMPLERS * (WINED3D_HIGHEST_SAMPLER_STATE + 1));
87 memcpy(dest->clipplane, source->clipplane, bsize * MAX_CLIPPLANES);
88 memcpy(dest->pixelShaderConstantsB, source->pixelShaderConstantsB, bsize * MAX_CONST_B);
89 memcpy(dest->pixelShaderConstantsI, source->pixelShaderConstantsI, bsize * MAX_CONST_I);
90 memcpy(dest->vertexShaderConstantsB, source->vertexShaderConstantsB, bsize * MAX_CONST_B);
91 memcpy(dest->vertexShaderConstantsI, source->vertexShaderConstantsI, bsize * MAX_CONST_I);
93 /* Dynamically sized arrays */
94 memcpy(dest->pixelShaderConstantsF, source->pixelShaderConstantsF, bsize * GL_LIMITS(pshader_constantsF));
95 memcpy(dest->vertexShaderConstantsF, source->vertexShaderConstantsF, bsize * GL_LIMITS(vshader_constantsF));
98 /** Set all members of a stateblock savedstate to the given value */
99 void stateblock_savedstates_set(
100 IWineD3DStateBlock* iface,
101 SAVEDSTATES* states,
102 BOOL value) {
104 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
105 unsigned bsize = sizeof(BOOL);
107 /* Single values */
108 states->indices = value;
109 states->material = value;
110 states->fvf = value;
111 states->viewport = value;
112 states->vertexDecl = value;
113 states->pixelShader = value;
114 states->vertexShader = value;
115 states->scissorRect = value;
117 /* Fixed size arrays */
118 memset(states->streamSource, value, bsize * MAX_STREAMS);
119 memset(states->streamFreq, value, bsize * MAX_STREAMS);
120 memset(states->textures, value, bsize * MAX_COMBINED_SAMPLERS);
121 memset(states->transform, value, bsize * (HIGHEST_TRANSFORMSTATE + 1));
122 memset(states->renderState, value, bsize * (WINEHIGHEST_RENDER_STATE + 1));
123 memset(states->textureState, value, bsize * MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1));
124 memset(states->samplerState, value, bsize * MAX_COMBINED_SAMPLERS * (WINED3D_HIGHEST_SAMPLER_STATE + 1));
125 memset(states->clipplane, value, bsize * MAX_CLIPPLANES);
126 memset(states->pixelShaderConstantsB, value, bsize * MAX_CONST_B);
127 memset(states->pixelShaderConstantsI, value, bsize * MAX_CONST_I);
128 memset(states->vertexShaderConstantsB, value, bsize * MAX_CONST_B);
129 memset(states->vertexShaderConstantsI, value, bsize * MAX_CONST_I);
131 /* Dynamically sized arrays */
132 memset(states->pixelShaderConstantsF, value, bsize * GL_LIMITS(pshader_constantsF));
133 memset(states->vertexShaderConstantsF, value, bsize * GL_LIMITS(vshader_constantsF));
136 void stateblock_copy(
137 IWineD3DStateBlock* destination,
138 IWineD3DStateBlock* source) {
139 int l;
141 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)source;
142 IWineD3DStateBlockImpl *Dest = (IWineD3DStateBlockImpl *)destination;
144 /* IUnknown fields */
145 Dest->lpVtbl = This->lpVtbl;
146 Dest->ref = This->ref;
148 /* IWineD3DStateBlock information */
149 Dest->parent = This->parent;
150 Dest->wineD3DDevice = This->wineD3DDevice;
151 Dest->blockType = This->blockType;
153 /* Saved states */
154 stateblock_savedstates_copy(source, &Dest->changed, &This->changed);
156 /* Single items */
157 Dest->fvf = This->fvf;
158 Dest->vertexDecl = This->vertexDecl;
159 Dest->vertexShader = This->vertexShader;
160 Dest->streamIsUP = This->streamIsUP;
161 Dest->pIndexData = This->pIndexData;
162 Dest->baseVertexIndex = This->baseVertexIndex;
163 /* Dest->lights = This->lights; */
164 Dest->clip_status = This->clip_status;
165 Dest->viewport = This->viewport;
166 Dest->material = This->material;
167 Dest->pixelShader = This->pixelShader;
168 Dest->glsl_program = This->glsl_program;
169 memcpy(&Dest->scissorRect, &This->scissorRect, sizeof(Dest->scissorRect));
171 /* Lights */
172 memset(This->activeLights, 0, sizeof(This->activeLights));
173 for(l = 0; l < LIGHTMAP_SIZE; l++) {
174 struct list *e1, *e2;
175 LIST_FOR_EACH_SAFE(e1, e2, &Dest->lightMap[l]) {
176 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
177 list_remove(&light->entry);
178 HeapFree(GetProcessHeap(), 0, light);
181 LIST_FOR_EACH(e1, &This->lightMap[l]) {
182 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry), *light2;
183 light2 = HeapAlloc(GetProcessHeap(), 0, sizeof(*light));
184 memcpy(light2, light, sizeof(*light));
185 list_add_tail(&This->lightMap[l], &light2->entry);
186 if(light2->glIndex != -1) This->activeLights[light2->glIndex] = light2;
190 /* Fixed size arrays */
191 memcpy(Dest->vertexShaderConstantB, This->vertexShaderConstantB, sizeof(BOOL) * MAX_CONST_B);
192 memcpy(Dest->vertexShaderConstantI, This->vertexShaderConstantI, sizeof(INT) * MAX_CONST_I * 4);
193 memcpy(Dest->pixelShaderConstantB, This->pixelShaderConstantB, sizeof(BOOL) * MAX_CONST_B);
194 memcpy(Dest->pixelShaderConstantI, This->pixelShaderConstantI, sizeof(INT) * MAX_CONST_I * 4);
196 memcpy(Dest->streamStride, This->streamStride, sizeof(UINT) * MAX_STREAMS);
197 memcpy(Dest->streamOffset, This->streamOffset, sizeof(UINT) * MAX_STREAMS);
198 memcpy(Dest->streamSource, This->streamSource, sizeof(IWineD3DVertexBuffer*) * MAX_STREAMS);
199 memcpy(Dest->streamFreq, This->streamFreq, sizeof(UINT) * MAX_STREAMS);
200 memcpy(Dest->streamFlags, This->streamFlags, sizeof(UINT) * MAX_STREAMS);
201 memcpy(Dest->transforms, This->transforms, sizeof(WINED3DMATRIX) * (HIGHEST_TRANSFORMSTATE + 1));
202 memcpy(Dest->clipplane, This->clipplane, sizeof(double) * MAX_CLIPPLANES * 4);
203 memcpy(Dest->renderState, This->renderState, sizeof(DWORD) * (WINEHIGHEST_RENDER_STATE + 1));
204 memcpy(Dest->textures, This->textures, sizeof(IWineD3DBaseTexture*) * MAX_COMBINED_SAMPLERS);
205 memcpy(Dest->textureDimensions, This->textureDimensions, sizeof(int) * MAX_COMBINED_SAMPLERS);
206 memcpy(Dest->textureState, This->textureState, sizeof(DWORD) * MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1));
207 memcpy(Dest->samplerState, This->samplerState, sizeof(DWORD) * MAX_COMBINED_SAMPLERS * (WINED3D_HIGHEST_SAMPLER_STATE + 1));
209 /* Dynamically sized arrays */
210 memcpy(Dest->vertexShaderConstantF, This->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
211 memcpy(Dest->pixelShaderConstantF, This->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
214 /**********************************************************
215 * IWineD3DStateBlockImpl IUnknown parts follows
216 **********************************************************/
217 static HRESULT WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
219 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
220 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
221 if (IsEqualGUID(riid, &IID_IUnknown)
222 || IsEqualGUID(riid, &IID_IWineD3DBase)
223 || IsEqualGUID(riid, &IID_IWineD3DStateBlock)){
224 IUnknown_AddRef(iface);
225 *ppobj = This;
226 return S_OK;
228 *ppobj = NULL;
229 return E_NOINTERFACE;
232 static ULONG WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
233 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
234 ULONG refCount = InterlockedIncrement(&This->ref);
236 TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
237 return refCount;
240 static ULONG WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
241 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
242 ULONG refCount = InterlockedDecrement(&This->ref);
244 TRACE("(%p) : Releasing from %d\n", This, refCount + 1);
246 if (!refCount) {
247 constants_entry *constant, *constant2;
248 int counter;
250 /* type 0 represents the primary stateblock, so free all the resources */
251 if (This->blockType == WINED3DSBT_INIT) {
252 /* NOTE: according to MSDN: The application is responsible for making sure the texture references are cleared down */
253 for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++) {
254 if (This->textures[counter]) {
255 /* release our 'internal' hold on the texture */
256 if(0 != IWineD3DBaseTexture_Release(This->textures[counter])) {
257 TRACE("Texture still referenced by stateblock, applications has leaked Stage = %u Texture = %p\n", counter, This->textures[counter]);
264 for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
265 struct list *e1, *e2;
266 LIST_FOR_EACH_SAFE(e1, e2, &This->lightMap[counter]) {
267 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
268 list_remove(&light->entry);
269 HeapFree(GetProcessHeap(), 0, light);
273 HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
274 HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
275 HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
276 HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
278 LIST_FOR_EACH_ENTRY_SAFE(constant, constant2, &This->set_vconstantsF, constants_entry, entry) {
279 HeapFree(GetProcessHeap(), 0, constant);
282 LIST_FOR_EACH_ENTRY_SAFE(constant, constant2, &This->set_pconstantsF, constants_entry, entry) {
283 HeapFree(GetProcessHeap(), 0, constant);
286 HeapFree(GetProcessHeap(), 0, This);
288 return refCount;
291 /**********************************************************
292 * IWineD3DStateBlockImpl parts follows
293 **********************************************************/
294 static HRESULT WINAPI IWineD3DStateBlockImpl_GetParent(IWineD3DStateBlock *iface, IUnknown **pParent) {
295 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
296 IUnknown_AddRef(This->parent);
297 *pParent = This->parent;
298 return WINED3D_OK;
301 static HRESULT WINAPI IWineD3DStateBlockImpl_GetDevice(IWineD3DStateBlock *iface, IWineD3DDevice** ppDevice){
303 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
305 *ppDevice = (IWineD3DDevice*)This->wineD3DDevice;
306 IWineD3DDevice_AddRef(*ppDevice);
307 return WINED3D_OK;
311 static HRESULT WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface){
313 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
314 IWineD3DStateBlockImpl *targetStateBlock = This->wineD3DDevice->stateBlock;
316 TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
318 /* If not recorded, then update can just recapture */
319 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED */ 0) {
320 IWineD3DStateBlockImpl* tmpBlock;
321 int i;
323 IWineD3DDevice_CreateStateBlock((IWineD3DDevice *)This->wineD3DDevice, This->blockType, (IWineD3DStateBlock**) &tmpBlock, NULL/*parent*/);
325 memcpy(This, tmpBlock, sizeof(IWineD3DStateBlockImpl));
327 /* Move the light elements from the tmpBlock to This. No need to duplicate them, but they have to be removed from tmpBlock
328 * and the pointers updated for the base element in This.
330 * No need to update This->activeLights because the lights objects are untouched(same address)
332 for(i = 0; i < LIGHTMAP_SIZE; i++) {
333 list_init(&This->lightMap[i]); /* This element contains rubish due to the memcpy */
334 list_move_tail(&This->lightMap[i], &tmpBlock->lightMap[i]); /* Cleans the list entry in tmpBlock */
337 IWineD3DStateBlock_Release((IWineD3DStateBlock *)tmpBlock);
339 } else {
340 unsigned int i, j;
342 /* Recorded => Only update 'changed' values */
343 if (This->vertexShader != targetStateBlock->vertexShader) {
344 TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
346 This->vertexShader = targetStateBlock->vertexShader;
349 /* Vertex Shader Float Constants */
350 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
351 if (This->changed.vertexShaderConstantsF[i]) {
352 TRACE("Setting %p from %p %d to { %f, %f, %f, %f }\n", This, targetStateBlock, i,
353 targetStateBlock->vertexShaderConstantF[i * 4],
354 targetStateBlock->vertexShaderConstantF[i * 4 + 1],
355 targetStateBlock->vertexShaderConstantF[i * 4 + 2],
356 targetStateBlock->vertexShaderConstantF[i * 4 + 3]);
358 This->vertexShaderConstantF[i * 4] = targetStateBlock->vertexShaderConstantF[i * 4];
359 This->vertexShaderConstantF[i * 4 + 1] = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
360 This->vertexShaderConstantF[i * 4 + 2] = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
361 This->vertexShaderConstantF[i * 4 + 3] = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
365 /* Vertex Shader Integer Constants */
366 for (i = 0; i < MAX_CONST_I; ++i) {
367 if (This->changed.vertexShaderConstantsI[i]) {
368 TRACE("Setting %p from %p %d to { %d, %d, %d, %d }\n", This, targetStateBlock, i,
369 targetStateBlock->vertexShaderConstantI[i * 4],
370 targetStateBlock->vertexShaderConstantI[i * 4 + 1],
371 targetStateBlock->vertexShaderConstantI[i * 4 + 2],
372 targetStateBlock->vertexShaderConstantI[i * 4 + 3]);
374 This->vertexShaderConstantI[i * 4] = targetStateBlock->vertexShaderConstantI[i * 4];
375 This->vertexShaderConstantI[i * 4 + 1] = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
376 This->vertexShaderConstantI[i * 4 + 2] = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
377 This->vertexShaderConstantI[i * 4 + 3] = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
381 /* Vertex Shader Boolean Constants */
382 for (i = 0; i < MAX_CONST_B; ++i) {
383 if (This->changed.vertexShaderConstantsB[i]) {
384 TRACE("Setting %p from %p %d to %s\n", This, targetStateBlock, i,
385 targetStateBlock->vertexShaderConstantB[i]? "TRUE":"FALSE");
387 This->vertexShaderConstantB[i] = targetStateBlock->vertexShaderConstantB[i];
391 /* Lights... For a recorded state block, we just had a chain of actions to perform,
392 so we need to walk that chain and update any actions which differ */
393 for(i = 0; i < LIGHTMAP_SIZE; i++) {
394 struct list *e, *f;
395 LIST_FOR_EACH(e, &This->lightMap[i]) {
396 BOOL updated = FALSE;
397 PLIGHTINFOEL *src = LIST_ENTRY(e, PLIGHTINFOEL, entry), *realLight;
398 if(!src->changed || !src->enabledChanged) continue;
400 /* Look up the light in the destination */
401 LIST_FOR_EACH(f, &targetStateBlock->lightMap[i]) {
402 realLight = LIST_ENTRY(f, PLIGHTINFOEL, entry);
403 if(realLight->OriginalIndex == src->OriginalIndex) {
404 if(src->changed) {
405 memcpy(&src->OriginalParms, &realLight->OriginalParms, sizeof(src->OriginalParms));
407 if(src->enabledChanged) {
408 /* Need to double check because enabledChanged does not catch enabled -> disabled -> enabled
409 * or disabled -> enabled -> disabled changes
411 if(realLight->glIndex == -1 && src->glIndex != -1) {
412 /* Light disabled */
413 This->activeLights[src->glIndex] = NULL;
414 } else if(realLight->glIndex != -1 && src->glIndex == -1){
415 /* Light enabled */
416 This->activeLights[realLight->glIndex] = src;
418 src->glIndex = realLight->glIndex;
420 updated = TRUE;
421 break;
425 if(updated) {
426 /* Found a light, all done, proceed with next hash entry */
427 continue;
428 } else if(src->changed) {
429 /* Otherwise assign defaul params */
430 memcpy(&src->OriginalParms, &WINED3D_default_light, sizeof(src->OriginalParms));
431 } else {
432 /* Not enabled by default */
433 src->glIndex = -1;
438 /* Recorded => Only update 'changed' values */
439 if (This->pixelShader != targetStateBlock->pixelShader) {
440 TRACE("Updating pixel shader from %p to %p\n", This->pixelShader, targetStateBlock->pixelShader);
442 This->pixelShader = targetStateBlock->pixelShader;
445 /* Pixel Shader Float Constants */
446 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
447 if (This->changed.pixelShaderConstantsF[i]) {
448 TRACE("Setting %p from %p %d to { %f, %f, %f, %f }\n", This, targetStateBlock, i,
449 targetStateBlock->pixelShaderConstantF[i * 4],
450 targetStateBlock->pixelShaderConstantF[i * 4 + 1],
451 targetStateBlock->pixelShaderConstantF[i * 4 + 2],
452 targetStateBlock->pixelShaderConstantF[i * 4 + 3]);
454 This->pixelShaderConstantF[i * 4] = targetStateBlock->pixelShaderConstantF[i * 4];
455 This->pixelShaderConstantF[i * 4 + 1] = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
456 This->pixelShaderConstantF[i * 4 + 2] = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
457 This->pixelShaderConstantF[i * 4 + 3] = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
461 /* Pixel Shader Integer Constants */
462 for (i = 0; i < MAX_CONST_I; ++i) {
463 if (This->changed.pixelShaderConstantsI[i]) {
464 TRACE("Setting %p from %p %d to { %d, %d, %d, %d }\n", This, targetStateBlock, i,
465 targetStateBlock->pixelShaderConstantI[i * 4],
466 targetStateBlock->pixelShaderConstantI[i * 4 + 1],
467 targetStateBlock->pixelShaderConstantI[i * 4 + 2],
468 targetStateBlock->pixelShaderConstantI[i * 4 + 3]);
470 This->pixelShaderConstantI[i * 4] = targetStateBlock->pixelShaderConstantI[i * 4];
471 This->pixelShaderConstantI[i * 4 + 1] = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
472 This->pixelShaderConstantI[i * 4 + 2] = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
473 This->pixelShaderConstantI[i * 4 + 3] = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
477 /* Pixel Shader Boolean Constants */
478 for (i = 0; i < MAX_CONST_B; ++i) {
479 if (This->changed.pixelShaderConstantsB[i]) {
480 TRACE("Setting %p from %p %d to %s\n", This, targetStateBlock, i,
481 targetStateBlock->pixelShaderConstantB[i]? "TRUE":"FALSE");
483 This->pixelShaderConstantB[i] = targetStateBlock->pixelShaderConstantB[i];
487 /* Others + Render & Texture */
488 for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
489 if (This->changed.transform[i] && memcmp(&targetStateBlock->transforms[i],
490 &This->transforms[i],
491 sizeof(WINED3DMATRIX)) != 0) {
492 TRACE("Updating transform %d\n", i);
493 memcpy(&This->transforms[i], &targetStateBlock->transforms[i], sizeof(WINED3DMATRIX));
497 if (This->changed.indices && ((This->pIndexData != targetStateBlock->pIndexData)
498 || (This->baseVertexIndex != targetStateBlock->baseVertexIndex))) {
499 TRACE("Updating pindexData to %p, baseVertexIndex to %d\n",
500 targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
501 This->pIndexData = targetStateBlock->pIndexData;
502 This->baseVertexIndex = targetStateBlock->baseVertexIndex;
505 if(This->changed.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
506 TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
508 This->vertexDecl = targetStateBlock->vertexDecl;
511 if(This->changed.fvf && This->fvf != targetStateBlock->fvf){
512 This->fvf = targetStateBlock->fvf;
515 if (This->changed.material && memcmp(&targetStateBlock->material,
516 &This->material,
517 sizeof(WINED3DMATERIAL)) != 0) {
518 TRACE("Updating material\n");
519 memcpy(&This->material, &targetStateBlock->material, sizeof(WINED3DMATERIAL));
522 if (This->changed.viewport && memcmp(&targetStateBlock->viewport,
523 &This->viewport,
524 sizeof(WINED3DVIEWPORT)) != 0) {
525 TRACE("Updating viewport\n");
526 memcpy(&This->viewport, &targetStateBlock->viewport, sizeof(WINED3DVIEWPORT));
529 if(This->changed.scissorRect && memcmp(&targetStateBlock->scissorRect,
530 &This->scissorRect,
531 sizeof(targetStateBlock->scissorRect)))
533 TRACE("Updating scissor rect\n");
534 memcpy(&targetStateBlock->scissorRect, &This->scissorRect, sizeof(targetStateBlock->scissorRect));
537 for (i = 0; i < MAX_STREAMS; i++) {
538 if (This->changed.streamSource[i] &&
539 ((This->streamStride[i] != targetStateBlock->streamStride[i]) ||
540 (This->streamSource[i] != targetStateBlock->streamSource[i]))) {
541 TRACE("Updating stream source %d to %p, stride to %d\n", i, targetStateBlock->streamSource[i],
542 targetStateBlock->streamStride[i]);
543 This->streamStride[i] = targetStateBlock->streamStride[i];
544 This->streamSource[i] = targetStateBlock->streamSource[i];
547 if (This->changed.streamFreq[i] &&
548 (This->streamFreq[i] != targetStateBlock->streamFreq[i]
549 || This->streamFlags[i] != targetStateBlock->streamFlags[i])){
550 TRACE("Updating stream frequency %d to %d flags to %d\n", i , targetStateBlock->streamFreq[i] ,
551 targetStateBlock->streamFlags[i]);
552 This->streamFreq[i] = targetStateBlock->streamFreq[i];
553 This->streamFlags[i] = targetStateBlock->streamFlags[i];
557 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
558 if (This->changed.clipplane[i] && memcmp(&targetStateBlock->clipplane[i],
559 &This->clipplane[i],
560 sizeof(This->clipplane)) != 0) {
562 TRACE("Updating clipplane %d\n", i);
563 memcpy(&This->clipplane[i], &targetStateBlock->clipplane[i],
564 sizeof(This->clipplane));
568 /* Render */
569 for (i = 0; i <= This->num_contained_render_states; i++) {
570 TRACE("Updating renderState %d to %d\n",
571 This->contained_render_states[i], targetStateBlock->renderState[This->contained_render_states[i]]);
572 This->renderState[This->contained_render_states[i]] = targetStateBlock->renderState[This->contained_render_states[i]];
575 /* Texture states */
576 for (j = 0; j < MAX_TEXTURES; j++) {
577 /* TODO: move over to using memcpy */
578 for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE ; i++) {
579 if (This->changed.textureState[j][i]) {
580 TRACE("Updating texturestagestate %d,%d to %d (was %d)\n", j,i, targetStateBlock->textureState[j][i],
581 This->textureState[j][i]);
582 This->textureState[j][i] = targetStateBlock->textureState[j][i];
587 /* Samplers */
588 /* TODO: move over to using memcpy */
589 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
590 if (This->changed.textures[j]) {
591 TRACE("Updating texture %d to %p (was %p)\n", j, targetStateBlock->textures[j], This->textures[j]);
592 This->textures[j] = targetStateBlock->textures[j];
594 for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE ; i++){ /* States are 1 based */
595 if (This->changed.samplerState[j][i]) {
596 TRACE("Updating sampler state %d,%d to %d (was %d)\n",
597 j, i, targetStateBlock->samplerState[j][i],
598 This->samplerState[j][i]);
599 This->samplerState[j][i] = targetStateBlock->samplerState[j][i];
605 TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
607 return WINED3D_OK;
610 static HRESULT WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
611 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
612 IWineD3DDevice* pDevice = (IWineD3DDevice*)This->wineD3DDevice;
614 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
615 should really perform a delta so that only the changes get updated*/
618 UINT i;
619 UINT j;
621 TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
623 /* FIXME: Only apply applicable states not all states */
625 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */This->blockType == WINED3DSBT_INIT || This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_VERTEXSTATE) {
627 for(i = 0; i < LIGHTMAP_SIZE; i++) {
628 struct list *e;
630 LIST_FOR_EACH(e, &This->lightMap[i]) {
631 PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
633 if(light->changed) {
634 IWineD3DDevice_SetLight(pDevice, light->OriginalIndex, &light->OriginalParms);
636 if(light->enabledChanged) {
637 IWineD3DDevice_SetLightEnable(pDevice, light->OriginalIndex, light->glIndex != -1);
642 /* Vertex Shader */
643 if (This->changed.vertexShader) {
644 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
647 /* Vertex Shader Constants */
648 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
649 if (This->changed.vertexShaderConstantsF[i])
650 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i, This->vertexShaderConstantF + i * 4, 1);
653 for (i = 0; i < MAX_CONST_I; i++) {
654 if (This->changed.vertexShaderConstantsI[i])
655 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i, This->vertexShaderConstantI + i * 4, 1);
658 for (i = 0; i < MAX_CONST_B; i++) {
659 if (This->changed.vertexShaderConstantsB[i])
660 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i, This->vertexShaderConstantB + i, 1);
664 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_PIXELSTATE) {
666 /* Pixel Shader */
667 if (This->changed.pixelShader) {
668 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
671 /* Pixel Shader Constants */
672 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
673 if (This->changed.pixelShaderConstantsF[i])
674 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i, This->pixelShaderConstantF + i * 4, 1);
677 for (i = 0; i < MAX_CONST_I; ++i) {
678 if (This->changed.pixelShaderConstantsI[i])
679 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i, This->pixelShaderConstantI + i * 4, 1);
682 for (i = 0; i < MAX_CONST_B; ++i) {
683 if (This->changed.pixelShaderConstantsB[i])
684 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i, This->pixelShaderConstantB + i, 1);
688 if (This->changed.fvf) {
689 IWineD3DDevice_SetFVF(pDevice, This->fvf);
692 if (This->changed.vertexDecl) {
693 IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
696 /* Others + Render & Texture */
697 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_INIT) {
698 for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
699 if (This->changed.transform[i])
700 IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
703 if (This->changed.indices) {
704 IWineD3DDevice_SetIndices(pDevice, This->pIndexData);
705 IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
708 if (This->changed.material )
709 IWineD3DDevice_SetMaterial(pDevice, &This->material);
711 if (This->changed.viewport)
712 IWineD3DDevice_SetViewport(pDevice, &This->viewport);
714 if (This->changed.scissorRect)
715 IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
717 /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
718 for (i=0; i<MAX_STREAMS; i++) {
719 if (This->changed.streamSource[i])
720 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
722 if (This->changed.streamFreq[i])
723 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
726 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
727 if (This->changed.clipplane[i]) {
728 float clip[4];
730 clip[0] = This->clipplane[i][0];
731 clip[1] = This->clipplane[i][1];
732 clip[2] = This->clipplane[i][2];
733 clip[3] = This->clipplane[i][3];
734 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
738 /* Texture states */
739 for (j = 0; j < MAX_TEXTURES; j++) { /* Set The texture first, just in case it resets the states? */
740 /* TODO: move over to memcpy */
741 for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE; i++) {
742 if (This->changed.textureState[j][i]) { /* tb_dx9_10 failes without this test */
743 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][i] = This->textureState[j][i];
744 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[j][i] = TRUE;
745 /* TODO: Record a display list to apply all gl states. For now apply by brute force */
746 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, i));
751 /* Samplers */
752 /* TODO: move over to memcpy */
753 for (j = 0 ; j < MAX_COMBINED_SAMPLERS; j++){
754 if (This->changed.textures[j]) {
755 if (j < MAX_FRAGMENT_SAMPLERS) {
756 IWineD3DDevice_SetTexture(pDevice, j, This->textures[j]);
757 } else {
758 IWineD3DDevice_SetTexture(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS, This->textures[j]);
761 for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; i++){
762 if (This->changed.samplerState[j][i]) {
763 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][i] = This->samplerState[j][i];
764 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[j][i] = TRUE;
767 /* SetTexture catches nop changes, so the above call does not assure that the sampler is updated */
768 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_SAMPLER(j));
771 } else if (This->blockType == WINED3DSBT_PIXELSTATE) {
773 for (j = 0; j < MAX_TEXTURES; j++) {
774 for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
775 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedPixelStates_T[i]] = This->textureState[j][SavedPixelStates_T[i]];
776 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, SavedPixelStates_T[i]));
780 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
781 for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
782 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedPixelStates_S[i]] = This->samplerState[j][SavedPixelStates_S[i]];
786 } else if (This->blockType == WINED3DSBT_VERTEXSTATE) {
788 for (j = 0; j < MAX_TEXTURES; j++) {
789 for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
790 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedVertexStates_T[i]] = This->textureState[j][SavedVertexStates_T[i]];
791 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, SavedVertexStates_T[i]));
795 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
796 for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
797 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedVertexStates_S[i]] = This->samplerState[j][SavedVertexStates_S[i]];
802 } else {
803 FIXME("Unrecognized state block type %d\n", This->blockType);
806 /* Render */
807 for (i = 0; i <= This->num_contained_render_states; i++) {
808 IWineD3DDevice_SetRenderState(pDevice, This->contained_render_states[i],
809 This->renderState[This->contained_render_states[i]]);
812 stateblock_savedstates_copy(iface, &((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed, &This->changed);
813 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
814 for(j = 0; j < MAX_TEXTURES - 1; j++) {
815 if(((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE) {
816 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = j;
817 break;
820 TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
822 return WINED3D_OK;
825 static HRESULT WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
826 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
827 IWineD3DDevice *device = (IWineD3DDevice *)This->wineD3DDevice;
828 IWineD3DDeviceImpl *ThisDevice = (IWineD3DDeviceImpl *)device;
829 union {
830 WINED3DLINEPATTERN lp;
831 DWORD d;
832 } lp;
833 union {
834 float f;
835 DWORD d;
836 } tmpfloat;
837 unsigned int i;
839 /* Note this may have a large overhead but it should only be executed
840 once, in order to initialize the complete state of the device and
841 all opengl equivalents */
842 TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
843 /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
844 This->blockType = WINED3DSBT_INIT;
846 /* Set some of the defaults for lights, transforms etc */
847 memcpy(&This->transforms[WINED3DTS_PROJECTION], &identity, sizeof(identity));
848 memcpy(&This->transforms[WINED3DTS_VIEW], &identity, sizeof(identity));
849 for (i = 0; i < 256; ++i) {
850 memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], &identity, sizeof(identity));
853 TRACE("Render states\n");
854 /* Render states: */
855 if (ThisDevice->depthStencilBuffer != NULL) {
856 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_TRUE);
857 } else {
858 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_FALSE);
860 IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE, WINED3DFILL_SOLID);
861 IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE, WINED3DSHADE_GOURAUD);
862 lp.lp.wRepeatFactor = 0;
863 lp.lp.wLinePattern = 0;
864 IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN, lp.d);
865 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE, TRUE);
866 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE, FALSE);
867 IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL, TRUE);
868 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND, WINED3DBLEND_ONE);
869 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND, WINED3DBLEND_ZERO);
870 IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE, WINED3DCULL_CCW);
871 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC, WINED3DCMP_LESSEQUAL);
872 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC, WINED3DCMP_ALWAYS);
873 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF, 0);
874 IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE, FALSE);
875 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
876 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE, FALSE);
877 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE, FALSE);
878 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE, 0);
879 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR, 0);
880 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE, WINED3DFOG_NONE);
881 tmpfloat.f = 0.0f;
882 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART, tmpfloat.d);
883 tmpfloat.f = 1.0f;
884 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND, tmpfloat.d);
885 tmpfloat.f = 1.0f;
886 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY, tmpfloat.d);
887 IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS, FALSE);
888 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS, 0);
889 IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE, FALSE);
890 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE, FALSE);
891 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL, WINED3DSTENCILOP_KEEP);
892 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
893 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS, WINED3DSTENCILOP_KEEP);
894 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
895 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
896 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC, WINED3DCMP_ALWAYS);
897 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
898 IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
899 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
900 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
901 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
902 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
903 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
904 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
905 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
906 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
907 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING, TRUE);
908 IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING, TRUE);
909 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT, 0);
910 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE, WINED3DFOG_NONE);
911 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX, TRUE);
912 IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER, TRUE);
913 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS, FALSE);
914 IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE, WINED3DMCS_COLOR1);
915 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE, WINED3DMCS_COLOR2);
916 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE, WINED3DMCS_MATERIAL);
917 IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE, WINED3DMCS_MATERIAL);
918 IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND, WINED3DVBF_DISABLE);
919 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE, 0);
920 IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
921 tmpfloat.f = 1.0f;
922 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE, tmpfloat.d);
923 tmpfloat.f = 1.0f;
924 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN, tmpfloat.d);
925 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE, FALSE);
926 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE, FALSE);
927 tmpfloat.f = 1.0f;
928 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A, tmpfloat.d);
929 tmpfloat.f = 0.0f;
930 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B, tmpfloat.d);
931 tmpfloat.f = 0.0f;
932 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C, tmpfloat.d);
933 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS, TRUE);
934 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
935 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE, WINED3DPATCHEDGE_DISCRETE);
936 tmpfloat.f = 1.0f;
937 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS, tmpfloat.d);
938 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN, 0xbaadcafe);
939 tmpfloat.f = 64.0f;
940 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX, tmpfloat.d);
941 IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
942 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE, 0x0000000F);
943 tmpfloat.f = 0.0f;
944 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR, tmpfloat.d);
945 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP, WINED3DBLENDOP_ADD);
946 IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE, WINED3DDEGREE_CUBIC);
947 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE, WINED3DDEGREE_LINEAR);
948 /* states new in d3d9 */
949 IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE, FALSE);
950 IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS, 0);
951 tmpfloat.f = 1.0f;
952 IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL, tmpfloat.d);
953 IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL, tmpfloat.d);
954 IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE, FALSE);
955 tmpfloat.f = 0.0f;
956 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X, tmpfloat.d);
957 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y, tmpfloat.d);
958 tmpfloat.f = 1.0f;
959 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z, tmpfloat.d);
960 tmpfloat.f = 0.0f;
961 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W, tmpfloat.d);
962 IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
963 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE, FALSE);
964 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL, WINED3DSTENCILOP_KEEP);
965 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
966 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS, WINED3DSTENCILOP_KEEP);
967 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC, WINED3DCMP_ALWAYS);
968 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1, 0x0000000F);
969 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2, 0x0000000F);
970 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3, 0x0000000F);
971 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR, 0xFFFFFFFF);
972 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE, 0);
973 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS, 0);
974 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8, 0);
975 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9, 0);
976 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
977 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
978 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
979 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
980 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
981 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
982 IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
983 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA, WINED3DBLEND_ONE);
984 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA, WINED3DBLEND_ZERO);
985 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA, WINED3DBLENDOP_ADD);
987 /* clipping status */
988 This->clip_status.ClipUnion = 0;
989 This->clip_status.ClipIntersection = 0xFFFFFFFF;
991 /* Texture Stage States - Put directly into state block, we will call function below */
992 for (i = 0; i < MAX_TEXTURES; i++) {
993 TRACE("Setting up default texture states for texture Stage %d\n", i);
994 memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], &identity, sizeof(identity));
995 This->textureState[i][WINED3DTSS_COLOROP ] = (i==0)? WINED3DTOP_MODULATE : WINED3DTOP_DISABLE;
996 This->textureState[i][WINED3DTSS_COLORARG1 ] = WINED3DTA_TEXTURE;
997 This->textureState[i][WINED3DTSS_COLORARG2 ] = WINED3DTA_CURRENT;
998 This->textureState[i][WINED3DTSS_ALPHAOP ] = (i==0)? WINED3DTOP_SELECTARG1 : WINED3DTOP_DISABLE;
999 This->textureState[i][WINED3DTSS_ALPHAARG1 ] = WINED3DTA_TEXTURE;
1000 This->textureState[i][WINED3DTSS_ALPHAARG2 ] = WINED3DTA_CURRENT;
1001 This->textureState[i][WINED3DTSS_BUMPENVMAT00 ] = (DWORD) 0.0;
1002 This->textureState[i][WINED3DTSS_BUMPENVMAT01 ] = (DWORD) 0.0;
1003 This->textureState[i][WINED3DTSS_BUMPENVMAT10 ] = (DWORD) 0.0;
1004 This->textureState[i][WINED3DTSS_BUMPENVMAT11 ] = (DWORD) 0.0;
1005 This->textureState[i][WINED3DTSS_TEXCOORDINDEX ] = i;
1006 This->textureState[i][WINED3DTSS_BUMPENVLSCALE ] = (DWORD) 0.0;
1007 This->textureState[i][WINED3DTSS_BUMPENVLOFFSET ] = (DWORD) 0.0;
1008 This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1009 This->textureState[i][WINED3DTSS_ADDRESSW ] = WINED3DTADDRESS_WRAP;
1010 This->textureState[i][WINED3DTSS_COLORARG0 ] = WINED3DTA_CURRENT;
1011 This->textureState[i][WINED3DTSS_ALPHAARG0 ] = WINED3DTA_CURRENT;
1012 This->textureState[i][WINED3DTSS_RESULTARG ] = WINED3DTA_CURRENT;
1014 This->lowest_disabled_stage = 1;
1016 /* Sampler states*/
1017 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; i++) {
1018 TRACE("Setting up default samplers states for sampler %d\n", i);
1019 This->samplerState[i][WINED3DSAMP_ADDRESSU ] = WINED3DTADDRESS_WRAP;
1020 This->samplerState[i][WINED3DSAMP_ADDRESSV ] = WINED3DTADDRESS_WRAP;
1021 This->samplerState[i][WINED3DSAMP_ADDRESSW ] = WINED3DTADDRESS_WRAP;
1022 This->samplerState[i][WINED3DSAMP_BORDERCOLOR ] = 0x00;
1023 This->samplerState[i][WINED3DSAMP_MAGFILTER ] = WINED3DTEXF_POINT;
1024 This->samplerState[i][WINED3DSAMP_MINFILTER ] = WINED3DTEXF_POINT;
1025 This->samplerState[i][WINED3DSAMP_MIPFILTER ] = WINED3DTEXF_NONE;
1026 This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS ] = 0;
1027 This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL ] = 0;
1028 This->samplerState[i][WINED3DSAMP_MAXANISOTROPY ] = 1;
1029 This->samplerState[i][WINED3DSAMP_SRGBTEXTURE ] = 0;
1030 This->samplerState[i][WINED3DSAMP_ELEMENTINDEX ] = 0; /* TODO: Indicates which element of a multielement texture to use */
1031 This->samplerState[i][WINED3DSAMP_DMAPOFFSET ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1034 /* Under DirectX you can have texture stage operations even if no texture is
1035 bound, whereas opengl will only do texture operations when a valid texture is
1036 bound. We emulate this by creating dummy textures and binding them to each
1037 texture stage, but disable all stages by default. Hence if a stage is enabled
1038 then the default texture will kick in until replaced by a SetTexture call */
1039 ENTER_GL();
1041 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
1042 /* The dummy texture does not have client storage backing */
1043 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1044 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1046 for (i = 0; i < GL_LIMITS(textures); i++) {
1047 GLubyte white = 255;
1049 /* Note this avoids calling settexture, so pretend it has been called */
1050 This->changed.textures[i] = TRUE;
1051 This->textures[i] = NULL;
1053 /* Make appropriate texture active */
1054 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
1055 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1056 checkGLcall("glActiveTextureARB");
1057 } else if (i > 0) {
1058 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
1061 /* Generate an opengl texture name */
1062 glGenTextures(1, &ThisDevice->dummyTextureName[i]);
1063 checkGLcall("glGenTextures");
1064 TRACE("Dummy Texture %d given name %d\n", i, ThisDevice->dummyTextureName[i]);
1066 /* Generate a dummy 2d texture (not using 1d because they cause many
1067 * DRI drivers fall back to sw) */
1068 This->textureDimensions[i] = GL_TEXTURE_2D;
1069 glBindTexture(GL_TEXTURE_2D, ThisDevice->dummyTextureName[i]);
1070 checkGLcall("glBindTexture");
1072 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 1, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white);
1073 checkGLcall("glTexImage2D");
1075 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
1076 /* Reenable because if supported it is enabled by default */
1077 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1078 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1081 LEAVE_GL();
1084 /* Defaulting palettes - Note these are device wide but reinitialized here for convenience*/
1085 for (i = 0; i < MAX_PALETTES; ++i) {
1086 int j;
1087 for (j = 0; j < 256; ++j) {
1088 This->wineD3DDevice->palettes[i][j].peRed = 0xFF;
1089 This->wineD3DDevice->palettes[i][j].peGreen = 0xFF;
1090 This->wineD3DDevice->palettes[i][j].peBlue = 0xFF;
1091 This->wineD3DDevice->palettes[i][j].peFlags = 0xFF;
1094 This->wineD3DDevice->currentPalette = 0;
1096 /* Set default GLSL program to NULL. We won't actually create one
1097 * until the app sets a vertex or pixel shader */
1098 This->glsl_program = NULL;
1100 TRACE("-----------------------> Device defaults now set up...\n");
1101 return WINED3D_OK;
1104 /**********************************************************
1105 * IWineD3DStateBlock VTbl follows
1106 **********************************************************/
1108 const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1110 /* IUnknown */
1111 IWineD3DStateBlockImpl_QueryInterface,
1112 IWineD3DStateBlockImpl_AddRef,
1113 IWineD3DStateBlockImpl_Release,
1114 /* IWineD3DStateBlock */
1115 IWineD3DStateBlockImpl_GetParent,
1116 IWineD3DStateBlockImpl_GetDevice,
1117 IWineD3DStateBlockImpl_Capture,
1118 IWineD3DStateBlockImpl_Apply,
1119 IWineD3DStateBlockImpl_InitStartupStateBlock