Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit b67081f

Browse files
csmartdalton86Skia Commit-Bot
authored andcommitted
Move setupGeometry() from GrGLGpu to GrGLOpsRenderPass
Change-Id: I8788b96e07216be738c0ce1babb810b05bf46694 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/273696 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Chris Dalton <csmartdalton@google.com>
1 parent d8c604b commit b67081f

File tree

5 files changed

+159
-176
lines changed

5 files changed

+159
-176
lines changed

src/gpu/gl/GrGLGpu.cpp

Lines changed: 37 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,6 @@ GrGLGpu::~GrGLGpu() {
425425
fCopyProgramArrayBuffer.reset();
426426
fMipmapProgramArrayBuffer.reset();
427427

428-
fHWProgram.reset();
429428
if (fHWProgramID) {
430429
// detach the current program so there is no confusion on OpenGL's part
431430
// that we want it to be deleted
@@ -501,7 +500,6 @@ void GrGLGpu::disconnect(DisconnectType type) {
501500
}
502501
}
503502

504-
fHWProgram.reset();
505503
fProgramCache.reset();
506504

507505
fHWProgramID = 0;
@@ -662,7 +660,6 @@ void GrGLGpu::onResetContext(uint32_t resetBits) {
662660

663661
if (resetBits & kProgram_GrGLBackendState) {
664662
fHWProgramID = 0;
665-
fHWProgram.reset();
666663
}
667664
++fResetTimestampForTextureParameters;
668665
}
@@ -1765,6 +1762,7 @@ void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
17651762

17661763
void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
17671764
GrSurfaceOrigin rtOrigin) {
1765+
SkASSERT(TriState::kYes_TriState == fHWScissorSettings.fEnabled);
17681766
auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
17691767
if (fHWScissorSettings.fRect != nativeScissor) {
17701768
GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
@@ -1813,26 +1811,27 @@ void GrGLGpu::disableWindowRectangles() {
18131811
#endif
18141812
}
18151813

1816-
bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
1814+
sk_sp<GrGLProgram> GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1815+
const GrProgramInfo& programInfo) {
18171816
this->handleDirtyContext();
18181817

1819-
if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1820-
this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1821-
}
1822-
1823-
sk_sp<GrGLProgram> program(fProgramCache->findOrCreateProgram(renderTarget, programInfo));
1818+
sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
18241819
if (!program) {
18251820
GrCapsDebugf(this->caps(), "Failed to create program!\n");
1826-
return false;
1821+
return nullptr;
18271822
}
18281823

1829-
this->flushProgram(std::move(program));
1824+
this->flushProgram(program->programID());
1825+
1826+
if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1827+
this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1828+
}
18301829

18311830
// Swizzle the blend to match what the shader will output.
18321831
this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
18331832
programInfo.pipeline().outputSwizzle());
18341833

1835-
fHWProgram->updateUniforms(renderTarget, programInfo);
1834+
program->updateUniforms(renderTarget, programInfo);
18361835

18371836
GrGLRenderTarget* glRT = static_cast<GrGLRenderTarget*>(renderTarget);
18381837
GrStencilSettings stencil;
@@ -1854,85 +1853,18 @@ bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& pr
18541853
// to be msaa-resolved (which will modify bound FBO state).
18551854
this->flushRenderTarget(glRT);
18561855

1857-
return true;
1858-
}
1859-
1860-
void GrGLGpu::flushProgram(sk_sp<GrGLProgram> program) {
1861-
if (!program) {
1862-
fHWProgram.reset();
1863-
fHWProgramID = 0;
1864-
return;
1865-
}
1866-
SkASSERT((program == fHWProgram) == (fHWProgramID == program->programID()));
1867-
if (program == fHWProgram) {
1868-
return;
1869-
}
1870-
auto id = program->programID();
1871-
SkASSERT(id);
1872-
GL_CALL(UseProgram(id));
1873-
fHWProgram = std::move(program);
1874-
fHWProgramID = id;
1856+
return program;
18751857
}
18761858

