1 // Copyright (c) 2014- 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 <d3d9.h>
19 
20 #include "Common/GPU/D3D9/D3D9StateCache.h"
21 #include "Common/GPU/thin3d.h"
22 #include "Core/Reporting.h"
23 #include "GPU/Common/StencilCommon.h"
24 #include "GPU/Directx9/FramebufferManagerDX9.h"
25 #include "GPU/Directx9/ShaderManagerDX9.h"
26 #include "GPU/Directx9/TextureCacheDX9.h"
27 
28 namespace DX9 {
29 
30 #define STR_HELPER(x) #x
31 #define STR(x) STR_HELPER(x)
32 
33 static const char *stencil_ps = R"(
34 sampler tex: register(s0);
35 // TODO: Don't use fixed registers?  Or don't overlap?
36 float4 u_stencilValue : register(c)" STR(CONST_PS_STENCILVALUE) R"();
37 struct PS_IN {
38   float2 v_texcoord0 : TEXCOORD0;
39 };
40 float roundAndScaleTo255f(in float x) { return floor(x * 255.99); }
41 float4 main(PS_IN In) : COLOR {
42   float4 index = tex2D(tex, In.v_texcoord0);
43   float shifted = roundAndScaleTo255f(index.a) / roundAndScaleTo255f(u_stencilValue.x);
44   clip(fmod(floor(shifted), 2.0) - 0.99);
45   return index.aaaa;
46 }
47 )";
48 
49 static const char *stencil_vs = R"(
50 struct VS_IN {
51   float4 a_position : POSITION;
52   float2 a_texcoord0 : TEXCOORD0;
53 };
54 struct VS_OUT {
55   float4 position : POSITION;
56   float2 v_texcoord0 : TEXCOORD0;
57 };
58 VS_OUT main(VS_IN In) {
59   VS_OUT Out;
60   Out.position = In.a_position;
61   Out.v_texcoord0 = In.a_texcoord0;
62   return Out;
63 }
64 )";
65 
NotifyStencilUpload(u32 addr,int size,StencilUpload flags)66 bool FramebufferManagerDX9::NotifyStencilUpload(u32 addr, int size, StencilUpload flags) {
67 	addr &= 0x3FFFFFFF;
68 	if (!MayIntersectFramebuffer(addr)) {
69 		return false;
70 	}
71 
72 	VirtualFramebuffer *dstBuffer = 0;
73 	for (size_t i = 0; i < vfbs_.size(); ++i) {
74 		VirtualFramebuffer *vfb = vfbs_[i];
75 		if (vfb->fb_address == addr) {
76 			dstBuffer = vfb;
77 		}
78 	}
79 	if (!dstBuffer) {
80 		return false;
81 	}
82 
83 	int values = 0;
84 	u8 usedBits = 0;
85 
86 	const u8 *src = Memory::GetPointer(addr);
87 	if (!src) {
88 		return false;
89 	}
90 
91 	switch (dstBuffer->format) {
92 	case GE_FORMAT_565:
93 		// Well, this doesn't make much sense.
94 		return false;
95 	case GE_FORMAT_5551:
96 		usedBits = StencilBits5551(src, dstBuffer->fb_stride * dstBuffer->bufferHeight);
97 		values = 2;
98 		break;
99 	case GE_FORMAT_4444:
100 		usedBits = StencilBits4444(src, dstBuffer->fb_stride * dstBuffer->bufferHeight);
101 		values = 16;
102 		break;
103 	case GE_FORMAT_8888:
104 		usedBits = StencilBits8888(src, dstBuffer->fb_stride * dstBuffer->bufferHeight);
105 		values = 256;
106 		break;
107 	case GE_FORMAT_INVALID:
108 		// Impossible.
109 		break;
110 	}
111 
112 	if (usedBits == 0) {
113 		if (flags == StencilUpload::STENCIL_IS_ZERO) {
114 			// Common when creating buffers, it's already 0.  We're done.
115 			return false;
116 		}
117 
118 		// Let's not bother with the shader if it's just zero.
119 		dxstate.scissorTest.disable();
120 		dxstate.colorMask.set(false, false, false, true);
121 		// TODO: Verify this clears only stencil/alpha.
122 		device_->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_STENCIL, D3DCOLOR_RGBA(0, 0, 0, 0), 0.0f, 0);
123 
124 		gstate_c.Dirty(DIRTY_BLEND_STATE | DIRTY_VIEWPORTSCISSOR_STATE);
125 		return true;
126 	}
127 
128 	if (stencilUploadFailed_) {
129 		return false;
130 	}
131 
132 	// TODO: Helper with logging?
133 	if (!stencilUploadPS_) {
134 		std::string errorMessage;
135 		bool success = CompilePixelShaderD3D9(device_, stencil_ps, &stencilUploadPS_, &errorMessage);
136 		if (!errorMessage.empty()) {
137 			if (success) {
138 				ERROR_LOG(G3D, "Warnings in shader compilation!");
139 			} else {
140 				ERROR_LOG(G3D, "Error in shader compilation!");
141 			}
142 			ERROR_LOG(G3D, "Messages: %s", errorMessage.c_str());
143 			ERROR_LOG(G3D, "Shader source:\n%s", stencil_ps);
144 			OutputDebugStringUTF8("Messages:\n");
145 			OutputDebugStringUTF8(errorMessage.c_str());
146 			Reporting::ReportMessage("D3D error in shader compilation: info: %s / code: %s", errorMessage.c_str(), stencil_ps);
147 		}
148 		if (!success) {
149 			if (stencilUploadPS_) {
150 				stencilUploadPS_->Release();
151 			}
152 			stencilUploadPS_ = nullptr;
153 		}
154 	}
155 	if (!stencilUploadVS_) {
156 		std::string errorMessage;
157 		bool success = CompileVertexShaderD3D9(device_, stencil_vs, &stencilUploadVS_, &errorMessage);
158 		if (!errorMessage.empty()) {
159 			if (success) {
160 				ERROR_LOG(G3D, "Warnings in shader compilation!");
161 			} else {
162 				ERROR_LOG(G3D, "Error in shader compilation!");
163 			}
164 			ERROR_LOG(G3D, "Messages: %s", errorMessage.c_str());
165 			ERROR_LOG(G3D, "Shader source:\n%s", stencil_vs);
166 			OutputDebugStringUTF8("Messages:\n");
167 			OutputDebugStringUTF8(errorMessage.c_str());
168 			Reporting::ReportMessage("D3D error in shader compilation: info: %s / code: %s", errorMessage.c_str(), stencil_vs);
169 		}
170 		if (!success) {
171 			if (stencilUploadVS_) {
172 				stencilUploadVS_->Release();
173 			}
174 			stencilUploadVS_ = nullptr;
175 		}
176 	}
177 	if (!stencilUploadPS_ || !stencilUploadVS_) {
178 		stencilUploadFailed_ = true;
179 		return false;
180 	}
181 
182 	shaderManagerDX9_->DirtyLastShader();
183 
184 	dxstate.colorMask.set(false, false, false, true);
185 	dxstate.stencilTest.enable();
186 	dxstate.stencilOp.set(D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE, D3DSTENCILOP_REPLACE);
187 
188 	u16 w = dstBuffer->renderWidth;
189 	u16 h = dstBuffer->renderHeight;
190 
191 	if (dstBuffer->fbo) {
192 		// Typically, STENCIL_IS_ZERO means it's already bound.
193 		Draw::RPAction stencilAction = flags == StencilUpload::STENCIL_IS_ZERO ? Draw::RPAction::KEEP : Draw::RPAction::CLEAR;
194 		draw_->BindFramebufferAsRenderTarget(dstBuffer->fbo, { Draw::RPAction::KEEP, Draw::RPAction::KEEP, stencilAction }, "StencilUpload");
195 	}
196 	D3DVIEWPORT9 vp{ 0, 0, w, h, 0.0f, 1.0f };
197 	device_->SetViewport(&vp);
198 
199 	float u1 = 1.0f;
200 	float v1 = 1.0f;
201 	Draw::Texture *tex = MakePixelTexture(src, dstBuffer->format, dstBuffer->fb_stride, dstBuffer->bufferWidth, dstBuffer->bufferHeight, u1, v1);
202 	if (!tex)
203 		return false;
204 
205 	// TODO: Ideally, we should clear alpha to zero here (but not RGB.)
206 
207 	dxstate.stencilFunc.set(D3DCMP_ALWAYS, 0xFF, 0xFF);
208 
209 	float coord[20] = {
210 		-1.0f,  1.0f, 0.0f, 0.0f, 0.0f,
211 		 1.0f,  1.0f, 0.0f, u1,   0.0f,
212 		-1.0f, -1.0f, 0.0f, 0.0f, v1,
213 		 1.0f, -1.0f, 0.0f, u1,   v1,
214 	};
215 
216 	device_->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
217 
218 	device_->SetVertexDeclaration(pFramebufferVertexDecl);
219 	device_->SetPixelShader(stencilUploadPS_);
220 	device_->SetVertexShader(stencilUploadVS_);
221 
222 	draw_->BindTextures(0, 1, &tex);
223 
224 	shaderManagerDX9_->DirtyLastShader();
225 	textureCacheDX9_->ForgetLastTexture();
226 
227 	for (int i = 1; i < values; i += i) {
228 		if (!(usedBits & i)) {
229 			// It's already zero, let's skip it.
230 			continue;
231 		}
232 		if (dstBuffer->format == GE_FORMAT_4444) {
233 			dxstate.stencilMask.set(i | (i << 4));
234 			const float f[4] = {i * (16.0f / 255.0f)};
235 			device_->SetPixelShaderConstantF(CONST_PS_STENCILVALUE, f, 1);
236 		} else if (dstBuffer->format == GE_FORMAT_5551) {
237 			dxstate.stencilMask.set(0xFF);
238 			const float f[4] = {i * (128.0f / 255.0f)};
239 			device_->SetPixelShaderConstantF(CONST_PS_STENCILVALUE, f, 1);
240 		} else {
241 			dxstate.stencilMask.set(i);
242 			const float f[4] = {i * (1.0f / 255.0f)};
243 			device_->SetPixelShaderConstantF(CONST_PS_STENCILVALUE, f, 1);
244 		}
245 		HRESULT hr = device_->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, coord, 5 * sizeof(float));
246 		if (FAILED(hr)) {
247 			ERROR_LOG_REPORT(G3D, "Failed to draw stencil bit %x: %08x", i, (uint32_t)hr);
248 		}
249 	}
250 
251 	tex->Release();
252 	dxstate.stencilMask.set(0xFF);
253 	dxstate.viewport.restore();
254 	RebindFramebuffer("RebindFramebuffer stencil");
255 	gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_BLEND_STATE | DIRTY_RASTER_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
256 	return true;
257 }
258 
259 } // namespace DX9
260