wined3d: Add separate methods for setting the primitive type.
[wine/multimedia.git] / dlls / wined3d / stateblock.c
blob8ac64711aa9d15c64183f7c4eb81fd80209a29c2
1 /*
2 * state block implementation
4 * Copyright 2002 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2005 Oliver Stieber
7 * Copyright 2007 Stefan Dösinger for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "config.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
30 /***************************************
31 * Stateblock helper functions follow
32 **************************************/
34 /** Allocates the correct amount of space for pixel and vertex shader constants,
35 * along with their set/changed flags on the given stateblock object
37 HRESULT allocate_shader_constants(IWineD3DStateBlockImpl* object) {
39 IWineD3DStateBlockImpl *This = object;
41 /* Allocate space for floating point constants */
42 object->pixelShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
43 if (!object->pixelShaderConstantF) goto fail;
45 object->changed.pixelShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(pshader_constantsF));
46 if (!object->changed.pixelShaderConstantsF) goto fail;
48 object->vertexShaderConstantF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
49 if (!object->vertexShaderConstantF) goto fail;
51 object->changed.vertexShaderConstantsF = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BOOL) * GL_LIMITS(vshader_constantsF));
52 if (!object->changed.vertexShaderConstantsF) goto fail;
54 object->contained_vs_consts_f = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * GL_LIMITS(vshader_constantsF));
55 if (!object->contained_vs_consts_f) goto fail;
57 object->contained_ps_consts_f = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD) * GL_LIMITS(pshader_constantsF));
58 if (!object->contained_ps_consts_f) goto fail;
60 return WINED3D_OK;
62 fail:
63 ERR("Failed to allocate memory\n");
64 HeapFree(GetProcessHeap(), 0, object->pixelShaderConstantF);
65 HeapFree(GetProcessHeap(), 0, object->changed.pixelShaderConstantsF);
66 HeapFree(GetProcessHeap(), 0, object->vertexShaderConstantF);
67 HeapFree(GetProcessHeap(), 0, object->changed.vertexShaderConstantsF);
68 HeapFree(GetProcessHeap(), 0, object->contained_vs_consts_f);
69 HeapFree(GetProcessHeap(), 0, object->contained_ps_consts_f);
70 return E_OUTOFMEMORY;
73 /** Copy all members of one stateblock to another */
74 static void stateblock_savedstates_copy(IWineD3DStateBlock* iface, SAVEDSTATES *dest, const SAVEDSTATES *source)
76 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
77 unsigned bsize = sizeof(BOOL);
79 /* Single values */
80 dest->primitive_type = source->primitive_type;
81 dest->indices = source->indices;
82 dest->material = source->material;
83 dest->viewport = source->viewport;
84 dest->vertexDecl = source->vertexDecl;
85 dest->pixelShader = source->pixelShader;
86 dest->vertexShader = source->vertexShader;
87 dest->scissorRect = dest->scissorRect;
89 /* Fixed size arrays */
90 dest->streamSource = source->streamSource;
91 dest->streamFreq = source->streamFreq;
92 dest->textures = source->textures;
93 memcpy(dest->transform, source->transform, sizeof(source->transform));
94 memcpy(dest->renderState, source->renderState, sizeof(source->renderState));
95 memcpy(dest->textureState, source->textureState, sizeof(source->textureState));
96 memcpy(dest->samplerState, source->samplerState, sizeof(source->samplerState));
97 dest->clipplane = source->clipplane;
98 dest->pixelShaderConstantsB = source->pixelShaderConstantsB;
99 dest->pixelShaderConstantsI = source->pixelShaderConstantsI;
100 dest->vertexShaderConstantsB = source->vertexShaderConstantsB;
101 dest->vertexShaderConstantsI = source->vertexShaderConstantsI;
103 /* Dynamically sized arrays */
104 memcpy(dest->pixelShaderConstantsF, source->pixelShaderConstantsF, bsize * GL_LIMITS(pshader_constantsF));
105 memcpy(dest->vertexShaderConstantsF, source->vertexShaderConstantsF, bsize * GL_LIMITS(vshader_constantsF));
108 static inline void stateblock_set_bits(DWORD *map, UINT map_size)
110 DWORD mask = (1 << (map_size & 0x1f)) - 1;
111 memset(map, 0xff, (map_size >> 5) * sizeof(*map));
112 if (mask) map[map_size >> 5] = mask;
115 /** Set all members of a stateblock savedstate to the given value */
116 void stateblock_savedstates_set(
117 IWineD3DStateBlock* iface,
118 SAVEDSTATES* states,
119 BOOL value) {
121 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
122 unsigned bsize = sizeof(BOOL);
124 /* Single values */
125 states->primitive_type = value;
126 states->indices = value;
127 states->material = value;
128 states->viewport = value;
129 states->vertexDecl = value;
130 states->pixelShader = value;
131 states->vertexShader = value;
132 states->scissorRect = value;
134 /* Fixed size arrays */
135 if (value)
137 int i;
138 states->streamSource = 0xffff;
139 states->streamFreq = 0xffff;
140 states->textures = 0xfffff;
141 stateblock_set_bits(states->transform, HIGHEST_TRANSFORMSTATE + 1);
142 stateblock_set_bits(states->renderState, WINEHIGHEST_RENDER_STATE + 1);
143 for (i = 0; i < MAX_TEXTURES; ++i) states->textureState[i] = 0x3ffff;
144 for (i = 0; i < MAX_COMBINED_SAMPLERS; ++i) states->samplerState[i] = 0x3fff;
145 states->clipplane = 0xffffffff;
146 states->pixelShaderConstantsB = 0xffff;
147 states->pixelShaderConstantsI = 0xffff;
148 states->vertexShaderConstantsB = 0xffff;
149 states->vertexShaderConstantsI = 0xffff;
151 else
153 states->streamSource = 0;
154 states->streamFreq = 0;
155 states->textures = 0;
156 memset(states->transform, 0, sizeof(states->transform));
157 memset(states->renderState, 0, sizeof(states->renderState));
158 memset(states->textureState, 0, sizeof(states->textureState));
159 memset(states->samplerState, 0, sizeof(states->samplerState));
160 states->clipplane = 0;
161 states->pixelShaderConstantsB = 0;
162 states->pixelShaderConstantsI = 0;
163 states->vertexShaderConstantsB = 0;
164 states->vertexShaderConstantsI = 0;
167 /* Dynamically sized arrays */
168 memset(states->pixelShaderConstantsF, value, bsize * GL_LIMITS(pshader_constantsF));
169 memset(states->vertexShaderConstantsF, value, bsize * GL_LIMITS(vshader_constantsF));
172 void stateblock_copy(
173 IWineD3DStateBlock* destination,
174 IWineD3DStateBlock* source) {
175 int l;
177 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)source;
178 IWineD3DStateBlockImpl *Dest = (IWineD3DStateBlockImpl *)destination;
180 /* IUnknown fields */
181 Dest->lpVtbl = This->lpVtbl;
182 Dest->ref = This->ref;
184 /* IWineD3DStateBlock information */
185 Dest->parent = This->parent;
186 Dest->wineD3DDevice = This->wineD3DDevice;
187 Dest->blockType = This->blockType;
189 /* Saved states */
190 stateblock_savedstates_copy(source, &Dest->changed, &This->changed);
192 /* Single items */
193 Dest->gl_primitive_type = This->gl_primitive_type;
194 Dest->vertexDecl = This->vertexDecl;
195 Dest->vertexShader = This->vertexShader;
196 Dest->streamIsUP = This->streamIsUP;
197 Dest->pIndexData = This->pIndexData;
198 Dest->baseVertexIndex = This->baseVertexIndex;
199 /* Dest->lights = This->lights; */
200 Dest->clip_status = This->clip_status;
201 Dest->viewport = This->viewport;
202 Dest->material = This->material;
203 Dest->pixelShader = This->pixelShader;
204 Dest->scissorRect = This->scissorRect;
206 /* Lights */
207 memset(This->activeLights, 0, sizeof(This->activeLights));
208 for(l = 0; l < LIGHTMAP_SIZE; l++) {
209 struct list *e1, *e2;
210 LIST_FOR_EACH_SAFE(e1, e2, &Dest->lightMap[l]) {
211 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
212 list_remove(&light->entry);
213 HeapFree(GetProcessHeap(), 0, light);
216 LIST_FOR_EACH(e1, &This->lightMap[l]) {
217 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry), *light2;
218 light2 = HeapAlloc(GetProcessHeap(), 0, sizeof(*light));
219 *light2 = *light;
220 list_add_tail(&Dest->lightMap[l], &light2->entry);
221 if(light2->glIndex != -1) Dest->activeLights[light2->glIndex] = light2;
225 /* Fixed size arrays */
226 memcpy(Dest->vertexShaderConstantB, This->vertexShaderConstantB, sizeof(BOOL) * MAX_CONST_B);
227 memcpy(Dest->vertexShaderConstantI, This->vertexShaderConstantI, sizeof(INT) * MAX_CONST_I * 4);
228 memcpy(Dest->pixelShaderConstantB, This->pixelShaderConstantB, sizeof(BOOL) * MAX_CONST_B);
229 memcpy(Dest->pixelShaderConstantI, This->pixelShaderConstantI, sizeof(INT) * MAX_CONST_I * 4);
231 memcpy(Dest->streamStride, This->streamStride, sizeof(UINT) * MAX_STREAMS);
232 memcpy(Dest->streamOffset, This->streamOffset, sizeof(UINT) * MAX_STREAMS);
233 memcpy(Dest->streamSource, This->streamSource, sizeof(IWineD3DVertexBuffer*) * MAX_STREAMS);
234 memcpy(Dest->streamFreq, This->streamFreq, sizeof(UINT) * MAX_STREAMS);
235 memcpy(Dest->streamFlags, This->streamFlags, sizeof(UINT) * MAX_STREAMS);
236 memcpy(Dest->transforms, This->transforms, sizeof(WINED3DMATRIX) * (HIGHEST_TRANSFORMSTATE + 1));
237 memcpy(Dest->clipplane, This->clipplane, sizeof(double) * MAX_CLIPPLANES * 4);
238 memcpy(Dest->renderState, This->renderState, sizeof(DWORD) * (WINEHIGHEST_RENDER_STATE + 1));
239 memcpy(Dest->textures, This->textures, sizeof(IWineD3DBaseTexture*) * MAX_COMBINED_SAMPLERS);
240 memcpy(Dest->textureState, This->textureState, sizeof(DWORD) * MAX_TEXTURES * (WINED3D_HIGHEST_TEXTURE_STATE + 1));
241 memcpy(Dest->samplerState, This->samplerState, sizeof(DWORD) * MAX_COMBINED_SAMPLERS * (WINED3D_HIGHEST_SAMPLER_STATE + 1));
243 /* Dynamically sized arrays */
244 memcpy(Dest->vertexShaderConstantF, This->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
245 memcpy(Dest->pixelShaderConstantF, This->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
248 /**********************************************************
249 * IWineD3DStateBlockImpl IUnknown parts follows
250 **********************************************************/
251 static HRESULT WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
253 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
254 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
255 if (IsEqualGUID(riid, &IID_IUnknown)
256 || IsEqualGUID(riid, &IID_IWineD3DBase)
257 || IsEqualGUID(riid, &IID_IWineD3DStateBlock)){
258 IUnknown_AddRef(iface);
259 *ppobj = This;
260 return S_OK;
262 *ppobj = NULL;
263 return E_NOINTERFACE;
266 static ULONG WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
267 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
268 ULONG refCount = InterlockedIncrement(&This->ref);
270 TRACE("(%p) : AddRef increasing from %d\n", This, refCount - 1);
271 return refCount;
274 static ULONG WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
275 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
276 ULONG refCount = InterlockedDecrement(&This->ref);
278 TRACE("(%p) : Releasing from %d\n", This, refCount + 1);
280 if (!refCount) {
281 int counter;
283 /* type 0 represents the primary stateblock, so free all the resources */
284 if (This->blockType == WINED3DSBT_INIT) {
285 /* NOTE: according to MSDN: The application is responsible for making sure the texture references are cleared down */
286 for (counter = 0; counter < MAX_COMBINED_SAMPLERS; counter++) {
287 if (This->textures[counter]) {
288 /* release our 'internal' hold on the texture */
289 if(0 != IWineD3DBaseTexture_Release(This->textures[counter])) {
290 TRACE("Texture still referenced by stateblock, applications has leaked Stage = %u Texture = %p\n", counter, This->textures[counter]);
296 for (counter = 0; counter < MAX_STREAMS; counter++) {
297 if(This->streamSource[counter]) {
298 if(0 != IWineD3DVertexBuffer_Release(This->streamSource[counter])) {
299 TRACE("Vertex buffer still referenced by stateblock, applications has leaked Stream %u, buffer %p\n", counter, This->streamSource[counter]);
303 if(This->pIndexData) IWineD3DIndexBuffer_Release(This->pIndexData);
304 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
305 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
307 for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
308 struct list *e1, *e2;
309 LIST_FOR_EACH_SAFE(e1, e2, &This->lightMap[counter]) {
310 PLIGHTINFOEL *light = LIST_ENTRY(e1, PLIGHTINFOEL, entry);
311 list_remove(&light->entry);
312 HeapFree(GetProcessHeap(), 0, light);
316 HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
317 HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
318 HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
319 HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
320 HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
321 HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
322 HeapFree(GetProcessHeap(), 0, This);
324 return refCount;
327 /**********************************************************
328 * IWineD3DStateBlockImpl parts follows
329 **********************************************************/
330 static HRESULT WINAPI IWineD3DStateBlockImpl_GetParent(IWineD3DStateBlock *iface, IUnknown **pParent) {
331 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
332 IUnknown_AddRef(This->parent);
333 *pParent = This->parent;
334 return WINED3D_OK;
337 static HRESULT WINAPI IWineD3DStateBlockImpl_GetDevice(IWineD3DStateBlock *iface, IWineD3DDevice** ppDevice){
339 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
341 *ppDevice = (IWineD3DDevice*)This->wineD3DDevice;
342 IWineD3DDevice_AddRef(*ppDevice);
343 return WINED3D_OK;
347 static inline void record_lights(IWineD3DStateBlockImpl *This, IWineD3DStateBlockImpl *targetStateBlock) {
348 UINT i;
350 /* Lights... For a recorded state block, we just had a chain of actions to perform,
351 * so we need to walk that chain and update any actions which differ
353 for(i = 0; i < LIGHTMAP_SIZE; i++) {
354 struct list *e, *f;
355 LIST_FOR_EACH(e, &This->lightMap[i]) {
356 BOOL updated = FALSE;
357 PLIGHTINFOEL *src = LIST_ENTRY(e, PLIGHTINFOEL, entry), *realLight;
358 if(!src->changed || !src->enabledChanged) continue;
360 /* Look up the light in the destination */
361 LIST_FOR_EACH(f, &targetStateBlock->lightMap[i]) {
362 realLight = LIST_ENTRY(f, PLIGHTINFOEL, entry);
363 if(realLight->OriginalIndex == src->OriginalIndex) {
364 if(src->changed) {
365 src->OriginalParms = realLight->OriginalParms;
367 if(src->enabledChanged) {
368 /* Need to double check because enabledChanged does not catch enabled -> disabled -> enabled
369 * or disabled -> enabled -> disabled changes
371 if(realLight->glIndex == -1 && src->glIndex != -1) {
372 /* Light disabled */
373 This->activeLights[src->glIndex] = NULL;
374 } else if(realLight->glIndex != -1 && src->glIndex == -1){
375 /* Light enabled */
376 This->activeLights[realLight->glIndex] = src;
378 src->glIndex = realLight->glIndex;
380 updated = TRUE;
381 break;
385 if(updated) {
386 /* Found a light, all done, proceed with next hash entry */
387 continue;
388 } else if(src->changed) {
389 /* Otherwise assign defaul params */
390 src->OriginalParms = WINED3D_default_light;
391 } else {
392 /* Not enabled by default */
393 src->glIndex = -1;
399 static HRESULT WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface){
401 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
402 IWineD3DStateBlockImpl *targetStateBlock = This->wineD3DDevice->stateBlock;
403 unsigned int i, j;
404 DWORD map;
406 TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
408 /* If not recorded, then update can just recapture */
409 if (This->blockType == WINED3DSBT_RECORDED) {
411 /* Recorded => Only update 'changed' values */
412 if (This->changed.vertexShader && This->vertexShader != targetStateBlock->vertexShader) {
413 TRACE("Updating vertex shader from %p to %p\n", This->vertexShader, targetStateBlock->vertexShader);
415 if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
416 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
417 This->vertexShader = targetStateBlock->vertexShader;
420 /* Vertex Shader Float Constants */
421 for (j = 0; j < This->num_contained_vs_consts_f; ++j) {
422 i = This->contained_vs_consts_f[j];
423 TRACE("Setting %p from %p %u to {%f, %f, %f, %f}\n", This, targetStateBlock, i,
424 targetStateBlock->vertexShaderConstantF[i * 4],
425 targetStateBlock->vertexShaderConstantF[i * 4 + 1],
426 targetStateBlock->vertexShaderConstantF[i * 4 + 2],
427 targetStateBlock->vertexShaderConstantF[i * 4 + 3]);
429 This->vertexShaderConstantF[i * 4] = targetStateBlock->vertexShaderConstantF[i * 4];
430 This->vertexShaderConstantF[i * 4 + 1] = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
431 This->vertexShaderConstantF[i * 4 + 2] = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
432 This->vertexShaderConstantF[i * 4 + 3] = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
435 /* Vertex Shader Integer Constants */
436 for (j = 0; j < This->num_contained_vs_consts_i; ++j) {
437 i = This->contained_vs_consts_i[j];
438 TRACE("Setting %p from %p %u to {%d, %d, %d, %d}\n", This, targetStateBlock, i,
439 targetStateBlock->vertexShaderConstantI[i * 4],
440 targetStateBlock->vertexShaderConstantI[i * 4 + 1],
441 targetStateBlock->vertexShaderConstantI[i * 4 + 2],
442 targetStateBlock->vertexShaderConstantI[i * 4 + 3]);
444 This->vertexShaderConstantI[i * 4] = targetStateBlock->vertexShaderConstantI[i * 4];
445 This->vertexShaderConstantI[i * 4 + 1] = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
446 This->vertexShaderConstantI[i * 4 + 2] = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
447 This->vertexShaderConstantI[i * 4 + 3] = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
450 /* Vertex Shader Boolean Constants */
451 for (j = 0; j < This->num_contained_vs_consts_b; ++j) {
452 i = This->contained_vs_consts_b[j];
453 TRACE("Setting %p from %p %u to %s\n", This, targetStateBlock, i,
454 targetStateBlock->vertexShaderConstantB[i] ? "TRUE" : "FALSE");
456 This->vertexShaderConstantB[i] = targetStateBlock->vertexShaderConstantB[i];
459 /* Pixel Shader Float Constants */
460 for (j = 0; j < This->num_contained_ps_consts_f; ++j) {
461 i = This->contained_ps_consts_f[j];
462 TRACE("Setting %p from %p %u to {%f, %f, %f, %f}\n", This, targetStateBlock, i,
463 targetStateBlock->pixelShaderConstantF[i * 4],
464 targetStateBlock->pixelShaderConstantF[i * 4 + 1],
465 targetStateBlock->pixelShaderConstantF[i * 4 + 2],
466 targetStateBlock->pixelShaderConstantF[i * 4 + 3]);
468 This->pixelShaderConstantF[i * 4] = targetStateBlock->pixelShaderConstantF[i * 4];
469 This->pixelShaderConstantF[i * 4 + 1] = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
470 This->pixelShaderConstantF[i * 4 + 2] = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
471 This->pixelShaderConstantF[i * 4 + 3] = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
474 /* Pixel Shader Integer Constants */
475 for (j = 0; j < This->num_contained_ps_consts_i; ++j) {
476 i = This->contained_ps_consts_i[j];
477 TRACE("Setting %p from %p %u to {%d, %d, %d, %d}\n", This, targetStateBlock, i,
478 targetStateBlock->pixelShaderConstantI[i * 4],
479 targetStateBlock->pixelShaderConstantI[i * 4 + 1],
480 targetStateBlock->pixelShaderConstantI[i * 4 + 2],
481 targetStateBlock->pixelShaderConstantI[i * 4 + 3]);
483 This->pixelShaderConstantI[i * 4] = targetStateBlock->pixelShaderConstantI[i * 4];
484 This->pixelShaderConstantI[i * 4 + 1] = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
485 This->pixelShaderConstantI[i * 4 + 2] = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
486 This->pixelShaderConstantI[i * 4 + 3] = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
489 /* Pixel Shader Boolean Constants */
490 for (j = 0; j < This->num_contained_ps_consts_b; ++j) {
491 i = This->contained_ps_consts_b[j];
492 TRACE("Setting %p from %p %u to %s\n", This, targetStateBlock, i,
493 targetStateBlock->pixelShaderConstantB[i] ? "TRUE" : "FALSE");
495 This->pixelShaderConstantB[i] = targetStateBlock->pixelShaderConstantB[i];
498 /* Others + Render & Texture */
499 for (i = 0; i < This->num_contained_transform_states; i++) {
500 TRACE("Updating transform %u\n", i);
501 This->transforms[This->contained_transform_states[i]] =
502 targetStateBlock->transforms[This->contained_transform_states[i]];
505 if (This->changed.primitive_type) This->gl_primitive_type = targetStateBlock->gl_primitive_type;
507 if (This->changed.indices && ((This->pIndexData != targetStateBlock->pIndexData)
508 || (This->baseVertexIndex != targetStateBlock->baseVertexIndex))) {
509 TRACE("Updating pIndexData to %p, baseVertexIndex to %d\n",
510 targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
511 if(targetStateBlock->pIndexData) IWineD3DIndexBuffer_AddRef(targetStateBlock->pIndexData);
512 if(This->pIndexData) IWineD3DIndexBuffer_Release(This->pIndexData);
513 This->pIndexData = targetStateBlock->pIndexData;
514 This->baseVertexIndex = targetStateBlock->baseVertexIndex;
517 if(This->changed.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
518 TRACE("Updating vertex declaration from %p to %p\n", This->vertexDecl, targetStateBlock->vertexDecl);
520 This->vertexDecl = targetStateBlock->vertexDecl;
523 if (This->changed.material && memcmp(&targetStateBlock->material,
524 &This->material,
525 sizeof(WINED3DMATERIAL)) != 0) {
526 TRACE("Updating material\n");
527 This->material = targetStateBlock->material;
530 if (This->changed.viewport && memcmp(&targetStateBlock->viewport,
531 &This->viewport,
532 sizeof(WINED3DVIEWPORT)) != 0) {
533 TRACE("Updating viewport\n");
534 This->viewport = targetStateBlock->viewport;
537 if(This->changed.scissorRect && memcmp(&targetStateBlock->scissorRect,
538 &This->scissorRect,
539 sizeof(targetStateBlock->scissorRect)))
541 TRACE("Updating scissor rect\n");
542 targetStateBlock->scissorRect = This->scissorRect;
545 map = This->changed.streamSource;
546 for (i = 0; map; map >>= 1, ++i)
548 if (!(map & 1)) continue;
550 if (This->streamStride[i] != targetStateBlock->streamStride[i]
551 || This->streamSource[i] != targetStateBlock->streamSource[i])
553 TRACE("Updating stream source %u to %p, stride to %u\n",
554 i, targetStateBlock->streamSource[i], targetStateBlock->streamStride[i]);
555 This->streamStride[i] = targetStateBlock->streamStride[i];
556 if(targetStateBlock->streamSource[i]) IWineD3DVertexBuffer_AddRef(targetStateBlock->streamSource[i]);
557 if(This->streamSource[i]) IWineD3DVertexBuffer_Release(This->streamSource[i]);
558 This->streamSource[i] = targetStateBlock->streamSource[i];
562 map = This->changed.streamFreq;
563 for (i = 0; map; map >>= 1, ++i)
565 if (!(map & 1)) continue;
567 if (This->streamFreq[i] != targetStateBlock->streamFreq[i]
568 || This->streamFlags[i] != targetStateBlock->streamFlags[i])
570 TRACE("Updating stream frequency %u to %u flags to %#x\n",
571 i, targetStateBlock->streamFreq[i], targetStateBlock->streamFlags[i]);
572 This->streamFreq[i] = targetStateBlock->streamFreq[i];
573 This->streamFlags[i] = targetStateBlock->streamFlags[i];
577 map = This->changed.clipplane;
578 for (i = 0; map; map >>= 1, ++i)
580 if (!(map & 1)) continue;
582 if (memcmp(targetStateBlock->clipplane[i], This->clipplane[i], sizeof(*This->clipplane)))
584 TRACE("Updating clipplane %u\n", i);
585 memcpy(This->clipplane[i], targetStateBlock->clipplane[i], sizeof(*This->clipplane));
589 /* Render */
590 for (i = 0; i < This->num_contained_render_states; i++) {
591 TRACE("Updating renderState %u to %u\n", This->contained_render_states[i],
592 targetStateBlock->renderState[This->contained_render_states[i]]);
593 This->renderState[This->contained_render_states[i]] = targetStateBlock->renderState[This->contained_render_states[i]];
596 /* Texture states */
597 for (j = 0; j < This->num_contained_tss_states; j++) {
598 DWORD stage = This->contained_tss_states[j].stage;
599 DWORD state = This->contained_tss_states[j].state;
601 TRACE("Updating texturestage state %u, %u to %u (was %u)\n", stage, state,
602 targetStateBlock->textureState[stage][state], This->textureState[stage][state]);
603 This->textureState[stage][state] = targetStateBlock->textureState[stage][state];
606 /* Samplers */
607 /* TODO: move over to using memcpy */
608 map = This->changed.textures;
609 for (i = 0; map; map >>= 1, ++i)
611 if (!(map & 1)) continue;
613 TRACE("Updating texture %u to %p (was %p)\n", i, targetStateBlock->textures[i], This->textures[i]);
614 This->textures[i] = targetStateBlock->textures[i];
617 for (j = 0; j < This->num_contained_sampler_states; j++) {
618 DWORD stage = This->contained_sampler_states[j].stage;
619 DWORD state = This->contained_sampler_states[j].state;
620 TRACE("Updating sampler state %u, %u to %u (was %u)\n", stage, state,
621 targetStateBlock->samplerState[stage][state], This->samplerState[stage][state]);
622 This->samplerState[stage][state] = targetStateBlock->samplerState[stage][state];
624 if(This->changed.pixelShader && This->pixelShader != targetStateBlock->pixelShader) {
625 if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
626 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
627 This->pixelShader = targetStateBlock->pixelShader;
630 record_lights(This, targetStateBlock);
631 } else if(This->blockType == WINED3DSBT_ALL) {
632 This->vertexDecl = targetStateBlock->vertexDecl;
633 memcpy(This->vertexShaderConstantB, targetStateBlock->vertexShaderConstantB, sizeof(This->vertexShaderConstantI));
634 memcpy(This->vertexShaderConstantI, targetStateBlock->vertexShaderConstantI, sizeof(This->vertexShaderConstantF));
635 memcpy(This->vertexShaderConstantF, targetStateBlock->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
636 This->gl_primitive_type = targetStateBlock->gl_primitive_type;
637 memcpy(This->streamStride, targetStateBlock->streamStride, sizeof(This->streamStride));
638 memcpy(This->streamOffset, targetStateBlock->streamOffset, sizeof(This->streamOffset));
639 memcpy(This->streamFreq, targetStateBlock->streamFreq, sizeof(This->streamFreq));
640 memcpy(This->streamFlags, targetStateBlock->streamFlags, sizeof(This->streamFlags));
641 This->baseVertexIndex = targetStateBlock->baseVertexIndex;
642 memcpy(This->transforms, targetStateBlock->transforms, sizeof(This->transforms));
643 record_lights(This, targetStateBlock);
644 memcpy(This->clipplane, targetStateBlock->clipplane, sizeof(This->clipplane));
645 This->clip_status = targetStateBlock->clip_status;
646 This->viewport = targetStateBlock->viewport;
647 This->material = targetStateBlock->material;
648 memcpy(This->pixelShaderConstantB, targetStateBlock->pixelShaderConstantB, sizeof(This->pixelShaderConstantI));
649 memcpy(This->pixelShaderConstantI, targetStateBlock->pixelShaderConstantI, sizeof(This->pixelShaderConstantF));
650 memcpy(This->pixelShaderConstantF, targetStateBlock->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
651 memcpy(This->renderState, targetStateBlock->renderState, sizeof(This->renderState));
652 memcpy(This->textures, targetStateBlock->textures, sizeof(This->textures));
653 memcpy(This->textureState, targetStateBlock->textureState, sizeof(This->textureState));
654 memcpy(This->samplerState, targetStateBlock->samplerState, sizeof(This->samplerState));
655 This->scissorRect = targetStateBlock->scissorRect;
657 if(targetStateBlock->pIndexData != This->pIndexData) {
658 if(targetStateBlock->pIndexData) IWineD3DIndexBuffer_AddRef(targetStateBlock->pIndexData);
659 if(This->pIndexData) IWineD3DIndexBuffer_Release(This->pIndexData);
660 This->pIndexData = targetStateBlock->pIndexData;
662 for(i = 0; i < MAX_STREAMS; i++) {
663 if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
664 if(targetStateBlock->streamSource[i]) IWineD3DVertexBuffer_AddRef(targetStateBlock->streamSource[i]);
665 if(This->streamSource[i]) IWineD3DVertexBuffer_Release(This->streamSource[i]);
666 This->streamSource[i] = targetStateBlock->streamSource[i];
669 if(This->vertexShader != targetStateBlock->vertexShader) {
670 if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
671 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
672 This->vertexShader = targetStateBlock->vertexShader;
674 if(This->pixelShader != targetStateBlock->pixelShader) {
675 if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
676 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
677 This->pixelShader = targetStateBlock->pixelShader;
679 } else if(This->blockType == WINED3DSBT_VERTEXSTATE) {
680 memcpy(This->vertexShaderConstantB, targetStateBlock->vertexShaderConstantB, sizeof(This->vertexShaderConstantI));
681 memcpy(This->vertexShaderConstantI, targetStateBlock->vertexShaderConstantI, sizeof(This->vertexShaderConstantF));
682 memcpy(This->vertexShaderConstantF, targetStateBlock->vertexShaderConstantF, sizeof(float) * GL_LIMITS(vshader_constantsF) * 4);
683 record_lights(This, targetStateBlock);
684 for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
685 This->renderState[SavedVertexStates_R[i]] = targetStateBlock->renderState[SavedVertexStates_R[i]];
687 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
688 for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
689 This->samplerState[j][SavedVertexStates_S[i]] = targetStateBlock->samplerState[j][SavedVertexStates_S[i]];
692 for (j = 0; j < MAX_TEXTURES; j++) {
693 for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
694 This->textureState[j][SavedVertexStates_R[i]] = targetStateBlock->textureState[j][SavedVertexStates_R[i]];
697 for(i = 0; i < MAX_STREAMS; i++) {
698 if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
699 if(targetStateBlock->streamSource[i]) IWineD3DVertexBuffer_AddRef(targetStateBlock->streamSource[i]);
700 if(This->streamSource[i]) IWineD3DVertexBuffer_Release(This->streamSource[i]);
701 This->streamSource[i] = targetStateBlock->streamSource[i];
704 if(This->vertexShader != targetStateBlock->vertexShader) {
705 if(targetStateBlock->vertexShader) IWineD3DVertexShader_AddRef(targetStateBlock->vertexShader);
706 if(This->vertexShader) IWineD3DVertexShader_Release(This->vertexShader);
707 This->vertexShader = targetStateBlock->vertexShader;
709 } else if(This->blockType == WINED3DSBT_PIXELSTATE) {
710 memcpy(This->pixelShaderConstantB, targetStateBlock->pixelShaderConstantB, sizeof(This->pixelShaderConstantI));
711 memcpy(This->pixelShaderConstantI, targetStateBlock->pixelShaderConstantI, sizeof(This->pixelShaderConstantF));
712 memcpy(This->pixelShaderConstantF, targetStateBlock->pixelShaderConstantF, sizeof(float) * GL_LIMITS(pshader_constantsF) * 4);
713 for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
714 This->renderState[SavedPixelStates_R[i]] = targetStateBlock->renderState[SavedPixelStates_R[i]];
716 for (j = 0; j < MAX_COMBINED_SAMPLERS; j++) {
717 for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
718 This->samplerState[j][SavedPixelStates_S[i]] = targetStateBlock->samplerState[j][SavedPixelStates_S[i]];
721 for (j = 0; j < MAX_TEXTURES; j++) {
722 for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
723 This->textureState[j][SavedPixelStates_R[i]] = targetStateBlock->textureState[j][SavedPixelStates_R[i]];
726 if(This->pixelShader != targetStateBlock->pixelShader) {
727 if(targetStateBlock->pixelShader) IWineD3DPixelShader_AddRef(targetStateBlock->pixelShader);
728 if(This->pixelShader) IWineD3DPixelShader_Release(This->pixelShader);
729 This->pixelShader = targetStateBlock->pixelShader;
733 TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
735 return WINED3D_OK;
738 static inline void apply_lights(IWineD3DDevice *pDevice, IWineD3DStateBlockImpl *This) {
739 UINT i;
740 for(i = 0; i < LIGHTMAP_SIZE; i++) {
741 struct list *e;
743 LIST_FOR_EACH(e, &This->lightMap[i]) {
744 const PLIGHTINFOEL *light = LIST_ENTRY(e, PLIGHTINFOEL, entry);
746 if(light->changed) {
747 IWineD3DDevice_SetLight(pDevice, light->OriginalIndex, &light->OriginalParms);
749 if(light->enabledChanged) {
750 IWineD3DDevice_SetLightEnable(pDevice, light->OriginalIndex, light->glIndex != -1);
756 static HRESULT WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
757 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
758 IWineD3DDevice* pDevice = (IWineD3DDevice*)This->wineD3DDevice;
760 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
761 should really perform a delta so that only the changes get updated*/
763 UINT i;
764 UINT j;
765 DWORD map;
767 TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
769 TRACE("Blocktype: %d\n", This->blockType);
771 if(This->blockType == WINED3DSBT_RECORDED) {
772 if (This->changed.vertexShader) {
773 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
775 /* Vertex Shader Constants */
776 for (i = 0; i < This->num_contained_vs_consts_f; i++) {
777 IWineD3DDevice_SetVertexShaderConstantF(pDevice, This->contained_vs_consts_f[i],
778 This->vertexShaderConstantF + This->contained_vs_consts_f[i] * 4, 1);
780 for (i = 0; i < This->num_contained_vs_consts_i; i++) {
781 IWineD3DDevice_SetVertexShaderConstantI(pDevice, This->contained_vs_consts_i[i],
782 This->vertexShaderConstantI + This->contained_vs_consts_i[i] * 4, 1);
784 for (i = 0; i < This->num_contained_vs_consts_b; i++) {
785 IWineD3DDevice_SetVertexShaderConstantB(pDevice, This->contained_vs_consts_b[i],
786 This->vertexShaderConstantB + This->contained_vs_consts_b[i], 1);
789 apply_lights(pDevice, This);
791 if (This->changed.pixelShader) {
792 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
794 /* Pixel Shader Constants */
795 for (i = 0; i < This->num_contained_ps_consts_f; i++) {
796 IWineD3DDevice_SetPixelShaderConstantF(pDevice, This->contained_ps_consts_f[i],
797 This->pixelShaderConstantF + This->contained_ps_consts_f[i] * 4, 1);
799 for (i = 0; i < This->num_contained_ps_consts_i; i++) {
800 IWineD3DDevice_SetPixelShaderConstantI(pDevice, This->contained_ps_consts_i[i],
801 This->pixelShaderConstantI + This->contained_ps_consts_i[i] * 4, 1);
803 for (i = 0; i < This->num_contained_ps_consts_b; i++) {
804 IWineD3DDevice_SetPixelShaderConstantB(pDevice, This->contained_ps_consts_b[i],
805 This->pixelShaderConstantB + This->contained_ps_consts_b[i], 1);
808 /* Render */
809 for (i = 0; i <= This->num_contained_render_states; i++) {
810 IWineD3DDevice_SetRenderState(pDevice, This->contained_render_states[i],
811 This->renderState[This->contained_render_states[i]]);
813 /* Texture states */
814 for (i = 0; i < This->num_contained_tss_states; i++) {
815 DWORD stage = This->contained_tss_states[i].stage;
816 DWORD state = This->contained_tss_states[i].state;
817 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[stage][state] = This->textureState[stage][state];
818 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[stage] |= 1 << state;
819 /* TODO: Record a display list to apply all gl states. For now apply by brute force */
820 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_TEXTURESTAGE(stage, state));
822 /* Sampler states */
823 for (i = 0; i < This->num_contained_sampler_states; i++) {
824 DWORD stage = This->contained_sampler_states[i].stage;
825 DWORD state = This->contained_sampler_states[i].state;
826 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[stage][state] = This->samplerState[stage][state];
827 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[stage] |= 1 << state;
828 IWineD3DDeviceImpl_MarkStateDirty((IWineD3DDeviceImpl *)pDevice, STATE_SAMPLER(stage));
831 for (i = 0; i < This->num_contained_transform_states; i++) {
832 IWineD3DDevice_SetTransform(pDevice, This->contained_transform_states[i],
833 &This->transforms[This->contained_transform_states[i]]);
836 if (This->changed.primitive_type)
838 This->wineD3DDevice->updateStateBlock->changed.primitive_type = TRUE;
839 This->wineD3DDevice->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
842 if (This->changed.indices) {
843 IWineD3DDevice_SetIndices(pDevice, This->pIndexData);
844 IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
847 if (This->changed.vertexDecl) {
848 IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
851 if (This->changed.material ) {
852 IWineD3DDevice_SetMaterial(pDevice, &This->material);
855 if (This->changed.viewport) {
856 IWineD3DDevice_SetViewport(pDevice, &This->viewport);
859 if (This->changed.scissorRect) {
860 IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
863 /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
864 map = This->changed.streamSource;
865 for (i = 0; map; map >>= 1, ++i)
867 if (map & 1) IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
870 map = This->changed.streamFreq;
871 for (i = 0; map; map >>= 1, ++i)
873 if (map & 1) IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
876 map = This->changed.textures;
877 for (i = 0; map; map >>= 1, ++i)
879 if (!(map & 1)) continue;
881 if (i < MAX_FRAGMENT_SAMPLERS) IWineD3DDevice_SetTexture(pDevice, i, This->textures[i]);
882 else IWineD3DDevice_SetTexture(pDevice, WINED3DVERTEXTEXTURESAMPLER0 + i - MAX_FRAGMENT_SAMPLERS,
883 This->textures[i]);
886 map = This->changed.clipplane;
887 for (i = 0; map; map >>= 1, ++i)
889 float clip[4];
891 if (!(map & 1)) continue;
893 clip[0] = This->clipplane[i][0];
894 clip[1] = This->clipplane[i][1];
895 clip[2] = This->clipplane[i][2];
896 clip[3] = This->clipplane[i][3];
897 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
899 } else if(This->blockType == WINED3DSBT_VERTEXSTATE) {
900 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
901 for (i = 0; i < GL_LIMITS(vshader_constantsF); i++) {
902 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i,
903 This->vertexShaderConstantF + i * 4, 1);
905 for (i = 0; i < MAX_CONST_I; i++) {
906 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i,
907 This->vertexShaderConstantI + i * 4, 1);
909 for (i = 0; i < MAX_CONST_B; i++) {
910 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i,
911 This->vertexShaderConstantB + i, 1);
914 apply_lights(pDevice, This);
916 for(i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
917 IWineD3DDevice_SetRenderState(pDevice, SavedVertexStates_R[i], This->renderState[SavedVertexStates_R[i]]);
919 for(j = 0; j < MAX_TEXTURES; j++) {
920 for(i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
921 IWineD3DDevice_SetTextureStageState(pDevice, j, SavedVertexStates_T[i],
922 This->textureState[j][SavedVertexStates_T[i]]);
926 for(j = 0; j < MAX_FRAGMENT_SAMPLERS; j++) {
927 for(i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
928 IWineD3DDevice_SetSamplerState(pDevice, j, SavedVertexStates_S[i],
929 This->samplerState[j][SavedVertexStates_S[i]]);
932 for(j = MAX_FRAGMENT_SAMPLERS; j < MAX_COMBINED_SAMPLERS; j++) {
933 for(i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
934 IWineD3DDevice_SetSamplerState(pDevice,
935 WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS,
936 SavedVertexStates_S[i],
937 This->samplerState[j][SavedVertexStates_S[i]]);
940 } else if(This->blockType == WINED3DSBT_PIXELSTATE) {
941 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
942 for (i = 0; i < GL_LIMITS(pshader_constantsF); i++) {
943 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i,
944 This->pixelShaderConstantF + i * 4, 1);
946 for (i = 0; i < MAX_CONST_I; i++) {
947 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i,
948 This->pixelShaderConstantI + i * 4, 1);
950 for (i = 0; i < MAX_CONST_B; i++) {
951 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i,
952 This->pixelShaderConstantB + i, 1);
955 for(i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
956 IWineD3DDevice_SetRenderState(pDevice, SavedPixelStates_R[i], This->renderState[SavedPixelStates_R[i]]);
958 for(j = 0; j < MAX_TEXTURES; j++) {
959 for(i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
960 IWineD3DDevice_SetTextureStageState(pDevice, j, SavedPixelStates_T[i],
961 This->textureState[j][SavedPixelStates_T[i]]);
965 for(j = 0; j < MAX_FRAGMENT_SAMPLERS; j++) {
966 for(i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
967 IWineD3DDevice_SetSamplerState(pDevice, j, SavedPixelStates_S[i],
968 This->samplerState[j][SavedPixelStates_S[i]]);
971 for(j = MAX_FRAGMENT_SAMPLERS; j < MAX_COMBINED_SAMPLERS; j++) {
972 for(i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
973 IWineD3DDevice_SetSamplerState(pDevice,
974 WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS,
975 SavedPixelStates_S[i],
976 This->samplerState[j][SavedPixelStates_S[i]]);
979 } else if(This->blockType == WINED3DSBT_ALL) {
980 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
981 for (i = 0; i < GL_LIMITS(vshader_constantsF); i++) {
982 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i,
983 This->vertexShaderConstantF + i * 4, 1);
985 for (i = 0; i < MAX_CONST_I; i++) {
986 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i,
987 This->vertexShaderConstantI + i * 4, 1);
989 for (i = 0; i < MAX_CONST_B; i++) {
990 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i,
991 This->vertexShaderConstantB + i, 1);
994 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
995 for (i = 0; i < GL_LIMITS(pshader_constantsF); i++) {
996 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i,
997 This->pixelShaderConstantF + i * 4, 1);
999 for (i = 0; i < MAX_CONST_I; i++) {
1000 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i,
1001 This->pixelShaderConstantI + i * 4, 1);
1003 for (i = 0; i < MAX_CONST_B; i++) {
1004 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i,
1005 This->pixelShaderConstantB + i, 1);
1008 apply_lights(pDevice, This);
1010 for(i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
1011 IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
1013 for(j = 0; j < MAX_TEXTURES; j++) {
1014 for (i = 0; i <= WINED3D_HIGHEST_TEXTURE_STATE; ++i)
1016 IWineD3DDevice_SetTextureStageState(pDevice, j, i, This->textureState[j][i]);
1020 /* Skip unused values between TEXTURE8 and WORLD0 ? */
1021 for(i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
1022 IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
1024 This->wineD3DDevice->updateStateBlock->gl_primitive_type = This->gl_primitive_type;
1025 IWineD3DDevice_SetIndices(pDevice, This->pIndexData);
1026 IWineD3DDevice_SetBaseVertexIndex(pDevice, This->baseVertexIndex);
1027 IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
1028 IWineD3DDevice_SetMaterial(pDevice, &This->material);
1029 IWineD3DDevice_SetViewport(pDevice, &This->viewport);
1030 IWineD3DDevice_SetScissorRect(pDevice, &This->scissorRect);
1032 /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
1033 for (i=0; i<MAX_STREAMS; i++) {
1034 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
1035 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
1037 for (j = 0 ; j < MAX_COMBINED_SAMPLERS; j++){
1038 UINT sampler = j < MAX_FRAGMENT_SAMPLERS ? j : WINED3DVERTEXTEXTURESAMPLER0 + j - MAX_FRAGMENT_SAMPLERS;
1040 IWineD3DDevice_SetTexture(pDevice, sampler, This->textures[j]);
1041 for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; ++i)
1043 IWineD3DDevice_SetSamplerState(pDevice, sampler, i, This->samplerState[j][i]);
1046 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
1047 float clip[4];
1049 clip[0] = This->clipplane[i][0];
1050 clip[1] = This->clipplane[i][1];
1051 clip[2] = This->clipplane[i][2];
1052 clip[3] = This->clipplane[i][3];
1053 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
1057 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = MAX_TEXTURES - 1;
1058 for(j = 0; j < MAX_TEXTURES - 1; j++) {
1059 if(((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][WINED3DTSS_COLOROP] == WINED3DTOP_DISABLE) {
1060 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->lowest_disabled_stage = j;
1061 break;
1064 TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
1066 return WINED3D_OK;
1069 static HRESULT WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
1070 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
1071 IWineD3DDevice *device = (IWineD3DDevice *)This->wineD3DDevice;
1072 IWineD3DDeviceImpl *ThisDevice = (IWineD3DDeviceImpl *)device;
1073 union {
1074 WINED3DLINEPATTERN lp;
1075 DWORD d;
1076 } lp;
1077 union {
1078 float f;
1079 DWORD d;
1080 } tmpfloat;
1081 unsigned int i;
1082 IWineD3DSwapChain *swapchain;
1083 IWineD3DSurface *backbuffer;
1084 WINED3DSURFACE_DESC desc = {0};
1085 UINT width, height;
1086 RECT scissorrect;
1087 HRESULT hr;
1089 /* Note this may have a large overhead but it should only be executed
1090 once, in order to initialize the complete state of the device and
1091 all opengl equivalents */
1092 TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
1093 /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
1094 This->blockType = WINED3DSBT_INIT;
1096 /* Set some of the defaults for lights, transforms etc */
1097 memcpy(&This->transforms[WINED3DTS_PROJECTION], identity, sizeof(identity));
1098 memcpy(&This->transforms[WINED3DTS_VIEW], identity, sizeof(identity));
1099 for (i = 0; i < 256; ++i) {
1100 memcpy(&This->transforms[WINED3DTS_WORLDMATRIX(i)], identity, sizeof(identity));
1103 TRACE("Render states\n");
1104 /* Render states: */
1105 if (ThisDevice->auto_depth_stencil_buffer != NULL) {
1106 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_TRUE);
1107 } else {
1108 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, WINED3DZB_FALSE);
1110 IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE, WINED3DFILL_SOLID);
1111 IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE, WINED3DSHADE_GOURAUD);
1112 lp.lp.wRepeatFactor = 0;
1113 lp.lp.wLinePattern = 0;
1114 IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN, lp.d);
1115 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE, TRUE);
1116 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE, FALSE);
1117 IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL, TRUE);
1118 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND, WINED3DBLEND_ONE);
1119 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND, WINED3DBLEND_ZERO);
1120 IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE, WINED3DCULL_CCW);
1121 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC, WINED3DCMP_LESSEQUAL);
1122 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC, WINED3DCMP_ALWAYS);
1123 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF, 0);
1124 IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE, FALSE);
1125 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
1126 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE, FALSE);
1127 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE, FALSE);
1128 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE, 0);
1129 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR, 0);
1130 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE, WINED3DFOG_NONE);
1131 tmpfloat.f = 0.0f;
1132 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART, tmpfloat.d);
1133 tmpfloat.f = 1.0f;
1134 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND, tmpfloat.d);
1135 tmpfloat.f = 1.0f;
1136 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY, tmpfloat.d);
1137 IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS, FALSE);
1138 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS, 0);
1139 IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE, FALSE);
1140 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE, FALSE);
1141 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL, WINED3DSTENCILOP_KEEP);
1142 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
1143 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS, WINED3DSTENCILOP_KEEP);
1144 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
1145 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
1146 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC, WINED3DCMP_ALWAYS);
1147 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
1148 IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
1149 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
1150 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
1151 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
1152 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
1153 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
1154 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
1155 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
1156 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
1157 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING, TRUE);
1158 IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING, TRUE);
1159 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT, 0);
1160 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE, WINED3DFOG_NONE);
1161 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX, TRUE);
1162 IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER, TRUE);
1163 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS, FALSE);
1164 IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE, WINED3DMCS_COLOR1);
1165 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE, WINED3DMCS_COLOR2);
1166 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE, WINED3DMCS_MATERIAL);
1167 IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE, WINED3DMCS_MATERIAL);
1168 IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND, WINED3DVBF_DISABLE);
1169 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE, 0);
1170 IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
1171 tmpfloat.f = 1.0f;
1172 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE, tmpfloat.d);
1173 tmpfloat.f = ((IWineD3DImpl *)This->wineD3DDevice->wineD3D)->dxVersion < 9 ? 0.0f : 1.0f;
1174 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN, tmpfloat.d);
1175 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE, FALSE);
1176 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE, FALSE);
1177 tmpfloat.f = 1.0f;
1178 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A, tmpfloat.d);
1179 tmpfloat.f = 0.0f;
1180 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B, tmpfloat.d);
1181 tmpfloat.f = 0.0f;
1182 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C, tmpfloat.d);
1183 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS, TRUE);
1184 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
1185 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE, WINED3DPATCHEDGE_DISCRETE);
1186 tmpfloat.f = 1.0f;
1187 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS, tmpfloat.d);
1188 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN, 0xbaadcafe);
1189 tmpfloat.f = GL_LIMITS(pointsize);
1190 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX, tmpfloat.d);
1191 IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
1192 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE, 0x0000000F);
1193 tmpfloat.f = 0.0f;
1194 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR, tmpfloat.d);
1195 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP, WINED3DBLENDOP_ADD);
1196 IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE, WINED3DDEGREE_CUBIC);
1197 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE, WINED3DDEGREE_LINEAR);
1198 /* states new in d3d9 */
1199 IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE, FALSE);
1200 IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS, 0);
1201 tmpfloat.f = 1.0f;
1202 IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL, tmpfloat.d);
1203 IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL, tmpfloat.d);
1204 IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE, FALSE);
1205 tmpfloat.f = 0.0f;
1206 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X, tmpfloat.d);
1207 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y, tmpfloat.d);
1208 tmpfloat.f = 1.0f;
1209 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z, tmpfloat.d);
1210 tmpfloat.f = 0.0f;
1211 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W, tmpfloat.d);
1212 IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
1213 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE, FALSE);
1214 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL, WINED3DSTENCILOP_KEEP);
1215 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL, WINED3DSTENCILOP_KEEP);
1216 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS, WINED3DSTENCILOP_KEEP);
1217 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC, WINED3DCMP_ALWAYS);
1218 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1, 0x0000000F);
1219 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2, 0x0000000F);
1220 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3, 0x0000000F);
1221 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR, 0xFFFFFFFF);
1222 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE, 0);
1223 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS, 0);
1224 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8, 0);
1225 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9, 0);
1226 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
1227 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
1228 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
1229 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
1230 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
1231 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
1232 IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
1233 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA, WINED3DBLEND_ONE);
1234 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA, WINED3DBLEND_ZERO);
1235 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA, WINED3DBLENDOP_ADD);
1237 /* clipping status */
1238 This->clip_status.ClipUnion = 0;
1239 This->clip_status.ClipIntersection = 0xFFFFFFFF;
1241 /* Texture Stage States - Put directly into state block, we will call function below */
1242 for (i = 0; i < MAX_TEXTURES; i++) {
1243 TRACE("Setting up default texture states for texture Stage %d\n", i);
1244 memcpy(&This->transforms[WINED3DTS_TEXTURE0 + i], identity, sizeof(identity));
1245 This->textureState[i][WINED3DTSS_COLOROP ] = (i==0)? WINED3DTOP_MODULATE : WINED3DTOP_DISABLE;
1246 This->textureState[i][WINED3DTSS_COLORARG1 ] = WINED3DTA_TEXTURE;
1247 This->textureState[i][WINED3DTSS_COLORARG2 ] = WINED3DTA_CURRENT;
1248 This->textureState[i][WINED3DTSS_ALPHAOP ] = (i==0)? WINED3DTOP_SELECTARG1 : WINED3DTOP_DISABLE;
1249 This->textureState[i][WINED3DTSS_ALPHAARG1 ] = WINED3DTA_TEXTURE;
1250 This->textureState[i][WINED3DTSS_ALPHAARG2 ] = WINED3DTA_CURRENT;
1251 This->textureState[i][WINED3DTSS_BUMPENVMAT00 ] = 0;
1252 This->textureState[i][WINED3DTSS_BUMPENVMAT01 ] = 0;
1253 This->textureState[i][WINED3DTSS_BUMPENVMAT10 ] = 0;
1254 This->textureState[i][WINED3DTSS_BUMPENVMAT11 ] = 0;
1255 This->textureState[i][WINED3DTSS_TEXCOORDINDEX ] = i;
1256 This->textureState[i][WINED3DTSS_BUMPENVLSCALE ] = 0;
1257 This->textureState[i][WINED3DTSS_BUMPENVLOFFSET ] = 0;
1258 This->textureState[i][WINED3DTSS_TEXTURETRANSFORMFLAGS ] = WINED3DTTFF_DISABLE;
1259 This->textureState[i][WINED3DTSS_COLORARG0 ] = WINED3DTA_CURRENT;
1260 This->textureState[i][WINED3DTSS_ALPHAARG0 ] = WINED3DTA_CURRENT;
1261 This->textureState[i][WINED3DTSS_RESULTARG ] = WINED3DTA_CURRENT;
1263 This->lowest_disabled_stage = 1;
1265 /* Sampler states*/
1266 for (i = 0 ; i < MAX_COMBINED_SAMPLERS; i++) {
1267 TRACE("Setting up default samplers states for sampler %d\n", i);
1268 This->samplerState[i][WINED3DSAMP_ADDRESSU ] = WINED3DTADDRESS_WRAP;
1269 This->samplerState[i][WINED3DSAMP_ADDRESSV ] = WINED3DTADDRESS_WRAP;
1270 This->samplerState[i][WINED3DSAMP_ADDRESSW ] = WINED3DTADDRESS_WRAP;
1271 This->samplerState[i][WINED3DSAMP_BORDERCOLOR ] = 0x00;
1272 This->samplerState[i][WINED3DSAMP_MAGFILTER ] = WINED3DTEXF_POINT;
1273 This->samplerState[i][WINED3DSAMP_MINFILTER ] = WINED3DTEXF_POINT;
1274 This->samplerState[i][WINED3DSAMP_MIPFILTER ] = WINED3DTEXF_NONE;
1275 This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS ] = 0;
1276 This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL ] = 0;
1277 This->samplerState[i][WINED3DSAMP_MAXANISOTROPY ] = 1;
1278 This->samplerState[i][WINED3DSAMP_SRGBTEXTURE ] = 0;
1279 This->samplerState[i][WINED3DSAMP_ELEMENTINDEX ] = 0; /* TODO: Indicates which element of a multielement texture to use */
1280 This->samplerState[i][WINED3DSAMP_DMAPOFFSET ] = 0; /* TODO: Vertex offset in the presampled displacement map */
1283 for(i = 0; i < GL_LIMITS(textures); i++) {
1284 /* Note: This avoids calling SetTexture, so pretend it has been called */
1285 This->changed.textures |= 1 << i;
1286 This->textures[i] = NULL;
1289 /* Set the default scissor rect values */
1290 desc.Width = &width;
1291 desc.Height = &height;
1293 /* check the return values, because the GetBackBuffer call isn't valid for ddraw */
1294 hr = IWineD3DDevice_GetSwapChain(device, 0, &swapchain);
1295 if( hr == WINED3D_OK && swapchain != NULL) {
1296 WINED3DVIEWPORT vp;
1298 hr = IWineD3DSwapChain_GetBackBuffer(swapchain, 0, WINED3DBACKBUFFER_TYPE_MONO, &backbuffer);
1299 if( hr == WINED3D_OK && backbuffer != NULL) {
1300 IWineD3DSurface_GetDesc(backbuffer, &desc);
1301 IWineD3DSurface_Release(backbuffer);
1303 scissorrect.left = 0;
1304 scissorrect.right = width;
1305 scissorrect.top = 0;
1306 scissorrect.bottom = height;
1307 hr = IWineD3DDevice_SetScissorRect(device, &scissorrect);
1308 if( hr != WINED3D_OK ) {
1309 ERR("This should never happen, expect rendering issues!\n");
1313 /* Set the default viewport */
1314 vp.X = 0;
1315 vp.Y = 0;
1316 vp.Width = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferWidth;
1317 vp.Height = ((IWineD3DSwapChainImpl *)swapchain)->presentParms.BackBufferHeight;
1318 vp.MinZ = 0.0f;
1319 vp.MaxZ = 1.0f;
1320 IWineD3DDevice_SetViewport(device, &vp);
1322 IWineD3DSwapChain_Release(swapchain);
1325 TRACE("-----------------------> Device defaults now set up...\n");
1326 return WINED3D_OK;
1329 /**********************************************************
1330 * IWineD3DStateBlock VTbl follows
1331 **********************************************************/
1333 const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
1335 /* IUnknown */
1336 IWineD3DStateBlockImpl_QueryInterface,
1337 IWineD3DStateBlockImpl_AddRef,
1338 IWineD3DStateBlockImpl_Release,
1339 /* IWineD3DStateBlock */
1340 IWineD3DStateBlockImpl_GetParent,
1341 IWineD3DStateBlockImpl_GetDevice,
1342 IWineD3DStateBlockImpl_Capture,
1343 IWineD3DStateBlockImpl_Apply,
1344 IWineD3DStateBlockImpl_InitStartupStateBlock