18771859
void GrGLGpu::flushProgram(GrGLuint id) {
18781860
SkASSERT(id);
18791861
if (fHWProgramID == id) {
1880-
SkASSERT(!fHWProgram);
18811862
return;
18821863
}
1883-
fHWProgram.reset();
18841864
GL_CALL(UseProgram(id));
18851865
fHWProgramID = id;
18861866
}
18871867

1888-
void GrGLGpu::setupGeometry(const GrBuffer* indexBuffer,
1889-
const GrBuffer* vertexBuffer,
1890-
int baseVertex,
1891-
const GrBuffer* instanceBuffer,
1892-
int baseInstance,
1893-
GrPrimitiveRestart enablePrimitiveRestart) {
1894-
SkASSERT((enablePrimitiveRestart == GrPrimitiveRestart::kNo) || indexBuffer);
1895-
1896-
GrGLAttribArrayState* attribState;
1897-
if (indexBuffer) {
1898-
SkASSERT(indexBuffer->isCpuBuffer() ||
1899-
!static_cast<const GrGpuBuffer*>(indexBuffer)->isMapped());
1900-
attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
1901-
} else {
1902-
attribState = fHWVertexArrayState.bindInternalVertexArray(this);
1903-
}
1904-
1905-
int numAttribs = fHWProgram->numVertexAttributes() + fHWProgram->numInstanceAttributes();
1906-
attribState->enableVertexArrays(this, numAttribs, enablePrimitiveRestart);
1907-
1908-
if (int vertexStride = fHWProgram->vertexStride()) {
1909-
SkASSERT(vertexBuffer);
1910-
SkASSERT(vertexBuffer->isCpuBuffer() ||
1911-
!static_cast<const GrGpuBuffer*>(vertexBuffer)->isMapped());
1912-
size_t bufferOffset = baseVertex * static_cast<size_t>(vertexStride);
1913-
for (int i = 0; i < fHWProgram->numVertexAttributes(); ++i) {
1914-
const auto& attrib = fHWProgram->vertexAttribute(i);
1915-
static constexpr int kDivisor = 0;
1916-
attribState->set(this, attrib.fLocation, vertexBuffer, attrib.fCPUType, attrib.fGPUType,
1917-
vertexStride, bufferOffset + attrib.fOffset, kDivisor);
1918-
}
1919-
}
1920-
if (int instanceStride = fHWProgram->instanceStride()) {
1921-
SkASSERT(instanceBuffer);
1922-
SkASSERT(instanceBuffer->isCpuBuffer() ||
1923-
!static_cast<const GrGpuBuffer*>(instanceBuffer)->isMapped());
1924-
size_t bufferOffset = baseInstance * static_cast<size_t>(instanceStride);
1925-
int attribIdx = fHWProgram->numVertexAttributes();
1926-
for (int i = 0; i < fHWProgram->numInstanceAttributes(); ++i, ++attribIdx) {
1927-
const auto& attrib = fHWProgram->instanceAttribute(i);
1928-
static constexpr int kDivisor = 1;
1929-
attribState->set(this, attrib.fLocation, instanceBuffer, attrib.fCPUType,
1930-
attrib.fGPUType, instanceStride, bufferOffset + attrib.fOffset,
1931-
kDivisor);
1932-
}
1933-
}
1934-
}
1935-
19361868
GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
19371869
this->handleDirtyContext();
19381870

@@ -2325,6 +2257,8 @@ void GrGLGpu::flushViewport(int width, int height) {
23252257
}
23262258

23272259
GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
2260+
fStats.incNumDraws();
2261+
23282262
if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
23292263
GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
23302264
GL_CALL(Enable(GR_GL_CULL_FACE));
@@ -2352,72 +2286,40 @@ GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
23522286
SK_ABORT("invalid GrPrimitiveType");
23532287
}
23542288

