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

Commit d42d200

Browse files
csmartdalton86Skia Commit-Bot
authored andcommitted
Reland "Move setupGeometry() from GrGLGpu to GrGLOpsRenderPass"
This is a reland of b67081f Original change's description: > 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> Change-Id: Ic30c9e1b1d9a3ae29623ff8239b9b1d7b2dad273 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/274057 Reviewed-by: Brian Salomon <bsalomon@google.com> Commit-Queue: Chris Dalton <csmartdalton@google.com>
1 parent ed38d20 commit d42d200

File tree

5 files changed

+163
-142
lines changed

5 files changed

+163
-142
lines changed

src/gpu/gl/GrGLGpu.cpp

Lines changed: 33 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,7 @@ void GrGLGpu::flushScissorTest(GrScissorTest scissorTest) {
17651765

17661766
void GrGLGpu::flushScissorRect(const SkIRect& scissor, int rtWidth, int rtHeight,
17671767
GrSurfaceOrigin rtOrigin) {
1768+
SkASSERT(fHWScissorSettings.fEnabled == TriState::kYes_TriState);
17681769
auto nativeScissor = GrNativeRect::MakeRelativeTo(rtOrigin, rtHeight, scissor);
17691770
if (fHWScissorSettings.fRect != nativeScissor) {
17701771
GL_CALL(Scissor(nativeScissor.fX, nativeScissor.fY, nativeScissor.fWidth,
@@ -1813,21 +1814,22 @@ void GrGLGpu::disableWindowRectangles() {
18131814
#endif
18141815
}
18151816

1816-
bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget, const GrProgramInfo& programInfo) {
1817+
bool GrGLGpu::flushGLState(GrRenderTarget* renderTarget,
1818+
const GrProgramInfo& programInfo) {
18171819
this->handleDirtyContext();
18181820

1819-
if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1820-
this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1821-
}
1822-
1823-
sk_sp<GrGLProgram> program(fProgramCache->findOrCreateProgram(renderTarget, programInfo));
1821+
sk_sp<GrGLProgram> program = fProgramCache->findOrCreateProgram(renderTarget, programInfo);
18241822
if (!program) {
18251823
GrCapsDebugf(this->caps(), "Failed to create program!\n");
18261824
return false;
18271825
}
18281826

18291827
this->flushProgram(std::move(program));
18301828

1829+
if (GrPrimitiveType::kPatches == programInfo.primitiveType()) {
1830+
this->flushPatchVertexCount(programInfo.tessellationPatchVertexCount());
1831+
}
1832+
18311833
// Swizzle the blend to match what the shader will output.
18321834
this->flushBlendAndColorWrite(programInfo.pipeline().getXferProcessor().getBlendInfo(),
18331835
programInfo.pipeline().outputSwizzle());
@@ -1885,54 +1887,6 @@ void GrGLGpu::flushProgram(GrGLuint id) {
18851887
fHWProgramID = id;
18861888
}
18871889

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-
19361890
GrGLenum GrGLGpu::bindBuffer(GrGpuBufferType type, const GrBuffer* buffer) {
19371891
this->handleDirtyContext();
19381892

@@ -2325,6 +2279,8 @@ void GrGLGpu::flushViewport(int width, int height) {
23252279
}
23262280

23272281
GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
2282+
fStats.incNumDraws();
2283+
23282284
if (this->glCaps().requiresCullFaceEnableDisableWhenDrawingLinesAfterNonLines() &&
23292285
GrIsPrimTypeLines(primitiveType) && !GrIsPrimTypeLines(fLastPrimitiveType)) {
23302286
GL_CALL(Enable(GR_GL_CULL_FACE));
@@ -2352,72 +2308,40 @@ GrGLenum GrGLGpu::prepareToDraw(GrPrimitiveType primitiveType) {
23522308
SK_ABORT("invalid GrPrimitiveType");
23532309
}
23542310

2355-
void GrGLGpu::draw(GrPrimitiveType primitiveType, const GrBuffer* vertexBuffer, int vertexCount,
2356-
int baseVertex) {
2311+
void GrGLGpu::drawArrays(GrPrimitiveType primitiveType, GrGLint baseVertex, GrGLsizei vertexCount) {
2312+
SkASSERT(!this->glCaps().drawArraysBaseVertexIsBroken() || 0 == baseVertex);
23572313
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();
2314+
GL_CALL(DrawArrays(glPrimType, baseVertex, vertexCount));
23662315
}
23672316

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-
}
2317+
void GrGLGpu::drawElements(GrPrimitiveType primitiveType, GrGLsizei indexCount, GrGLenum indexType,
2318+
const void* indices) {
2319+
GrGLenum glPrimType = this->prepareToDraw(primitiveType);
2320+
GL_CALL(DrawElements(glPrimType, indexCount, indexType, indices));
23752321
}
23762322

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) {
2323+
void GrGLGpu::drawRangeElements(GrPrimitiveType primitiveType, GrGLuint minIndexValue,
2324+
GrGLuint maxIndexValue, GrGLsizei indexCount, GrGLenum indexType,
2325+
const void* indices) {
2326+
SkASSERT(this->glCaps().drawRangeElementsSupport());
23812327
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();
2328+
GL_CALL(DrawRangeElements(glPrimType, minIndexValue, maxIndexValue, indexCount, indexType,
2329+
indices));
23912330
}
23922331

2393-
void GrGLGpu::drawInstanced(GrPrimitiveType primitiveType, const GrBuffer* instanceBuffer,
2394-
int instanceCount, int baseInstance, const GrBuffer* vertexBuffer,
2395-
int vertexCount, int baseVertex) {
2332+
void GrGLGpu::drawArraysInstanced(GrPrimitiveType primitiveType, GrGLint baseVertex,
2333+
GrGLsizei vertexCount, GrGLsizei instanceCount) {
2334+
SkASSERT(instanceCount <= this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount));
23962335
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-
}
2336+
GL_CALL(DrawArraysInstanced(glPrimType, baseVertex, vertexCount, instanceCount));
24052337
}
24062338

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) {
2339+
void GrGLGpu::drawElementsInstanced(GrPrimitiveType primitiveType, GrGLsizei indexCount,
2340+
GrGLenum indexType, const void* indices,
2341+
GrGLsizei instanceCount) {
2342+
SkASSERT(instanceCount <= this->glCaps().maxInstancesPerDrawWithoutCrashing(instanceCount));
24112343
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-
}
2344+
GL_CALL(DrawElementsInstanced(glPrimType, indexCount, indexType, indices, instanceCount));
24212345
}
24222346

24232347
void GrGLGpu::onResolveRenderTarget(GrRenderTarget* target, const SkIRect& resolveRect,
@@ -3922,6 +3846,7 @@ void GrGLGpu::testingOnly_flushGpuAndSync() {
39223846

39233847
GrGLAttribArrayState* GrGLGpu::HWVertexArrayState::bindInternalVertexArray(GrGLGpu* gpu,
39243848
const GrBuffer* ibuf) {
3849+
SkASSERT(!ibuf || ibuf->isCpuBuffer() || !static_cast<const GrGpuBuffer*>(ibuf)->isMapped());
39253850
GrGLAttribArrayState* attribState;
39263851

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

src/gpu/gl/GrGLGpu.h

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,46 @@ class GrGLGpu final : public GrGpu {
7676
// Flushes state from GrProgramInfo to GL. Returns false if the state couldn't be set.
7777
bool flushGLState(GrRenderTarget*, const GrProgramInfo&);
7878
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);
79+
80+
// Returns the last program bound by flushGLState(), or nullptr if a different program has since
81+
// been put into use via some other method (e.g., resetContext, copySurfaceAsDraw).
82+
// The returned GrGLProgram can be used for binding textures and vertex attributes.
83+
GrGLProgram* currentProgram() {
84+
this->handleDirtyContext();
85+
return fHWProgram.get();
8286
}
8387

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);
88+
// Binds the vertex array that should be used for internal draws, enables 'numAttribs' vertex
89+
// arrays, and flushes the desired primitive restart settings. If an index buffer is provided,
90+
// it will be bound to the vertex array. Otherwise the index buffer binding will be left
91+
// unchanged.
92+
//
93+
// NOTE: This binds the default VAO (ID=zero) unless we are on a core profile, in which case we
94+
// use a dummy array instead.
95+
GrGLAttribArrayState* bindInternalVertexArray(const GrBuffer* indexBuffer, int numAttribs,
96+
GrPrimitiveRestart primitiveRestart) {
97+
auto* attribState = fHWVertexArrayState.bindInternalVertexArray(this, indexBuffer);
98+
attribState->enableVertexArrays(this, numAttribs, primitiveRestart);
99+
return attribState;
100+
}
101+
GrGLAttribArrayState* bindInternalVertexArray(const GrBuffer* indexBuffer, GrPrimitiveRestart);
102+
103+
// These methods invoke their GL namesakes, with added bookkeeping and assertions. The caller is
104+
// responsible to ensure the desired GL state is configured before calling:
105+
//
106+
// - Call flushGLState()
107+
// - If scissor test got enabled, call flushScissorRect()
108+
// - If the pipeline has textures, call currentProgram()->bindTextures()
109+
// - Setup index and attrib arrays via currentProgram()
110+
void drawArrays(GrPrimitiveType, GrGLint baseVertex, GrGLsizei vertexCount);
111+
void drawElements(GrPrimitiveType, GrGLsizei indexCount, GrGLenum indexType,
112+
const void* indices);
113+
void drawRangeElements(GrPrimitiveType, GrGLuint minIndexValue, GrGLuint maxIndexValue,
114+
GrGLsizei indexCount, GrGLenum indexType, const void* indices);
115+
void drawArraysInstanced(GrPrimitiveType, GrGLint baseVertex, GrGLsizei vertexCount,
116+
GrGLsizei instanceCount);
117+
void drawElementsInstanced(GrPrimitiveType, GrGLsizei indexCount, GrGLenum indexType,
118+
const void* indices, GrGLsizei instanceCount);
96119

97120
// The GrGLOpsRenderPass does not buffer up draws before submitting them to the gpu.
98121
// Thus this is the implementation of the clear call for the corresponding passthrough function
@@ -293,14 +316,6 @@ class GrGLGpu final : public GrGpu {
293316
// Version for programs that aren't GrGLProgram.
294317
void flushProgram(GrGLuint);
295318

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-
304319
// Applies any necessary workarounds and returns the GL primitive type to use in draw calls.
305320
GrGLenum prepareToDraw(GrPrimitiveType primitiveType);
306321

@@ -568,7 +583,7 @@ class GrGLGpu final : public GrGpu {
568583
* state. This binds the default VAO (ID=zero) unless we are on a core profile, in which
569584
* case we use a dummy array instead.
570585
*
571-
* If an index buffer is privided, it will be bound to the vertex array. Otherwise the
586+
* If an index buffer is provided, it will be bound to the vertex array. Otherwise the
572587
* index buffer binding will be left unchanged.
573588
*
574589
* The returned GrGLAttribArrayState should be used to set vertex attribute arrays.

0 commit comments

Comments
 (0)