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