2355-
void GrGLGpu::draw(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer, int vertexCount,
2356-
int baseVertex) {
2289+
void GrGLGpu::drawArrays(GrPrimitiveType primitiveType, GrGLint baseVertex, GrGLsizei vertexCount) {
2290+
SkASSERT(!this->glCaps().drawArraysBaseVertexIsBroken() || 0 == baseVertex);
23572291
GrGLenum glPrimType = this->prepareToDraw(primitiveType);
2358-
if (this->glCaps().drawArraysBaseVertexIsBroken()) {
2359-
this->setupGeometry(nullptr, vertexBuffer, baseVertex, nullptr, 0, GrPrimitiveRestart::kNo);
2360-
GL_CALL(DrawArrays(glPrimType, 0, vertexCount));
2361-
} else {
2362-
this->setupGeometry(nullptr, vertexBuffer, 0, nullptr, 0, GrPrimitiveRestart::kNo);
2363-
GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
2364-
}
2365-
fStats.incNumDraws();
2292+
GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
23662293
}
23672294

2368-
static const GrGLvoid* element_ptr(const GrBuffer* indexBuffer, int baseIndex) {
2369-
size_t baseOffset = baseIndex * sizeof(uint16_t);
2370-
if (indexBuffer->isCpuBuffer()) {
2371-
return static_cast<const GrCpuBuffer*>(indexBuffer)->data() + baseOffset;
2372-
} else {
2373-
return reinterpret_cast<const GrGLvoid*>(baseOffset);
2374-
}
2295+
void GrGLGpu::drawElements(GrPrimitiveType primitiveType, GrGLsizei indexCount, GrGLenum indexType,
2296+
const void* indices) {
2297+
GrGLenum glPrimType = this->prepareToDraw(primitiveType);
2298+
GL_CALL(DrawElements(glPrimType, indexCount, indexType, indices));
23752299
}
23762300

2377-
void GrGLGpu::drawIndexed(GrPrimitiveType primitiveType, const GrBuffer* indexBuffer,
2378-
int indexCount, int baseIndex, GrPrimitiveRestart primitiveRestart,
2379-
uint16_t minIndexValue, uint16_t maxIndexValue,
2380-
const GrBuffer* vertexBuffer, int baseVertex) {
2301+
void GrGLGpu::drawRangeElements(GrPrimitiveType primitiveType, GrGLuint minIndexValue,
2302+
GrGLuint maxIndexValue, GrGLsizei indexCount, GrGLenum indexType,
2303+
const void* indices) {
2304+
SkASSERT(this->glCaps().drawRangeElementsSupport());
23812305
GrGLenum glPrimType = this->prepareToDraw(primitiveType);
2382-
const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
2383-
this->setupGeometry(indexBuffer, vertexBuffer, baseVertex, nullptr, 0, primitiveRestart);
2384-
if (this->glCaps().drawRangeElementsSupport()) {
2385-
GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount,
2386-
GR_GL_UNSIGNED_SHORT, elementPtr));
2387-
} else {
2388-
GL_CALL(DrawElements(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr));
2389-
}
2390-
fStats.incNumDraws();
2306+
GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount, indexType,
2307+
indices));
23912308
}
23922309

2393-
void GrGLGpu::drawInstanced(GrPrimitiveType primitiveType, const GrBuffer* instanceBuffer,
2394-
int instanceCount, int baseInstance, const GrBuffer* vertexBuffer,
2395-
int vertexCount, int baseVertex) {
2310+
void GrGLGpu::drawArraysInstanced(GrPrimitiveType primitiveType, GrGLint baseVertex,
2311+
GrGLsizei vertexCount, GrGLsizei instanceCount) {
2312+
SkASSERT(instanceCount <= this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount));
23962313
GrGLenum glPrimType = this->prepareToDraw(primitiveType);
2397-
int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2398-
for (int i = 0; i < instanceCount; i += maxInstances) {
2399-
this->setupGeometry(nullptr, vertexBuffer, 0, instanceBuffer, baseInstance + i,
2400-
GrPrimitiveRestart::kNo);
2401-
GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount,
2402-
std::min(instanceCount - i, maxInstances)));
2403-
fStats.incNumDraws();
2404-
}
2314+
GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, instanceCount));
24052315
}
24062316

