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 <set>
19 
20 #include "Common/Serialize/Serializer.h"
21 #include "Common/GraphicsContext.h"
22 #include "Common/System/System.h"
23 #include "Common/Profiler/Profiler.h"
24 #include "Common/Data/Text/I18n.h"
25 #include "Core/Debugger/Breakpoints.h"
26 #include "Core/MemMapHelpers.h"
27 #include "Core/MIPS/MIPS.h"
28 #include "Core/Host.h"
29 #include "Core/Config.h"
30 #include "Core/ConfigValues.h"
31 #include "Core/Reporting.h"
32 #include "Core/System.h"
33 
34 #include "Common/GPU/D3D9/D3D9StateCache.h"
35 
36 #include "GPU/GPUState.h"
37 #include "GPU/ge_constants.h"
38 #include "GPU/GeDisasm.h"
39 
40 #include "GPU/Common/FramebufferManagerCommon.h"
41 #include "GPU/Debugger/Debugger.h"
42 #include "GPU/Directx9/ShaderManagerDX9.h"
43 #include "GPU/Directx9/GPU_DX9.h"
44 #include "GPU/Directx9/FramebufferManagerDX9.h"
45 #include "GPU/Directx9/DrawEngineDX9.h"
46 #include "GPU/Directx9/TextureCacheDX9.h"
47 
48 #include "Core/HLE/sceKernelThread.h"
49 #include "Core/HLE/sceKernelInterrupt.h"
50 #include "Core/HLE/sceGe.h"
51 
52 namespace DX9 {
53 
GPU_DX9(GraphicsContext * gfxCtx,Draw::DrawContext * draw)54 GPU_DX9::GPU_DX9(GraphicsContext *gfxCtx, Draw::DrawContext *draw)
55 	: GPUCommon(gfxCtx, draw),
56 		depalShaderCache_(draw),
57 		drawEngine_(draw) {
58 	device_ = (LPDIRECT3DDEVICE9)draw->GetNativeObject(Draw::NativeObject::DEVICE);
59 	deviceEx_ = (LPDIRECT3DDEVICE9EX)draw->GetNativeObject(Draw::NativeObject::DEVICE_EX);
60 
61 	shaderManagerDX9_ = new ShaderManagerDX9(draw, device_);
62 	framebufferManagerDX9_ = new FramebufferManagerDX9(draw);
63 	framebufferManager_ = framebufferManagerDX9_;
64 	textureCacheDX9_ = new TextureCacheDX9(draw);
65 	textureCache_ = textureCacheDX9_;
66 	drawEngineCommon_ = &drawEngine_;
67 	shaderManager_ = shaderManagerDX9_;
68 
69 	drawEngine_.SetShaderManager(shaderManagerDX9_);
70 	drawEngine_.SetTextureCache(textureCacheDX9_);
71 	drawEngine_.SetFramebufferManager(framebufferManagerDX9_);
72 	drawEngine_.Init();
73 	framebufferManagerDX9_->SetTextureCache(textureCacheDX9_);
74 	framebufferManagerDX9_->SetShaderManager(shaderManagerDX9_);
75 	framebufferManagerDX9_->SetDrawEngine(&drawEngine_);
76 	framebufferManagerDX9_->Init();
77 	textureCacheDX9_->SetFramebufferManager(framebufferManagerDX9_);
78 	textureCacheDX9_->SetDepalShaderCache(&depalShaderCache_);
79 	textureCacheDX9_->SetShaderManager(shaderManagerDX9_);
80 
81 	// Sanity check gstate
82 	if ((int *)&gstate.transferstart - (int *)&gstate != 0xEA) {
83 		ERROR_LOG(G3D, "gstate has drifted out of sync!");
84 	}
85 
86 	// No need to flush before the tex scale/offset commands if we are baking
87 	// the tex scale/offset into the vertices anyway.
88 	UpdateCmdInfo();
89 	CheckGPUFeatures();
90 
91 	BuildReportingInfo();
92 
93 	// Some of our defaults are different from hw defaults, let's assert them.
94 	// We restore each frame anyway, but here is convenient for tests.
95 	dxstate.Restore();
96 	textureCache_->NotifyConfigChanged();
97 
98 	if (g_Config.bHardwareTessellation) {
99 		// Disable hardware tessellation bacause DX9 is still unsupported.
100 		ERROR_LOG(G3D, "Hardware Tessellation is unsupported, falling back to software tessellation");
101 		auto gr = GetI18NCategory("Graphics");
102 		host->NotifyUserMessage(gr->T("Turn off Hardware Tessellation - unsupported"), 2.5f, 0xFF3030FF);
103 	}
104 }
105 
106 // TODO: Move this detection elsewhere when it's needed elsewhere, not before. It's ugly.
107 // Source: https://envytools.readthedocs.io/en/latest/hw/pciid.html#gf100
108 enum NVIDIAGeneration {
109 	NV_PRE_KEPLER,
110 	NV_KEPLER,
111 	NV_MAXWELL,
112 	NV_PASCAL,
113 	NV_VOLTA,
114 	NV_TURING,  // or later
115 };
116 
NVIDIAGetDeviceGeneration(int deviceID)117 static NVIDIAGeneration NVIDIAGetDeviceGeneration(int deviceID) {
118 	if (deviceID >= 0x1180 && deviceID <= 0x11bf)
119 		return NV_KEPLER;  // GK104
120 	if (deviceID >= 0x11c0 && deviceID <= 0x11fa)
121 		return NV_KEPLER;  // GK106
122 	if (deviceID >= 0x0fc0 && deviceID <= 0x0fff)
123 		return NV_KEPLER;  // GK107
124 	if (deviceID >= 0x1003 && deviceID <= 0x1028)
125 		return NV_KEPLER;  // GK110(B)
126 	if (deviceID >= 0x1280 && deviceID <= 0x12ba)
127 		return NV_KEPLER;  // GK208
128 	if (deviceID >= 0x1381 && deviceID <= 0x13b0)
129 		return NV_MAXWELL;  // GM107
130 	if (deviceID >= 0x1340 && deviceID <= 0x134d)
131 		return NV_MAXWELL;  // GM108
132 	if (deviceID >= 0x13c0 && deviceID <= 0x13d9)
133 		return NV_MAXWELL;  // GM204
134 	if (deviceID >= 0x1401 && deviceID <= 0x1427)
135 		return NV_MAXWELL;  // GM206
136 	if (deviceID >= 0x15f7 && deviceID <= 0x15f9)
137 		return NV_PASCAL;  // GP100
138 	if (deviceID >= 0x15f7 && deviceID <= 0x15f9)
139 		return NV_PASCAL;  // GP100
140 	if (deviceID >= 0x1b00 && deviceID <= 0x1b38)
141 		return NV_PASCAL;  // GP102
142 	if (deviceID >= 0x1b80 && deviceID <= 0x1be1)
143 		return NV_PASCAL;  // GP104
144 	if (deviceID >= 0x1c02 && deviceID <= 0x1c62)
145 		return NV_PASCAL;  // GP106
146 	if (deviceID >= 0x1c81 && deviceID <= 0x1c92)
147 		return NV_PASCAL;  // GP107
148 	if (deviceID >= 0x1d01 && deviceID <= 0x1d12)
149 		return NV_PASCAL;  // GP108
150 	if (deviceID >= 0x1d81 && deviceID <= 0x1dba)
151 		return NV_VOLTA;   // GV100
152 	if (deviceID >= 0x1e02 && deviceID <= 0x1e3c)
153 		return NV_TURING;  // TU102
154 	if (deviceID >= 0x1e82 && deviceID <= 0x1ed0)
155 		return NV_TURING;  // TU104
156 	if (deviceID >= 0x1f02 && deviceID <= 0x1f51)
157 		return NV_TURING;  // TU104
158 	if (deviceID >= 0x1e02)
159 		return NV_TURING;  // More TU models or later, probably.
160 	return NV_PRE_KEPLER;
161 }
162 
CheckGPUFeatures()163 void GPU_DX9::CheckGPUFeatures() {
164 	u32 features = 0;
165 	features |= GPU_SUPPORTS_16BIT_FORMATS;
166 	features |= GPU_SUPPORTS_BLEND_MINMAX;
167 	features |= GPU_SUPPORTS_TEXTURE_LOD_CONTROL;
168 
169 	// Accurate depth is required because the Direct3D API does not support inverse Z.
170 	// So we cannot incorrectly use the viewport transform as the depth range on Direct3D.
171 	// TODO: Breaks text in PaRappa for some reason?
172 	features |= GPU_SUPPORTS_ACCURATE_DEPTH;
173 
174 	auto vendor = draw_->GetDeviceCaps().vendor;
175 	if (!PSP_CoreParameter().compat.flags().DisableRangeCulling) {
176 		// VS range culling (killing triangles in the vertex shader using NaN) causes problems on Intel.
177 		// Also causes problems on old NVIDIA.
178 		switch (vendor) {
179 		case Draw::GPUVendor::VENDOR_INTEL:
180 			break;
181 		case Draw::GPUVendor::VENDOR_NVIDIA:
182 			// Older NVIDIAs don't seem to like NaNs in their DX9 vertex shaders.
183 			// No idea if KEPLER is the right cutoff, but let's go with it.
184 			if (NVIDIAGetDeviceGeneration(draw_->GetDeviceCaps().deviceID) >= NV_KEPLER) {
185 				features |= GPU_SUPPORTS_VS_RANGE_CULLING;
186 			}
187 			break;
188 		default:
189 			features |= GPU_SUPPORTS_VS_RANGE_CULLING;
190 			break;
191 		}
192 	}
193 
194 	D3DCAPS9 caps;
195 	ZeroMemory(&caps, sizeof(caps));
196 	HRESULT result = 0;
197 	if (deviceEx_) {
198 		result = deviceEx_->GetDeviceCaps(&caps);
199 	} else {
200 		result = device_->GetDeviceCaps(&caps);
201 	}
202 	if (FAILED(result)) {
203 		WARN_LOG_REPORT(G3D, "Direct3D9: Failed to get the device caps!");
204 	} else {
205 		if ((caps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) != 0 && caps.MaxAnisotropy > 1)
206 			features |= GPU_SUPPORTS_ANISOTROPY;
207 		if ((caps.TextureCaps & (D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_POW2)) == 0)
208 			features |= GPU_SUPPORTS_TEXTURE_NPOT;
209 	}
210 
211 	if (!g_Config.bHighQualityDepth) {
212 		features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT;
213 	} else if (PSP_CoreParameter().compat.flags().PixelDepthRounding) {
214 		// Assume we always have a 24-bit depth buffer.
215 		features |= GPU_SCALE_DEPTH_FROM_24BIT_TO_16BIT;
216 	} else if (PSP_CoreParameter().compat.flags().VertexDepthRounding) {
217 		features |= GPU_ROUND_DEPTH_TO_16BIT;
218 	}
219 
220 	if (PSP_CoreParameter().compat.flags().ClearToRAM) {
221 		features |= GPU_USE_CLEAR_RAM_HACK;
222 	}
223 
224 	gstate_c.featureFlags = features;
225 }
226 
~GPU_DX9()227 GPU_DX9::~GPU_DX9() {
228 	framebufferManagerDX9_->DestroyAllFBOs();
229 	delete framebufferManagerDX9_;
230 	delete textureCache_;
231 	shaderManagerDX9_->ClearCache(true);
232 	delete shaderManagerDX9_;
233 }
234 
235 // Needs to be called on GPU thread, not reporting thread.
BuildReportingInfo()236 void GPU_DX9::BuildReportingInfo() {
237 	using namespace Draw;
238 	DrawContext *thin3d = gfxCtx_->GetDrawContext();
239 
240 	reportingPrimaryInfo_ = thin3d->GetInfoString(InfoField::VENDORSTRING);
241 	reportingFullInfo_ = reportingPrimaryInfo_ + " - " + System_GetProperty(SYSPROP_GPUDRIVER_VERSION) + " - " + thin3d->GetInfoString(InfoField::SHADELANGVERSION);
242 }
243 
DeviceLost()244 void GPU_DX9::DeviceLost() {
245 	// Simply drop all caches and textures.
246 	shaderManagerDX9_->ClearCache(false);
247 	textureCacheDX9_->Clear(false);
248 	GPUCommon::DeviceLost();
249 }
250 
DeviceRestore()251 void GPU_DX9::DeviceRestore() {
252 	GPUCommon::DeviceRestore();
253 	// Nothing needed.
254 }
255 
InitClear()256 void GPU_DX9::InitClear() {
257 	if (!framebufferManager_->UseBufferedRendering()) {
258 		dxstate.depthWrite.set(true);
259 		dxstate.colorMask.set(true, true, true, true);
260 		device_->Clear(0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.f, 0);
261 	}
262 }
263 
BeginHostFrame()264 void GPU_DX9::BeginHostFrame() {
265 	GPUCommon::BeginHostFrame();
266 	UpdateCmdInfo();
267 	if (resized_) {
268 		CheckGPUFeatures();
269 		framebufferManager_->Resized();
270 		drawEngine_.Resized();
271 		shaderManagerDX9_->DirtyShader();
272 		textureCacheDX9_->NotifyConfigChanged();
273 		resized_ = false;
274 	}
275 }
276 
ReapplyGfxState()277 void GPU_DX9::ReapplyGfxState() {
278 	dxstate.Restore();
279 	GPUCommon::ReapplyGfxState();
280 }
281 
BeginFrame()282 void GPU_DX9::BeginFrame() {
283 	textureCacheDX9_->StartFrame();
284 	drawEngine_.BeginFrame();
285 	depalShaderCache_.Decimate();
286 	// fragmentTestCache_.Decimate();
287 
288 	GPUCommon::BeginFrame();
289 	shaderManagerDX9_->DirtyShader();
290 
291 	framebufferManager_->BeginFrame();
292 }
293 
SetDisplayFramebuffer(u32 framebuf,u32 stride,GEBufferFormat format)294 void GPU_DX9::SetDisplayFramebuffer(u32 framebuf, u32 stride, GEBufferFormat format) {
295 	GPUDebug::NotifyDisplay(framebuf, stride, format);
296 	framebufferManagerDX9_->SetDisplayFramebuffer(framebuf, stride, format);
297 }
298 
CopyDisplayToOutput(bool reallyDirty)299 void GPU_DX9::CopyDisplayToOutput(bool reallyDirty) {
300 	dxstate.depthWrite.set(true);
301 	dxstate.colorMask.set(true, true, true, true);
302 
303 	drawEngine_.Flush();
304 
305 	framebufferManagerDX9_->CopyDisplayToOutput(reallyDirty);
306 	framebufferManagerDX9_->EndFrame();
307 
308 	// shaderManager_->EndFrame();
309 	shaderManagerDX9_->DirtyLastShader();
310 
311 	gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);
312 }
313 
FinishDeferred()314 void GPU_DX9::FinishDeferred() {
315 	// This finishes reading any vertex data that is pending.
316 	drawEngine_.FinishDeferred();
317 }
318 
CheckFlushOp(int cmd,u32 diff)319 inline void GPU_DX9::CheckFlushOp(int cmd, u32 diff) {
320 	const u8 cmdFlags = cmdInfo_[cmd].flags;
321 	if (diff && (cmdFlags & FLAG_FLUSHBEFOREONCHANGE)) {
322 		if (dumpThisFrame_) {
323 			NOTICE_LOG(G3D, "================ FLUSH ================");
324 		}
325 		drawEngine_.Flush();
326 	}
327 }
328 
PreExecuteOp(u32 op,u32 diff)329 void GPU_DX9::PreExecuteOp(u32 op, u32 diff) {
330 	CheckFlushOp(op >> 24, diff);
331 }
332 
ExecuteOp(u32 op,u32 diff)333 void GPU_DX9::ExecuteOp(u32 op, u32 diff) {
334 	const u8 cmd = op >> 24;
335 	const CommandInfo info = cmdInfo_[cmd];
336 	const u8 cmdFlags = info.flags;
337 	if ((cmdFlags & FLAG_EXECUTE) || (diff && (cmdFlags & FLAG_EXECUTEONCHANGE))) {
338 		(this->*info.func)(op, diff);
339 	} else if (diff) {
340 		uint64_t dirty = info.flags >> 8;
341 		if (dirty)
342 			gstate_c.Dirty(dirty);
343 	}
344 }
345 
GetStats(char * buffer,size_t bufsize)346 void GPU_DX9::GetStats(char *buffer, size_t bufsize) {
347 	size_t offset = FormatGPUStatsCommon(buffer, bufsize);
348 	buffer += offset;
349 	bufsize -= offset;
350 	if ((int)bufsize < 0)
351 		return;
352 	snprintf(buffer, bufsize,
353 		"Vertex, Fragment shaders loaded: %i, %i\n",
354 		shaderManagerDX9_->GetNumVertexShaders(),
355 		shaderManagerDX9_->GetNumFragmentShaders()
356 	);
357 }
358 
ClearCacheNextFrame()359 void GPU_DX9::ClearCacheNextFrame() {
360 	textureCacheDX9_->ClearNextFrame();
361 }
362 
ClearShaderCache()363 void GPU_DX9::ClearShaderCache() {
364 	shaderManagerDX9_->ClearCache(true);
365 }
366 
DoState(PointerWrap & p)367 void GPU_DX9::DoState(PointerWrap &p) {
368 	GPUCommon::DoState(p);
369 
370 	// TODO: Some of these things may not be necessary.
371 	// None of these are necessary when saving.
372 	if (p.mode == p.MODE_READ && !PSP_CoreParameter().frozen) {
373 		textureCache_->Clear(true);
374 		depalShaderCache_.Clear();
375 		drawEngine_.ClearTrackedVertexArrays();
376 
377 		gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);
378 		framebufferManager_->DestroyAllFBOs();
379 	}
380 }
381 
DebugGetShaderIDs(DebugShaderType type)382 std::vector<std::string> GPU_DX9::DebugGetShaderIDs(DebugShaderType type) {
383 	switch (type) {
384 	case SHADER_TYPE_VERTEXLOADER:
385 		return drawEngine_.DebugGetVertexLoaderIDs();
386 	case SHADER_TYPE_DEPAL:
387 		return depalShaderCache_.DebugGetShaderIDs(type);
388 	default:
389 		return shaderManagerDX9_->DebugGetShaderIDs(type);
390 	}
391 }
392 
DebugGetShaderString(std::string id,DebugShaderType type,DebugShaderStringType stringType)393 std::string GPU_DX9::DebugGetShaderString(std::string id, DebugShaderType type, DebugShaderStringType stringType) {
394 	switch (type) {
395 	case SHADER_TYPE_VERTEXLOADER:
396 		return drawEngine_.DebugGetVertexLoaderString(id, stringType);
397 	case SHADER_TYPE_DEPAL:
398 		return depalShaderCache_.DebugGetShaderString(id, type, stringType);
399 	default:
400 		return shaderManagerDX9_->DebugGetShaderString(id, type, stringType);
401 	}
402 }
403 
404 }  // namespace DX9
405