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/GLES/DrawEngineGLES.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/LogReporting.h"
21
#include "Common/MemoryUtil.h"
22
#include "Common/TimeUtil.h"
23
#include "Core/MemMap.h"
24
#include "Core/System.h"
25
#include "Core/Config.h"
26
#include "Core/CoreTiming.h"
27
28
#include "Common/GPU/OpenGL/GLDebugLog.h"
29
#include "Common/Profiler/Profiler.h"
30
31
#include "GPU/Math3D.h"
32
#include "GPU/GPUState.h"
33
#include "GPU/ge_constants.h"
34
35
#include "GPU/Common/TextureDecoder.h"
36
#include "GPU/Common/SplineCommon.h"
37
#include "GPU/Common/VertexDecoderCommon.h"
38
#include "GPU/Common/SoftwareTransformCommon.h"
39
#include "GPU/Debugger/Debugger.h"
40
#include "GPU/GLES/FragmentTestCacheGLES.h"
41
#include "GPU/GLES/StateMappingGLES.h"
42
#include "GPU/GLES/TextureCacheGLES.h"
43
#include "GPU/GLES/DrawEngineGLES.h"
44
#include "GPU/GLES/ShaderManagerGLES.h"
45
#include "GPU/GLES/GPU_GLES.h"
46
47
static const GLuint glprim[8] = {
48
// Points, which are expanded to triangles.
49
GL_TRIANGLES,
50
// Lines and line strips, which are also expanded to triangles.
51
GL_TRIANGLES,
52
GL_TRIANGLES,
53
GL_TRIANGLES,
54
GL_TRIANGLE_STRIP,
55
GL_TRIANGLE_FAN,
56
// Rectangles, which are expanded to triangles.
57
GL_TRIANGLES,
58
};
59
60
enum {
61
TRANSFORMED_VERTEX_BUFFER_SIZE = VERTEX_BUFFER_MAX * sizeof(TransformedVertex)
62
};
63
64
DrawEngineGLES::DrawEngineGLES(Draw::DrawContext *draw) : inputLayoutMap_(16), draw_(draw) {
65
render_ = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
66
67
decOptions_.expandAllWeightsToFloat = false;
68
decOptions_.expand8BitNormalsToFloat = false;
69
70
indexGen.Setup(decIndex_);
71
72
InitDeviceObjects();
73
74
tessDataTransferGLES = new TessellationDataTransferGLES(render_);
75
tessDataTransfer = tessDataTransferGLES;
76
}
77
78
DrawEngineGLES::~DrawEngineGLES() {
79
DestroyDeviceObjects();
80
81
delete tessDataTransferGLES;
82
}
83
84
void DrawEngineGLES::DeviceLost() {
85
DestroyDeviceObjects();
86
draw_ = nullptr;
87
render_ = nullptr;
88
}
89
90
void DrawEngineGLES::DeviceRestore(Draw::DrawContext *draw) {
91
draw_ = draw;
92
render_ = (GLRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER);
93
InitDeviceObjects();
94
}
95
96
void DrawEngineGLES::InitDeviceObjects() {
97
_assert_msg_(render_ != nullptr, "Render manager must be set");
98
99
for (int i = 0; i < GLRenderManager::MAX_INFLIGHT_FRAMES; i++) {
100
frameData_[i].pushVertex = render_->CreatePushBuffer(i, GL_ARRAY_BUFFER, 2048 * 1024, "game_vertex");
101
frameData_[i].pushIndex = render_->CreatePushBuffer(i, GL_ELEMENT_ARRAY_BUFFER, 256 * 1024, "game_index");
102
}
103
104
int stride = sizeof(TransformedVertex);
105
std::vector<GLRInputLayout::Entry> entries;
106
entries.push_back({ ATTR_POSITION, 4, GL_FLOAT, GL_FALSE, offsetof(TransformedVertex, x) });
107
entries.push_back({ ATTR_TEXCOORD, 3, GL_FLOAT, GL_FALSE, offsetof(TransformedVertex, u) });
108
entries.push_back({ ATTR_COLOR0, 4, GL_UNSIGNED_BYTE, GL_TRUE, offsetof(TransformedVertex, color0) });
109
entries.push_back({ ATTR_COLOR1, 3, GL_UNSIGNED_BYTE, GL_TRUE, offsetof(TransformedVertex, color1) });
110
entries.push_back({ ATTR_NORMAL, 1, GL_FLOAT, GL_FALSE, offsetof(TransformedVertex, fog) });
111
softwareInputLayout_ = render_->CreateInputLayout(entries, stride);
112
113
draw_->SetInvalidationCallback(std::bind(&DrawEngineGLES::Invalidate, this, std::placeholders::_1));
114
}
115
116
void DrawEngineGLES::DestroyDeviceObjects() {
117
if (!draw_) {
118
return;
119
}
120
draw_->SetInvalidationCallback(InvalidationCallback());
121
122
// Beware: this could be called twice in a row, sometimes.
123
for (int i = 0; i < GLRenderManager::MAX_INFLIGHT_FRAMES; i++) {
124
if (!frameData_[i].pushVertex && !frameData_[i].pushIndex)
125
continue;
126
127
if (frameData_[i].pushVertex)
128
render_->DeletePushBuffer(frameData_[i].pushVertex);
129
if (frameData_[i].pushIndex)
130
render_->DeletePushBuffer(frameData_[i].pushIndex);
131
frameData_[i].pushVertex = nullptr;
132
frameData_[i].pushIndex = nullptr;
133
}
134
135
ClearTrackedVertexArrays();
136
137
if (softwareInputLayout_)
138
render_->DeleteInputLayout(softwareInputLayout_);
139
softwareInputLayout_ = nullptr;
140
141
ClearInputLayoutMap();
142
}
143
144
void DrawEngineGLES::ClearInputLayoutMap() {
145
inputLayoutMap_.Iterate([&](const uint32_t &key, GLRInputLayout *il) {
146
render_->DeleteInputLayout(il);
147
});
148
inputLayoutMap_.Clear();
149
}
150
151
void DrawEngineGLES::BeginFrame() {
152
FrameData &frameData = frameData_[render_->GetCurFrame()];
153
frameData.pushIndex->Begin();
154
frameData.pushVertex->Begin();
155
156
lastRenderStepId_ = -1;
157
}
158
159
void DrawEngineGLES::EndFrame() {
160
FrameData &frameData = frameData_[render_->GetCurFrame()];
161
frameData.pushIndex->End();
162
frameData.pushVertex->End();
163
tessDataTransferGLES->EndFrame();
164
}
165
166
struct GlTypeInfo {
167
u16 type;
168
u8 count;
169
u8 normalized;
170
};
171
172
static const GlTypeInfo GLComp[] = {
173
{0}, // DEC_NONE,
174
{GL_FLOAT, 1, GL_FALSE}, // DEC_FLOAT_1,
175
{GL_FLOAT, 2, GL_FALSE}, // DEC_FLOAT_2,
176
{GL_FLOAT, 3, GL_FALSE}, // DEC_FLOAT_3,
177
{GL_FLOAT, 4, GL_FALSE}, // DEC_FLOAT_4,
178
{GL_BYTE, 4, GL_TRUE}, // DEC_S8_3,
179
{GL_SHORT, 4, GL_TRUE},// DEC_S16_3,
180
{GL_UNSIGNED_BYTE, 1, GL_TRUE},// DEC_U8_1,
181
{GL_UNSIGNED_BYTE, 2, GL_TRUE},// DEC_U8_2,
182
{GL_UNSIGNED_BYTE, 3, GL_TRUE},// DEC_U8_3,
183
{GL_UNSIGNED_BYTE, 4, GL_TRUE},// DEC_U8_4,
184
{GL_UNSIGNED_SHORT, 1, GL_TRUE},// DEC_U16_1,
185
{GL_UNSIGNED_SHORT, 2, GL_TRUE},// DEC_U16_2,
186
{GL_UNSIGNED_SHORT, 3, GL_TRUE},// DEC_U16_3,
187
{GL_UNSIGNED_SHORT, 4, GL_TRUE},// DEC_U16_4,
188
};
189
190
static inline void VertexAttribSetup(int attrib, int fmt, int offset, std::vector<GLRInputLayout::Entry> &entries) {
191
if (fmt) {
192
const GlTypeInfo &type = GLComp[fmt];
193
GLRInputLayout::Entry entry;
194
entry.offset = offset;
195
entry.location = attrib;
196
entry.normalized = type.normalized;
197
entry.type = type.type;
198
entry.count = type.count;
199
entries.push_back(entry);
200
}
201
}
202
203
// TODO: Use VBO and get rid of the vertexData pointers - with that, we will supply only offsets
204
GLRInputLayout *DrawEngineGLES::SetupDecFmtForDraw(const DecVtxFormat &decFmt) {
205
uint32_t key = decFmt.id;
206
GLRInputLayout *inputLayout;
207
if (inputLayoutMap_.Get(key, &inputLayout)) {
208
return inputLayout;
209
}
210
211
std::vector<GLRInputLayout::Entry> entries;
212
VertexAttribSetup(ATTR_W1, decFmt.w0fmt, decFmt.w0off, entries);
213
VertexAttribSetup(ATTR_W2, decFmt.w1fmt, decFmt.w1off, entries);
214
VertexAttribSetup(ATTR_TEXCOORD, decFmt.uvfmt, decFmt.uvoff, entries);
215
VertexAttribSetup(ATTR_COLOR0, decFmt.c0fmt, decFmt.c0off, entries);
216
VertexAttribSetup(ATTR_COLOR1, decFmt.c1fmt, decFmt.c1off, entries);
217
VertexAttribSetup(ATTR_NORMAL, decFmt.nrmfmt, decFmt.nrmoff, entries);
218
VertexAttribSetup(ATTR_POSITION, DecVtxFormat::PosFmt(), decFmt.posoff, entries);
219
220
int stride = decFmt.stride;
221
inputLayout = render_->CreateInputLayout(entries, stride);
222
inputLayoutMap_.Insert(key, inputLayout);
223
return inputLayout;
224
}
225
226
// A new render step means we need to flush any dynamic state. Really, any state that is reset in
227
// GLQueueRunner::PerformRenderPass.
228
void DrawEngineGLES::Invalidate(InvalidationCallbackFlags flags) {
229
if (flags & InvalidationCallbackFlags::RENDER_PASS_STATE) {
230
// Dirty everything that has dynamic state that will need re-recording.
231
gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_DEPTHSTENCIL_STATE | DIRTY_BLEND_STATE | DIRTY_RASTER_STATE | DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
232
}
233
}
234
235
void DrawEngineGLES::DoFlush() {
236
PROFILE_THIS_SCOPE("flush");
237
FrameData &frameData = frameData_[render_->GetCurFrame()];
238
VShaderID vsid;
239
240
if (!render_->IsInRenderPass()) {
241
// Something went badly wrong. Try to survive by simply skipping the draw, though.
242
_dbg_assert_msg_(false, "Trying to DoFlush while not in a render pass. This is bad.");
243
// can't goto bail here, skips too many variable initializations. So let's wipe the most important stuff.
244
indexGen.Reset();
245
numDecodedVerts_ = 0;
246
numDrawVerts_ = 0;
247
numDrawInds_ = 0;
248
vertexCountInDrawCalls_ = 0;
249
decodeVertsCounter_ = 0;
250
decodeIndsCounter_ = 0;
251
return;
252
}
253
254
bool textureNeedsApply = false;
255
if (gstate_c.IsDirty(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS) && !gstate.isModeClear() && gstate.isTextureMapEnabled()) {
256
textureCache_->SetTexture();
257
gstate_c.Clean(DIRTY_TEXTURE_IMAGE | DIRTY_TEXTURE_PARAMS);
258
textureNeedsApply = true;
259
} else if (gstate.getTextureAddress(0) == (gstate.getFrameBufRawAddress() | 0x04000000)) {
260
// This catches the case of clearing a texture. (#10957)
261
gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);
262
}
263
264
GEPrimitiveType prim = prevPrim_;
265
266
Shader *vshader = shaderManager_->ApplyVertexShader(CanUseHardwareTransform(prim), useHWTessellation_, dec_, decOptions_.expandAllWeightsToFloat, decOptions_.applySkinInDecode || !CanUseHardwareTransform(prim), &vsid);
267
268
GLRBuffer *vertexBuffer = nullptr;
269
GLRBuffer *indexBuffer = nullptr;
270
uint32_t vertexBufferOffset = 0;
271
uint32_t indexBufferOffset = 0;
272
273
if (vshader->UseHWTransform()) {
274
if (decOptions_.applySkinInDecode && (lastVType_ & GE_VTYPE_WEIGHT_MASK)) {
275
// If software skinning, we're predecoding into "decoded". So make sure we're done, then push that content.
276
DecodeVerts(decoded_);
277
uint32_t size = numDecodedVerts_ * dec_->GetDecVtxFmt().stride;
278
u8 *dest = (u8 *)frameData.pushVertex->Allocate(size, 4, &vertexBuffer, &vertexBufferOffset);
279
memcpy(dest, decoded_, size);
280
} else {
281
// Figure out how much pushbuffer space we need to allocate.
282
int vertsToDecode = ComputeNumVertsToDecode();
283
u8 *dest = (u8 *)frameData.pushVertex->Allocate(vertsToDecode * dec_->GetDecVtxFmt().stride, 4, &vertexBuffer, &vertexBufferOffset);
284
DecodeVerts(dest);
285
}
286
287
int vertexCount;
288
int maxIndex;
289
bool useElements;
290
DecodeVerts(decoded_);
291
DecodeIndsAndGetData(&prim, &vertexCount, &maxIndex, &useElements, false);
292
293
if (useElements) {
294
uint32_t esz = sizeof(uint16_t) * vertexCount;
295
void *dest = frameData.pushIndex->Allocate(esz, 2, &indexBuffer, &indexBufferOffset);
296
// TODO: When we need to apply an index offset, we can apply it directly when copying the indices here.
297
// Of course, minding the maximum value of 65535...
298
memcpy(dest, decIndex_, esz);
299
}
300
301
bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE;
302
if (gstate.isModeThrough()) {
303
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255);
304
} else {
305
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && ((hasColor && (gstate.materialupdate & 1)) || gstate.getMaterialAmbientA() == 255) && (!gstate.isLightingEnabled() || gstate.getAmbientA() == 255);
306
}
307
308
if (textureNeedsApply) {
309
textureCache_->ApplyTexture();
310
}
311
312
// Need to ApplyDrawState after ApplyTexture because depal can launch a render pass and that wrecks the state.
313
ApplyDrawState(prim);
314
ApplyDrawStateLate(false, 0);
315
316
LinkedShader *program = shaderManager_->ApplyFragmentShader(vsid, vshader, pipelineState_, framebufferManager_->UseBufferedRendering());
317
GLRInputLayout *inputLayout = SetupDecFmtForDraw(dec_->GetDecVtxFmt());
318
if (useElements) {
319
render_->DrawIndexed(inputLayout,
320
vertexBuffer, vertexBufferOffset,
321
indexBuffer, indexBufferOffset,
322
glprim[prim], vertexCount, GL_UNSIGNED_SHORT);
323
} else {
324
render_->Draw(
325
inputLayout, vertexBuffer, vertexBufferOffset,
326
glprim[prim], 0, vertexCount);
327
}
328
} else {
329
PROFILE_THIS_SCOPE("soft");
330
if (!decOptions_.applySkinInDecode) {
331
decOptions_.applySkinInDecode = true;
332
lastVType_ |= (1 << 26);
333
dec_ = GetVertexDecoder(lastVType_);
334
}
335
DecodeVerts(decoded_);
336
int vertexCount = DecodeInds();
337
338
bool hasColor = (lastVType_ & GE_VTYPE_COL_MASK) != GE_VTYPE_COL_NONE;
339
if (gstate.isModeThrough()) {
340
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && (hasColor || gstate.getMaterialAmbientA() == 255);
341
} else {
342
gstate_c.vertexFullAlpha = gstate_c.vertexFullAlpha && ((hasColor && (gstate.materialupdate & 1)) || gstate.getMaterialAmbientA() == 255) && (!gstate.isLightingEnabled() || gstate.getAmbientA() == 255);
343
}
344
345
gpuStats.numUncachedVertsDrawn += vertexCount;
346
prim = IndexGenerator::GeneralPrim((GEPrimitiveType)drawInds_[0].prim);
347
348
u16 *inds = decIndex_;
349
SoftwareTransformResult result{};
350
// TODO: Keep this static? Faster than repopulating?
351
SoftwareTransformParams params{};
352
params.decoded = decoded_;
353
params.transformed = transformed_;
354
params.transformedExpanded = transformedExpanded_;
355
params.fbman = framebufferManager_;
356
params.texCache = textureCache_;
357
params.allowClear = true; // Clear in OpenGL respects scissor rects, so we'll use it.
358
params.allowSeparateAlphaClear = true;
359
params.flippedY = framebufferManager_->UseBufferedRendering();
360
params.usesHalfZ = false;
361
362
// We need correct viewport values in gstate_c already.
363
if (gstate_c.IsDirty(DIRTY_VIEWPORTSCISSOR_STATE)) {
364
ConvertViewportAndScissor(framebufferManager_->UseBufferedRendering(),
365
framebufferManager_->GetRenderWidth(), framebufferManager_->GetRenderHeight(),
366
framebufferManager_->GetTargetBufferWidth(), framebufferManager_->GetTargetBufferHeight(),
367
vpAndScissor_);
368
UpdateCachedViewportState(vpAndScissor_);
369
}
370
371
int maxIndex = numDecodedVerts_;
372
373
// TODO: Split up into multiple draw calls for GLES 2.0 where you can't guarantee support for more than 0x10000 verts.
374
if (gl_extensions.IsGLES && !gl_extensions.GLES3) {
375
constexpr int vertexCountLimit = 0x10000 / 3;
376
if (vertexCount > vertexCountLimit) {
377
WARN_LOG_REPORT_ONCE(manyVerts, Log::G3D, "Truncating vertex count from %d to %d", vertexCount, vertexCountLimit);
378
vertexCount = vertexCountLimit;
379
}
380
}
381
382
SoftwareTransform swTransform(params);
383
384
const Lin::Vec3 trans(gstate_c.vpXOffset, gstate_c.vpYOffset, gstate_c.vpZOffset);
385
const Lin::Vec3 scale(gstate_c.vpWidthScale, gstate_c.vpHeightScale, gstate_c.vpDepthScale);
386
const bool invertedY = gstate_c.vpHeight * (params.flippedY ? 1.0 : -1.0f) < 0;
387
swTransform.SetProjMatrix(gstate.projMatrix, gstate_c.vpWidth < 0, invertedY, trans, scale);
388
389
swTransform.Transform(prim, dec_->VertexType(), dec_->GetDecVtxFmt(), numDecodedVerts_, &result);
390
// Non-zero depth clears are unusual, but some drivers don't match drawn depth values to cleared values.
391
// Games sometimes expect exact matches (see #12626, for example) for equal comparisons.
392
if (result.action == SW_CLEAR && everUsedEqualDepth_ && gstate.isClearModeDepthMask() && result.depth > 0.0f && result.depth < 1.0f)
393
result.action = SW_NOT_READY;
394
395
if (textureNeedsApply) {
396
gstate_c.pixelMapped = result.pixelMapped;
397
textureCache_->ApplyTexture();
398
gstate_c.pixelMapped = false;
399
}
400
401
// Need to ApplyDrawState after ApplyTexture because depal can launch a render pass and that wrecks the state.
402
ApplyDrawState(prim);
403
404
if (result.action == SW_NOT_READY)
405
swTransform.BuildDrawingParams(prim, vertexCount, dec_->VertexType(), inds, RemainingIndices(inds), numDecodedVerts_, VERTEX_BUFFER_MAX, &result);
406
if (result.setSafeSize)
407
framebufferManager_->SetSafeSize(result.safeWidth, result.safeHeight);
408
409
ApplyDrawStateLate(result.setStencil, result.stencilValue);
410
411
LinkedShader *linked = shaderManager_->ApplyFragmentShader(vsid, vshader, pipelineState_, framebufferManager_->UseBufferedRendering());
412
if (!linked) {
413
// Not much we can do here. Let's skip drawing.
414
goto bail;
415
}
416
417
if (result.action == SW_DRAW_INDEXED) {
418
vertexBufferOffset = (uint32_t)frameData.pushVertex->Push(result.drawBuffer, numDecodedVerts_ * sizeof(TransformedVertex), 4, &vertexBuffer);
419
indexBufferOffset = (uint32_t)frameData.pushIndex->Push(inds, sizeof(uint16_t) * result.drawNumTrans, 2, &indexBuffer);
420
render_->DrawIndexed(
421
softwareInputLayout_, vertexBuffer, vertexBufferOffset, indexBuffer, indexBufferOffset,
422
glprim[prim], result.drawNumTrans, GL_UNSIGNED_SHORT);
423
} else if (result.action == SW_CLEAR) {
424
u32 clearColor = result.color;
425
float clearDepth = result.depth;
426
427
bool colorMask = gstate.isClearModeColorMask();
428
bool alphaMask = gstate.isClearModeAlphaMask();
429
bool depthMask = gstate.isClearModeDepthMask();
430
431
GLbitfield target = 0;
432
// Without this, we will clear RGB when clearing stencil, which breaks games.
433
uint8_t rgbaMask = (colorMask ? 7 : 0) | (alphaMask ? 8 : 0);
434
if (colorMask || alphaMask) target |= GL_COLOR_BUFFER_BIT;
435
if (alphaMask) target |= GL_STENCIL_BUFFER_BIT;
436
if (depthMask) target |= GL_DEPTH_BUFFER_BIT;
437
438
render_->Clear(clearColor, clearDepth, clearColor >> 24, target, rgbaMask, vpAndScissor_.scissorX, vpAndScissor_.scissorY, vpAndScissor_.scissorW, vpAndScissor_.scissorH);
439
framebufferManager_->SetColorUpdated(gstate_c.skipDrawReason);
440
441
if (gstate_c.Use(GPU_USE_CLEAR_RAM_HACK) && colorMask && (alphaMask || gstate_c.framebufFormat == GE_FORMAT_565)) {
442
int scissorX1 = gstate.getScissorX1();
443
int scissorY1 = gstate.getScissorY1();
444
int scissorX2 = gstate.getScissorX2() + 1;
445
int scissorY2 = gstate.getScissorY2() + 1;
446
framebufferManager_->ApplyClearToMemory(scissorX1, scissorY1, scissorX2, scissorY2, clearColor);
447
}
448
gstate_c.Dirty(DIRTY_BLEND_STATE); // Make sure the color mask gets re-applied.
449
}
450
decOptions_.applySkinInDecode = g_Config.bSoftwareSkinning;
451
}
452
453
bail:
454
ResetAfterDrawInline();
455
framebufferManager_->SetColorUpdated(gstate_c.skipDrawReason);
456
GPUDebug::NotifyDraw();
457
}
458
459
// TODO: Refactor this to a single USE flag.
460
bool DrawEngineGLES::SupportsHWTessellation() {
461
bool hasTexelFetch = gl_extensions.GLES3 || (!gl_extensions.IsGLES && gl_extensions.VersionGEThan(3, 3, 0)) || gl_extensions.EXT_gpu_shader4;
462
return hasTexelFetch && gstate_c.UseAll(GPU_USE_VERTEX_TEXTURE_FETCH | GPU_USE_TEXTURE_FLOAT | GPU_USE_INSTANCE_RENDERING);
463
}
464
465
bool DrawEngineGLES::UpdateUseHWTessellation(bool enable) const {
466
return enable && SupportsHWTessellation();
467
}
468
469
void TessellationDataTransferGLES::SendDataToShader(const SimpleVertex *const *points, int size_u, int size_v, u32 vertType, const Spline::Weight2D &weights) {
470
bool hasColor = (vertType & GE_VTYPE_COL_MASK) != 0;
471
bool hasTexCoord = (vertType & GE_VTYPE_TC_MASK) != 0;
472
473
int size = size_u * size_v;
474
float *pos = new float[size * 4];
475
float *tex = hasTexCoord ? new float[size * 4] : nullptr;
476
float *col = hasColor ? new float[size * 4] : nullptr;
477
int stride = 4;
478
479
CopyControlPoints(pos, tex, col, stride, stride, stride, points, size, vertType);
480
// Removed the 1D texture support, it's unlikely to be relevant for performance.
481
// Control Points
482
if (prevSizeU < size_u || prevSizeV < size_v) {
483
prevSizeU = size_u;
484
prevSizeV = size_v;
485
if (data_tex[0])
486
renderManager_->DeleteTexture(data_tex[0]);
487
data_tex[0] = renderManager_->CreateTexture(GL_TEXTURE_2D, size_u * 3, size_v, 1, 1);
488
renderManager_->TextureImage(data_tex[0], 0, size_u * 3, size_v, 1, Draw::DataFormat::R32G32B32A32_FLOAT, nullptr, GLRAllocType::NONE, false);
489
renderManager_->FinalizeTexture(data_tex[0], 0, false);
490
}
491
renderManager_->BindTexture(TEX_SLOT_SPLINE_POINTS, data_tex[0]);
492
// Position
493
renderManager_->TextureSubImage(TEX_SLOT_SPLINE_POINTS, data_tex[0], 0, 0, 0, size_u, size_v, Draw::DataFormat::R32G32B32A32_FLOAT, (u8 *)pos, GLRAllocType::NEW);
494
// Texcoord
495
if (hasTexCoord)
496
renderManager_->TextureSubImage(TEX_SLOT_SPLINE_POINTS, data_tex[0], 0, size_u, 0, size_u, size_v, Draw::DataFormat::R32G32B32A32_FLOAT, (u8 *)tex, GLRAllocType::NEW);
497
// Color
498
if (hasColor)
499
renderManager_->TextureSubImage(TEX_SLOT_SPLINE_POINTS, data_tex[0], 0, size_u * 2, 0, size_u, size_v, Draw::DataFormat::R32G32B32A32_FLOAT, (u8 *)col, GLRAllocType::NEW);
500
501
// Weight U
502
if (prevSizeWU < weights.size_u) {
503
prevSizeWU = weights.size_u;
504
if (data_tex[1])
505
renderManager_->DeleteTexture(data_tex[1]);
506
data_tex[1] = renderManager_->CreateTexture(GL_TEXTURE_2D, weights.size_u * 2, 1, 1, 1);
507
renderManager_->TextureImage(data_tex[1], 0, weights.size_u * 2, 1, 1, Draw::DataFormat::R32G32B32A32_FLOAT, nullptr, GLRAllocType::NONE, false);
508
renderManager_->FinalizeTexture(data_tex[1], 0, false);
509
}
510
renderManager_->BindTexture(TEX_SLOT_SPLINE_WEIGHTS_U, data_tex[1]);
511
renderManager_->TextureSubImage(TEX_SLOT_SPLINE_WEIGHTS_U, data_tex[1], 0, 0, 0, weights.size_u * 2, 1, Draw::DataFormat::R32G32B32A32_FLOAT, (u8 *)weights.u, GLRAllocType::NONE);
512
513
// Weight V
514
if (prevSizeWV < weights.size_v) {
515
prevSizeWV = weights.size_v;
516
if (data_tex[2])
517
renderManager_->DeleteTexture(data_tex[2]);
518
data_tex[2] = renderManager_->CreateTexture(GL_TEXTURE_2D, weights.size_v * 2, 1, 1, 1);
519
renderManager_->TextureImage(data_tex[2], 0, weights.size_v * 2, 1, 1, Draw::DataFormat::R32G32B32A32_FLOAT, nullptr, GLRAllocType::NONE, false);
520
renderManager_->FinalizeTexture(data_tex[2], 0, false);
521
}
522
renderManager_->BindTexture(TEX_SLOT_SPLINE_WEIGHTS_V, data_tex[2]);
523
renderManager_->TextureSubImage(TEX_SLOT_SPLINE_WEIGHTS_V, data_tex[2], 0, 0, 0, weights.size_v * 2, 1, Draw::DataFormat::R32G32B32A32_FLOAT, (u8 *)weights.v, GLRAllocType::NONE);
524
}
525
526
void TessellationDataTransferGLES::EndFrame() {
527
for (int i = 0; i < 3; i++) {
528
if (data_tex[i]) {
529
renderManager_->DeleteTexture(data_tex[i]);
530
data_tex[i] = nullptr;
531
}
532
}
533
prevSizeU = prevSizeV = prevSizeWU = prevSizeWV = 0;
534
}
535
536