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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->wineD3DDevice)->wineD3D))->gl_info
29 /**********************************************************
30 * IWineD3DStateBlockImpl IUnknown parts follows
31 **********************************************************/
32 HRESULT WINAPI IWineD3DStateBlockImpl_QueryInterface(IWineD3DStateBlock *iface,REFIID riid,LPVOID *ppobj)
34 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
35 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
36 if (IsEqualGUID(riid, &IID_IUnknown)
37 || IsEqualGUID(riid, &IID_IWineD3DStateBlock)){
38 IUnknown_AddRef(iface);
45 ULONG WINAPI IWineD3DStateBlockImpl_AddRef(IWineD3DStateBlock *iface) {
46 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
47 ULONG refCount = InterlockedIncrement(&This->ref);
49 TRACE("(%p) : AddRef increasing from %ld\n", This, refCount - 1);
53 ULONG WINAPI IWineD3DStateBlockImpl_Release(IWineD3DStateBlock *iface) {
54 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
55 ULONG refCount = InterlockedDecrement(&This->ref);
57 TRACE("(%p) : Releasing from %ld\n", This, refCount + 1);
60 /* type 0 represents the primary stateblock, so free all the resources */
61 if (This->blockType == WINED3DSBT_INIT) {
63 FIXME("Releasing primary stateblock\n");
64 /* Free any streams still bound */
65 for (counter = 0 ; counter < MAX_STREAMS ; counter++) {
66 if (This->streamSource[counter] != NULL) {
67 IWineD3DVertexBuffer_Release(This->streamSource[counter]);
68 This->streamSource[counter] = NULL;
72 /* free any index data */
73 if (This->pIndexData) {
74 IWineD3DIndexBuffer_Release(This->pIndexData);
75 This->pIndexData = NULL;
78 if (NULL != This->pixelShader) {
79 IWineD3DPixelShader_Release(This->pixelShader);
82 if (NULL != This->vertexShader) {
83 IWineD3DVertexShader_Release(This->vertexShader);
86 /* NOTE: according to MSDN: The applicaion is responsible for making sure the texture references are cleared down */
87 for (counter = 0; counter < GL_LIMITS(textures); counter++) {
88 if (This->textures[counter]) {
89 /* release our 'internal' hold on the texture */
90 if(0 != IWineD3DBaseTexture_Release(This->textures[counter])) {
91 TRACE("Texture still referenced by stateblock, applications has leaked Stage = %u Texture = %p \n", counter, This->textures[counter]);
97 HeapFree(GetProcessHeap(), 0, This);
102 /**********************************************************
103 * IWineD3DStateBlockImpl parts follows
104 **********************************************************/
105 HRESULT WINAPI IWineD3DStateBlockImpl_GetParent(IWineD3DStateBlock *iface, IUnknown **pParent) {
106 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
107 IUnknown_AddRef(This->parent);
108 *pParent = This->parent;
112 HRESULT WINAPI IWineD3DStateBlockImpl_GetDevice(IWineD3DStateBlock *iface, IWineD3DDevice** ppDevice){
114 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
116 *ppDevice = (IWineD3DDevice*)This->wineD3DDevice;
117 IWineD3DDevice_AddRef(*ppDevice);
122 HRESULT WINAPI IWineD3DStateBlockImpl_Capture(IWineD3DStateBlock *iface){
124 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
125 IWineD3DStateBlockImpl *targetStateBlock = This->wineD3DDevice->stateBlock;
127 TRACE("(%p) : Updating state block %p ------------------v\n", targetStateBlock, This);
129 /* If not recorded, then update can just recapture */
130 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED */ 0) {
131 IWineD3DStateBlockImpl* tmpBlock;
132 PLIGHTINFOEL *tmp = This->lights;
134 IWineD3DDevice_CreateStateBlock((IWineD3DDevice *)This->wineD3DDevice, This->blockType, (IWineD3DStateBlock**) &tmpBlock, NULL/*parent*/);
136 /* Note just swap the light chains over so when deleting, the old one goes */
137 memcpy(This, tmpBlock, sizeof(IWineD3DStateBlockImpl));
138 tmpBlock->lights = tmp;
140 /* Delete the temporary one (which points to the old light chain though */
141 IWineD3DStateBlock_Release((IWineD3DStateBlock *)tmpBlock);
142 /*IDirect3DDevice_DeleteStateBlock(pDevice, tmpBlock);*/
149 /* Recorded => Only update 'changed' values */
150 if (This->vertexShader != targetStateBlock->vertexShader) {
151 This->vertexShader = targetStateBlock->vertexShader;
152 TRACE("Updating vertex shader to %p\n", targetStateBlock->vertexShader);
155 /* Vertex Shader Constants */
156 for (i = 0; i < MAX_VSHADER_CONSTANTS; ++i) {
157 if (This->set.vertexShaderConstants[i]) {
158 TRACE("Setting %p from %p %d to %f\n", This, targetStateBlock, i, targetStateBlock->vertexShaderConstantF[i * 4 + 1]);
159 This->vertexShaderConstantB[i] = targetStateBlock->vertexShaderConstantB[i];
160 This->vertexShaderConstantF[i * 4] = targetStateBlock->vertexShaderConstantF[i * 4];
161 This->vertexShaderConstantF[i * 4 + 1] = targetStateBlock->vertexShaderConstantF[i * 4 + 1];
162 This->vertexShaderConstantF[i * 4 + 2] = targetStateBlock->vertexShaderConstantF[i * 4 + 2];
163 This->vertexShaderConstantF[i * 4 + 3] = targetStateBlock->vertexShaderConstantF[i * 4 + 3];
164 This->vertexShaderConstantI[i * 4] = targetStateBlock->vertexShaderConstantI[i * 4];
165 This->vertexShaderConstantI[i * 4 + 1] = targetStateBlock->vertexShaderConstantI[i * 4 + 1];
166 This->vertexShaderConstantI[i * 4 + 2] = targetStateBlock->vertexShaderConstantI[i * 4 + 2];
167 This->vertexShaderConstantI[i * 4 + 3] = targetStateBlock->vertexShaderConstantI[i * 4 + 3];
168 This->vertexShaderConstantT[i] = targetStateBlock->vertexShaderConstantT[i];
172 /* Lights... For a recorded state block, we just had a chain of actions to perform,
173 so we need to walk that chain and update any actions which differ */
175 while (src != NULL) {
176 PLIGHTINFOEL *realLight = NULL;
178 /* Locate the light in the live lights */
179 realLight = targetStateBlock->lights;
180 while (realLight != NULL && realLight->OriginalIndex != src->OriginalIndex) realLight = realLight->next;
182 if (realLight == NULL) {
183 FIXME("A captured light no longer exists...?\n");
186 /* If 'changed' then its a SetLight command. Rather than comparing to see
187 if the OriginalParms have changed and then copy them (twice through
188 memory) just do the copy */
190 TRACE("Updating lights for light %ld\n", src->OriginalIndex);
191 memcpy(&src->OriginalParms, &realLight->OriginalParms, sizeof(PLIGHTINFOEL));
194 /* If 'enabledchanged' then its a LightEnable command */
195 if (src->enabledChanged) {
196 TRACE("Updating lightEnabled for light %ld\n", src->OriginalIndex);
197 src->lightEnabled = realLight->lightEnabled;
205 /* Recorded => Only update 'changed' values */
206 if (This->pixelShader != targetStateBlock->pixelShader) {
207 This->pixelShader = targetStateBlock->pixelShader;
208 TRACE("Updating pixrl shader to %p\n", targetStateBlock->pixelShader);
211 /* Pixel Shader Constants */
212 for (i = 0; i < MAX_PSHADER_CONSTANTS; ++i) {
213 if (This->set.pixelShaderConstants[i]) {
214 TRACE("Setting %p from %p %d to %f\n", This, targetStateBlock, i, targetStateBlock->pixelShaderConstantF[i * 4 + 1]);
215 This->pixelShaderConstantB[i] = targetStateBlock->pixelShaderConstantB[i];
216 This->pixelShaderConstantF[i * 4] = targetStateBlock->pixelShaderConstantF[i * 4];
217 This->pixelShaderConstantF[i * 4 + 1] = targetStateBlock->pixelShaderConstantF[i * 4 + 1];
218 This->pixelShaderConstantF[i * 4 + 2] = targetStateBlock->pixelShaderConstantF[i * 4 + 2];
219 This->pixelShaderConstantF[i * 4 + 3] = targetStateBlock->pixelShaderConstantF[i * 4 + 3];
220 This->pixelShaderConstantI[i * 4] = targetStateBlock->pixelShaderConstantI[i * 4];
221 This->pixelShaderConstantI[i * 4 + 1] = targetStateBlock->pixelShaderConstantI[i * 4 + 1];
222 This->pixelShaderConstantI[i * 4 + 2] = targetStateBlock->pixelShaderConstantI[i * 4 + 2];
223 This->pixelShaderConstantI[i * 4 + 3] = targetStateBlock->pixelShaderConstantI[i * 4 + 3];
224 This->pixelShaderConstantT[i] = targetStateBlock->pixelShaderConstantT[i];
228 /* Others + Render & Texture */
229 for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
230 if (This->set.transform[i] && memcmp(&targetStateBlock->transforms[i],
231 &This->transforms[i],
232 sizeof(D3DMATRIX)) != 0) {
233 TRACE("Updating transform %d\n", i);
234 memcpy(&This->transforms[i], &targetStateBlock->transforms[i], sizeof(D3DMATRIX));
238 if (This->set.indices && ((This->pIndexData != targetStateBlock->pIndexData)
239 || (This->baseVertexIndex != targetStateBlock->baseVertexIndex))) {
240 TRACE("Updating pindexData to %p, baseVertexIndex to %d\n",
241 targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
242 This->pIndexData = targetStateBlock->pIndexData;
243 This->baseVertexIndex = targetStateBlock->baseVertexIndex;
246 if(This->set.vertexDecl && This->vertexDecl != targetStateBlock->vertexDecl){
247 This->vertexDecl = targetStateBlock->vertexDecl;
250 if(This->set.fvf && This->fvf != targetStateBlock->fvf){
251 This->fvf = targetStateBlock->fvf;
254 if (This->set.material && memcmp(&targetStateBlock->material,
256 sizeof(D3DMATERIAL9)) != 0) {
257 TRACE("Updating material\n");
258 memcpy(&This->material, &targetStateBlock->material, sizeof(D3DMATERIAL9));
261 if (This->set.viewport && memcmp(&targetStateBlock->viewport,
263 sizeof(D3DVIEWPORT9)) != 0) {
264 TRACE("Updating viewport\n");
265 memcpy(&This->viewport, &targetStateBlock->viewport, sizeof(D3DVIEWPORT9));
268 for (i = 0; i < MAX_STREAMS; i++) {
269 if (This->set.streamSource[i] &&
270 ((This->streamStride[i] != targetStateBlock->streamStride[i]) ||
271 (This->streamSource[i] != targetStateBlock->streamSource[i]))) {
272 TRACE("Updating stream source %d to %p, stride to %d\n", i, targetStateBlock->streamSource[i],
273 targetStateBlock->streamStride[i]);
274 This->streamStride[i] = targetStateBlock->streamStride[i];
275 This->streamSource[i] = targetStateBlock->streamSource[i];
278 if (This->set.streamFreq[i] &&
279 (This->streamFreq[i] != targetStateBlock->streamFreq[i]
280 || This->streamFlags[i] != targetStateBlock->streamFlags[i])){
281 TRACE("Updating stream frequency %d to %d flags to %d\n", i , targetStateBlock->streamFreq[i] ,
282 targetStateBlock->streamFlags[i]);
283 This->streamFreq[i] = targetStateBlock->streamFreq[i];
284 This->streamFlags[i] = targetStateBlock->streamFlags[i];
288 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
289 if (This->set.clipplane[i] && memcmp(&targetStateBlock->clipplane[i],
291 sizeof(This->clipplane)) != 0) {
293 TRACE("Updating clipplane %d\n", i);
294 memcpy(&This->clipplane[i], &targetStateBlock->clipplane[i],
295 sizeof(This->clipplane));
300 for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
302 if (This->set.renderState[i] && (This->renderState[i] != targetStateBlock->renderState[i])) {
303 TRACE("Updating renderState %d to %ld\n", i, targetStateBlock->renderState[i]);
304 This->renderState[i] = targetStateBlock->renderState[i];
308 /* FIXME: textures are up to MAX_SAMPLERS for d3d9? */
310 for (j = 0; j < GL_LIMITS(textures); j++) {
311 /* TODO: move over to using memcpy */
312 for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE ; i++) {
313 if (This->set.textureState[j][i]) {
314 TRACE("Updating texturestagestate %d,%d to %ld (was %ld)\n", j,i, targetStateBlock->textureState[j][i],
315 This->textureState[j][i]);
316 This->textureState[j][i] = targetStateBlock->textureState[j][i];
320 if (This->set.textures[j]) {
321 TRACE("Updating texture %d to %p (was %p)\n", j, targetStateBlock->textures[j], This->textures[j]);
322 This->textures[j] = targetStateBlock->textures[j];
328 /* TODO: move over to using memcpy */
329 for (j = 0 ; j < GL_LIMITS(samplers); j++){
330 for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE ; i++){ /* States are 1 based */
331 if (This->set.samplerState[j][i]) {
332 TRACE("Updating sampler state %d,%d to %ld (was %ld)\n",
333 j, i, targetStateBlock->samplerState[j][i],
334 This->samplerState[j][i]);
335 This->samplerState[j][i] = targetStateBlock->samplerState[j][i];
341 TRACE("(%p) : Updated state block %p ------------------^\n", targetStateBlock, This);
346 HRESULT WINAPI IWineD3DStateBlockImpl_Apply(IWineD3DStateBlock *iface){
347 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
348 IWineD3DDevice* pDevice = (IWineD3DDevice*)This->wineD3DDevice;
350 /*Copy thing over to updateBlock is isRecording otherwise StateBlock,
351 should really perform a delta so that only the changes get updated*/
357 TRACE("(%p) : Applying state block %p ------------------v\n", This, pDevice);
359 /* FIXME: Only apply applicable states not all states */
361 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */This->blockType == WINED3DSBT_INIT || This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_VERTEXSTATE) {
364 PLIGHTINFOEL *toDo = This->lights;
365 while (toDo != NULL) {
367 IWineD3DDevice_SetLight(pDevice, toDo->OriginalIndex, &toDo->OriginalParms);
368 if (toDo->enabledChanged)
369 IWineD3DDevice_SetLightEnable(pDevice, toDo->OriginalIndex, toDo->lightEnabled);
374 if (This->set.vertexShader && This->changed.vertexShader) {
375 IWineD3DDevice_SetVertexShader(pDevice, This->vertexShader);
378 /* Vertex Shader Constants */
379 for (i = 0; i < MAX_VSHADER_CONSTANTS; ++i) {
380 if (This->set.vertexShaderConstants[i] && This->changed.vertexShaderConstants[i]) {
381 switch (This->vertexShaderConstantT[i]) {
382 case WINESHADERCNST_FLOAT:
383 IWineD3DDevice_SetVertexShaderConstantF(pDevice, i, This->vertexShaderConstantF + i * 4, 1);
385 case WINESHADERCNST_BOOL:
386 IWineD3DDevice_SetVertexShaderConstantB(pDevice, i, This->vertexShaderConstantB + i, 1);
388 case WINESHADERCNST_INTEGER:
389 IWineD3DDevice_SetVertexShaderConstantI(pDevice, i, This->vertexShaderConstantI + i * 4, 1);
391 case WINESHADERCNST_NONE:
392 IWineD3DDevice_SetVertexShaderConstantN(pDevice, i, 1);
400 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == D3DSBT_ALL || This->blockType == D3DSBT_PIXELSTATE) {
403 if (This->set.pixelShader && This->changed.pixelShader) {
404 IWineD3DDevice_SetPixelShader(pDevice, This->pixelShader);
407 /* Pixel Shader Constants */
408 for (i = 0; i < MAX_PSHADER_CONSTANTS; ++i) {
409 if (This->set.pixelShaderConstants[i] && This->changed.pixelShaderConstants[i]) {
410 switch (This->pixelShaderConstantT[i]) {
411 case WINESHADERCNST_FLOAT:
412 IWineD3DDevice_SetPixelShaderConstantF(pDevice, i, This->pixelShaderConstantF + i * 4, 1);
414 case WINESHADERCNST_BOOL:
415 IWineD3DDevice_SetPixelShaderConstantB(pDevice, i, This->pixelShaderConstantB + i, 1);
417 case WINESHADERCNST_INTEGER:
418 IWineD3DDevice_SetPixelShaderConstantI(pDevice, i, This->pixelShaderConstantI + i * 4, 1);
420 case WINESHADERCNST_NONE:
421 IWineD3DDevice_SetPixelShaderConstantN(pDevice, i, 1);
428 if (This->set.fvf && This->changed.fvf) {
429 IWineD3DDevice_SetFVF(pDevice, This->fvf);
432 if (This->set.vertexDecl && This->changed.vertexDecl) {
433 IWineD3DDevice_SetVertexDeclaration(pDevice, This->vertexDecl);
436 /* Others + Render & Texture */
437 if (/*TODO: 'magic' statetype, replace with BOOL This->blockType == D3DSBT_RECORDED || */ This->blockType == WINED3DSBT_ALL || This->blockType == WINED3DSBT_INIT) {
438 for (i = 1; i <= HIGHEST_TRANSFORMSTATE; i++) {
439 if (This->set.transform[i] && This->changed.transform[i])
440 IWineD3DDevice_SetTransform(pDevice, i, &This->transforms[i]);
443 if (This->set.indices && This->changed.indices)
444 IWineD3DDevice_SetIndices(pDevice, This->pIndexData, This->baseVertexIndex);
446 if (This->set.material && This->changed.material )
447 IWineD3DDevice_SetMaterial(pDevice, &This->material);
449 if (This->set.viewport && This->changed.viewport)
450 IWineD3DDevice_SetViewport(pDevice, &This->viewport);
452 /* TODO: Proper implementation using SetStreamSource offset (set to 0 for the moment)\n") */
453 for (i=0; i<MAX_STREAMS; i++) {
454 if (This->set.streamSource[i] && This->changed.streamSource[i])
455 IWineD3DDevice_SetStreamSource(pDevice, i, This->streamSource[i], 0, This->streamStride[i]);
457 if (This->set.streamFreq[i] && This->changed.streamFreq[i])
458 IWineD3DDevice_SetStreamSourceFreq(pDevice, i, This->streamFreq[i] | This->streamFlags[i]);
461 for (i = 0; i < GL_LIMITS(clipplanes); i++) {
462 if (This->set.clipplane[i] && This->changed.clipplane[i]) {
465 clip[0] = This->clipplane[i][0];
466 clip[1] = This->clipplane[i][1];
467 clip[2] = This->clipplane[i][2];
468 clip[3] = This->clipplane[i][3];
469 IWineD3DDevice_SetClipPlane(pDevice, i, clip);
474 for (i = 1; i <= WINEHIGHEST_RENDER_STATE; i++) {
475 if (This->set.renderState[i] && This->changed.renderState[i])
476 IWineD3DDevice_SetRenderState(pDevice, i, This->renderState[i]);
479 /* FIXME: Texture are set against samplers... not just TextureStages */
481 for (j = 0; j < GL_LIMITS(textures); j++) { /* Set The texture first, just in case it resets the states? */
482 if (This->set.textures[j] && This->changed.textures[j]) {
483 IWineD3DDevice_SetTexture(pDevice, j, This->textures[j]);
485 /* TODO: move over to memcpy */
486 for (i = 1; i <= WINED3D_HIGHEST_TEXTURE_STATE; i++) {
487 if (This->set.textureState[j][i] && This->changed.textureState[j][i]) { /* tb_dx9_10 failes without this test */
488 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][i] = This->textureState[j][i];
489 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.textureState[j][i] = TRUE;
490 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.textureState[j][i] = TRUE;
496 /* TODO: move over to memcpy */
497 for (j = 0 ; j < GL_LIMITS(samplers); j++){
498 for (i = 1; i <= WINED3D_HIGHEST_SAMPLER_STATE; i++){
499 if (This->set.samplerState[j][i] && This->changed.samplerState[j][i]) {
500 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][i] = This->samplerState[j][i];
501 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->set.samplerState[j][i] = TRUE;
502 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->changed.samplerState[j][i] = TRUE;
508 } else if (This->blockType == WINED3DSBT_PIXELSTATE) {
510 for (i = 0; i < NUM_SAVEDPIXELSTATES_R; i++) {
511 if (This->set.renderState[SavedPixelStates_R[i]] && This->changed.renderState[SavedPixelStates_R[i]])
512 IWineD3DDevice_SetRenderState(pDevice, SavedPixelStates_R[i], This->renderState[SavedPixelStates_R[i]]);
516 for (j = 0; j < GL_LIMITS(textures); j++) {
517 for (i = 0; i < NUM_SAVEDPIXELSTATES_T; i++) {
518 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedPixelStates_T[i]] = This->textureState[j][SavedPixelStates_T[i]];
522 for (j = 0; j < GL_LIMITS(samplers); j++) {
523 for (i = 0; i < NUM_SAVEDPIXELSTATES_S; i++) {
524 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedPixelStates_S[i]] = This->samplerState[j][SavedPixelStates_S[i]];
528 } else if (This->blockType == WINED3DSBT_VERTEXSTATE) {
530 for (i = 0; i < NUM_SAVEDVERTEXSTATES_R; i++) {
531 if ( This->set.renderState[SavedVertexStates_R[i]] && This->changed.renderState[SavedVertexStates_R[i]])
532 IWineD3DDevice_SetRenderState(pDevice, SavedVertexStates_R[i], This->renderState[SavedVertexStates_R[i]]);
535 for (j = 0; j < GL_LIMITS(textures); j++) {
536 for (i = 0; i < NUM_SAVEDVERTEXSTATES_T; i++) {
537 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->textureState[j][SavedVertexStates_T[i]] = This->textureState[j][SavedVertexStates_T[i]];
541 for (j = 0; j < GL_LIMITS(textures); j++) {
542 for (i = 0; i < NUM_SAVEDVERTEXSTATES_S; i++) {
543 ((IWineD3DDeviceImpl *)pDevice)->stateBlock->samplerState[j][SavedVertexStates_S[i]] = This->samplerState[j][SavedVertexStates_S[i]];
549 FIXME("Unrecognized state block type %d\n", This->blockType);
551 memcpy(&((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed, &This->changed, sizeof(((IWineD3DDeviceImpl*)pDevice)->stateBlock->changed));
552 TRACE("(%p) : Applied state block %p ------------------^\n", This, pDevice);
557 HRESULT WINAPI IWineD3DStateBlockImpl_InitStartupStateBlock(IWineD3DStateBlock* iface) {
558 IWineD3DStateBlockImpl *This = (IWineD3DStateBlockImpl *)iface;
559 IWineD3DDevice *device = (IWineD3DDevice *)This->wineD3DDevice;
560 IWineD3DDeviceImpl *ThisDevice = (IWineD3DDeviceImpl *)device;
571 /* Note this may have a large overhead but it should only be executed
572 once, in order to initialize the complete state of the device and
573 all opengl equivalents */
574 TRACE("(%p) -----------------------> Setting up device defaults... %p\n", This, This->wineD3DDevice);
575 /* TODO: make a special stateblock type for the primary stateblock (it never gets applied so it doesn't need a real type) */
576 This->blockType = WINED3DSBT_INIT;
578 /* Set some of the defaults for lights, transforms etc */
579 memcpy(&This->transforms[D3DTS_PROJECTION], &identity, sizeof(identity));
580 memcpy(&This->transforms[D3DTS_VIEW], &identity, sizeof(identity));
581 for (i = 0; i < 256; ++i) {
582 memcpy(&This->transforms[D3DTS_WORLDMATRIX(i)], &identity, sizeof(identity));
585 TRACE("Render states\n");
587 if (ThisDevice->depthStencilBuffer != NULL) {
588 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, D3DZB_TRUE);
590 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZENABLE, D3DZB_FALSE);
592 IWineD3DDevice_SetRenderState(device, WINED3DRS_FILLMODE, D3DFILL_SOLID);
593 IWineD3DDevice_SetRenderState(device, WINED3DRS_SHADEMODE, D3DSHADE_GOURAUD);
594 lp.lp.wRepeatFactor = 0;
595 lp.lp.wLinePattern = 0;
596 IWineD3DDevice_SetRenderState(device, WINED3DRS_LINEPATTERN, lp.d);
597 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZWRITEENABLE, TRUE);
598 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHATESTENABLE, FALSE);
599 IWineD3DDevice_SetRenderState(device, WINED3DRS_LASTPIXEL, TRUE);
600 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLEND, D3DBLEND_ONE);
601 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLEND, D3DBLEND_ZERO);
602 IWineD3DDevice_SetRenderState(device, WINED3DRS_CULLMODE, D3DCULL_CCW);
603 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZFUNC, D3DCMP_LESSEQUAL);
604 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAFUNC, D3DCMP_ALWAYS);
605 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHAREF, 0xff); /*??*/
606 IWineD3DDevice_SetRenderState(device, WINED3DRS_DITHERENABLE, FALSE);
607 IWineD3DDevice_SetRenderState(device, WINED3DRS_ALPHABLENDENABLE, FALSE);
608 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGENABLE, FALSE);
609 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARENABLE, FALSE);
610 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZVISIBLE, 0);
611 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGCOLOR, 0);
612 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGTABLEMODE, D3DFOG_NONE);
614 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGSTART, tmpfloat.d);
616 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGEND, tmpfloat.d);
618 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGDENSITY, tmpfloat.d);
619 IWineD3DDevice_SetRenderState(device, WINED3DRS_EDGEANTIALIAS, FALSE);
620 IWineD3DDevice_SetRenderState(device, WINED3DRS_ZBIAS, 0);
621 IWineD3DDevice_SetRenderState(device, WINED3DRS_RANGEFOGENABLE, FALSE);
622 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILENABLE, FALSE);
623 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFAIL, D3DSTENCILOP_KEEP);
624 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP);
625 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILPASS, D3DSTENCILOP_KEEP);
627 /* Setting stencil func also uses values for stencil ref/mask, so manually set defaults
628 * so only a single call performed (and ensure defaults initialized before making that call)
630 * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILREF, 0);
631 * IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILMASK, 0xFFFFFFFF);
633 This->renderState[WINED3DRS_STENCILREF] = 0;
634 This->renderState[WINED3DRS_STENCILMASK] = 0xFFFFFFFF;
635 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILFUNC, D3DCMP_ALWAYS);
636 IWineD3DDevice_SetRenderState(device, WINED3DRS_STENCILWRITEMASK, 0xFFFFFFFF);
637 IWineD3DDevice_SetRenderState(device, WINED3DRS_TEXTUREFACTOR, 0xFFFFFFFF);
638 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP0, 0);
639 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP1, 0);
640 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP2, 0);
641 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP3, 0);
642 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP4, 0);
643 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP5, 0);
644 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP6, 0);
645 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP7, 0);
646 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPING, TRUE);
647 IWineD3DDevice_SetRenderState(device, WINED3DRS_LIGHTING, TRUE);
648 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENT, 0);
649 IWineD3DDevice_SetRenderState(device, WINED3DRS_FOGVERTEXMODE, D3DFOG_NONE);
650 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORVERTEX, TRUE);
651 IWineD3DDevice_SetRenderState(device, WINED3DRS_LOCALVIEWER, TRUE);
652 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALIZENORMALS, FALSE);
653 IWineD3DDevice_SetRenderState(device, WINED3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
654 IWineD3DDevice_SetRenderState(device, WINED3DRS_SPECULARMATERIALSOURCE, D3DMCS_COLOR2);
655 IWineD3DDevice_SetRenderState(device, WINED3DRS_AMBIENTMATERIALSOURCE, D3DMCS_COLOR2);
656 IWineD3DDevice_SetRenderState(device, WINED3DRS_EMISSIVEMATERIALSOURCE, D3DMCS_MATERIAL);
657 IWineD3DDevice_SetRenderState(device, WINED3DRS_VERTEXBLEND, D3DVBF_DISABLE);
658 IWineD3DDevice_SetRenderState(device, WINED3DRS_CLIPPLANEENABLE, 0);
659 IWineD3DDevice_SetRenderState(device, WINED3DRS_SOFTWAREVERTEXPROCESSING, FALSE);
661 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE, tmpfloat.d);
663 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MIN, tmpfloat.d);
664 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSPRITEENABLE, FALSE);
665 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALEENABLE, FALSE);
666 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_A, TRUE);
667 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_B, TRUE);
668 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSCALE_C, TRUE);
669 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEANTIALIAS, TRUE);
670 IWineD3DDevice_SetRenderState(device, WINED3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
671 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHEDGESTYLE, D3DPATCHEDGE_DISCRETE);
673 IWineD3DDevice_SetRenderState(device, WINED3DRS_PATCHSEGMENTS, tmpfloat.d);
674 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEBUGMONITORTOKEN, D3DDMT_DISABLE);
676 IWineD3DDevice_SetRenderState(device, WINED3DRS_POINTSIZE_MAX, tmpfloat.d);
677 IWineD3DDevice_SetRenderState(device, WINED3DRS_INDEXEDVERTEXBLENDENABLE, FALSE);
678 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE, 0x0000000F);
680 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWEENFACTOR, tmpfloat.d);
681 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOP, D3DBLENDOP_ADD);
682 IWineD3DDevice_SetRenderState(device, WINED3DRS_POSITIONDEGREE, WINED3DDEGREE_CUBIC);
683 IWineD3DDevice_SetRenderState(device, WINED3DRS_NORMALDEGREE, WINED3DDEGREE_LINEAR);
684 /* states new in d3d9 */
685 IWineD3DDevice_SetRenderState(device, WINED3DRS_SCISSORTESTENABLE, FALSE);
686 IWineD3DDevice_SetRenderState(device, WINED3DRS_SLOPESCALEDEPTHBIAS, 0);
688 IWineD3DDevice_SetRenderState(device, WINED3DRS_MINTESSELLATIONLEVEL, tmpfloat.d);
689 IWineD3DDevice_SetRenderState(device, WINED3DRS_MAXTESSELLATIONLEVEL, tmpfloat.d);
690 IWineD3DDevice_SetRenderState(device, WINED3DRS_ANTIALIASEDLINEENABLE, FALSE);
692 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_X, tmpfloat.d);
693 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Y, tmpfloat.d);
695 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_Z, tmpfloat.d);
697 IWineD3DDevice_SetRenderState(device, WINED3DRS_ADAPTIVETESS_W, tmpfloat.d);
698 IWineD3DDevice_SetRenderState(device, WINED3DRS_ENABLEADAPTIVETESSELLATION, FALSE);
699 IWineD3DDevice_SetRenderState(device, WINED3DRS_TWOSIDEDSTENCILMODE, FALSE);
700 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFAIL, D3DSTENCILOP_KEEP);
701 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILZFAIL, D3DSTENCILOP_KEEP);
702 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILPASS, D3DSTENCILOP_KEEP);
703 IWineD3DDevice_SetRenderState(device, WINED3DRS_CCW_STENCILFUNC, D3DCMP_ALWAYS);
704 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE1, 0x0000000F);
705 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE2, 0x0000000F);
706 IWineD3DDevice_SetRenderState(device, WINED3DRS_COLORWRITEENABLE3, 0x0000000F);
707 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDFACTOR, 0xFFFFFFFF);
708 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRGBWRITEENABLE, 0);
709 IWineD3DDevice_SetRenderState(device, WINED3DRS_DEPTHBIAS, 0);
710 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP8, 0);
711 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP9, 0);
712 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP10, 0);
713 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP11, 0);
714 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP12, 0);
715 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP13, 0);
716 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP14, 0);
717 IWineD3DDevice_SetRenderState(device, WINED3DRS_WRAP15, 0);
718 IWineD3DDevice_SetRenderState(device, WINED3DRS_SEPARATEALPHABLENDENABLE, FALSE);
719 IWineD3DDevice_SetRenderState(device, WINED3DRS_SRCBLENDALPHA, D3DBLEND_ONE);
720 IWineD3DDevice_SetRenderState(device, WINED3DRS_DESTBLENDALPHA, D3DBLEND_ZERO);
721 IWineD3DDevice_SetRenderState(device, WINED3DRS_BLENDOPALPHA, D3DBLENDOP_ADD);
723 /* clipping status */
724 This->clip_status.ClipUnion = 0;
725 This->clip_status.ClipIntersection = 0xFFFFFFFF;
727 /* Texture Stage States - Put directly into state block, we will call function below */
728 for (i = 0; i < GL_LIMITS(textures); i++) {
729 TRACE("Setting up default texture states for texture Stage %d\n", i);
730 memcpy(&This->transforms[D3DTS_TEXTURE0 + i], &identity, sizeof(identity));
731 This->textureState[i][D3DTSS_COLOROP ] = (i==0)? D3DTOP_MODULATE : D3DTOP_DISABLE;
732 This->textureState[i][D3DTSS_COLORARG1 ] = D3DTA_TEXTURE;
733 This->textureState[i][D3DTSS_COLORARG2 ] = D3DTA_CURRENT;
734 This->textureState[i][D3DTSS_ALPHAOP ] = (i==0)? D3DTOP_SELECTARG1 : D3DTOP_DISABLE;
735 This->textureState[i][D3DTSS_ALPHAARG1 ] = D3DTA_TEXTURE;
736 This->textureState[i][D3DTSS_ALPHAARG2 ] = D3DTA_CURRENT;
737 This->textureState[i][D3DTSS_BUMPENVMAT00 ] = (DWORD) 0.0;
738 This->textureState[i][D3DTSS_BUMPENVMAT01 ] = (DWORD) 0.0;
739 This->textureState[i][D3DTSS_BUMPENVMAT10 ] = (DWORD) 0.0;
740 This->textureState[i][D3DTSS_BUMPENVMAT11 ] = (DWORD) 0.0;
741 This->textureState[i][D3DTSS_TEXCOORDINDEX ] = i;
742 This->textureState[i][D3DTSS_BUMPENVLSCALE ] = (DWORD) 0.0;
743 This->textureState[i][D3DTSS_BUMPENVLOFFSET ] = (DWORD) 0.0;
744 This->textureState[i][D3DTSS_TEXTURETRANSFORMFLAGS ] = D3DTTFF_DISABLE;
745 This->textureState[i][D3DTSS_ADDRESSW ] = D3DTADDRESS_WRAP;
746 This->textureState[i][D3DTSS_COLORARG0 ] = D3DTA_CURRENT;
747 This->textureState[i][D3DTSS_ALPHAARG0 ] = D3DTA_CURRENT;
748 This->textureState[i][D3DTSS_RESULTARG ] = D3DTA_CURRENT;
752 for (i = 0 ; i < GL_LIMITS(samplers); i++) {
753 TRACE("Setting up default samplers states for sampler %d\n", i);
754 This->samplerState[i][WINED3DSAMP_ADDRESSU ] = D3DTADDRESS_WRAP;
755 This->samplerState[i][WINED3DSAMP_ADDRESSV ] = D3DTADDRESS_WRAP;
756 This->samplerState[i][WINED3DSAMP_ADDRESSW ] = D3DTADDRESS_WRAP;
757 This->samplerState[i][WINED3DSAMP_BORDERCOLOR ] = 0x00;
758 This->samplerState[i][WINED3DSAMP_MAGFILTER ] = D3DTEXF_POINT;
759 This->samplerState[i][WINED3DSAMP_MINFILTER ] = D3DTEXF_POINT;
760 This->samplerState[i][WINED3DSAMP_MIPFILTER ] = D3DTEXF_NONE;
761 This->samplerState[i][WINED3DSAMP_MIPMAPLODBIAS ] = 0;
762 This->samplerState[i][WINED3DSAMP_MAXMIPLEVEL ] = 0;
763 This->samplerState[i][WINED3DSAMP_MAXANISOTROPY ] = 1;
764 This->samplerState[i][WINED3DSAMP_SRGBTEXTURE ] = 0; /* TODO: Gamma correction value*/
765 This->samplerState[i][WINED3DSAMP_ELEMENTINDEX ] = 0; /* TODO: Indicates which element of a multielement texture to use */
766 This->samplerState[i][WINED3DSAMP_DMAPOFFSET ] = 256; /* TODO: Vertex offset in the presampled displacement map */
769 /* Under DirectX you can have texture stage operations even if no texture is
770 bound, whereas opengl will only do texture operations when a valid texture is
771 bound. We emulate this by creating dummy textures and binding them to each
772 texture stage, but disable all stages by default. Hence if a stage is enabled
773 then the default texture will kick in until replaced by a SetTexture call */
777 for (i = 0; i < GL_LIMITS(textures); i++) {
780 /* Note this avoids calling settexture, so pretend it has been called */
781 This->set.textures[i] = TRUE;
782 This->changed.textures[i] = TRUE;
783 This->textures[i] = NULL;
785 /* Make appropriate texture active */
786 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
789 FIXME("Program using multiple concurrent textures which this opengl implementation doesn't support\n");
792 /* Generate an opengl texture name */
793 glGenTextures(1, &ThisDevice->dummyTextureName[i]);
794 checkGLcall("glGenTextures");
795 TRACE("Dummy Texture %d given name %d\n", i, ThisDevice->dummyTextureName[i]);
797 /* Generate a dummy 1d texture */
798 This->textureDimensions[i] = GL_TEXTURE_1D;
799 glBindTexture(GL_TEXTURE_1D, ThisDevice->dummyTextureName[i]);
800 checkGLcall("glBindTexture");
802 glTexImage1D(GL_TEXTURE_1D, 0, GL_LUMINANCE, 1, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, &white);
803 checkGLcall("glTexImage1D");
804 #if 1 /* TODO: move the setting texture states off to basetexture */
805 /* Reapply all the texture state information to this texture */
806 IWineD3DDevice_SetupTextureStates(device, i, REAPPLY_ALL);
812 /* Defaulting palettes - Note these are device wide but reinitialized here for convenience*/
813 for (i = 0; i < MAX_PALETTES; ++i) {
815 for (j = 0; j < 256; ++j) {
816 This->wineD3DDevice->palettes[i][j].peRed = 0xFF;
817 This->wineD3DDevice->palettes[i][j].peGreen = 0xFF;
818 This->wineD3DDevice->palettes[i][j].peBlue = 0xFF;
819 This->wineD3DDevice->palettes[i][j].peFlags = 0xFF;
822 This->wineD3DDevice->currentPalette = 0;
824 TRACE("-----------------------> Device defaults now set up...\n");
828 /**********************************************************
829 * IWineD3DStateBlock VTbl follows
830 **********************************************************/
832 const IWineD3DStateBlockVtbl IWineD3DStateBlock_Vtbl =
835 IWineD3DStateBlockImpl_QueryInterface,
836 IWineD3DStateBlockImpl_AddRef,
837 IWineD3DStateBlockImpl_Release,
838 /* IWineD3DStateBlock */
839 IWineD3DStateBlockImpl_GetParent,
840 IWineD3DStateBlockImpl_GetDevice,
841 IWineD3DStateBlockImpl_Capture,
842 IWineD3DStateBlockImpl_Apply,
843 IWineD3DStateBlockImpl_InitStartupStateBlock