CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/GPU/D3D11/DrawEngineD3D11.cpp
Views: 1401
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 <algorithm>
19
20
#include "Common/Log.h"
21
#include "Common/MemoryUtil.h"
22
#include "Common/TimeUtil.h"
23
#include "Common/Profiler/Profiler.h"
24
25
#include "Core/MemMap.h"
26
#include "Core/System.h"
27
#include "Core/Config.h"
28
#include "Core/CoreTiming.h"
29
30
#include "GPU/Math3D.h"
31
#include "GPU/GPUState.h"
32
#include "GPU/ge_constants.h"
33
34
#include "GPU/Common/TextureDecoder.h"
35
#include "GPU/Common/SplineCommon.h"
36
#include "GPU/Common/TransformCommon.h"
37
#include "GPU/Common/VertexDecoderCommon.h"
38
#include "GPU/Common/SoftwareTransformCommon.h"
39
#include "GPU/Debugger/Debugger.h"
40
#include "GPU/D3D11/FramebufferManagerD3D11.h"
41
#include "GPU/D3D11/TextureCacheD3D11.h"
42
#include "GPU/D3D11/DrawEngineD3D11.h"
43
#include "GPU/D3D11/ShaderManagerD3D11.h"
44
#include "GPU/D3D11/GPU_D3D11.h"
45
46
const D3D11_PRIMITIVE_TOPOLOGY d3d11prim[8] = {
47
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, // Points are expanded to triangles.
48
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, // Lines are expanded to triangles too.
49
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, // Lines are expanded to triangles too.
50
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST,
51
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP,
52
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, // Fans not supported
53
D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST, // Need expansion - though we could do it with geom shaders in most cases
54
};
55
56
enum {
57
VERTEX_PUSH_SIZE = 1024 * 1024 * 16,
58
INDEX_PUSH_SIZE = 1024 * 1024 * 4,
59
};
60
61
static const D3D11_INPUT_ELEMENT_DESC TransformedVertexElements[] = {
62
{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, offsetof(TransformedVertex, pos), D3D11_INPUT_PER_VERTEX_DATA, 0 },
63
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, offsetof(TransformedVertex, uv), D3D11_INPUT_PER_VERTEX_DATA, 0 },
64
{ "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, offsetof(TransformedVertex, color0), D3D11_INPUT_PER_VERTEX_DATA, 0 },
65
{ "COLOR", 1, DXGI_FORMAT_R8G8B8A8_UNORM, 0, offsetof(TransformedVertex, color1), D3D11_INPUT_PER_VERTEX_DATA, 0 },
66
{ "NORMAL", 0, DXGI_FORMAT_R32_FLOAT, 0, offsetof(TransformedVertex, fog), D3D11_INPUT_PER_VERTEX_DATA, 0 },
67
};
68
69
DrawEngineD3D11::DrawEngineD3D11(Draw::DrawContext *draw, ID3D11Device *device, ID3D11DeviceContext *context)
70
: draw_(draw),
71
device_(device),
72
context_(context),
73
inputLayoutMap_(32),
74
blendCache_(32),
75
blendCache1_(32),
76
depthStencilCache_(64),
77
rasterCache_(4) {
78
device1_ = (ID3D11Device1 *)draw->GetNativeObject(Draw::NativeObject::DEVICE_EX);
79
context1_ = (ID3D11DeviceContext1 *)draw->GetNativeObject(Draw::NativeObject::CONTEXT_EX);
80
decOptions_.expandAllWeightsToFloat = true;
81
decOptions_.expand8BitNormalsToFloat = true;
82
83
// Allocate nicely aligned memory. Maybe graphics drivers will
84
// appreciate it.
85
// All this is a LOT of memory, need to see if we can cut down somehow.
86
indexGen.Setup(decIndex_);
87
88
InitDeviceObjects();
89
90
// Vertex pushing buffers. For uniforms we use short DISCARD buffers, but we could use
91
// this kind of buffer there as well with D3D11.1. We might be able to use the same buffer
92
// for both vertices and indices, and possibly all three data types.
93
}
94
95
DrawEngineD3D11::~DrawEngineD3D11() {
96
DestroyDeviceObjects();
97
}
98
99
void DrawEngineD3D11::InitDeviceObjects() {
100
pushVerts_ = new PushBufferD3D11(device_, VERTEX_PUSH_SIZE, D3D11_BIND_VERTEX_BUFFER);
101
pushInds_ = new PushBufferD3D11(device_, INDEX_PUSH_SIZE, D3D11_BIND_INDEX_BUFFER);
102
103
tessDataTransferD3D11 = new TessellationDataTransferD3D11(context_, device_);
104
tessDataTransfer = tessDataTransferD3D11;
105
106
draw_->SetInvalidationCallback(std::bind(&DrawEngineD3D11::Invalidate, this, std::placeholders::_1));
107
}
108
109
void DrawEngineD3D11::ClearInputLayoutMap() {
110
inputLayoutMap_.Iterate([&](const InputLayoutKey &key, ID3D11InputLayout *il) {
111
if (il)
112
il->Release();
113
});
114
inputLayoutMap_.Clear();
115
}
116
117
void DrawEngineD3D11::NotifyConfigChanged() {
118
DrawEngineCommon::NotifyConfigChanged();
119
ClearInputLayoutMap();
120
}
121
122
void DrawEngineD3D11::DestroyDeviceObjects() {
123
if (draw_) {
124
draw_->SetInvalidationCallback(InvalidationCallback());
125
}
126
127
ClearTrackedVertexArrays();
128
ClearInputLayoutMap();
129
delete tessDataTransferD3D11;
130
tessDataTransferD3D11 = nullptr;
131
tessDataTransfer = nullptr;
132
delete pushVerts_;
133
delete pushInds_;
134
depthStencilCache_.Iterate([&](const uint64_t &key, ID3D11DepthStencilState *ds) {
135
ds->Release();
136
});
137
depthStencilCache_.Clear();
138
blendCache_.Iterate([&](const uint64_t &key, ID3D11BlendState *bs) {
139
bs->Release();
140
});
141
blendCache_.Clear();
142
blendCache1_.Iterate([&](const uint64_t &key, ID3D11BlendState1 *bs) {
143
bs->Release();
144
});
145
blendCache1_.Clear();
146
rasterCache_.Iterate([&](const uint32_t &key, ID3D11RasterizerState *rs) {
147
rs->Release();
148
});
149
rasterCache_.Clear();
150
}
151
152
struct DeclTypeInfo {
153
DXGI_FORMAT type;
154
const char * name;
155
};
156
157
static const DeclTypeInfo VComp[] = {
158
{ DXGI_FORMAT_UNKNOWN, "NULL" }, // DEC_NONE,
159
{ DXGI_FORMAT_R32_FLOAT, "D3DDECLTYPE_FLOAT1 " }, // DEC_FLOAT_1,
160
{ DXGI_FORMAT_R32G32_FLOAT, "D3DDECLTYPE_FLOAT2 " }, // DEC_FLOAT_2,
161
{ DXGI_FORMAT_R32G32B32_FLOAT, "D3DDECLTYPE_FLOAT3 " }, // DEC_FLOAT_3,
162
{ DXGI_FORMAT_R32G32B32A32_FLOAT, "D3DDECLTYPE_FLOAT4 " }, // DEC_FLOAT_4,
163
164
{ DXGI_FORMAT_R8G8B8A8_SNORM, "UNUSED" }, // DEC_S8_3,
165
166
{ DXGI_FORMAT_R16G16B16A16_SNORM, "D3DDECLTYPE_SHORT4N " }, // DEC_S16_3,
167
{ DXGI_FORMAT_R8G8B8A8_UNORM, "D3DDECLTYPE_UBYTE4N " }, // DEC_U8_1,
168
{ DXGI_FORMAT_R8G8B8A8_UNORM, "D3DDECLTYPE_UBYTE4N " }, // DEC_U8_2,
169
{ DXGI_FORMAT_R8G8B8A8_UNORM, "D3DDECLTYPE_UBYTE4N " }, // DEC_U8_3,
170
{ DXGI_FORMAT_R8G8B8A8_UNORM, "D3DDECLTYPE_UBYTE4N " }, // DEC_U8_4,
171
172
{ DXGI_FORMAT_UNKNOWN, "UNUSED_DEC_U16_1" }, // DEC_U16_1,
173
{ DXGI_FORMAT_UNKNOWN, "UNUSED_DEC_U16_2" }, // DEC_U16_2,
174
{ DXGI_FORMAT_R16G16B16A16_UNORM ,"D3DDECLTYPE_USHORT4N "}, // DEC_U16_3,
175
{ DXGI_FORMAT_R16G16B16A16_UNORM ,"D3DDECLTYPE_USHORT4N "}, // DEC_U16_4,
176
};
177
178
static void VertexAttribSetup(D3D11_INPUT_ELEMENT_DESC * VertexElement, u8 fmt, u8 offset, const char *semantic, u8 semantic_index = 0) {
179
memset(VertexElement, 0, sizeof(D3D11_INPUT_ELEMENT_DESC));
180
VertexElement->AlignedByteOffset = offset;
181
VertexElement->Format = VComp[fmt].type;
182
VertexElement->SemanticName = semantic;
183
VertexElement->SemanticIndex = semantic_index;
184
}
185
186
ID3D11InputLayout *DrawEngineD3D11::SetupDecFmtForDraw(D3D11VertexShader *vshader, const DecVtxFormat &decFmt, u32 pspFmt) {
187
// TODO: Instead of one for each vshader, we can reduce it to one for each type of shader
188
// that reads TEXCOORD or not, etc. Not sure if worth it.
189
const InputLayoutKey key{ vshader, decFmt.id };
190
ID3D11InputLayout *inputLayout;
191
if (inputLayoutMap_.Get(key, &inputLayout)) {
192
return inputLayout;
193
} else {
194
D3D11_INPUT_ELEMENT_DESC VertexElements[8];
195
D3D11_INPUT_ELEMENT_DESC *VertexElement = &VertexElements[0];
196
197
// Vertices Elements orders
198
// WEIGHT
199
if (decFmt.w0fmt != 0) {
200
VertexAttribSetup(VertexElement, decFmt.w0fmt, decFmt.w0off, "TEXCOORD", 1);
201
VertexElement++;
202
}
203
204
if (decFmt.w1fmt != 0) {
205
VertexAttribSetup(VertexElement, decFmt.w1fmt, decFmt.w1off, "TEXCOORD", 2);
206
VertexElement++;
207
}
208
209
// TC
210
if (decFmt.uvfmt != 0) {
211
VertexAttribSetup(VertexElement, decFmt.uvfmt, decFmt.uvoff, "TEXCOORD", 0);
212
VertexElement++;
213
}
214
215
// COLOR
216
if (decFmt.c0fmt != 0) {
217
VertexAttribSetup(VertexElement, decFmt.c0fmt, decFmt.c0off, "COLOR", 0);
218
VertexElement++;
219
}
220
// Never used ?
221
if (decFmt.c1fmt != 0) {
222
VertexAttribSetup(VertexElement, decFmt.c1fmt, decFmt.c1off, "COLOR", 1);
223
VertexElement++;
224
}
225
226
// NORMAL
227
if (decFmt.nrmfmt != 0) {
228
VertexAttribSetup(VertexElement, decFmt.nrmfmt, decFmt.nrmoff, "NORMAL", 0);
229
VertexElement++;
230
}
231
232
// POSITION
233
// Always
234
VertexAttribSetup(VertexElement, DecVtxFormat::PosFmt(), decFmt.posoff, "POSITION", 0);
235
VertexElement++;
236
237
// Create declaration
238
HRESULT hr = device_->CreateInputLayout(VertexElements, VertexElement - VertexElements, vshader->bytecode().data(), vshader->bytecode().size(), &inputLayout);
239
if (FAILED(hr)) {
240
ERROR_LOG(Log::G3D, "Failed to create input layout!");
241
inputLayout = nullptr;
242
}
243
244
// Add it to map
245
inputLayoutMap_.Insert(key, inputLayout);
246
return inputLayout;
247
}
248
}
249
250
void DrawEngineD3D11::BeginFrame() {
251
pushVerts_->Reset();
252
pushInds_->Reset();
253
254
lastRenderStepId_ = -1;
255
}
256
257
// In D3D, we're synchronous and state carries over so all we reset here on a new step is the viewport/scissor.
258
void DrawEngineD3D11::Invalidate(InvalidationCallbackFlags flags) {
259
if (flags & InvalidationCallbackFlags::RENDER_PASS_STATE) {
260
gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
261
}
262
}
263
264
// The inline wrapper in the header checks for numDrawCalls_ == 0
265
void DrawEngineD3D11::DoFlush() {
266
bool textureNeedsApply = false;
267
if (gstate_c.IsDirty(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS) && !gstate.isModeClear() && gstate.isTextureMapEnabled()) {
268
textureCache_->SetTexture();
269
gstate_c.Clean(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
270
textureNeedsApply = true;
271
} else if (gstate.getTextureAddress(0) == (gstate.getFrameBufRawAddress() | 0x04000000)) {
272
// This catches the case of clearing a texture. (#10957)
273
gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);
274
}
275
276
// This is not done on every drawcall, we collect vertex data
277
// until critical state changes. That's when we draw (flush).
278
279
GEPrimitiveType prim = prevPrim_;
280
281
// Always use software for flat shading to fix the provoking index.
282
bool tess = gstate_c.submitType == SubmitType::HW_BEZIER || gstate_c.submitType == SubmitType::HW_SPLINE;
283
bool useHWTransform = CanUseHardwareTransform(prim) && (tess || gstate.getShadeMode() != GE_SHADE_FLAT);
284
285
if (useHWTransform) {
286
ID3D11Buffer *vb_ = nullptr;
287
ID3D11Buffer *ib_ = nullptr;
288
289
int vertexCount;
290
int maxIndex;
291
bool useElements;
292
DecodeVerts(decoded_);
293
DecodeIndsAndGetData(&prim, &vertexCount, &maxIndex, &useElements, false);
294
gpuStats.numUncachedVertsDrawn += vertexCount;
295
296
bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE;
297
if (gstate.isModeThrough()) {
298
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255);
299
} else {
300
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && ((hasColor && (gstate.materialupdate & 1)) || gstate.getMaterialAmbientA() == 255) && (!gstate.isLightingEnabled() || gstate.getAmbientA() == 255);
301
}
302
303
if (textureNeedsApply) {
304
textureCache_->ApplyTexture();
305
}
306
307
// Need to ApplyDrawState after ApplyTexture because depal can launch a render pass and that wrecks the state.
308
ApplyDrawState(prim);
309
ApplyDrawStateLate(true, dynState_.stencilRef);
310
311
D3D11VertexShader *vshader;
312
D3D11FragmentShader *fshader;
313
shaderManager_->GetShaders(prim, dec_, &vshader, &fshader, pipelineState_, useHWTransform, useHWTessellation_, decOptions_.expandAllWeightsToFloat, decOptions_.applySkinInDecode);
314
ID3D11InputLayout *inputLayout = SetupDecFmtForDraw(vshader, dec_->GetDecVtxFmt(), dec_->VertexType());
315
context_->PSSetShader(fshader->GetShader(), nullptr, 0);
316
context_->VSSetShader(vshader->GetShader(), nullptr, 0);
317
shaderManager_->UpdateUniforms(framebufferManager_->UseBufferedRendering());
318
shaderManager_->BindUniforms();
319
320
context_->IASetInputLayout(inputLayout);
321
UINT stride = dec_->GetDecVtxFmt().stride;
322
context_->IASetPrimitiveTopology(d3d11prim[prim]);
323
324
if (!vb_) {
325
// Push!
326
UINT vOffset;
327
int vSize = numDecodedVerts_ * dec_->GetDecVtxFmt().stride;
328
uint8_t *vptr = pushVerts_->BeginPush(context_, &vOffset, vSize);
329
memcpy(vptr, decoded_, vSize);
330
pushVerts_->EndPush(context_);
331
ID3D11Buffer *buf = pushVerts_->Buf();
332
context_->IASetVertexBuffers(0, 1, &buf, &stride, &vOffset);
333
if (useElements) {
334
UINT iOffset;
335
int iSize = 2 * vertexCount;
336
uint8_t *iptr = pushInds_->BeginPush(context_, &iOffset, iSize);
337
memcpy(iptr, decIndex_, iSize);
338
pushInds_->EndPush(context_);
339
context_->IASetIndexBuffer(pushInds_->Buf(), DXGI_FORMAT_R16_UINT, iOffset);
340
context_->DrawIndexed(vertexCount, 0, 0);
341
} else {
342
context_->Draw(vertexCount, 0);
343
}
344
} else {
345
UINT offset = 0;
346
context_->IASetVertexBuffers(0, 1, &vb_, &stride, &offset);
347
if (useElements) {
348
context_->IASetIndexBuffer(ib_, DXGI_FORMAT_R16_UINT, 0);
349
context_->DrawIndexed(vertexCount, 0, 0);
350
} else {
351
context_->Draw(vertexCount, 0);
352
}
353
}
354
} else {
355
PROFILE_THIS_SCOPE("soft");
356
if (!decOptions_.applySkinInDecode) {
357
decOptions_.applySkinInDecode = true;
358
lastVType_ |= (1 << 26);
359
dec_ = GetVertexDecoder(lastVType_);
360
}
361
DecodeVerts(decoded_);
362
int vertexCount = DecodeInds();
363
364
bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE;
365
if (gstate.isModeThrough()) {
366
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255);
367
} else {
368
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && ((hasColor && (gstate.materialupdate & 1)) || gstate.getMaterialAmbientA() == 255) && (!gstate.isLightingEnabled() || gstate.getAmbientA() == 255);
369
}
370
371
gpuStats.numUncachedVertsDrawn += vertexCount;
372
prim = IndexGenerator::GeneralPrim((GEPrimitiveType)drawInds_[0].prim);
373
VERBOSE_LOG(Log::G3D, "Flush prim %i SW! %i verts in one go", prim, vertexCount);
374
375
u16 *inds = decIndex_;
376
SoftwareTransformResult result{};
377
SoftwareTransformParams params{};
378
params.decoded = decoded_;
379
params.transformed = transformed_;
380
params.transformedExpanded = transformedExpanded_;
381
params.fbman = framebufferManager_;
382
params.texCache = textureCache_;
383
params.allowClear = true;
384
params.allowSeparateAlphaClear = false; // D3D11 doesn't support separate alpha clears
385
params.flippedY = false;
386
params.usesHalfZ = true;
387
388
if (gstate.getShadeMode() == GE_SHADE_FLAT) {
389
// We need to rotate the index buffer to simulate a different provoking vertex.
390
// We do this before line expansion etc.
391
IndexBufferProvokingLastToFirst(prim, inds, vertexCount);
392
}
393
394
// We need correct viewport values in gstate_c already.
395
if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {
396
ViewportAndScissor vpAndScissor;
397
ConvertViewportAndScissor(framebufferManager_->UseBufferedRendering(),
398
framebufferManager_->GetRenderWidth(), framebufferManager_->GetRenderHeight(),
399
framebufferManager_->GetTargetBufferWidth(), framebufferManager_->GetTargetBufferHeight(),
400
vpAndScissor);
401
UpdateCachedViewportState(vpAndScissor);
402
}
403
404
SoftwareTransform swTransform(params);
405
406
const Lin::Vec3 trans(gstate_c.vpXOffset, -gstate_c.vpYOffset, gstate_c.vpZOffset * 0.5f + 0.5f);
407
const Lin::Vec3 scale(gstate_c.vpWidthScale, -gstate_c.vpHeightScale, gstate_c.vpDepthScale * 0.5f);
408
swTransform.SetProjMatrix(gstate.projMatrix, gstate_c.vpWidth < 0, gstate_c.vpHeight < 0, trans, scale);
409
410
swTransform.Transform(prim, dec_->VertexType(), dec_->GetDecVtxFmt(), numDecodedVerts_, &result);
411
// Non-zero depth clears are unusual, but some drivers don't match drawn depth values to cleared values.
412
// Games sometimes expect exact matches (see #12626, for example) for equal comparisons.
413
if (result.action == SW_CLEAR && everUsedEqualDepth_ && gstate.isClearModeDepthMask() && result.depth > 0.0f && result.depth < 1.0f)
414
result.action = SW_NOT_READY;
415
416
if (textureNeedsApply) {
417
gstate_c.pixelMapped = result.pixelMapped;
418
textureCache_->ApplyTexture();
419
gstate_c.pixelMapped = false;
420
}
421
422
// Need to ApplyDrawState after ApplyTexture because depal can launch a render pass and that wrecks the state.
423
ApplyDrawState(prim);
424
425
if (result.action == SW_NOT_READY)
426
swTransform.BuildDrawingParams(prim, vertexCount, dec_->VertexType(), inds, RemainingIndices(inds), numDecodedVerts_, VERTEX_BUFFER_MAX, &result);
427
if (result.setSafeSize)
428
framebufferManager_->SetSafeSize(result.safeWidth, result.safeHeight);
429
430
ApplyDrawStateLate(result.setStencil, result.stencilValue);
431
432
if (result.action == SW_DRAW_INDEXED) {
433
D3D11VertexShader *vshader;
434
D3D11FragmentShader *fshader;
435
shaderManager_->GetShaders(prim, dec_, &vshader, &fshader, pipelineState_, false, false, decOptions_.expandAllWeightsToFloat, true);
436
context_->PSSetShader(fshader->GetShader(), nullptr, 0);
437
context_->VSSetShader(vshader->GetShader(), nullptr, 0);
438
shaderManager_->UpdateUniforms(framebufferManager_->UseBufferedRendering());
439
shaderManager_->BindUniforms();
440
441
// We really do need a vertex layout for each vertex shader (or at least check its ID bits for what inputs it uses)!
442
// Some vertex shaders ignore one of the inputs, and then the layout created from it will lack it, which will be a problem for others.
443
InputLayoutKey key{ vshader, 0xFFFFFFFF }; // Let's use 0xFFFFFFFF to signify TransformedVertex
444
ID3D11InputLayout *layout;
445
if (!inputLayoutMap_.Get(key, &layout)) {
446
ASSERT_SUCCESS(device_->CreateInputLayout(TransformedVertexElements, ARRAY_SIZE(TransformedVertexElements), vshader->bytecode().data(), vshader->bytecode().size(), &layout));
447
inputLayoutMap_.Insert(key, layout);
448
}
449
context_->IASetInputLayout(layout);
450
context_->IASetPrimitiveTopology(d3d11prim[prim]);
451
452
UINT stride = sizeof(TransformedVertex);
453
UINT vOffset = 0;
454
int vSize = numDecodedVerts_ * stride;
455
uint8_t *vptr = pushVerts_->BeginPush(context_, &vOffset, vSize);
456
memcpy(vptr, result.drawBuffer, vSize);
457
pushVerts_->EndPush(context_);
458
ID3D11Buffer *buf = pushVerts_->Buf();
459
context_->IASetVertexBuffers(0, 1, &buf, &stride, &vOffset);
460
UINT iOffset;
461
int iSize = sizeof(uint16_t) * result.drawNumTrans;
462
uint8_t *iptr = pushInds_->BeginPush(context_, &iOffset, iSize);
463
memcpy(iptr, inds, iSize);
464
pushInds_->EndPush(context_);
465
context_->IASetIndexBuffer(pushInds_->Buf(), DXGI_FORMAT_R16_UINT, iOffset);
466
context_->DrawIndexed(result.drawNumTrans, 0, 0);
467
} else if (result.action == SW_CLEAR) {
468
u32 clearColor = result.color;
469
float clearDepth = result.depth;
470
471
uint32_t clearFlag = 0;
472
473
if (gstate.isClearModeColorMask()) clearFlag |= Draw::FBChannel::FB_COLOR_BIT;
474
if (gstate.isClearModeAlphaMask()) clearFlag |= Draw::FBChannel::FB_STENCIL_BIT;
475
if (gstate.isClearModeDepthMask()) clearFlag |= Draw::FBChannel::FB_DEPTH_BIT;
476
477
if (clearFlag & Draw::FBChannel::FB_COLOR_BIT) {
478
framebufferManager_->SetColorUpdated(gstate_c.skipDrawReason);
479
}
480
481
uint8_t clearStencil = clearColor >> 24;
482
draw_->Clear(clearFlag, clearColor, clearDepth, clearStencil);
483
484
if (gstate_c.Use(GPU_USE_CLEAR_RAM_HACK) && gstate.isClearModeColorMask() && (gstate.isClearModeAlphaMask() || gstate_c.framebufFormat == GE_FORMAT_565)) {
485
int scissorX1 = gstate.getScissorX1();
486
int scissorY1 = gstate.getScissorY1();
487
int scissorX2 = gstate.getScissorX2() + 1;
488
int scissorY2 = gstate.getScissorY2() + 1;
489
framebufferManager_->ApplyClearToMemory(scissorX1, scissorY1, scissorX2, scissorY2, clearColor);
490
}
491
}
492
decOptions_.applySkinInDecode = g_Config.bSoftwareSkinning;
493
}
494
495
ResetAfterDrawInline();
496
framebufferManager_->SetColorUpdated(gstate_c.skipDrawReason);
497
GPUDebug::NotifyDraw();
498
}
499
500
TessellationDataTransferD3D11::TessellationDataTransferD3D11(ID3D11DeviceContext *context, ID3D11Device *device)
501
: context_(context), device_(device) {
502
desc.Usage = D3D11_USAGE_DYNAMIC;
503
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
504
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
505
desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
506
}
507
508
TessellationDataTransferD3D11::~TessellationDataTransferD3D11() {
509
for (int i = 0; i < 3; ++i) {
510
if (buf[i]) buf[i]->Release();
511
if (view[i]) view[i]->Release();
512
}
513
}
514
515
template <typename T>
516
static void DoRelease(T *&ptr) {
517
if (ptr) {
518
ptr->Release();
519
ptr = nullptr;
520
}
521
}
522
523
void TessellationDataTransferD3D11::SendDataToShader(const SimpleVertex *const *points, int size_u, int size_v, u32 vertType, const Spline::Weight2D &weights) {
524
struct TessData {
525
float pos[3]; float pad1;
526
float uv[2]; float pad2[2];
527
float color[4];
528
};
529
530
int size = size_u * size_v;
531
532
if (prevSize < size || !buf[0]) {
533
prevSize = size;
534
DoRelease(buf[0]);
535
DoRelease(view[0]);
536
537
desc.ByteWidth = size * sizeof(TessData);
538
desc.StructureByteStride = sizeof(TessData);
539
device_->CreateBuffer(&desc, nullptr, &buf[0]);
540
if (buf[0])
541
device_->CreateShaderResourceView(buf[0], nullptr, &view[0]);
542
if (!buf[0] || !view[0])
543
return;
544
context_->VSSetShaderResources(0, 1, &view[0]);
545
}
546
D3D11_MAPPED_SUBRESOURCE map{};
547
HRESULT hr = context_->Map(buf[0], 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
548
if (FAILED(hr))
549
return;
550
uint8_t *data = (uint8_t *)map.pData;
551
552
float *pos = (float *)(data);
553
float *tex = (float *)(data + offsetof(TessData, uv));
554
float *col = (float *)(data + offsetof(TessData, color));
555
int stride = sizeof(TessData) / sizeof(float);
556
557
CopyControlPoints(pos, tex, col, stride, stride, stride, points, size, vertType);
558
559
context_->Unmap(buf[0], 0);
560
561
using Spline::Weight;
562
563
// Weights U
564
if (prevSizeWU < weights.size_u || !buf[1]) {
565
prevSizeWU = weights.size_u;
566
DoRelease(buf[1]);
567
DoRelease(view[1]);
568
569
desc.ByteWidth = weights.size_u * sizeof(Weight);
570
desc.StructureByteStride = sizeof(Weight);
571
device_->CreateBuffer(&desc, nullptr, &buf[1]);
572
if (buf[1])
573
device_->CreateShaderResourceView(buf[1], nullptr, &view[1]);
574
if (!buf[1] || !view[1])
575
return;
576
context_->VSSetShaderResources(1, 1, &view[1]);
577
}
578
hr = context_->Map(buf[1], 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
579
if (SUCCEEDED(hr))
580
memcpy(map.pData, weights.u, weights.size_u * sizeof(Weight));
581
context_->Unmap(buf[1], 0);
582
583
// Weights V
584
if (prevSizeWV < weights.size_v) {
585
prevSizeWV = weights.size_v;
586
DoRelease(buf[2]);
587
DoRelease(view[2]);
588
589
desc.ByteWidth = weights.size_v * sizeof(Weight);
590
desc.StructureByteStride = sizeof(Weight);
591
device_->CreateBuffer(&desc, nullptr, &buf[2]);
592
if (buf[2])
593
device_->CreateShaderResourceView(buf[2], nullptr, &view[2]);
594
if (!buf[2] || !view[2])
595
return;
596
context_->VSSetShaderResources(2, 1, &view[2]);
597
}
598
hr = context_->Map(buf[2], 0, D3D11_MAP_WRITE_DISCARD, 0, &map);
599
if (SUCCEEDED(hr))
600
memcpy(map.pData, weights.v, weights.size_v * sizeof(Weight));
601
context_->Unmap(buf[2], 0);
602
}
603
604