wined3d: Optimize bool and int vs constants.
[wine.git] / dlls / wined3d / stateblock.c
blobba71dc3255424495a1bdaacbce724b197c676313
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 (j = 0; j < This->num_contained_vs_consts_i; ++j) {
367 i = This->contained_vs_consts_i[j];
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];
380 /* Vertex Shader Boolean Constants */
381 for (j = 0; j < This->num_contained_vs_consts_b; ++j) {
382 i = This->contained_vs_consts_b[j];
383 TRACE("Setting %p from %p %d to %s\n", This, targetStateBlock, i,
384 targetStateBlock->vertexShaderConstantB[i]? "TRUE":"FALSE");
386 This->vertexShaderConstantB[i] = targetStateBlock->vertexShaderConstantB[i];
389 /* Lights... For a recorded state block, we just had a chain of actions to perform,
390 so we need to walk that chain and update any actions which differ */
391 for(i = 0; i < LIGHTMAP_SIZE; i++) {
392 struct list *e, *f;
393 LIST_FOR_EACH(e, &This->lightMap[i]) {
394 BOOL updated = FALSE;
395 PLIGHTINFOEL *src = LIST_ENTRY(e, PLIGHTINFOEL, entry), *realLight;
396 if(!src->changed || !src->enabledChanged) continue;
398 /* Look up the light in the destination */
399 LIST_FOR_EACH(f, &targetStateBlock->lightMap[i]) {
400 realLight = LIST_ENTRY(f, PLIGHTINFOEL, entry);
401 if(realLight->OriginalIndex == src->OriginalIndex) {
402 if(src->changed) {
403 memcpy(&src->OriginalParms, &realLight->OriginalParms, sizeof(src->OriginalParms));
405 if(src->enabledChanged) {
406 /* Need to double check because enabledChanged does not catch enabled -> disabled -> enabled
407 * or disabled -> enabled -> disabled changes
409 if(realLight->glIndex == -1 && src->glIndex != -1) {
410 /* Light disabled */
411 This->activeLights[src->glIndex] = NULL;
412 } else if(realLight->glIndex != -1 && src->glIndex == -1){
413 /* Light enabled */
414 This->activeLights[realLight->glIndex] = src;
416 src->glIndex = realLight->glIndex;
418 updated = TRUE;
419 break;
423 if(updated) {
424 /* Found a light, all done, proceed with next hash entry */
425 continue;
426 } else if(src->changed) {
427 /* Otherwise assign defaul params */
428 memcpy(&src->OriginalParms, &WINED3D_default_light, sizeof(src->OriginalParms));
429 } else {
430 /* Not enabled by default */
431 src->glIndex = -1;
436 /* Recorded => Only update 'changed' values */
437 if (This->pixelShader != targetStateBlock->pixelShader) {
438 TRACE("Updating pixel shader from %p to %p\n", This->pixelShader, targetStateBlock->pixelShader);
440 This->pixelShader = targetStateBlock->pixelShader;
443 /* Pixel Shader Float Constants */
444 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
445 if (This->changed.pixelShaderConstantsF[i]) {
446 TRACE("Setting %p from %p %d to { %f, %f, %f, %f }\n", This, targetStateBlock, i,
447 targetStateBlock->pixelShaderConstantF[i * 4],
448 targetStateBlock->pixelShaderConstantF[i * 4 + 1],
449 targetStateBlock->pixelShaderConstantF[i * 4 + 2],
450 targetStateBlock->pixelShaderConstantF[i * 4 + 3]);
452 This->pixelShaderConstantF[i * 4] = targetStateBlock->pixelShaderConstantF[i * 4];
453 This->pixelShaderConstantF[i * 4 + 1] = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
454 This->pixelShaderConstantF[i * 4 + 2] = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
455 This->pixelShaderConstantF[i * 4 + 3] = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
459 /* Pixel Shader Integer Constants */
460 for (i = 0; i < MAX_CONST_I; ++i) {
461 if (This->changed.pixelShaderConstantsI[i]) {
462 TRACE("Setting %p from %p %d to { %d, %d, %d, %d }\n", This, targetStateBlock, i,
463 targetStateBlock->pixelShaderConstantI[i * 4],
464 targetStateBlock->pixelShaderConstantI[i * 4 + 1],
465 targetStateBlock->pixelShaderConstantI[i * 4 + 2],
466 targetStateBlock->pixelShaderConstantI[i * 4 + 3]);
468 This->pixelShaderConstantI[i * 4] = targetStateBlock->pixelShaderConstantI[i * 4];
469 This->pixelShaderConstantI[i * 4 + 1] = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
470 This->pixelShaderConstantI[i * 4 + 2] = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
471 This->pixelShaderConstantI[i * 4 + 3] = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
475 /* Pixel Shader Boolean Constants */
476 for (i = 0; i < MAX_CONST_B; ++i) {
477 if (This->changed.pixelShaderConstantsB[i]) {
478 TRACE("Setting %p from %p %d to %s\n", This, targetStateBlock, i,
479 targetStateBlock->pixelShaderConstantB[i]? "TRUE":"FALSE");
481 This->pixelShaderConstantB[i] = targetStateBlock->pixelShaderConstantB[i];
485 /* Others + Render & Texture */
486 for (i = 0; i < This->num_contained_transform_states; i++) {
487 TRACE("Updating transform %d\n", i);
488 memcpy(&This->transforms[This->contained_transform_states[i]],
489 &targetStateBlock->transforms[This->contained_transform_states[i]],
490 sizeof(WINED3DMATRIX));
493 if (This->changed.indices && ((This->pIndexData != targetStateBlock->pIndexData)
494 || (This->baseVertexIndex != targetStateBlock->baseVertexIndex))) {
495 TRACE("Updating pindexData to %p, baseVertexIndex to %d\n",
496 targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
497 This->pIndexData = targetStateBlock->pIndexData;
498 This->baseVertexIndex = targetStateBlock->baseVertexIndex;
501 if(This->changed.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
502 TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
504 This->vertexDecl = targetStateBlock->vertexDecl;
507 if(This->changed.fvf && This->fvf != targetStateBlock->fvf){
508 This->fvf = targetStateBlock->fvf;
511 if (This->changed.material && memcmp(&targetStateBlock->material,
512 &This->material,
513 sizeof(WINED3DMATERIAL)) != 0) {
514 TRACE("Updating material\n");
515 memcpy(&This->material, &targetStateBlock->material, sizeof(WINED3DMATERIAL));
518 if (This->changed.viewport && memcmp(&targetStateBlock->viewport,
519 &This->viewport,
520 sizeof(WINED3DVIEWPORT)) != 0) {
521 TRACE("Updating viewport\n");
522 memcpy(&This->viewport, &targetStateBlock->viewport, sizeof(WINED3DVIEWPORT));
525 if(This->changed.scissorRect && memcmp(&targetStateBlock->scissorRect,
526 &This->scissorRect,
527 sizeof(targetStateBlock->scissorRect)))
529 TRACE("Updating scissor rect\n");
530 memcpy(&targetStateBlock->scissorRect, &This->scissorRect, sizeof(targetStateBlock->scissorRect));
533 for (i = 0; i < MAX_STREAMS; i++) {
534 if (This->changed.streamSource[i] &&
535 ((This->streamStride[i] != targetStateBlock->streamStride[i]) ||
536 (This->streamSource[i] != targetStateBlock->streamSource[i]))) {
537 TRACE("Updating stream source %d to %p, stride to %d\n", i, targetStateBlock->streamSource[i],
538 targetStateBlock->streamStride[i]);
539 This->streamStride[i] = targetStateBlock->streamStride[i];
540 This->streamSource[i] = targetStateBlock->streamSource[i];
543 if (This->changed.streamFreq[i] &&
544 (This->streamFreq[i] != targetStateBlock->streamFreq[i]
545 || This->streamFlags[i] != targetStateBlock->streamFlags[i])){
546 TRACE("Updating stream frequency %d to %d flags to %d\n", i , targetStateBlock->streamFreq[i] ,
547 targetStateBlock->streamFlags[i]);
548 This->streamFreq[i] = targetStateBlock->streamFreq[i];
549 This->streamFlags[i] = targetStateBlock->streamFlags[i];
553 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
554 if (This->changed.clipplane[i] && memcmp(&targetStateBlock->clipplane[i],
555 &This->clipplane[i],
556 sizeof(This->clipplane)) != 0) {
558 TRACE("Updating clipplane %d\n", i);
559 memcpy(&This->clipplane[i], &targetStateBlock->clipplane[i],
560 sizeof(This->clipplane));
564 /* Render */
565 for (i = 0; i <= This->num_contained_render_states; i++) {
566 TRACE("Updating renderState %d to %d\n",
567 This->contained_render_states[i], targetStateBlock->renderState[This->contained_render_states[i]]);
568 This->renderState[This->contained_render_states[i]] = targetStateBlock->renderState[This->contained_render_states[i]];
571 /* Texture states */
572 for (j = 0; j < MAX_TEXTURES; j++) {
573 /* TODO: move over to using memcpy */
574 for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE ; i++) {
575 if (This->changed.textureState[j][i]) {
576 TRACE("Updating texturestagestate %d,%d to %d (was %d)\n", j,i, targetStateBlock->textureState[j][i],
577 This->textureState[j][i]);
578 This->textureState[j][i] = targetStateBlock->textureState[j][i];
583 /* Samplers */
584 /* TODO: move over to using memcpy */
585 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
586 if (This->changed.textures[j]) {
587 TRACE("Updating texture %d to %p (was %p)\n", j, targetStateBlock->textures[j], This->textures[j]);
588 This->textures[j] = targetStateBlock->textures[j];
590 for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE ; i++){ /* States are 1 based */
591 if (This->changed.samplerState[j][i]) {
592 TRACE("Updating sampler state %d,%d to %d (was %d)\n",
593 j, i, targetStateBlock->samplerState[j][i],
594 This->samplerState[j][i]);
595 This->samplerState[j][i] = targetStateBlock->samplerState[j][i];
601 TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
603 return WINED3D_OK;
606 static HRESULT WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
607 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
608 IWineD3DDevice* pDevice = (IWineD3DDevice*)This->wineD3DDevice;
610 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
611 should really perform a delta so that only the changes get updated*/
614 UINT i;
615 UINT j;
617 TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
619 /* FIXME: Only apply applicable states not all states */
621 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */This->blockType == WINED3DSBT_INIT || This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_VERTEXSTATE) {
623 for(i = 0; i < LIGHTMAP_SIZE; i++) {
624 struct list *e;
626 LIST_FOR_EACH(e, &This->lightMap[i]) {
627 PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
629 if(light->changed) {
630 IWineD3DDevice_SetLight(pDevice, light->OriginalIndex, &light->OriginalParms);
632 if(light->enabledChanged) {
633 IWineD3DDevice_SetLightEnable(pDevice, light->OriginalIndex, light->glIndex != -1);
638 /* Vertex Shader */
639 if (This->changed.vertexShader) {
640 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
643 /* Vertex Shader Constants */
644 for (i = 0; i < GL_LIMITS(vshader_constantsF); ++i) {
645 if (This->changed.vertexShaderConstantsF[i])
646 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i, This->vertexShaderConstantF + i * 4, 1);
649 for (i = 0; i < This->num_contained_vs_consts_i; i++) {
650 IWineD3DDevice_SetVertexShaderConstantI(pDevice, This->contained_vs_consts_i[i],
651 This->vertexShaderConstantI + This->contained_vs_consts_i[i] * 4, 1);
654 for (i = 0; i < This->num_contained_vs_consts_b; i++) {
655 IWineD3DDevice_SetVertexShaderConstantB(pDevice, This->contained_vs_consts_b[i],
656 This->vertexShaderConstantB + This->contained_vs_consts_b[i], 1);
660 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_PIXELSTATE) {
662 /* Pixel Shader */
663 if (This->changed.pixelShader) {
664 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
667 /* Pixel Shader Constants */
668 for (i = 0; i < GL_LIMITS(pshader_constantsF); ++i) {
669 if (This->changed.pixelShaderConstantsF[i])
670 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i, This->pixelShaderConstantF + i * 4, 1);
673 for (i = 0; i < MAX_CONST_I; ++i) {
674 if (This->changed.pixelShaderConstantsI[i])
675 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i, This->pixelShaderConstantI + i * 4, 1);
678 for (i = 0; i < MAX_CONST_B; ++i) {
679 if (This->changed.pixelShaderConstantsB[i])
680 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i, This->pixelShaderConstantB + i, 1);
684 if (This->changed.fvf) {
685 IWineD3DDevice_SetFVF(pDevice, This->fvf);
688 if (This->changed.vertexDecl) {
689 IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
692 /* Others + Render & Texture */
693 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_INIT) {
694 for (i = 0; i < This->num_contained_transform_states; i++) {
695 IWineD3DDevice_SetTransform(pDevice, This->contained_transform_states[i],
696 &This->transforms[This->contained_transform_states[i]]);
699 if (This->changed.indices) {
700 IWineD3DDevice_SetIndices(pDevice, This->pIndexData);
701 IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
704 if (This->changed.material )
705 IWineD3DDevice_SetMaterial(pDevice, &This->material);
707 if (This->changed.viewport)
708 IWineD3DDevice_SetViewport(pDevice, &This->viewport);
710 if (This->changed.scissorRect)
711 IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
713 /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
714 for (i=0; i<MAX_STREAMS; i++) {
715 if (This->changed.streamSource[i])
716 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
718 if (This->changed.streamFreq[i])
719 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
722 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
723 if (This->changed.clipplane[i]) {
724 float clip[4];
726 clip[0] = This->clipplane[i][0];
727 clip[1] = This->clipplane[i][1];
728 clip[2] = This->clipplane[i][2];
729 clip[3] = This->clipplane[i][3];
730 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
734 /* Texture states */
735 for (j = 0; j < MAX_TEXTURES; j++) { /* Set The texture first, just in case it resets the states? */
736 /* TODO: move over to memcpy */
737 for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE; i++) {
738 if (This->changed.textureState[j][i]) { /* tb_dx9_10 failes without this test */
739 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][i] = This->textureState[j][i];
740 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[j][i] = TRUE;
741 /* TODO: Record a display list to apply all gl states. For now apply by brute force */
742 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, i));
747 /* Samplers */
748 /* TODO: move over to memcpy */
749 for (j = 0 ; j < MAX_COMBINED_SAMPLERS; j++){
750 if (This->changed.textures[j]) {
751 if (j < MAX_FRAGMENT_SAMPLERS) {
752 IWineD3DDevice_SetTexture(pDevice, j, This->textures[j]);
753 } else {
754 IWineD3DDevice_SetTexture(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS, This->textures[j]);
757 for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; i++){
758 if (This->changed.samplerState[j][i]) {
759 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][i] = This->samplerState[j][i];
760 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[j][i] = TRUE;
763 /* SetTexture catches nop changes, so the above call does not assure that the sampler is updated */
764 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_SAMPLER(j));
767 } else if (This->blockType == WINED3DSBT_PIXELSTATE) {
769 for (j = 0; j < MAX_TEXTURES; j++) {
770 for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
771 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedPixelStates_T[i]] = This->textureState[j][SavedPixelStates_T[i]];
772 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, SavedPixelStates_T[i]));
776 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
777 for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
778 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedPixelStates_S[i]] = This->samplerState[j][SavedPixelStates_S[i]];
782 } else if (This->blockType == WINED3DSBT_VERTEXSTATE) {
784 for (j = 0; j < MAX_TEXTURES; j++) {
785 for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
786 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedVertexStates_T[i]] = This->textureState[j][SavedVertexStates_T[i]];
787 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(j, SavedVertexStates_T[i]));
791 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
792 for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
793 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedVertexStates_S[i]] = This->samplerState[j][SavedVertexStates_S[i]];
798 } else {
799 FIXME("Unrecognized state block type %d\n", This->blockType);
802 /* Render */
803 for (i = 0; i <= This->num_contained_render_states; i++) {
804 IWineD3DDevice_SetRenderState(pDevice, This->contained_render_states[i],
805 This->renderState[This->contained_render_states[i]]);
808 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
809 for(j = 0; j < MAX_TEXTURES - 1; j++) {
810 if(((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE) {
811 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = j;
812 break;
815 TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
817 return WINED3D_OK;
820 static HRESULT WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
821 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
822 IWineD3DDevice *device = (IWineD3DDevice *)This->wineD3DDevice;
823 IWineD3DDeviceImpl *ThisDevice = (IWineD3DDeviceImpl *)device;
824 union {
825 WINED3DLINEPATTERN lp;
826 DWORD d;
827 } lp;
828 union {
829 float f;
830 DWORD d;
831 } tmpfloat;
832 unsigned int i;
834 /* Note this may have a large overhead but it should only be executed
835 once, in order to initialize the complete state of the device and
836 all opengl equivalents */
837 TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
838 /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
839 This->blockType = WINED3DSBT_INIT;
841 /* Set some of the defaults for lights, transforms etc */
842 memcpy(&This->transforms[WINED3DTS_PROJECTION], &identity, sizeof(identity));
843 memcpy(&This->transforms[WINED3DTS_VIEW], &identity, sizeof(identity));
844 for (i = 0; i < 256; ++i) {
845 memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], &identity, sizeof(identity));
848 TRACE("Render states\n");
849 /* Render states: */
850 if (ThisDevice->depthStencilBuffer != NULL) {
851 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_TRUE);
852 } else {
853 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_FALSE);
855 IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE, WINED3DFILL_SOLID);
856 IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE, WINED3DSHADE_GOURAUD);
857 lp.lp.wRepeatFactor = 0;
858 lp.lp.wLinePattern = 0;
859 IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN, lp.d);
860 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE, TRUE);
861 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE, FALSE);
862 IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL, TRUE);
863 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND, WINED3DBLEND_ONE);
864 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND, WINED3DBLEND_ZERO);
865 IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE, WINED3DCULL_CCW);
866 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC, WINED3DCMP_LESSEQUAL);
867 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC, WINED3DCMP_ALWAYS);
868 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF, 0);
869 IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE, FALSE);
870 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
871 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE, FALSE);
872 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE, FALSE);
873 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE, 0);
874 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR, 0);
875 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE, WINED3DFOG_NONE);
876 tmpfloat.f = 0.0f;
877 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART, tmpfloat.d);
878 tmpfloat.f = 1.0f;
879 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND, tmpfloat.d);
880 tmpfloat.f = 1.0f;
881 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY, tmpfloat.d);
882 IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS, FALSE);
883 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS, 0);
884 IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE, FALSE);
885 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE, FALSE);
886 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL, WINED3DSTENCILOP_KEEP);
887 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
888 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS, WINED3DSTENCILOP_KEEP);
889 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
890 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
891 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC, WINED3DCMP_ALWAYS);
892 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
893 IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
894 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
895 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
896 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
897 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
898 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
899 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
900 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
901 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
902 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING, TRUE);
903 IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING, TRUE);
904 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT, 0);
905 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE, WINED3DFOG_NONE);
906 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX, TRUE);
907 IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER, TRUE);
908 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS, FALSE);
909 IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE, WINED3DMCS_COLOR1);
910 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE, WINED3DMCS_COLOR2);
911 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE, WINED3DMCS_MATERIAL);
912 IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE, WINED3DMCS_MATERIAL);
913 IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND, WINED3DVBF_DISABLE);
914 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE, 0);
915 IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
916 tmpfloat.f = 1.0f;
917 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE, tmpfloat.d);
918 tmpfloat.f = 1.0f;
919 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN, tmpfloat.d);
920 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE, FALSE);
921 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE, FALSE);
922 tmpfloat.f = 1.0f;
923 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A, tmpfloat.d);
924 tmpfloat.f = 0.0f;
925 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B, tmpfloat.d);
926 tmpfloat.f = 0.0f;
927 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C, tmpfloat.d);
928 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS, TRUE);
929 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
930 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE, WINED3DPATCHEDGE_DISCRETE);
931 tmpfloat.f = 1.0f;
932 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS, tmpfloat.d);
933 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN, 0xbaadcafe);
934 tmpfloat.f = 64.0f;
935 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX, tmpfloat.d);
936 IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
937 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE, 0x0000000F);
938 tmpfloat.f = 0.0f;
939 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR, tmpfloat.d);
940 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP, WINED3DBLENDOP_ADD);
941 IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE, WINED3DDEGREE_CUBIC);
942 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE, WINED3DDEGREE_LINEAR);
943 /* states new in d3d9 */
944 IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE, FALSE);
945 IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS, 0);
946 tmpfloat.f = 1.0f;
947 IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL, tmpfloat.d);
948 IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL, tmpfloat.d);
949 IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE, FALSE);
950 tmpfloat.f = 0.0f;
951 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X, tmpfloat.d);
952 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y, tmpfloat.d);
953 tmpfloat.f = 1.0f;
954 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z, tmpfloat.d);
955 tmpfloat.f = 0.0f;
956 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W, tmpfloat.d);
957 IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
958 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE, FALSE);
959 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL, WINED3DSTENCILOP_KEEP);
960 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
961 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS, WINED3DSTENCILOP_KEEP);
962 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC, WINED3DCMP_ALWAYS);
963 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1, 0x0000000F);
964 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2, 0x0000000F);
965 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3, 0x0000000F);
966 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR, 0xFFFFFFFF);
967 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE, 0);
968 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS, 0);
969 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8, 0);
970 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9, 0);
971 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
972 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
973 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
974 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
975 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
976 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
977 IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
978 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA, WINED3DBLEND_ONE);
979 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA, WINED3DBLEND_ZERO);
980 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA, WINED3DBLENDOP_ADD);
982 /* clipping status */
983 This->clip_status.ClipUnion = 0;
984 This->clip_status.ClipIntersection = 0xFFFFFFFF;
986 /* Texture Stage States - Put directly into state block, we will call function below */
987 for (i = 0; i < MAX_TEXTURES; i++) {
988 TRACE("Setting up default texture states for texture Stage %d\n", i);
989 memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], &identity, sizeof(identity));
990 This->textureState[i][WINED3DTSS_COLOROP ] = (i==0)? WINED3DTOP_MODULATE : WINED3DTOP_DISABLE;
991 This->textureState[i][WINED3DTSS_COLORARG1 ] = WINED3DTA_TEXTURE;
992 This->textureState[i][WINED3DTSS_COLORARG2 ] = WINED3DTA_CURRENT;
993 This->textureState[i][WINED3DTSS_ALPHAOP ] = (i==0)? WINED3DTOP_SELECTARG1 : WINED3DTOP_DISABLE;
994 This->textureState[i][WINED3DTSS_ALPHAARG1 ] = WINED3DTA_TEXTURE;
995 This->textureState[i][WINED3DTSS_ALPHAARG2 ] = WINED3DTA_CURRENT;
996 This->textureState[i][WINED3DTSS_BUMPENVMAT00 ] = (DWORD) 0.0;
997 This->textureState[i][WINED3DTSS_BUMPENVMAT01 ] = (DWORD) 0.0;
998 This->textureState[i][WINED3DTSS_BUMPENVMAT10 ] = (DWORD) 0.0;
999 This->textureState[i][WINED3DTSS_BUMPENVMAT11 ] = (DWORD) 0.0;
1000 This->textureState[i][WINED3DTSS_TEXCOORDINDEX ] = i;
1001 This->textureState[i][WINED3DTSS_BUMPENVLSCALE ] = (DWORD) 0.0;
1002 This->textureState[i][WINED3DTSS_BUMPENVLOFFSET ] = (DWORD) 0.0;
1003 This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1004 This->textureState[i][WINED3DTSS_ADDRESSW ] = WINED3DTADDRESS_WRAP;
1005 This->textureState[i][WINED3DTSS_COLORARG0 ] = WINED3DTA_CURRENT;
1006 This->textureState[i][WINED3DTSS_ALPHAARG0 ] = WINED3DTA_CURRENT;
1007 This->textureState[i][WINED3DTSS_RESULTARG ] = WINED3DTA_CURRENT;
1009 This->lowest_disabled_stage = 1;
1011 /* Sampler states*/
1012 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; i++) {
1013 TRACE("Setting up default samplers states for sampler %d\n", i);
1014 This->samplerState[i][WINED3DSAMP_ADDRESSU ] = WINED3DTADDRESS_WRAP;
1015 This->samplerState[i][WINED3DSAMP_ADDRESSV ] = WINED3DTADDRESS_WRAP;
1016 This->samplerState[i][WINED3DSAMP_ADDRESSW ] = WINED3DTADDRESS_WRAP;
1017 This->samplerState[i][WINED3DSAMP_BORDERCOLOR ] = 0x00;
1018 This->samplerState[i][WINED3DSAMP_MAGFILTER ] = WINED3DTEXF_POINT;
1019 This->samplerState[i][WINED3DSAMP_MINFILTER ] = WINED3DTEXF_POINT;
1020 This->samplerState[i][WINED3DSAMP_MIPFILTER ] = WINED3DTEXF_NONE;
1021 This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS ] = 0;
1022 This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL ] = 0;
1023 This->samplerState[i][WINED3DSAMP_MAXANISOTROPY ] = 1;
1024 This->samplerState[i][WINED3DSAMP_SRGBTEXTURE ] = 0;
1025 This->samplerState[i][WINED3DSAMP_ELEMENTINDEX ] = 0; /* TODO: Indicates which element of a multielement texture to use */
1026 This->samplerState[i][WINED3DSAMP_DMAPOFFSET ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1029 /* Under DirectX you can have texture stage operations even if no texture is
1030 bound, whereas opengl will only do texture operations when a valid texture is
1031 bound. We emulate this by creating dummy textures and binding them to each
1032 texture stage, but disable all stages by default. Hence if a stage is enabled
1033 then the default texture will kick in until replaced by a SetTexture call */
1034 ENTER_GL();
1036 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
1037 /* The dummy texture does not have client storage backing */
1038 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1039 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1041 for (i = 0; i < GL_LIMITS(textures); i++) {
1042 GLubyte white = 255;
1044 /* Note this avoids calling settexture, so pretend it has been called */
1045 This->changed.textures[i] = TRUE;
1046 This->textures[i] = NULL;
1048 /* Make appropriate texture active */
1049 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
1050 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1051 checkGLcall("glActiveTextureARB");
1052 } else if (i > 0) {
1053 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
1056 /* Generate an opengl texture name */
1057 glGenTextures(1, &ThisDevice->dummyTextureName[i]);
1058 checkGLcall("glGenTextures");
1059 TRACE("Dummy Texture %d given name %d\n", i, ThisDevice->dummyTextureName[i]);
1061 /* Generate a dummy 2d texture (not using 1d because they cause many
1062 * DRI drivers fall back to sw) */
1063 This->textureDimensions[i] = GL_TEXTURE_2D;
1064 glBindTexture(GL_TEXTURE_2D, ThisDevice->dummyTextureName[i]);
1065 checkGLcall("glBindTexture");
1067 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 1, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white);
1068 checkGLcall("glTexImage2D");
1070 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
1071 /* Reenable because if supported it is enabled by default */
1072 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1073 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1076 LEAVE_GL();
1079 /* Defaulting palettes - Note these are device wide but reinitialized here for convenience*/
1080 for (i = 0; i < MAX_PALETTES; ++i) {
1081 int j;
1082 for (j = 0; j < 256; ++j) {
1083 This->wineD3DDevice->palettes[i][j].peRed = 0xFF;
1084 This->wineD3DDevice->palettes[i][j].peGreen = 0xFF;
1085 This->wineD3DDevice->palettes[i][j].peBlue = 0xFF;
1086 This->wineD3DDevice->palettes[i][j].peFlags = 0xFF;
1089 This->wineD3DDevice->currentPalette = 0;
1091 /* Set default GLSL program to NULL. We won't actually create one
1092 * until the app sets a vertex or pixel shader */
1093 This->glsl_program = NULL;
1095 TRACE("-----------------------> Device defaults now set up...\n");
1096 return WINED3D_OK;
1099 /**********************************************************
1100 * IWineD3DStateBlock VTbl follows
1101 **********************************************************/
1103 const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1105 /* IUnknown */
1106 IWineD3DStateBlockImpl_QueryInterface,
1107 IWineD3DStateBlockImpl_AddRef,
1108 IWineD3DStateBlockImpl_Release,
1109 /* IWineD3DStateBlock */
1110 IWineD3DStateBlockImpl_GetParent,
1111 IWineD3DStateBlockImpl_GetDevice,
1112 IWineD3DStateBlockImpl_Capture,
1113 IWineD3DStateBlockImpl_Apply,
1114 IWineD3DStateBlockImpl_InitStartupStateBlock