2407-
void GrGLGpu::drawIndexedInstanced(
2408-
GrPrimitiveType primitiveType, const GrBuffer* indexBuffer, int indexCount, int baseIndex,
2409-
GrPrimitiveRestart primitiveRestart, const GrBuffer* instanceBuffer, int instanceCount,
2410-
int baseInstance, const GrBuffer* vertexBuffer, int baseVertex) {
2317+
void GrGLGpu::drawElementsInstanced(GrPrimitiveType primitiveType, GrGLsizei indexCount,
2318+
GrGLenum indexType, const void* indices,
2319+
GrGLsizei instanceCount) {
2320+
SkASSERT(instanceCount <= this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount));
24112321
GrGLenum glPrimType = this->prepareToDraw(primitiveType);
2412-
const GrGLvoid* elementPtr = element_ptr(indexBuffer, baseIndex);
2413-
int maxInstances = this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount);
2414-
for (int i = 0; i < instanceCount; i += maxInstances) {
2415-
this->setupGeometry(indexBuffer, vertexBuffer, baseVertex,
2416-
instanceBuffer, baseInstance + i, primitiveRestart);
2417-
GL_CALL(DrawElementsInstanced(glPrimType, indexCount, GR_GL_UNSIGNED_SHORT, elementPtr,
2418-
std::min(instanceCount - i, maxInstances)));
2419-
fStats.incNumDraws();
2420-
}
2322+
GL_CALL(DrawElementsInstanced(glPrimType, indexCount, indexType, indices, instanceCount));
24212323
}
24222324

