1 // Copyright (c) 2012- PPSSPP Project.
2 
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
6 
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 // GNU General Public License 2.0 for more details.
11 
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
14 
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17 
18 #include <d3d11.h>
19 #include <d3d11_1.h>
20 
21 #include <algorithm>
22 
23 #include "Common/Data/Convert/SmallDataConvert.h"
24 
25 #include "GPU/Math3D.h"
26 #include "GPU/GPUState.h"
27 #include "GPU/ge_constants.h"
28 #include "GPU/Common/GPUStateUtils.h"
29 #include "Core/System.h"
30 #include "Core/Config.h"
31 #include "Core/Reporting.h"
32 
33 #include "GPU/Common/FramebufferManagerCommon.h"
34 #include "GPU/D3D11/DrawEngineD3D11.h"
35 #include "GPU/D3D11/StateMappingD3D11.h"
36 #include "GPU/D3D11/FramebufferManagerD3D11.h"
37 #include "GPU/D3D11/TextureCacheD3D11.h"
38 
39 // These tables all fit into u8s.
40 static const D3D11_BLEND d3d11BlendFactorLookup[(size_t)BlendFactor::COUNT] = {
41 	D3D11_BLEND_ZERO,
42 	D3D11_BLEND_ONE,
43 	D3D11_BLEND_SRC_COLOR,
44 	D3D11_BLEND_INV_SRC_COLOR,
45 	D3D11_BLEND_DEST_COLOR,
46 	D3D11_BLEND_INV_DEST_COLOR,
47 	D3D11_BLEND_SRC_ALPHA,
48 	D3D11_BLEND_INV_SRC_ALPHA,
49 	D3D11_BLEND_DEST_ALPHA,
50 	D3D11_BLEND_INV_DEST_ALPHA,
51 	D3D11_BLEND_BLEND_FACTOR,
52 	D3D11_BLEND_INV_BLEND_FACTOR,
53 	D3D11_BLEND_BLEND_FACTOR,
54 	D3D11_BLEND_INV_BLEND_FACTOR,
55 	D3D11_BLEND_SRC1_COLOR,
56 	D3D11_BLEND_INV_SRC1_COLOR,
57 	D3D11_BLEND_SRC1_ALPHA,
58 	D3D11_BLEND_INV_SRC1_ALPHA,
59 };
60 
61 static const D3D11_BLEND_OP d3d11BlendEqLookup[(size_t)BlendEq::COUNT] = {
62 	D3D11_BLEND_OP_ADD,
63 	D3D11_BLEND_OP_SUBTRACT,
64 	D3D11_BLEND_OP_REV_SUBTRACT,
65 	D3D11_BLEND_OP_MIN,
66 	D3D11_BLEND_OP_MAX,
67 };
68 
69 static const D3D11_CULL_MODE cullingMode[] = {
70 	D3D11_CULL_BACK,
71 	D3D11_CULL_FRONT,
72 };
73 
74 static const D3D11_COMPARISON_FUNC compareOps[] = {
75 	D3D11_COMPARISON_NEVER,
76 	D3D11_COMPARISON_ALWAYS,
77 	D3D11_COMPARISON_EQUAL,
78 	D3D11_COMPARISON_NOT_EQUAL,
79 	D3D11_COMPARISON_LESS,
80 	D3D11_COMPARISON_LESS_EQUAL,
81 	D3D11_COMPARISON_GREATER,
82 	D3D11_COMPARISON_GREATER_EQUAL,
83 };
84 
85 static const D3D11_STENCIL_OP stencilOps[] = {
86 	D3D11_STENCIL_OP_KEEP,
87 	D3D11_STENCIL_OP_ZERO,
88 	D3D11_STENCIL_OP_REPLACE,
89 	D3D11_STENCIL_OP_INVERT,
90 	D3D11_STENCIL_OP_INCR_SAT,
91 	D3D11_STENCIL_OP_DECR_SAT,
92 	D3D11_STENCIL_OP_KEEP, // reserved
93 	D3D11_STENCIL_OP_KEEP, // reserved
94 };
95 
96 static const D3D11_PRIMITIVE_TOPOLOGY primToD3D11[8] = {
97 	D3D11_PRIMITIVE_TOPOLOGY_POINTLIST,
98 	D3D11_PRIMITIVE_TOPOLOGY_LINELIST,
99 	D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP,
100 	D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST,
101 	D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP,
102 	D3D11_PRIMITIVE_TOPOLOGY_UNDEFINED,  // D3D11 doesn't do triangle fans.
103 	D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST,
104 };
105 
106 static const D3D11_LOGIC_OP logicOps[] = {
107 	D3D11_LOGIC_OP_CLEAR,
108 	D3D11_LOGIC_OP_AND,
109 	D3D11_LOGIC_OP_AND_REVERSE,
110 	D3D11_LOGIC_OP_COPY,
111 	D3D11_LOGIC_OP_AND_INVERTED,
112 	D3D11_LOGIC_OP_NOOP,
113 	D3D11_LOGIC_OP_XOR,
114 	D3D11_LOGIC_OP_OR,
115 	D3D11_LOGIC_OP_NOR,
116 	D3D11_LOGIC_OP_EQUIV,
117 	D3D11_LOGIC_OP_INVERT,
118 	D3D11_LOGIC_OP_OR_REVERSE,
119 	D3D11_LOGIC_OP_COPY_INVERTED,
120 	D3D11_LOGIC_OP_OR_INVERTED,
121 	D3D11_LOGIC_OP_NAND,
122 	D3D11_LOGIC_OP_SET,
123 };
124 
ResetFramebufferRead()125 void DrawEngineD3D11::ResetFramebufferRead() {
126 	if (fboTexBound_) {
127 		ID3D11ShaderResourceView *srv = nullptr;
128 		context_->PSSetShaderResources(0, 1, &srv);
129 		fboTexBound_ = false;
130 	}
131 }
132 
133 class FramebufferManagerD3D11;
134 class ShaderManagerD3D11;
135 
ApplyDrawState(int prim)136 void DrawEngineD3D11::ApplyDrawState(int prim) {
137 	dynState_.topology = primToD3D11[prim];
138 
139 	if (!gstate_c.IsDirty(DIRTY_BLEND_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS | DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE)) {
140 		// nothing to do
141 		return;
142 	}
143 
144 	bool useBufferedRendering = framebufferManager_->UseBufferedRendering();
145 	// Blend
146 	if (gstate_c.IsDirty(DIRTY_BLEND_STATE)) {
147 		gstate_c.SetAllowFramebufferRead(!g_Config.bDisableSlowFramebufEffects);
148 		if (gstate.isModeClear()) {
149 			keys_.blend.value = 0;  // full wipe
150 			keys_.blend.blendEnable = false;
151 			dynState_.useBlendColor = false;
152 			// Color Test
153 			bool alphaMask = gstate.isClearModeAlphaMask();
154 			bool colorMask = gstate.isClearModeColorMask();
155 			keys_.blend.colorWriteMask = (colorMask ? (1 | 2 | 4) : 0) | (alphaMask ? 8 : 0);
156 		} else {
157 			keys_.blend.value = 0;
158 			// Set blend - unless we need to do it in the shader.
159 			GenericBlendState blendState;
160 			ConvertBlendState(blendState, gstate_c.allowFramebufferRead);
161 
162 			GenericMaskState maskState;
163 			ConvertMaskState(maskState, gstate_c.allowFramebufferRead);
164 
165 			if (blendState.applyFramebufferRead || maskState.applyFramebufferRead) {
166 				if (ApplyFramebufferRead(&fboTexNeedsBind_)) {
167 					// The shader takes over the responsibility for blending, so recompute.
168 					ApplyStencilReplaceAndLogicOpIgnoreBlend(blendState.replaceAlphaWithStencil, blendState);
169 				} else {
170 					// Until next time, force it off.
171 					ResetFramebufferRead();
172 					gstate_c.SetAllowFramebufferRead(false);
173 				}
174 				gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE);
175 			} else if (blendState.resetFramebufferRead) {
176 				ResetFramebufferRead();
177 				gstate_c.Dirty(DIRTY_FRAGMENTSHADER_STATE);
178 			}
179 
180 			if (blendState.enabled) {
181 				keys_.blend.blendEnable = true;
182 				keys_.blend.logicOpEnable = false;
183 				keys_.blend.blendOpColor = d3d11BlendEqLookup[(size_t)blendState.eqColor];
184 				keys_.blend.blendOpAlpha = d3d11BlendEqLookup[(size_t)blendState.eqAlpha];
185 				keys_.blend.srcColor = d3d11BlendFactorLookup[(size_t)blendState.srcColor];
186 				keys_.blend.srcAlpha = d3d11BlendFactorLookup[(size_t)blendState.srcAlpha];
187 				keys_.blend.destColor = d3d11BlendFactorLookup[(size_t)blendState.dstColor];
188 				keys_.blend.destAlpha = d3d11BlendFactorLookup[(size_t)blendState.dstAlpha];
189 				if (blendState.dirtyShaderBlendFixValues) {
190 					gstate_c.Dirty(DIRTY_SHADERBLEND);
191 				}
192 				dynState_.useBlendColor = blendState.useBlendColor;
193 				if (blendState.useBlendColor) {
194 					dynState_.blendColor = blendState.blendColor;
195 				}
196 			} else {
197 				keys_.blend.blendEnable = false;
198 				dynState_.useBlendColor = false;
199 			}
200 
201 			if (gstate_c.Supports(GPU_SUPPORTS_LOGIC_OP)) {
202 				// Logic Ops
203 				if (gstate.isLogicOpEnabled() && gstate.getLogicOp() != GE_LOGIC_COPY) {
204 					keys_.blend.blendEnable = false;  // Can't have both blend & logic op - although I think the PSP can!
205 					keys_.blend.logicOpEnable = true;
206 					keys_.blend.logicOp = logicOps[gstate.getLogicOp()];
207 				} else {
208 					keys_.blend.logicOpEnable = false;
209 				}
210 			}
211 
212 			keys_.blend.colorWriteMask = (maskState.rgba[0] ? 1 : 0) | (maskState.rgba[1] ? 2 : 0) | (maskState.rgba[2] ? 4 : 0) | (maskState.rgba[3] ? 8 : 0);
213 		}
214 
215 		if (!device1_) {
216 			ID3D11BlendState *bs = blendCache_.Get(keys_.blend.value);
217 			if (bs == nullptr) {
218 				D3D11_BLEND_DESC desc{};
219 				D3D11_RENDER_TARGET_BLEND_DESC &rt = desc.RenderTarget[0];
220 				rt.BlendEnable = keys_.blend.blendEnable;
221 				rt.BlendOp = (D3D11_BLEND_OP)keys_.blend.blendOpColor;
222 				rt.BlendOpAlpha = (D3D11_BLEND_OP)keys_.blend.blendOpAlpha;
223 				rt.SrcBlend = (D3D11_BLEND)keys_.blend.srcColor;
224 				rt.DestBlend = (D3D11_BLEND)keys_.blend.destColor;
225 				rt.SrcBlendAlpha = (D3D11_BLEND)keys_.blend.srcAlpha;
226 				rt.DestBlendAlpha = (D3D11_BLEND)keys_.blend.destAlpha;
227 				rt.RenderTargetWriteMask = keys_.blend.colorWriteMask;
228 				ASSERT_SUCCESS(device_->CreateBlendState(&desc, &bs));
229 				blendCache_.Insert(keys_.blend.value, bs);
230 			}
231 			blendState_ = bs;
232 		} else {
233 			ID3D11BlendState1 *bs1 = blendCache1_.Get(keys_.blend.value);
234 			if (bs1 == nullptr) {
235 				D3D11_BLEND_DESC1 desc1{};
236 				D3D11_RENDER_TARGET_BLEND_DESC1 &rt = desc1.RenderTarget[0];
237 				rt.BlendEnable = keys_.blend.blendEnable;
238 				rt.BlendOp = (D3D11_BLEND_OP)keys_.blend.blendOpColor;
239 				rt.BlendOpAlpha = (D3D11_BLEND_OP)keys_.blend.blendOpAlpha;
240 				rt.SrcBlend = (D3D11_BLEND)keys_.blend.srcColor;
241 				rt.DestBlend = (D3D11_BLEND)keys_.blend.destColor;
242 				rt.SrcBlendAlpha = (D3D11_BLEND)keys_.blend.srcAlpha;
243 				rt.DestBlendAlpha = (D3D11_BLEND)keys_.blend.destAlpha;
244 				rt.RenderTargetWriteMask = keys_.blend.colorWriteMask;
245 				rt.LogicOpEnable = keys_.blend.logicOpEnable;
246 				rt.LogicOp = (D3D11_LOGIC_OP)keys_.blend.logicOp;
247 				ASSERT_SUCCESS(device1_->CreateBlendState1(&desc1, &bs1));
248 				blendCache1_.Insert(keys_.blend.value, bs1);
249 			}
250 			blendState1_ = bs1;
251 		}
252 	}
253 
254 	if (gstate_c.IsDirty(DIRTY_RASTER_STATE)) {
255 		keys_.raster.value = 0;
256 		bool wantCull = !gstate.isModeClear() && prim != GE_PRIM_RECTANGLES && gstate.isCullEnabled();
257 		keys_.raster.cullMode = wantCull ? (gstate.getCullMode() ? D3D11_CULL_FRONT : D3D11_CULL_BACK) : D3D11_CULL_NONE;
258 
259 		if (gstate.isModeClear() || gstate.isModeThrough()) {
260 			// TODO: Might happen in clear mode if not through...
261 			keys_.raster.depthClipEnable = 1;
262 		} else {
263 			if (gstate.getDepthRangeMin() == 0 || gstate.getDepthRangeMax() == 65535) {
264 				// TODO: Still has a bug where we clamp to depth range if one is not the full range.
265 				// But the alternate is not clamping in either direction...
266 				keys_.raster.depthClipEnable = !gstate.isDepthClampEnabled() || !gstate_c.Supports(GPU_SUPPORTS_DEPTH_CLAMP);
267 			} else {
268 				// We just want to clip in this case, the clamp would be clipped anyway.
269 				keys_.raster.depthClipEnable = 1;
270 			}
271 		}
272 		ID3D11RasterizerState *rs = rasterCache_.Get(keys_.raster.value);
273 		if (rs == nullptr) {
274 			D3D11_RASTERIZER_DESC desc{};
275 			desc.CullMode = (D3D11_CULL_MODE)(keys_.raster.cullMode);
276 			desc.FillMode = D3D11_FILL_SOLID;
277 			desc.ScissorEnable = TRUE;
278 			desc.FrontCounterClockwise = TRUE;
279 			desc.DepthClipEnable = keys_.raster.depthClipEnable;
280 			ASSERT_SUCCESS(device_->CreateRasterizerState(&desc, &rs));
281 			rasterCache_.Insert(keys_.raster.value, rs);
282 		}
283 		rasterState_ = rs;
284 	}
285 
286 	if (gstate_c.IsDirty(DIRTY_DEPTHSTENCIL_STATE)) {
287 		GenericStencilFuncState stencilState;
288 		ConvertStencilFuncState(stencilState);
289 
290 		if (gstate.isModeClear()) {
291 			keys_.depthStencil.value = 0;
292 			keys_.depthStencil.depthTestEnable = true;
293 			keys_.depthStencil.depthCompareOp = D3D11_COMPARISON_ALWAYS;
294 			keys_.depthStencil.depthWriteEnable = gstate.isClearModeDepthMask();
295 			if (gstate.isClearModeDepthMask()) {
296 				framebufferManager_->SetDepthUpdated();
297 			}
298 
299 			// Stencil Test
300 			bool alphaMask = gstate.isClearModeAlphaMask();
301 			if (alphaMask) {
302 				keys_.depthStencil.stencilTestEnable = true;
303 				keys_.depthStencil.stencilCompareFunc = D3D11_COMPARISON_ALWAYS;
304 				keys_.depthStencil.stencilPassOp = D3D11_STENCIL_OP_REPLACE;
305 				keys_.depthStencil.stencilFailOp = D3D11_STENCIL_OP_REPLACE;
306 				keys_.depthStencil.stencilDepthFailOp = D3D11_STENCIL_OP_REPLACE;
307 				dynState_.useStencil = true;
308 				// In clear mode, the stencil value is set to the alpha value of the vertex.
309 				// A normal clear will be 2 points, the second point has the color.
310 				// We override this value in the pipeline from software transform for clear rectangles.
311 				dynState_.stencilRef = 0xFF;
312 				// But we still apply the stencil write mask.
313 				keys_.depthStencil.stencilWriteMask = stencilState.writeMask;
314 			} else {
315 				keys_.depthStencil.stencilTestEnable = false;
316 				dynState_.useStencil = false;
317 			}
318 
319 		} else {
320 			keys_.depthStencil.value = 0;
321 			// Depth Test
322 			if (gstate.isDepthTestEnabled()) {
323 				keys_.depthStencil.depthTestEnable = true;
324 				keys_.depthStencil.depthCompareOp = compareOps[gstate.getDepthTestFunction()];
325 				keys_.depthStencil.depthWriteEnable = gstate.isDepthWriteEnabled();
326 				if (gstate.isDepthWriteEnabled()) {
327 					framebufferManager_->SetDepthUpdated();
328 				}
329 			} else {
330 				keys_.depthStencil.depthTestEnable = false;
331 				keys_.depthStencil.depthWriteEnable = false;
332 				keys_.depthStencil.depthCompareOp = D3D11_COMPARISON_ALWAYS;
333 			}
334 
335 			// Stencil Test
336 			if (stencilState.enabled) {
337 				keys_.depthStencil.stencilTestEnable = true;
338 				keys_.depthStencil.stencilCompareFunc = compareOps[stencilState.testFunc];
339 				keys_.depthStencil.stencilPassOp = stencilOps[stencilState.zPass];
340 				keys_.depthStencil.stencilFailOp = stencilOps[stencilState.sFail];
341 				keys_.depthStencil.stencilDepthFailOp = stencilOps[stencilState.zFail];
342 				keys_.depthStencil.stencilCompareMask = stencilState.testMask;
343 				keys_.depthStencil.stencilWriteMask = stencilState.writeMask;
344 				dynState_.useStencil = true;
345 				dynState_.stencilRef = stencilState.testRef;
346 			} else {
347 				keys_.depthStencil.stencilTestEnable = false;
348 				dynState_.useStencil = false;
349 			}
350 		}
351 		ID3D11DepthStencilState *ds = depthStencilCache_.Get(keys_.depthStencil.value);
352 		if (ds == nullptr) {
353 			D3D11_DEPTH_STENCIL_DESC desc{};
354 			desc.DepthEnable = keys_.depthStencil.depthTestEnable;
355 			desc.DepthWriteMask = keys_.depthStencil.depthWriteEnable ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO;
356 			desc.DepthFunc = (D3D11_COMPARISON_FUNC)keys_.depthStencil.depthCompareOp;
357 			desc.StencilEnable = keys_.depthStencil.stencilTestEnable;
358 			desc.StencilReadMask = keys_.depthStencil.stencilCompareMask;
359 			desc.StencilWriteMask = keys_.depthStencil.stencilWriteMask;
360 			desc.FrontFace.StencilFailOp = (D3D11_STENCIL_OP)keys_.depthStencil.stencilFailOp;
361 			desc.FrontFace.StencilPassOp = (D3D11_STENCIL_OP)keys_.depthStencil.stencilPassOp;
362 			desc.FrontFace.StencilDepthFailOp = (D3D11_STENCIL_OP)keys_.depthStencil.stencilDepthFailOp;
363 			desc.FrontFace.StencilFunc = (D3D11_COMPARISON_FUNC)keys_.depthStencil.stencilCompareFunc;
364 			desc.BackFace = desc.FrontFace;
365 			ASSERT_SUCCESS(device_->CreateDepthStencilState(&desc, &ds));
366 			depthStencilCache_.Insert(keys_.depthStencil.value, ds);
367 		}
368 		depthStencilState_ = ds;
369 	}
370 
371 	if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {
372 		ViewportAndScissor vpAndScissor;
373 		ConvertViewportAndScissor(useBufferedRendering,
374 			framebufferManager_->GetRenderWidth(), framebufferManager_->GetRenderHeight(),
375 			framebufferManager_->GetTargetBufferWidth(), framebufferManager_->GetTargetBufferHeight(),
376 			vpAndScissor);
377 
378 		float depthMin = vpAndScissor.depthRangeMin;
379 		float depthMax = vpAndScissor.depthRangeMax;
380 
381 		if (depthMin < 0.0f) depthMin = 0.0f;
382 		if (depthMax > 1.0f) depthMax = 1.0f;
383 		if (vpAndScissor.dirtyDepth) {
384 			gstate_c.Dirty(DIRTY_DEPTHRANGE);
385 		}
386 
387 		Draw::Viewport &vp = dynState_.viewport;
388 		vp.TopLeftX = vpAndScissor.viewportX;
389 		vp.TopLeftY = vpAndScissor.viewportY;
390 		vp.Width = vpAndScissor.viewportW;
391 		vp.Height = vpAndScissor.viewportH;
392 		vp.MinDepth = depthMin;
393 		vp.MaxDepth = depthMax;
394 
395 		if (vpAndScissor.dirtyProj) {
396 			gstate_c.Dirty(DIRTY_PROJMATRIX);
397 		}
398 
399 		D3D11_RECT &scissor = dynState_.scissor;
400 		if (vpAndScissor.scissorEnable) {
401 			scissor.left = vpAndScissor.scissorX;
402 			scissor.top = vpAndScissor.scissorY;
403 			scissor.right = vpAndScissor.scissorX + std::max(0, vpAndScissor.scissorW);
404 			scissor.bottom = vpAndScissor.scissorY + std::max(0, vpAndScissor.scissorH);
405 		} else {
406 			scissor.left = 0;
407 			scissor.top = 0;
408 			scissor.right = framebufferManager_->GetRenderWidth();
409 			scissor.bottom = framebufferManager_->GetRenderHeight();
410 		}
411 	}
412 
413 	if (gstate_c.IsDirty(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS) && !gstate.isModeClear() && gstate.isTextureMapEnabled()) {
414 		textureCache_->SetTexture();
415 		gstate_c.Clean(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
416 	} else if (gstate.getTextureAddress(0) == ((gstate.getFrameBufRawAddress() | 0x04000000) & 0x3FFFFFFF)) {
417 		// This catches the case of clearing a texture.
418 		gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);
419 	}
420 }
421 
ApplyDrawStateLate(bool applyStencilRef,uint8_t stencilRef)422 void DrawEngineD3D11::ApplyDrawStateLate(bool applyStencilRef, uint8_t stencilRef) {
423 	if (!gstate.isModeClear()) {
424 		if (fboTexNeedsBind_) {
425 			framebufferManager_->BindFramebufferAsColorTexture(1, framebufferManager_->GetCurrentRenderVFB(), BINDFBCOLOR_MAY_COPY);
426 			// No sampler required, we do a plain Load in the pixel shader.
427 			fboTexBound_ = true;
428 			fboTexNeedsBind_ = false;
429 		}
430 		textureCache_->ApplyTexture();
431 	}
432 
433 	// we go through Draw here because it automatically handles screen rotation, as needed in UWP on mobiles.
434 	if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {
435 		draw_->SetViewports(1, &dynState_.viewport);
436 		draw_->SetScissorRect(dynState_.scissor.left, dynState_.scissor.top, dynState_.scissor.right - dynState_.scissor.left, dynState_.scissor.bottom - dynState_.scissor.top);
437 	}
438 	if (gstate_c.IsDirty(DIRTY_RASTER_STATE)) {
439 		context_->RSSetState(rasterState_);
440 	}
441 	if (gstate_c.IsDirty(DIRTY_BLEND_STATE)) {
442 		// Need to do this AFTER ApplyTexture because the process of depallettization can ruin the blend state.
443 		float blendColor[4];
444 		Uint8x4ToFloat4(blendColor, dynState_.blendColor);
445 		if (device1_) {
446 			context1_->OMSetBlendState(blendState1_, blendColor, 0xFFFFFFFF);
447 		} else {
448 			context_->OMSetBlendState(blendState_, blendColor, 0xFFFFFFFF);
449 		}
450 	}
451 	if (gstate_c.IsDirty(DIRTY_DEPTHSTENCIL_STATE) || applyStencilRef) {
452 		context_->OMSetDepthStencilState(depthStencilState_, applyStencilRef ? stencilRef : dynState_.stencilRef);
453 	}
454 	gstate_c.Clean(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_RASTER_STATE | DIRTY_BLEND_STATE);
455 
456 	// Must dirty blend state here so we re-copy next time.  Example: Lunar's spell effects.
457 	if (fboTexBound_)
458 		gstate_c.Dirty(DIRTY_BLEND_STATE);
459 }
460