2 * state block implementation
4 * Copyright 2002 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
31 /***************************************
32 * Stateblock helper functions follow
33 **************************************/
35 /* Allocates the correct amount of space for pixel and vertex shader constants,
36 * along with their set/changed flags on the given stateblock object
38 static HRESULT stateblock_allocate_shader_constants(IWineD3DStateBlockImpl *object)
40 IWineD3DStateBlockImpl *This = object;
42 /* Allocate space for floating point constants */
43 object->pixelShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
44 if (!object->pixelShaderConstantF) goto fail;
46 object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(pshader_constantsF));
47 if (!object->changed.pixelShaderConstantsF) goto fail;
49 object->vertexShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
50 if (!object->vertexShaderConstantF) goto fail;
52 object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(vshader_constantsF));
53 if (!object->changed.vertexShaderConstantsF) goto fail;
55 object->contained_vs_consts_f = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * GL_LIMITS(vshader_constantsF));
56 if (!object->contained_vs_consts_f) goto fail;
58 object->contained_ps_consts_f = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * GL_LIMITS(pshader_constantsF));
59 if (!object->contained_ps_consts_f) goto fail;
64 ERR("Failed to allocate memory\n");
65 HeapFree(GetProcessHeap(), 0, object->pixelShaderConstantF);
66 HeapFree(GetProcessHeap(), 0, object->changed.pixelShaderConstantsF);
67 HeapFree(GetProcessHeap(), 0, object->vertexShaderConstantF);
68 HeapFree(GetProcessHeap(), 0, object->changed.vertexShaderConstantsF);
69 HeapFree(GetProcessHeap(), 0, object->contained_vs_consts_f);
70 HeapFree(GetProcessHeap(), 0, object->contained_ps_consts_f);
74 /* Copy all members of one stateblock to another */
75 static void stateblock_savedstates_copy(SAVEDSTATES *dest, const SAVEDSTATES *source,
76 const struct wined3d_gl_info *gl_info)
79 dest->primitive_type = source->primitive_type;
80 dest->indices = source->indices;
81 dest->material = source->material;
82 dest->viewport = source->viewport;
83 dest->vertexDecl = source->vertexDecl;
84 dest->pixelShader = source->pixelShader;
85 dest->vertexShader = source->vertexShader;
86 dest->scissorRect = dest->scissorRect;
88 /* Fixed size arrays */
89 dest->streamSource = source->streamSource;
90 dest->streamFreq = source->streamFreq;
91 dest->textures = source->textures;
92 memcpy(dest->transform, source->transform, sizeof(source->transform));
93 memcpy(dest->renderState, source->renderState, sizeof(source->renderState));
94 memcpy(dest->textureState, source->textureState, sizeof(source->textureState));
95 memcpy(dest->samplerState, source->samplerState, sizeof(source->samplerState));
96 dest->clipplane = source->clipplane;
97 dest->pixelShaderConstantsB = source->pixelShaderConstantsB;
98 dest->pixelShaderConstantsI = source->pixelShaderConstantsI;
99 dest->vertexShaderConstantsB = source->vertexShaderConstantsB;
100 dest->vertexShaderConstantsI = source->vertexShaderConstantsI;
102 /* Dynamically sized arrays */
103 memcpy(dest->pixelShaderConstantsF, source->pixelShaderConstantsF,
104 sizeof(BOOL) * gl_info->max_pshader_constantsF);
105 memcpy(dest->vertexShaderConstantsF, source->vertexShaderConstantsF,
106 sizeof(BOOL) * gl_info->max_vshader_constantsF);
109 static inline void stateblock_set_bits(DWORD *map, UINT map_size)
111 DWORD mask = (1 << (map_size & 0x1f)) - 1;
112 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
113 if (mask) map[map_size >> 5] = mask;
116 /* Set all members of a stateblock savedstate to the given value */
117 static void stateblock_savedstates_set(SAVEDSTATES *states, BOOL value, const struct wined3d_gl_info *gl_info)
120 states->primitive_type = value;
121 states->indices = value;
122 states->material = value;
123 states->viewport = value;
124 states->vertexDecl = value;
125 states->pixelShader = value;
126 states->vertexShader = value;
127 states->scissorRect = value;
129 /* Fixed size arrays */
133 states->streamSource = 0xffff;
134 states->streamFreq = 0xffff;
135 states->textures = 0xfffff;
136 stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
137 stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
138 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
139 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3fff;
140 states->clipplane = 0xffffffff;
141 states->pixelShaderConstantsB = 0xffff;
142 states->pixelShaderConstantsI = 0xffff;
143 states->vertexShaderConstantsB = 0xffff;
144 states->vertexShaderConstantsI = 0xffff;
148 states->streamSource = 0;
149 states->streamFreq = 0;
150 states->textures = 0;
151 memset(states->transform, 0, sizeof(states->transform));
152 memset(states->renderState, 0, sizeof(states->renderState));
153 memset(states->textureState, 0, sizeof(states->textureState));
154 memset(states->samplerState, 0, sizeof(states->samplerState));
155 states->clipplane = 0;
156 states->pixelShaderConstantsB = 0;
157 states->pixelShaderConstantsI = 0;
158 states->vertexShaderConstantsB = 0;
159 states->vertexShaderConstantsI = 0;
162 /* Dynamically sized arrays */
163 memset(states->pixelShaderConstantsF, value, sizeof(BOOL) * gl_info->max_pshader_constantsF);
164 memset(states->vertexShaderConstantsF, value, sizeof(BOOL) * gl_info->max_vshader_constantsF);
167 static void stateblock_copy_values(IWineD3DStateBlockImpl *dst, const IWineD3DStateBlockImpl *src,
168 const struct wined3d_gl_info *gl_info)
173 dst->gl_primitive_type = src->gl_primitive_type;
174 dst->vertexDecl = src->vertexDecl;
175 dst->vertexShader = src->vertexShader;
176 dst->streamIsUP = src->streamIsUP;
177 dst->pIndexData = src->pIndexData;
178 dst->IndexFmt = src->IndexFmt;
179 dst->baseVertexIndex = src->baseVertexIndex;
180 dst->clip_status = src->clip_status;
181 dst->viewport = src->viewport;
182 dst->material = src->material;
183 dst->pixelShader = src->pixelShader;
184 dst->scissorRect = src->scissorRect;
187 memset(dst->activeLights, 0, sizeof(dst->activeLights));
188 for (l = 0; l < LIGHTMAP_SIZE; ++l)
190 struct list *e1, *e2;
191 LIST_FOR_EACH_SAFE(e1, e2, &dst->lightMap[l])
193 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
194 list_remove(&light->entry);
195 HeapFree(GetProcessHeap(), 0, light);
198 LIST_FOR_EACH(e1, &src->lightMap[l])
200 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry), *light2;
201 light2 = HeapAlloc(GetProcessHeap(), 0, sizeof(*light));
203 list_add_tail(&dst->lightMap[l], &light2->entry);
204 if (light2->glIndex != -1) dst->activeLights[light2->glIndex] = light2;
208 /* Fixed size arrays */
209 memcpy(dst->vertexShaderConstantB, src->vertexShaderConstantB, sizeof(dst->vertexShaderConstantB));
210 memcpy(dst->vertexShaderConstantI, src->vertexShaderConstantI, sizeof(dst->vertexShaderConstantI));
211 memcpy(dst->pixelShaderConstantB, src->pixelShaderConstantB, sizeof(dst->pixelShaderConstantB));
212 memcpy(dst->pixelShaderConstantI, src->pixelShaderConstantI, sizeof(dst->pixelShaderConstantI));
214 memcpy(dst->streamStride, src->streamStride, sizeof(dst->streamStride));
215 memcpy(dst->streamOffset, src->streamOffset, sizeof(dst->streamOffset));
216 memcpy(dst->streamSource, src->streamSource, sizeof(dst->streamSource));
217 memcpy(dst->streamFreq, src->streamFreq, sizeof(dst->streamFreq));
218 memcpy(dst->streamFlags, src->streamFlags, sizeof(dst->streamFlags));
219 memcpy(dst->transforms, src->transforms, sizeof(dst->transforms));
220 memcpy(dst->clipplane, src->clipplane, sizeof(dst->clipplane));
221 memcpy(dst->renderState, src->renderState, sizeof(dst->renderState));
222 memcpy(dst->textures, src->textures, sizeof(dst->textures));
223 memcpy(dst->textureState, src->textureState, sizeof(dst->textureState));
224 memcpy(dst->samplerState, src->samplerState, sizeof(dst->samplerState));
226 /* Dynamically sized arrays */
227 memcpy(dst->vertexShaderConstantF, src->vertexShaderConstantF, sizeof(float) * gl_info->max_vshader_constantsF * 4);
228 memcpy(dst->pixelShaderConstantF, src->pixelShaderConstantF, sizeof(float) * gl_info->max_pshader_constantsF * 4);
231 void stateblock_init_contained_states(IWineD3DStateBlockImpl *stateblock)
233 const struct wined3d_gl_info *gl_info = &stateblock->wineD3DDevice->adapter->gl_info;
236 for (i = 0; i <= WINEHIGHEST_RENDER_STATE >> 5; ++i)
238 DWORD map = stateblock->changed.renderState[i];
239 for (j = 0; map; map >>= 1, ++j)
241 if (!(map & 1)) continue;
243 stateblock->contained_render_states[stateblock->num_contained_render_states] = (i << 5) | j;
244 ++stateblock->num_contained_render_states;
248 for (i = 0; i <= HIGHEST_TRANSFORMSTATE >> 5; ++i)
250 DWORD map = stateblock->changed.transform[i];
251 for (j = 0; map; map >>= 1, ++j)
253 if (!(map & 1)) continue;
255 stateblock->contained_transform_states[stateblock->num_contained_transform_states] = (i << 5) | j;
256 ++stateblock->num_contained_transform_states;
260 for (i = 0; i < gl_info->max_vshader_constantsF; ++i)
262 if (stateblock->changed.vertexShaderConstantsF[i])
264 stateblock->contained_vs_consts_f[stateblock->num_contained_vs_consts_f] = i;
265 ++stateblock->num_contained_vs_consts_f;
269 for (i = 0; i < MAX_CONST_I; ++i)
271 if (stateblock->changed.vertexShaderConstantsI & (1 << i))
273 stateblock->contained_vs_consts_i[stateblock->num_contained_vs_consts_i] = i;
274 ++stateblock->num_contained_vs_consts_i;
278 for (i = 0; i < MAX_CONST_B; ++i)
280 if (stateblock->changed.vertexShaderConstantsB & (1 << i))
282 stateblock->contained_vs_consts_b[stateblock->num_contained_vs_consts_b] = i;
283 ++stateblock->num_contained_vs_consts_b;
287 for (i = 0; i < gl_info->max_pshader_constantsF; ++i)
289 if (stateblock->changed.pixelShaderConstantsF[i])
291 stateblock->contained_ps_consts_f[stateblock->num_contained_ps_consts_f] = i;
292 ++stateblock->num_contained_ps_consts_f;
296 for (i = 0; i < MAX_CONST_I; ++i)
298 if (stateblock->changed.pixelShaderConstantsI & (1 << i))
300 stateblock->contained_ps_consts_i[stateblock->num_contained_ps_consts_i] = i;
301 ++stateblock->num_contained_ps_consts_i;
305 for (i = 0; i < MAX_CONST_B; ++i)
307 if (stateblock->changed.pixelShaderConstantsB & (1 << i))
309 stateblock->contained_ps_consts_b[stateblock->num_contained_ps_consts_b] = i;
310 ++stateblock->num_contained_ps_consts_b;
314 for (i = 0; i < MAX_TEXTURES; ++i)
316 DWORD map = stateblock->changed.textureState[i];
318 for(j = 0; map; map >>= 1, ++j)
320 if (!(map & 1)) continue;
322 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
323 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
324 ++stateblock->num_contained_tss_states;
328 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
330 DWORD map = stateblock->changed.samplerState[i];
332 for (j = 0; map; map >>= 1, ++j)
334 if (!(map & 1)) continue;
336 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
337 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
338 ++stateblock->num_contained_sampler_states;
343 /**********************************************************
344 * IWineD3DStateBlockImpl IUnknown parts follows
345 **********************************************************/
346 static HRESULT WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
348 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
349 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
350 if (IsEqualGUID(riid, &IID_IUnknown)
351 || IsEqualGUID(riid, &IID_IWineD3DBase)
352 || IsEqualGUID(riid, &IID_IWineD3DStateBlock)){
353 IUnknown_AddRef(iface);
358 return E_NOINTERFACE;
361 static ULONG WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
362 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
363 ULONG refCount = InterlockedIncrement(&This->ref);
365 TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
369 static ULONG WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
370 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
371 ULONG refCount = InterlockedDecrement(&This->ref);
373 TRACE("(%p) : Releasing from %d\n", This, refCount + 1);
378 if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
380 for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++)
382 if (This->textures[counter]) IWineD3DBaseTexture_Release(This->textures[counter]);
385 for (counter = 0; counter < MAX_STREAMS; counter++) {
386 if(This->streamSource[counter]) {
387 if (IWineD3DBuffer_Release(This->streamSource[counter]))
389 TRACE("Vertex buffer still referenced by stateblock, applications has leaked Stream %u, buffer %p\n", counter, This->streamSource[counter]);
393 if(This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
394 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
395 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
397 for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
398 struct list *e1, *e2;
399 LIST_FOR_EACH_SAFE(e1, e2, &This->lightMap[counter]) {
400 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
401 list_remove(&light->entry);
402 HeapFree(GetProcessHeap(), 0, light);
406 HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
407 HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
408 HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
409 HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
410 HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
411 HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
412 HeapFree(GetProcessHeap(), 0, This);
417 /**********************************************************
418 * IWineD3DStateBlockImpl parts follows
419 **********************************************************/
420 static HRESULT WINAPI IWineD3DStateBlockImpl_GetParent(IWineD3DStateBlock *iface, IUnknown **pParent) {
421 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
422 IUnknown_AddRef(This->parent);
423 *pParent = This->parent;
427 static HRESULT WINAPI IWineD3DStateBlockImpl_GetDevice(IWineD3DStateBlock *iface, IWineD3DDevice** ppDevice){
429 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
431 *ppDevice = (IWineD3DDevice*)This->wineD3DDevice;
432 IWineD3DDevice_AddRef(*ppDevice);
437 static void record_lights(IWineD3DStateBlockImpl *This, const IWineD3DStateBlockImpl *targetStateBlock)
441 /* Lights... For a recorded state block, we just had a chain of actions to perform,
442 * so we need to walk that chain and update any actions which differ
444 for(i = 0; i < LIGHTMAP_SIZE; i++) {
446 LIST_FOR_EACH(e, &This->lightMap[i]) {
447 BOOL updated = FALSE;
448 PLIGHTINFOEL *src = LIST_ENTRY(e, PLIGHTINFOEL, entry), *realLight;
449 if (!src->changed && !src->enabledChanged) continue;
451 /* Look up the light in the destination */
452 LIST_FOR_EACH(f, &targetStateBlock->lightMap[i]) {
453 realLight = LIST_ENTRY(f, PLIGHTINFOEL, entry);
454 if(realLight->OriginalIndex == src->OriginalIndex) {
456 src->OriginalParms = realLight->OriginalParms;
458 if(src->enabledChanged) {
459 /* Need to double check because enabledChanged does not catch enabled -> disabled -> enabled
460 * or disabled -> enabled -> disabled changes
462 if(realLight->glIndex == -1 && src->glIndex != -1) {
464 This->activeLights[src->glIndex] = NULL;
465 } else if(realLight->glIndex != -1 && src->glIndex == -1){
467 This->activeLights[realLight->glIndex] = src;
469 src->glIndex = realLight->glIndex;
477 /* Found a light, all done, proceed with next hash entry */
479 } else if(src->changed) {
480 /* Otherwise assign defaul params */
481 src->OriginalParms = WINED3D_default_light;
483 /* Not enabled by default */
490 static HRESULT WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface){
492 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
493 IWineD3DStateBlockImpl *targetStateBlock = This->wineD3DDevice->stateBlock;
497 TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
499 /* If not recorded, then update can just recapture */
500 if (This->blockType == WINED3DSBT_RECORDED) {
502 /* Recorded => Only update 'changed' values */
503 if (This->changed.vertexShader && This->vertexShader != targetStateBlock->vertexShader) {
504 TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
506 if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
507 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
508 This->vertexShader = targetStateBlock->vertexShader;
511 /* Vertex Shader Float Constants */
512 for (j = 0; j < This->num_contained_vs_consts_f; ++j) {
513 i = This->contained_vs_consts_f[j];
514 TRACE("Setting %p from %p %u to {%f, %f, %f, %f}\n", This, targetStateBlock, i,
515 targetStateBlock->vertexShaderConstantF[i * 4],
516 targetStateBlock->vertexShaderConstantF[i * 4 + 1],
517 targetStateBlock->vertexShaderConstantF[i * 4 + 2],
518 targetStateBlock->vertexShaderConstantF[i * 4 + 3]);
520 This->vertexShaderConstantF[i * 4] = targetStateBlock->vertexShaderConstantF[i * 4];
521 This->vertexShaderConstantF[i * 4 + 1] = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
522 This->vertexShaderConstantF[i * 4 + 2] = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
523 This->vertexShaderConstantF[i * 4 + 3] = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
526 /* Vertex Shader Integer Constants */
527 for (j = 0; j < This->num_contained_vs_consts_i; ++j) {
528 i = This->contained_vs_consts_i[j];
529 TRACE("Setting %p from %p %u to {%d, %d, %d, %d}\n", This, targetStateBlock, i,
530 targetStateBlock->vertexShaderConstantI[i * 4],
531 targetStateBlock->vertexShaderConstantI[i * 4 + 1],
532 targetStateBlock->vertexShaderConstantI[i * 4 + 2],
533 targetStateBlock->vertexShaderConstantI[i * 4 + 3]);
535 This->vertexShaderConstantI[i * 4] = targetStateBlock->vertexShaderConstantI[i * 4];
536 This->vertexShaderConstantI[i * 4 + 1] = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
537 This->vertexShaderConstantI[i * 4 + 2] = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
538 This->vertexShaderConstantI[i * 4 + 3] = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
541 /* Vertex Shader Boolean Constants */
542 for (j = 0; j < This->num_contained_vs_consts_b; ++j) {
543 i = This->contained_vs_consts_b[j];
544 TRACE("Setting %p from %p %u to %s\n", This, targetStateBlock, i,
545 targetStateBlock->vertexShaderConstantB[i] ? "TRUE" : "FALSE");
547 This->vertexShaderConstantB[i] = targetStateBlock->vertexShaderConstantB[i];
550 /* Pixel Shader Float Constants */
551 for (j = 0; j < This->num_contained_ps_consts_f; ++j) {
552 i = This->contained_ps_consts_f[j];
553 TRACE("Setting %p from %p %u to {%f, %f, %f, %f}\n", This, targetStateBlock, i,
554 targetStateBlock->pixelShaderConstantF[i * 4],
555 targetStateBlock->pixelShaderConstantF[i * 4 + 1],
556 targetStateBlock->pixelShaderConstantF[i * 4 + 2],
557 targetStateBlock->pixelShaderConstantF[i * 4 + 3]);
559 This->pixelShaderConstantF[i * 4] = targetStateBlock->pixelShaderConstantF[i * 4];
560 This->pixelShaderConstantF[i * 4 + 1] = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
561 This->pixelShaderConstantF[i * 4 + 2] = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
562 This->pixelShaderConstantF[i * 4 + 3] = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
565 /* Pixel Shader Integer Constants */
566 for (j = 0; j < This->num_contained_ps_consts_i; ++j) {
567 i = This->contained_ps_consts_i[j];
568 TRACE("Setting %p from %p %u to {%d, %d, %d, %d}\n", This, targetStateBlock, i,
569 targetStateBlock->pixelShaderConstantI[i * 4],
570 targetStateBlock->pixelShaderConstantI[i * 4 + 1],
571 targetStateBlock->pixelShaderConstantI[i * 4 + 2],
572 targetStateBlock->pixelShaderConstantI[i * 4 + 3]);
574 This->pixelShaderConstantI[i * 4] = targetStateBlock->pixelShaderConstantI[i * 4];
575 This->pixelShaderConstantI[i * 4 + 1] = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
576 This->pixelShaderConstantI[i * 4 + 2] = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
577 This->pixelShaderConstantI[i * 4 + 3] = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
580 /* Pixel Shader Boolean Constants */
581 for (j = 0; j < This->num_contained_ps_consts_b; ++j) {
582 i = This->contained_ps_consts_b[j];
583 TRACE("Setting %p from %p %u to %s\n", This, targetStateBlock, i,
584 targetStateBlock->pixelShaderConstantB[i] ? "TRUE" : "FALSE");
586 This->pixelShaderConstantB[i] = targetStateBlock->pixelShaderConstantB[i];
589 /* Others + Render & Texture */
590 for (i = 0; i < This->num_contained_transform_states; i++) {
591 TRACE("Updating transform %u\n", i);
592 This->transforms[This->contained_transform_states[i]] =
593 targetStateBlock->transforms[This->contained_transform_states[i]];
596 if (This->changed.primitive_type) This->gl_primitive_type = targetStateBlock->gl_primitive_type;
598 if (This->changed.indices && ((This->pIndexData != targetStateBlock->pIndexData)
599 || (This->baseVertexIndex != targetStateBlock->baseVertexIndex)
600 || (This->IndexFmt != targetStateBlock->IndexFmt))) {
601 TRACE("Updating pIndexData to %p, baseVertexIndex to %d\n",
602 targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
603 if(targetStateBlock->pIndexData) IWineD3DBuffer_AddRef(targetStateBlock->pIndexData);
604 if(This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
605 This->pIndexData = targetStateBlock->pIndexData;
606 This->baseVertexIndex = targetStateBlock->baseVertexIndex;
607 This->IndexFmt = targetStateBlock->IndexFmt;
610 if(This->changed.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
611 TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
613 if (targetStateBlock->vertexDecl) IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
614 if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
615 This->vertexDecl = targetStateBlock->vertexDecl;
618 if (This->changed.material && memcmp(&targetStateBlock->material,
620 sizeof(WINED3DMATERIAL)) != 0) {
621 TRACE("Updating material\n");
622 This->material = targetStateBlock->material;
625 if (This->changed.viewport && memcmp(&targetStateBlock->viewport,
627 sizeof(WINED3DVIEWPORT)) != 0) {
628 TRACE("Updating viewport\n");
629 This->viewport = targetStateBlock->viewport;
632 if(This->changed.scissorRect && memcmp(&targetStateBlock->scissorRect,
634 sizeof(targetStateBlock->scissorRect)))
636 TRACE("Updating scissor rect\n");
637 targetStateBlock->scissorRect = This->scissorRect;
640 map = This->changed.streamSource;
641 for (i = 0; map; map >>= 1, ++i)
643 if (!(map & 1)) continue;
645 if (This->streamStride[i] != targetStateBlock->streamStride[i]
646 || This->streamSource[i] != targetStateBlock->streamSource[i])
648 TRACE("Updating stream source %u to %p, stride to %u\n",
649 i, targetStateBlock->streamSource[i], targetStateBlock->streamStride[i]);
650 This->streamStride[i] = targetStateBlock->streamStride[i];
651 if (targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
652 if (This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
653 This->streamSource[i] = targetStateBlock->streamSource[i];
657 map = This->changed.streamFreq;
658 for (i = 0; map; map >>= 1, ++i)
660 if (!(map & 1)) continue;
662 if (This->streamFreq[i] != targetStateBlock->streamFreq[i]
663 || This->streamFlags[i] != targetStateBlock->streamFlags[i])
665 TRACE("Updating stream frequency %u to %u flags to %#x\n",
666 i, targetStateBlock->streamFreq[i], targetStateBlock->streamFlags[i]);
667 This->streamFreq[i] = targetStateBlock->streamFreq[i];
668 This->streamFlags[i] = targetStateBlock->streamFlags[i];
672 map = This->changed.clipplane;
673 for (i = 0; map; map >>= 1, ++i)
675 if (!(map & 1)) continue;
677 if (memcmp(targetStateBlock->clipplane[i], This->clipplane[i], sizeof(*This->clipplane)))
679 TRACE("Updating clipplane %u\n", i);
680 memcpy(This->clipplane[i], targetStateBlock->clipplane[i], sizeof(*This->clipplane));
685 for (i = 0; i < This->num_contained_render_states; i++) {
686 TRACE("Updating renderState %u to %u\n", This->contained_render_states[i],
687 targetStateBlock->renderState[This->contained_render_states[i]]);
688 This->renderState[This->contained_render_states[i]] = targetStateBlock->renderState[This->contained_render_states[i]];
692 for (j = 0; j < This->num_contained_tss_states; j++) {
693 DWORD stage = This->contained_tss_states[j].stage;
694 DWORD state = This->contained_tss_states[j].state;
696 TRACE("Updating texturestage state %u, %u to %u (was %u)\n", stage, state,
697 targetStateBlock->textureState[stage][state], This->textureState[stage][state]);
698 This->textureState[stage][state] = targetStateBlock->textureState[stage][state];
702 map = This->changed.textures;
703 for (i = 0; map; map >>= 1, ++i)
705 if (!(map & 1)) continue;
707 TRACE("Updating texture %u to %p (was %p)\n", i, targetStateBlock->textures[i], This->textures[i]);
708 if (targetStateBlock->textures[i]) IWineD3DBaseTexture_AddRef(targetStateBlock->textures[i]);
709 if (This->textures[i]) IWineD3DBaseTexture_Release(This->textures[i]);
710 This->textures[i] = targetStateBlock->textures[i];
713 for (j = 0; j < This->num_contained_sampler_states; j++) {
714 DWORD stage = This->contained_sampler_states[j].stage;
715 DWORD state = This->contained_sampler_states[j].state;
716 TRACE("Updating sampler state %u, %u to %u (was %u)\n", stage, state,
717 targetStateBlock->samplerState[stage][state], This->samplerState[stage][state]);
718 This->samplerState[stage][state] = targetStateBlock->samplerState[stage][state];
720 if(This->changed.pixelShader && This->pixelShader != targetStateBlock->pixelShader) {
721 if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
722 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
723 This->pixelShader = targetStateBlock->pixelShader;
726 record_lights(This, targetStateBlock);
727 } else if(This->blockType == WINED3DSBT_ALL) {
728 memcpy(This->vertexShaderConstantB, targetStateBlock->vertexShaderConstantB, sizeof(This->vertexShaderConstantI));
729 memcpy(This->vertexShaderConstantI, targetStateBlock->vertexShaderConstantI, sizeof(This->vertexShaderConstantF));
730 memcpy(This->vertexShaderConstantF, targetStateBlock->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
731 This->gl_primitive_type = targetStateBlock->gl_primitive_type;
732 memcpy(This->streamStride, targetStateBlock->streamStride, sizeof(This->streamStride));
733 memcpy(This->streamOffset, targetStateBlock->streamOffset, sizeof(This->streamOffset));
734 memcpy(This->streamFreq, targetStateBlock->streamFreq, sizeof(This->streamFreq));
735 memcpy(This->streamFlags, targetStateBlock->streamFlags, sizeof(This->streamFlags));
736 This->baseVertexIndex = targetStateBlock->baseVertexIndex;
737 memcpy(This->transforms, targetStateBlock->transforms, sizeof(This->transforms));
738 record_lights(This, targetStateBlock);
739 memcpy(This->clipplane, targetStateBlock->clipplane, sizeof(This->clipplane));
740 This->clip_status = targetStateBlock->clip_status;
741 This->viewport = targetStateBlock->viewport;
742 This->material = targetStateBlock->material;
743 memcpy(This->pixelShaderConstantB, targetStateBlock->pixelShaderConstantB, sizeof(This->pixelShaderConstantI));
744 memcpy(This->pixelShaderConstantI, targetStateBlock->pixelShaderConstantI, sizeof(This->pixelShaderConstantF));
745 memcpy(This->pixelShaderConstantF, targetStateBlock->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
746 memcpy(This->renderState, targetStateBlock->renderState, sizeof(This->renderState));
747 memcpy(This->textureState, targetStateBlock->textureState, sizeof(This->textureState));
748 memcpy(This->samplerState, targetStateBlock->samplerState, sizeof(This->samplerState));
749 This->scissorRect = targetStateBlock->scissorRect;
751 if (This->vertexDecl != targetStateBlock->vertexDecl)
753 if (targetStateBlock->vertexDecl) IWineD3DVertexDeclaration_AddRef(targetStateBlock->vertexDecl);
754 if (This->vertexDecl) IWineD3DVertexDeclaration_Release(This->vertexDecl);
755 This->vertexDecl = targetStateBlock->vertexDecl;
758 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
760 if (targetStateBlock->textures[i] != This->textures[i])
762 if (targetStateBlock->textures[i]) IWineD3DBaseTexture_AddRef(targetStateBlock->textures[i]);
763 if (This->textures[i]) IWineD3DBaseTexture_Release(This->textures[i]);
764 This->textures[i] = targetStateBlock->textures[i];
768 if(targetStateBlock->pIndexData != This->pIndexData ||
769 targetStateBlock->IndexFmt != This->IndexFmt) {
770 if (targetStateBlock->pIndexData) IWineD3DBuffer_AddRef(targetStateBlock->pIndexData);
771 if (This->pIndexData) IWineD3DBuffer_Release(This->pIndexData);
772 This->pIndexData = targetStateBlock->pIndexData;
773 This->IndexFmt = targetStateBlock->IndexFmt;
775 for(i = 0; i < MAX_STREAMS; i++) {
776 if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
777 if(targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
778 if(This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
779 This->streamSource[i] = targetStateBlock->streamSource[i];
782 if(This->vertexShader != targetStateBlock->vertexShader) {
783 if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
784 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
785 This->vertexShader = targetStateBlock->vertexShader;
787 if(This->pixelShader != targetStateBlock->pixelShader) {
788 if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
789 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
790 This->pixelShader = targetStateBlock->pixelShader;
792 } else if(This->blockType == WINED3DSBT_VERTEXSTATE) {
793 memcpy(This->vertexShaderConstantB, targetStateBlock->vertexShaderConstantB, sizeof(This->vertexShaderConstantI));
794 memcpy(This->vertexShaderConstantI, targetStateBlock->vertexShaderConstantI, sizeof(This->vertexShaderConstantF));
795 memcpy(This->vertexShaderConstantF, targetStateBlock->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
796 record_lights(This, targetStateBlock);
797 for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
798 This->renderState[SavedVertexStates_R[i]] = targetStateBlock->renderState[SavedVertexStates_R[i]];
800 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
801 for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
802 This->samplerState[j][SavedVertexStates_S[i]] = targetStateBlock->samplerState[j][SavedVertexStates_S[i]];
805 for (j = 0; j < MAX_TEXTURES; j++) {
806 for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
807 This->textureState[j][SavedVertexStates_R[i]] = targetStateBlock->textureState[j][SavedVertexStates_R[i]];
810 for(i = 0; i < MAX_STREAMS; i++) {
811 if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
812 if (targetStateBlock->streamSource[i]) IWineD3DBuffer_AddRef(targetStateBlock->streamSource[i]);
813 if (This->streamSource[i]) IWineD3DBuffer_Release(This->streamSource[i]);
814 This->streamSource[i] = targetStateBlock->streamSource[i];
817 if(This->vertexShader != targetStateBlock->vertexShader) {
818 if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
819 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
820 This->vertexShader = targetStateBlock->vertexShader;
822 } else if(This->blockType == WINED3DSBT_PIXELSTATE) {
823 memcpy(This->pixelShaderConstantB, targetStateBlock->pixelShaderConstantB, sizeof(This->pixelShaderConstantI));
824 memcpy(This->pixelShaderConstantI, targetStateBlock->pixelShaderConstantI, sizeof(This->pixelShaderConstantF));
825 memcpy(This->pixelShaderConstantF, targetStateBlock->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
826 for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
827 This->renderState[SavedPixelStates_R[i]] = targetStateBlock->renderState[SavedPixelStates_R[i]];
829 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
830 for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
831 This->samplerState[j][SavedPixelStates_S[i]] = targetStateBlock->samplerState[j][SavedPixelStates_S[i]];
834 for (j = 0; j < MAX_TEXTURES; j++) {
835 for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
836 This->textureState[j][SavedPixelStates_R[i]] = targetStateBlock->textureState[j][SavedPixelStates_R[i]];
839 if(This->pixelShader != targetStateBlock->pixelShader) {
840 if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
841 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
842 This->pixelShader = targetStateBlock->pixelShader;
846 TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
851 static void apply_lights(IWineD3DDevice *pDevice, const IWineD3DStateBlockImpl *This)
854 for(i = 0; i < LIGHTMAP_SIZE; i++) {
857 LIST_FOR_EACH(e, &This->lightMap[i]) {
858 const PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
861 IWineD3DDevice_SetLight(pDevice, light->OriginalIndex, &light->OriginalParms);
863 if(light->enabledChanged) {
864 IWineD3DDevice_SetLightEnable(pDevice, light->OriginalIndex, light->glIndex != -1);
870 static HRESULT WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
871 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
872 IWineD3DDevice* pDevice = (IWineD3DDevice*)This->wineD3DDevice;
874 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
875 should really perform a delta so that only the changes get updated*/
881 TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
883 TRACE("Blocktype: %d\n", This->blockType);
885 if(This->blockType == WINED3DSBT_RECORDED) {
886 if (This->changed.vertexShader) {
887 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
889 /* Vertex Shader Constants */
890 for (i = 0; i < This->num_contained_vs_consts_f; i++) {
891 IWineD3DDevice_SetVertexShaderConstantF(pDevice, This->contained_vs_consts_f[i],
892 This->vertexShaderConstantF + This->contained_vs_consts_f[i] * 4, 1);
894 for (i = 0; i < This->num_contained_vs_consts_i; i++) {
895 IWineD3DDevice_SetVertexShaderConstantI(pDevice, This->contained_vs_consts_i[i],
896 This->vertexShaderConstantI + This->contained_vs_consts_i[i] * 4, 1);
898 for (i = 0; i < This->num_contained_vs_consts_b; i++) {
899 IWineD3DDevice_SetVertexShaderConstantB(pDevice, This->contained_vs_consts_b[i],
900 This->vertexShaderConstantB + This->contained_vs_consts_b[i], 1);
903 apply_lights(pDevice, This);
905 if (This->changed.pixelShader) {
906 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
908 /* Pixel Shader Constants */
909 for (i = 0; i < This->num_contained_ps_consts_f; i++) {
910 IWineD3DDevice_SetPixelShaderConstantF(pDevice, This->contained_ps_consts_f[i],
911 This->pixelShaderConstantF + This->contained_ps_consts_f[i] * 4, 1);
913 for (i = 0; i < This->num_contained_ps_consts_i; i++) {
914 IWineD3DDevice_SetPixelShaderConstantI(pDevice, This->contained_ps_consts_i[i],
915 This->pixelShaderConstantI + This->contained_ps_consts_i[i] * 4, 1);
917 for (i = 0; i < This->num_contained_ps_consts_b; i++) {
918 IWineD3DDevice_SetPixelShaderConstantB(pDevice, This->contained_ps_consts_b[i],
919 This->pixelShaderConstantB + This->contained_ps_consts_b[i], 1);
923 for (i = 0; i <= This->num_contained_render_states; i++) {
924 IWineD3DDevice_SetRenderState(pDevice, This->contained_render_states[i],
925 This->renderState[This->contained_render_states[i]]);
928 for (i = 0; i < This->num_contained_tss_states; i++) {
929 DWORD stage = This->contained_tss_states[i].stage;
930 DWORD state = This->contained_tss_states[i].state;
931 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[stage][state] = This->textureState[stage][state];
932 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[stage] |= 1 << state;
933 /* TODO: Record a display list to apply all gl states. For now apply by brute force */
934 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(stage, state));
937 for (i = 0; i < This->num_contained_sampler_states; i++) {
938 DWORD stage = This->contained_sampler_states[i].stage;
939 DWORD state = This->contained_sampler_states[i].state;
940 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[stage][state] = This->samplerState[stage][state];
941 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[stage] |= 1 << state;
942 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_SAMPLER(stage));
945 for (i = 0; i < This->num_contained_transform_states; i++) {
946 IWineD3DDevice_SetTransform(pDevice, This->contained_transform_states[i],
947 &This->transforms[This->contained_transform_states[i]]);
950 if (This->changed.primitive_type)
952 This->wineD3DDevice->updateStateBlock->changed.primitive_type = TRUE;
953 This->wineD3DDevice->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
956 if (This->changed.indices)
958 IWineD3DDevice_SetIndexBuffer(pDevice, This->pIndexData, This->IndexFmt);
959 IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
962 if (This->changed.vertexDecl) {
963 IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
966 if (This->changed.material ) {
967 IWineD3DDevice_SetMaterial(pDevice, &This->material);
970 if (This->changed.viewport) {
971 IWineD3DDevice_SetViewport(pDevice, &This->viewport);
974 if (This->changed.scissorRect) {
975 IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
978 /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
979 map = This->changed.streamSource;
980 for (i = 0; map; map >>= 1, ++i)
982 if (map & 1) IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
985 map = This->changed.streamFreq;
986 for (i = 0; map; map >>= 1, ++i)
988 if (map & 1) IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
991 map = This->changed.textures;
992 for (i = 0; map; map >>= 1, ++i)
994 if (!(map & 1)) continue;
996 if (i < MAX_FRAGMENT_SAMPLERS) IWineD3DDevice_SetTexture(pDevice, i, This->textures[i]);
997 else IWineD3DDevice_SetTexture(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS,
1001 map = This->changed.clipplane;
1002 for (i = 0; map; map >>= 1, ++i)
1006 if (!(map & 1)) continue;
1008 clip[0] = This->clipplane[i][0];
1009 clip[1] = This->clipplane[i][1];
1010 clip[2] = This->clipplane[i][2];
1011 clip[3] = This->clipplane[i][3];
1012 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
1014 } else if(This->blockType == WINED3DSBT_VERTEXSTATE) {
1015 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
1016 for (i = 0; i < GL_LIMITS(vshader_constantsF); i++) {
1017 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i,
1018 This->vertexShaderConstantF + i * 4, 1);
1020 for (i = 0; i < MAX_CONST_I; i++) {
1021 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i,
1022 This->vertexShaderConstantI + i * 4, 1);
1024 for (i = 0; i < MAX_CONST_B; i++) {
1025 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i,
1026 This->vertexShaderConstantB + i, 1);
1029 apply_lights(pDevice, This);
1031 for(i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
1032 IWineD3DDevice_SetRenderState(pDevice, SavedVertexStates_R[i], This->renderState[SavedVertexStates_R[i]]);
1034 for(j = 0; j < MAX_TEXTURES; j++) {
1035 for(i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
1036 IWineD3DDevice_SetTextureStageState(pDevice, j, SavedVertexStates_T[i],
1037 This->textureState[j][SavedVertexStates_T[i]]);
1041 for(j = 0; j < MAX_FRAGMENT_SAMPLERS; j++) {
1042 for(i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
1043 IWineD3DDevice_SetSamplerState(pDevice, j, SavedVertexStates_S[i],
1044 This->samplerState[j][SavedVertexStates_S[i]]);
1047 for(j = MAX_FRAGMENT_SAMPLERS; j < MAX_COMBINED_SAMPLERS; j++) {
1048 for(i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
1049 IWineD3DDevice_SetSamplerState(pDevice,
1050 WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS,
1051 SavedVertexStates_S[i],
1052 This->samplerState[j][SavedVertexStates_S[i]]);
1055 } else if(This->blockType == WINED3DSBT_PIXELSTATE) {
1056 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
1057 for (i = 0; i < GL_LIMITS(pshader_constantsF); i++) {
1058 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i,
1059 This->pixelShaderConstantF + i * 4, 1);
1061 for (i = 0; i < MAX_CONST_I; i++) {
1062 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i,
1063 This->pixelShaderConstantI + i * 4, 1);
1065 for (i = 0; i < MAX_CONST_B; i++) {
1066 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i,
1067 This->pixelShaderConstantB + i, 1);
1070 for(i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
1071 IWineD3DDevice_SetRenderState(pDevice, SavedPixelStates_R[i], This->renderState[SavedPixelStates_R[i]]);
1073 for(j = 0; j < MAX_TEXTURES; j++) {
1074 for(i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
1075 IWineD3DDevice_SetTextureStageState(pDevice, j, SavedPixelStates_T[i],
1076 This->textureState[j][SavedPixelStates_T[i]]);
1080 for(j = 0; j < MAX_FRAGMENT_SAMPLERS; j++) {
1081 for(i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
1082 IWineD3DDevice_SetSamplerState(pDevice, j, SavedPixelStates_S[i],
1083 This->samplerState[j][SavedPixelStates_S[i]]);
1086 for(j = MAX_FRAGMENT_SAMPLERS; j < MAX_COMBINED_SAMPLERS; j++) {
1087 for(i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
1088 IWineD3DDevice_SetSamplerState(pDevice,
1089 WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS,
1090 SavedPixelStates_S[i],
1091 This->samplerState[j][SavedPixelStates_S[i]]);
1094 } else if(This->blockType == WINED3DSBT_ALL) {
1095 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
1096 for (i = 0; i < GL_LIMITS(vshader_constantsF); i++) {
1097 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i,
1098 This->vertexShaderConstantF + i * 4, 1);
1100 for (i = 0; i < MAX_CONST_I; i++) {
1101 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i,
1102 This->vertexShaderConstantI + i * 4, 1);
1104 for (i = 0; i < MAX_CONST_B; i++) {
1105 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i,
1106 This->vertexShaderConstantB + i, 1);
1109 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
1110 for (i = 0; i < GL_LIMITS(pshader_constantsF); i++) {
1111 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i,
1112 This->pixelShaderConstantF + i * 4, 1);
1114 for (i = 0; i < MAX_CONST_I; i++) {
1115 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i,
1116 This->pixelShaderConstantI + i * 4, 1);
1118 for (i = 0; i < MAX_CONST_B; i++) {
1119 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i,
1120 This->pixelShaderConstantB + i, 1);
1123 apply_lights(pDevice, This);
1125 for(i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
1126 IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
1128 for(j = 0; j < MAX_TEXTURES; j++) {
1129 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
1131 IWineD3DDevice_SetTextureStageState(pDevice, j, i, This->textureState[j][i]);
1135 /* Skip unused values between TEXTURE8 and WORLD0 ? */
1136 for(i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
1137 IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
1139 This->wineD3DDevice->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
1140 IWineD3DDevice_SetIndexBuffer(pDevice, This->pIndexData, This->IndexFmt);
1141 IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
1142 IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
1143 IWineD3DDevice_SetMaterial(pDevice, &This->material);
1144 IWineD3DDevice_SetViewport(pDevice, &This->viewport);
1145 IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
1147 /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
1148 for (i=0; i<MAX_STREAMS; i++) {
1149 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
1150 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
1152 for (j = 0 ; j < MAX_COMBINED_SAMPLERS; j++){
1153 UINT sampler = j < MAX_FRAGMENT_SAMPLERS ? j : WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS;
1155 IWineD3DDevice_SetTexture(pDevice, sampler, This->textures[j]);
1156 for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; ++i)
1158 IWineD3DDevice_SetSamplerState(pDevice, sampler, i, This->samplerState[j][i]);
1161 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
1164 clip[0] = This->clipplane[i][0];
1165 clip[1] = This->clipplane[i][1];
1166 clip[2] = This->clipplane[i][2];
1167 clip[3] = This->clipplane[i][3];
1168 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
1172 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
1173 for(j = 0; j < MAX_TEXTURES - 1; j++) {
1174 if(((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE) {
1175 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = j;
1179 TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
1184 static HRESULT WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
1185 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
1186 IWineD3DDevice *device = (IWineD3DDevice *)This->wineD3DDevice;
1187 IWineD3DDeviceImpl *ThisDevice = (IWineD3DDeviceImpl *)device;
1189 WINED3DLINEPATTERN lp;
1197 IWineD3DSwapChain *swapchain;
1198 IWineD3DSurface *backbuffer;
1201 /* Note this may have a large overhead but it should only be executed
1202 once, in order to initialize the complete state of the device and
1203 all opengl equivalents */
1204 TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
1205 /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
1206 This->blockType = WINED3DSBT_INIT;
1208 /* Set some of the defaults for lights, transforms etc */
1209 memcpy(&This->transforms[WINED3DTS_PROJECTION], identity, sizeof(identity));
1210 memcpy(&This->transforms[WINED3DTS_VIEW], identity, sizeof(identity));
1211 for (i = 0; i < 256; ++i) {
1212 memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], identity, sizeof(identity));
1215 TRACE("Render states\n");
1216 /* Render states: */
1217 if (ThisDevice->auto_depth_stencil_buffer != NULL) {
1218 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_TRUE);
1220 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_FALSE);
1222 IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE, WINED3DFILL_SOLID);
1223 IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE, WINED3DSHADE_GOURAUD);
1224 lp.lp.wRepeatFactor = 0;
1225 lp.lp.wLinePattern = 0;
1226 IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN, lp.d);
1227 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE, TRUE);
1228 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE, FALSE);
1229 IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL, TRUE);
1230 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND, WINED3DBLEND_ONE);
1231 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND, WINED3DBLEND_ZERO);
1232 IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE, WINED3DCULL_CCW);
1233 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC, WINED3DCMP_LESSEQUAL);
1234 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC, WINED3DCMP_ALWAYS);
1235 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF, 0);
1236 IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE, FALSE);
1237 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
1238 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE, FALSE);
1239 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE, FALSE);
1240 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE, 0);
1241 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR, 0);
1242 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE, WINED3DFOG_NONE);
1244 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART, tmpfloat.d);
1246 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND, tmpfloat.d);
1248 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY, tmpfloat.d);
1249 IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS, FALSE);
1250 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS, 0);
1251 IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE, FALSE);
1252 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE, FALSE);
1253 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL, WINED3DSTENCILOP_KEEP);
1254 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
1255 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS, WINED3DSTENCILOP_KEEP);
1256 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
1257 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
1258 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC, WINED3DCMP_ALWAYS);
1259 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
1260 IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
1261 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
1262 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
1263 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
1264 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
1265 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
1266 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
1267 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
1268 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
1269 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING, TRUE);
1270 IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING, TRUE);
1271 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT, 0);
1272 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE, WINED3DFOG_NONE);
1273 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX, TRUE);
1274 IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER, TRUE);
1275 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS, FALSE);
1276 IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE, WINED3DMCS_COLOR1);
1277 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE, WINED3DMCS_COLOR2);
1278 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE, WINED3DMCS_MATERIAL);
1279 IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE, WINED3DMCS_MATERIAL);
1280 IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND, WINED3DVBF_DISABLE);
1281 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE, 0);
1282 IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
1284 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE, tmpfloat.d);
1285 tmpfloat.f = ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion < 9 ? 0.0f : 1.0f;
1286 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN, tmpfloat.d);
1287 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE, FALSE);
1288 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE, FALSE);
1290 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A, tmpfloat.d);
1292 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B, tmpfloat.d);
1294 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C, tmpfloat.d);
1295 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS, TRUE);
1296 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1297 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE, WINED3DPATCHEDGE_DISCRETE);
1299 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS, tmpfloat.d);
1300 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN, 0xbaadcafe);
1301 tmpfloat.f = GL_LIMITS(pointsize);
1302 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX, tmpfloat.d);
1303 IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
1304 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE, 0x0000000F);
1306 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR, tmpfloat.d);
1307 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP, WINED3DBLENDOP_ADD);
1308 IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE, WINED3DDEGREE_CUBIC);
1309 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE, WINED3DDEGREE_LINEAR);
1310 /* states new in d3d9 */
1311 IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE, FALSE);
1312 IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS, 0);
1314 IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL, tmpfloat.d);
1315 IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL, tmpfloat.d);
1316 IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE, FALSE);
1318 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X, tmpfloat.d);
1319 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y, tmpfloat.d);
1321 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z, tmpfloat.d);
1323 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W, tmpfloat.d);
1324 IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
1325 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE, FALSE);
1326 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL, WINED3DSTENCILOP_KEEP);
1327 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
1328 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS, WINED3DSTENCILOP_KEEP);
1329 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC, WINED3DCMP_ALWAYS);
1330 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1, 0x0000000F);
1331 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2, 0x0000000F);
1332 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3, 0x0000000F);
1333 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR, 0xFFFFFFFF);
1334 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE, 0);
1335 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS, 0);
1336 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8, 0);
1337 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9, 0);
1338 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
1339 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
1340 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
1341 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
1342 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
1343 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
1344 IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1345 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA, WINED3DBLEND_ONE);
1346 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA, WINED3DBLEND_ZERO);
1347 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA, WINED3DBLENDOP_ADD);
1349 /* clipping status */
1350 This->clip_status.ClipUnion = 0;
1351 This->clip_status.ClipIntersection = 0xFFFFFFFF;
1353 /* Texture Stage States - Put directly into state block, we will call function below */
1354 for (i = 0; i < MAX_TEXTURES; i++) {
1355 TRACE("Setting up default texture states for texture Stage %d\n", i);
1356 memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], identity, sizeof(identity));
1357 This->textureState[i][WINED3DTSS_COLOROP ] = (i==0)? WINED3DTOP_MODULATE : WINED3DTOP_DISABLE;
1358 This->textureState[i][WINED3DTSS_COLORARG1 ] = WINED3DTA_TEXTURE;
1359 This->textureState[i][WINED3DTSS_COLORARG2 ] = WINED3DTA_CURRENT;
1360 This->textureState[i][WINED3DTSS_ALPHAOP ] = (i==0)? WINED3DTOP_SELECTARG1 : WINED3DTOP_DISABLE;
1361 This->textureState[i][WINED3DTSS_ALPHAARG1 ] = WINED3DTA_TEXTURE;
1362 This->textureState[i][WINED3DTSS_ALPHAARG2 ] = WINED3DTA_CURRENT;
1363 This->textureState[i][WINED3DTSS_BUMPENVMAT00 ] = 0;
1364 This->textureState[i][WINED3DTSS_BUMPENVMAT01 ] = 0;
1365 This->textureState[i][WINED3DTSS_BUMPENVMAT10 ] = 0;
1366 This->textureState[i][WINED3DTSS_BUMPENVMAT11 ] = 0;
1367 This->textureState[i][WINED3DTSS_TEXCOORDINDEX ] = i;
1368 This->textureState[i][WINED3DTSS_BUMPENVLSCALE ] = 0;
1369 This->textureState[i][WINED3DTSS_BUMPENVLOFFSET ] = 0;
1370 This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1371 This->textureState[i][WINED3DTSS_COLORARG0 ] = WINED3DTA_CURRENT;
1372 This->textureState[i][WINED3DTSS_ALPHAARG0 ] = WINED3DTA_CURRENT;
1373 This->textureState[i][WINED3DTSS_RESULTARG ] = WINED3DTA_CURRENT;
1375 This->lowest_disabled_stage = 1;
1378 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; i++) {
1379 TRACE("Setting up default samplers states for sampler %d\n", i);
1380 This->samplerState[i][WINED3DSAMP_ADDRESSU ] = WINED3DTADDRESS_WRAP;
1381 This->samplerState[i][WINED3DSAMP_ADDRESSV ] = WINED3DTADDRESS_WRAP;
1382 This->samplerState[i][WINED3DSAMP_ADDRESSW ] = WINED3DTADDRESS_WRAP;
1383 This->samplerState[i][WINED3DSAMP_BORDERCOLOR ] = 0x00;
1384 This->samplerState[i][WINED3DSAMP_MAGFILTER ] = WINED3DTEXF_POINT;
1385 This->samplerState[i][WINED3DSAMP_MINFILTER ] = WINED3DTEXF_POINT;
1386 This->samplerState[i][WINED3DSAMP_MIPFILTER ] = WINED3DTEXF_NONE;
1387 This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS ] = 0;
1388 This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL ] = 0;
1389 This->samplerState[i][WINED3DSAMP_MAXANISOTROPY ] = 1;
1390 This->samplerState[i][WINED3DSAMP_SRGBTEXTURE ] = 0;
1391 This->samplerState[i][WINED3DSAMP_ELEMENTINDEX ] = 0; /* TODO: Indicates which element of a multielement texture to use */
1392 This->samplerState[i][WINED3DSAMP_DMAPOFFSET ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1395 for(i = 0; i < GL_LIMITS(textures); i++) {
1396 /* Note: This avoids calling SetTexture, so pretend it has been called */
1397 This->changed.textures |= 1 << i;
1398 This->textures[i] = NULL;
1401 /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1402 hr = IWineD3DDevice_GetSwapChain(device, 0, &swapchain);
1403 if( hr == WINED3D_OK && swapchain != NULL) {
1406 hr = IWineD3DSwapChain_GetBackBuffer(swapchain, 0, WINED3DBACKBUFFER_TYPE_MONO, &backbuffer);
1407 if (SUCCEEDED(hr) && backbuffer)
1409 WINED3DSURFACE_DESC desc;
1412 IWineD3DSurface_GetDesc(backbuffer, &desc);
1413 IWineD3DSurface_Release(backbuffer);
1415 /* Set the default scissor rect values */
1416 scissorrect.left = 0;
1417 scissorrect.right = desc.width;
1418 scissorrect.top = 0;
1419 scissorrect.bottom = desc.height;
1420 hr = IWineD3DDevice_SetScissorRect(device, &scissorrect);
1421 if (FAILED(hr)) ERR("This should never happen, expect rendering issues!\n");
1424 /* Set the default viewport */
1427 vp.Width = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferWidth;
1428 vp.Height = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferHeight;
1431 IWineD3DDevice_SetViewport(device, &vp);
1433 IWineD3DSwapChain_Release(swapchain);
1436 TRACE("-----------------------> Device defaults now set up...\n");
1440 /**********************************************************
1441 * IWineD3DStateBlock VTbl follows
1442 **********************************************************/
1444 static const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1447 IWineD3DStateBlockImpl_QueryInterface,
1448 IWineD3DStateBlockImpl_AddRef,
1449 IWineD3DStateBlockImpl_Release,
1450 /* IWineD3DStateBlock */
1451 IWineD3DStateBlockImpl_GetParent,
1452 IWineD3DStateBlockImpl_GetDevice,
1453 IWineD3DStateBlockImpl_Capture,
1454 IWineD3DStateBlockImpl_Apply,
1455 IWineD3DStateBlockImpl_InitStartupStateBlock
1458 HRESULT stateblock_init(IWineD3DStateBlockImpl *stateblock, IWineD3DDeviceImpl *device,
1459 WINED3DSTATEBLOCKTYPE type, IUnknown *parent)
1461 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1465 stateblock->lpVtbl = &IWineD3DStateBlock_Vtbl;
1466 stateblock->ref = 1;
1467 stateblock->parent = parent;
1468 stateblock->wineD3DDevice = device;
1469 stateblock->blockType = type;
1471 for (i = 0; i < LIGHTMAP_SIZE; i++)
1473 list_init(&stateblock->lightMap[i]);
1476 hr = stateblock_allocate_shader_constants(stateblock);
1477 if (FAILED(hr)) return hr;
1479 /* The WINED3DSBT_INIT stateblock type is used during initialization to
1480 * produce a placeholder stateblock so other functions called can update a
1482 if (type == WINED3DSBT_INIT || type == WINED3DSBT_RECORDED) return WINED3D_OK;
1484 /* Otherwise, might as well set the whole state block to the appropriate values */
1485 if (device->stateBlock)
1488 stateblock_savedstates_copy(&stateblock->changed, &device->stateBlock->changed, gl_info);
1491 stateblock_copy_values(stateblock, device->stateBlock, gl_info);
1495 memset(stateblock->streamFreq, 1, sizeof(stateblock->streamFreq));
1498 TRACE("Updating changed flags appropriate for type %#x.\n", type);
1500 if (type == WINED3DSBT_ALL)
1502 TRACE("ALL => Pretend everything has changed.\n");
1503 stateblock_savedstates_set(&stateblock->changed, TRUE, gl_info);
1505 /* Lights are not part of the changed / set structure. */
1506 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1509 LIST_FOR_EACH(e, &stateblock->lightMap[i])
1511 PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
1512 light->changed = TRUE;
1513 light->enabledChanged = TRUE;
1517 for (i = 1; i <= WINEHIGHEST_RENDER_STATE; ++i)
1519 stateblock->contained_render_states[i - 1] = i;
1521 stateblock->num_contained_render_states = WINEHIGHEST_RENDER_STATE;
1523 /* TODO: Filter unused transforms between TEXTURE8 and WORLD0? */
1524 for (i = 1; i <= HIGHEST_TRANSFORMSTATE; ++i)
1526 stateblock->contained_transform_states[i - 1] = i;
1528 stateblock->num_contained_transform_states = HIGHEST_TRANSFORMSTATE;
1530 for (i = 0; i < gl_info->max_vshader_constantsF; ++i)
1532 stateblock->contained_vs_consts_f[i] = i;
1534 stateblock->num_contained_vs_consts_f = gl_info->max_vshader_constantsF;
1536 for (i = 0; i < MAX_CONST_I; ++i)
1538 stateblock->contained_vs_consts_i[i] = i;
1540 stateblock->num_contained_vs_consts_i = MAX_CONST_I;
1542 for (i = 0; i < MAX_CONST_B; ++i)
1544 stateblock->contained_vs_consts_b[i] = i;
1546 stateblock->num_contained_vs_consts_b = MAX_CONST_B;
1548 for (i = 0; i < gl_info->max_pshader_constantsF; ++i)
1550 stateblock->contained_ps_consts_f[i] = i;
1552 stateblock->num_contained_ps_consts_f = gl_info->max_pshader_constantsF;
1554 for (i = 0; i < MAX_CONST_I; ++i)
1556 stateblock->contained_ps_consts_i[i] = i;
1558 stateblock->num_contained_ps_consts_i = MAX_CONST_I;
1560 for (i = 0; i < MAX_CONST_B; ++i)
1562 stateblock->contained_ps_consts_b[i] = i;
1564 stateblock->num_contained_ps_consts_b = MAX_CONST_B;
1566 for (i = 0; i < MAX_TEXTURES; ++i)
1568 for (j = 0; j <= WINED3D_HIGHEST_TEXTURE_STATE; ++j)
1570 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
1571 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = j;
1572 ++stateblock->num_contained_tss_states;
1576 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i)
1578 for (j = 1; j <= WINED3D_HIGHEST_SAMPLER_STATE; ++j)
1580 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
1581 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = j;
1582 ++stateblock->num_contained_sampler_states;
1586 for (i = 0; i < MAX_STREAMS; ++i)
1588 if (stateblock->streamSource[i]) IWineD3DBuffer_AddRef(stateblock->streamSource[i]);
1591 if (stateblock->pIndexData) IWineD3DBuffer_AddRef(stateblock->pIndexData);
1592 if (stateblock->vertexShader) IWineD3DVertexShader_AddRef(stateblock->vertexShader);
1593 if (stateblock->pixelShader) IWineD3DPixelShader_AddRef(stateblock->pixelShader);
1595 else if (type == WINED3DSBT_PIXELSTATE)
1597 TRACE("PIXELSTATE => Pretend all pixel states have changed.\n");
1598 stateblock_savedstates_set(&stateblock->changed, FALSE, gl_info);
1600 /* Pixel Shader Constants. */
1601 for (i = 0; i < gl_info->max_pshader_constantsF; ++i)
1603 stateblock->contained_ps_consts_f[i] = i;
1604 stateblock->changed.pixelShaderConstantsF[i] = TRUE;
1606 stateblock->num_contained_ps_consts_f = gl_info->max_pshader_constantsF;
1608 for (i = 0; i < MAX_CONST_B; ++i)
1610 stateblock->contained_ps_consts_b[i] = i;
1611 stateblock->changed.pixelShaderConstantsB |= (1 << i);
1613 stateblock->num_contained_ps_consts_b = MAX_CONST_B;
1615 for (i = 0; i < MAX_CONST_I; ++i)
1617 stateblock->contained_ps_consts_i[i] = i;
1618 stateblock->changed.pixelShaderConstantsI |= (1 << i);
1620 stateblock->num_contained_ps_consts_i = MAX_CONST_I;
1622 for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++)
1624 DWORD rs = SavedPixelStates_R[i];
1625 stateblock->changed.renderState[rs >> 5] |= 1 << (rs & 0x1f);
1626 stateblock->contained_render_states[i] = rs;
1628 stateblock->num_contained_render_states = NUM_SAVEDPIXELSTATES_R;
1630 for (i = 0; i < MAX_TEXTURES; ++i)
1632 for (j = 0; j < NUM_SAVEDPIXELSTATES_T; ++j)
1634 DWORD state = SavedPixelStates_T[j];
1635 stateblock->changed.textureState[i] |= 1 << state;
1636 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
1637 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = state;
1638 ++stateblock->num_contained_tss_states;
1642 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; ++i)
1644 for (j = 0; j < NUM_SAVEDPIXELSTATES_S; ++j)
1646 DWORD state = SavedPixelStates_S[j];
1647 stateblock->changed.samplerState[i] |= 1 << state;
1648 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
1649 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = state;
1650 stateblock->num_contained_sampler_states++;
1654 stateblock->changed.pixelShader = TRUE;
1655 if (stateblock->pixelShader) IWineD3DPixelShader_AddRef(stateblock->pixelShader);
1657 /* Pixel state blocks do not contain vertex buffers. Set them to NULL
1658 * to avoid wrong refcounting on them. This makes releasing the buffer
1660 for (i = 0; i < MAX_STREAMS; ++i)
1662 stateblock->streamSource[i] = NULL;
1664 stateblock->pIndexData = NULL;
1665 stateblock->vertexShader = NULL;
1667 else if (type == WINED3DSBT_VERTEXSTATE)
1669 TRACE("VERTEXSTATE => Pretend all vertex shates have changed.\n");
1670 stateblock_savedstates_set(&stateblock->changed, FALSE, gl_info);
1672 /* Vertex Shader Constants. */
1673 for (i = 0; i < gl_info->max_vshader_constantsF; ++i)
1675 stateblock->changed.vertexShaderConstantsF[i] = TRUE;
1676 stateblock->contained_vs_consts_f[i] = i;
1678 stateblock->num_contained_vs_consts_f = gl_info->max_vshader_constantsF;
1680 for (i = 0; i < MAX_CONST_B; ++i)
1682 stateblock->contained_vs_consts_b[i] = i;
1683 stateblock->changed.vertexShaderConstantsB |= (1 << i);
1685 stateblock->num_contained_vs_consts_b = MAX_CONST_B;
1687 for (i = 0; i < MAX_CONST_I; ++i)
1689 stateblock->contained_vs_consts_i[i] = i;
1690 stateblock->changed.vertexShaderConstantsI |= (1 << i);
1692 stateblock->num_contained_vs_consts_i = MAX_CONST_I;
1694 for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++)
1696 DWORD rs = SavedVertexStates_R[i];
1697 stateblock->changed.renderState[rs >> 5] |= 1 << (rs & 0x1f);
1698 stateblock->contained_render_states[i] = rs;
1700 stateblock->num_contained_render_states = NUM_SAVEDVERTEXSTATES_R;
1702 for (i = 0; i < MAX_TEXTURES; ++i)
1704 for (j = 0; j < NUM_SAVEDVERTEXSTATES_T; ++j)
1706 DWORD state = SavedVertexStates_T[j];
1707 stateblock->changed.textureState[i] |= 1 << state;
1708 stateblock->contained_tss_states[stateblock->num_contained_tss_states].stage = i;
1709 stateblock->contained_tss_states[stateblock->num_contained_tss_states].state = state;
1710 ++stateblock->num_contained_tss_states;
1714 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; ++i)
1716 for (j = 0; j < NUM_SAVEDVERTEXSTATES_S; ++j)
1718 DWORD state = SavedVertexStates_S[j];
1719 stateblock->changed.samplerState[i] |= 1 << state;
1720 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].stage = i;
1721 stateblock->contained_sampler_states[stateblock->num_contained_sampler_states].state = state;
1722 ++stateblock->num_contained_sampler_states;
1726 for (i = 0; i < LIGHTMAP_SIZE; ++i)
1729 LIST_FOR_EACH(e, &stateblock->lightMap[i])
1731 PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
1732 light->changed = TRUE;
1733 light->enabledChanged = TRUE;
1737 for (i = 0; i < MAX_STREAMS; ++i)
1739 if (stateblock->streamSource[i]) IWineD3DBuffer_AddRef(stateblock->streamSource[i]);
1742 stateblock->changed.vertexShader = TRUE;
1743 if (stateblock->vertexShader) IWineD3DVertexShader_AddRef(stateblock->vertexShader);
1745 stateblock->pIndexData = NULL;
1746 stateblock->pixelShader = NULL;
1750 FIXME("Unrecognized state block type %#x.\n", type);