24232325
void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
@@ -3922,6 +3824,7 @@ void GrGLGpu::testingOnly_flushGpuAndSync() {
39223824

39233825
GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
39243826
const GrBuffer* ibuf) {
3827+
SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
39253828
GrGLAttribArrayState* attribState;
39263829

39273830
if (gpu->glCaps().isCoreProfile()) {

src/gpu/gl/GrGLGpu.h

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,42 @@ class GrGLGpu final : public GrGpu {
7373
// If the caller wishes to bind an index buffer to a specific VAO, it can call glBind directly.
7474
GrGLenum bindBuffer(GrGpuBufferType type, const GrBuffer*);
7575

76-
// Flushes state from GrProgramInfo to GL. Returns false if the state couldn't be set.
77-
bool flushGLState(GrRenderTarget*, const GrProgramInfo&);
76+
// Flushes state from GrProgramInfo to GL. Returns nullptr if the state couldn't be set.
77+
// The returned GrGLProgram can be used for binding textures and vertex attributes.
78+
//
79+
// The caller must keep the returned GrGLProgram alive until it is done issuing draws.
80+
sk_sp<GrGLProgram> flushGLState(GrRenderTarget*, const GrProgramInfo&) SK_WARN_UNUSED_RESULT;
7881
void flushScissorRect(const SkIRect&, int rtWidth, int rtHeight, GrSurfaceOrigin);
79-
void bindTextures(const GrPrimitiveProcessor& primProc, const GrPipeline& pipeline,
80-
const GrSurfaceProxy* const primProcTextures[] = nullptr) {
81-
fHWProgram->bindTextures(primProc, pipeline, primProcTextures);
82-
}
8382

84-
// These methods issue draws using the current GL state. The client must call flushGLState,
85-
// followed by flushScissorRect and/or bindTextures if applicable, before using these method.
86-
void draw(GrPrimitiveType, const GrBuffer* vertexBuffer, int vertexCount, int baseVertex);
87-
void drawIndexed(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount, int baseIndex,
88-
GrPrimitiveRestart, uint16_t minIndexValue, uint16_t maxIndexValue,
89-
const GrBuffer* vertexBuffer, int baseVertex);
90-
void drawInstanced(GrPrimitiveType, const GrBuffer* instanceBuffer, int instanceCount, int
91-
baseInstance, const GrBuffer* vertexBuffer, int vertexCount, int baseVertex);
92-
void drawIndexedInstanced(GrPrimitiveType, const GrBuffer* indexBuffer, int indexCount,
93-
int baseIndex, GrPrimitiveRestart, const GrBuffer* instanceBuffer,
94-
int instanceCount, int baseInstance, const GrBuffer* vertexBuffer,
95-
int baseVertex);
83+
// Binds the vertex array that should be used for internal draws, enables 'numAttribs' vertex
84+
// arrays, and flushes the desired primitive restart settings. The returned GrGLAttribArrayState
85+
// should be used to set vertex attribute arrays.
86+
//
87+
// If an index buffer is provided, it will be bound to the vertex array. Otherwise the index
88+
// buffer binding will be left unchanged.
89+
//
90+
// NOTE: This binds the default VAO (ID=zero) unless we are on a core profile, in which case we
91+
// use a dummy array instead.
92+
GrGLAttribArrayState* bindInternalVertexArray(const GrBuffer* indexBuffer, int numAttribs,
93+
GrPrimitiveRestart primitiveRestart) {
94+
auto* attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
95+
attribState->enableVertexArrays(this, numAttribs, primitiveRestart);
96+
return attribState;
97+
}
98+
GrGLAttribArrayState* bindInternalVertexArray(const GrBuffer* indexBuffer, GrPrimitiveRestart);
99+
100+
// These methods invoke their GL namesakes, with added bookkeeping and assertions. The caller is
101+
// responsible to ensure the desired GL state is configured before calling (via flushGLState(),
102+
// flushScissor(), and bindInternalVertexArray()).
103+
void drawArrays(GrPrimitiveType, GrGLint baseVertex, GrGLsizei vertexCount);
104+
void drawElements(GrPrimitiveType, GrGLsizei indexCount, GrGLenum indexType,
105+
const void* indices);
106+
void drawRangeElements(GrPrimitiveType, GrGLuint minIndexValue, GrGLuint maxIndexValue,
107+
GrGLsizei indexCount, GrGLenum indexType, const void* indices);
108+
void drawArraysInstanced(GrPrimitiveType, GrGLint baseVertex, GrGLsizei vertexCount,
109+
GrGLsizei instanceCount);
110+
void drawElementsInstanced(GrPrimitiveType, GrGLsizei indexCount, GrGLenum indexType,
111+
const void* indices, GrGLsizei instanceCount);
96112

97113
// The GrGLOpsRenderPass does not buffer up draws before submitting them to the gpu.
98114
// Thus this is the implementation of the clear call for the corresponding passthrough function
@@ -288,19 +304,9 @@ class GrGLGpu final : public GrGpu {
288304
// binds texture unit in GL
289305
void setTextureUnit(int unitIdx);
290306

291-
void flushProgram(sk_sp<GrGLProgram>);
292-
293307
// Version for programs that aren't GrGLProgram.
294308
void flushProgram(GrGLuint);
295309

296-
// Sets up vertex/instance attribute pointers and strides.
297-
void setupGeometry(const GrBuffer* indexBuffer,
298-
const GrBuffer* vertexBuffer,
299-
int baseVertex,
300-
const GrBuffer* instanceBuffer,
301-
int baseInstance,
302-
GrPrimitiveRestart);
303-
304310
// Applies any necessary workarounds and returns the GL primitive type to use in draw calls.
305311
GrGLenum prepareToDraw(GrPrimitiveType primitiveType);
306312

@@ -463,7 +469,6 @@ class GrGLGpu final : public GrGpu {
463469
int fHWActiveTextureUnitIdx;
464470

465471
GrGLuint fHWProgramID;
466-
sk_sp<GrGLProgram> fHWProgram;
467472

468473
enum TriState {
469474
kNo_TriState,
@@ -568,7 +573,7 @@ class GrGLGpu final : public GrGpu {
568573
* state. This binds the default VAO (ID=zero) unless we are on a core profile, in which
569574
* case we use a dummy array instead.
570575
*
571-
* If an index buffer is privided, it will be bound to the vertex array. Otherwise the
576+
* If an index buffer is provided, it will be bound to the vertex array. Otherwise the
572577
* index buffer binding will be left unchanged.
573578
*
574579
* The returned GrGLAttribArrayState should be used to set vertex attribute arrays.

0 commit comments

Comments
 (0)