Hash :
6136cbcb
Author :
Date :
2020-09-23T21:31:05
Metal: Implement transform feedback
- XFB is currently emulated by writing to storage buffers.
- Metal doesn't allow vertex shader to both write to storage buffers and
to stage output (i.e clip position). So if GL_RASTERIZER_DISCARD is
NOT enabled, the draw with XFB enabled will have 2 passes:
+ First pass: vertex shader writes to XFB buffers + not write to stage
output + disable rasterizer.
+ Second pass: vertex shader writes to stage output (i.e.
[[position]]) + enable rasterizer. If GL_RASTERIZER_DISCARD is
enabled, the second pass is omitted.
+ This effectively executes the same vertex shader twice. TODO:
possible improvement is writing vertex outputs to buffer in first
pass then re-use that buffer as input for second pass which has a
passthrough vertex shader.
- If GL_RASTERIZER_DISCARD is enabled, and XFB is enabled:
+ Only first pass above will be executed, and the render pass will use
an empty 1x1 texture attachment since rasterization is not needed.
- If GL_RASTERIZER_DISCARD is enabled, but XFB is NOT enabled:
+ we still enable Metal rasterizer.
+ but vertex shader must emulate the discard by writing gl_Position =
(-3, -3, -3, 1). This effectively moves the vertex out of clip
space's visible area.
+ This is because GLSL still allows vertex shader to write to stage
output when rasterizer is disabled. However, Metal doesn't allow
that. In Metal, if rasterizer is disabled, then vertex shader must
not write to stage output.
- See src/libANGLE/renderer/metal/doc/TransformFeedback.md for more
details.
Bug: angleproject:2634
Change-Id: I6c700e031052560326b7f660ee7597202d38e6aa
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2408594
Reviewed-by: Jonah Ryan-Davis <jonahr@google.com>
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Jonah Ryan-Davis <jonahr@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
//
// Copyright 2020 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// QueryMtl.mm:
// Defines the class interface for QueryMtl, implementing QueryImpl.
//
#include "libANGLE/renderer/metal/QueryMtl.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
namespace rx
{
QueryMtl::QueryMtl(gl::QueryType type) : QueryImpl(type) {}
QueryMtl::~QueryMtl() {}
void QueryMtl::onDestroy(const gl::Context *context)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
if (!getAllocatedVisibilityOffsets().empty())
{
contextMtl->onOcclusionQueryDestroy(context, this);
}
mVisibilityResultBuffer = nullptr;
}
angle::Result QueryMtl::begin(const gl::Context *context)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
switch (getType())
{
case gl::QueryType::AnySamples:
case gl::QueryType::AnySamplesConservative:
if (!mVisibilityResultBuffer)
{
// Allocate buffer
ANGLE_TRY(mtl::Buffer::MakeBuffer(contextMtl, mtl::kOcclusionQueryResultSize,
nullptr, &mVisibilityResultBuffer));
ANGLE_MTL_OBJC_SCOPE
{
mVisibilityResultBuffer->get().label =
[NSString stringWithFormat:@"QueryMtl=%p", this];
}
}
ANGLE_TRY(contextMtl->onOcclusionQueryBegin(context, this));
break;
case gl::QueryType::TransformFeedbackPrimitivesWritten:
mTransformFeedbackPrimitivesDrawn = 0;
break;
default:
UNIMPLEMENTED();
break;
}
return angle::Result::Continue;
}
angle::Result QueryMtl::end(const gl::Context *context)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
switch (getType())
{
case gl::QueryType::AnySamples:
case gl::QueryType::AnySamplesConservative:
contextMtl->onOcclusionQueryEnd(context, this);
break;
case gl::QueryType::TransformFeedbackPrimitivesWritten:
// There could be transform feedback in progress, so add the primitives drawn so far
// from the current transform feedback object.
onTransformFeedbackEnd(context);
break;
default:
UNIMPLEMENTED();
break;
}
return angle::Result::Continue;
}
angle::Result QueryMtl::queryCounter(const gl::Context *context)
{
UNIMPLEMENTED();
return angle::Result::Continue;
}
template <typename T>
angle::Result QueryMtl::waitAndGetResult(const gl::Context *context, T *params)
{
ASSERT(params);
ContextMtl *contextMtl = mtl::GetImpl(context);
switch (getType())
{
case gl::QueryType::AnySamples:
case gl::QueryType::AnySamplesConservative:
{
ASSERT(mVisibilityResultBuffer);
if (mVisibilityResultBuffer->hasPendingWorks(contextMtl))
{
contextMtl->flushCommandBufer();
}
// map() will wait for the pending GPU works to finish
const uint8_t *visibilityResultBytes = mVisibilityResultBuffer->mapReadOnly(contextMtl);
uint64_t queryResult;
memcpy(&queryResult, visibilityResultBytes, sizeof(queryResult));
mVisibilityResultBuffer->unmap(contextMtl);
*params = queryResult ? GL_TRUE : GL_FALSE;
}
break;
case gl::QueryType::TransformFeedbackPrimitivesWritten:
*params = static_cast<T>(mTransformFeedbackPrimitivesDrawn);
break;
default:
UNIMPLEMENTED();
break;
}
return angle::Result::Continue;
}
angle::Result QueryMtl::isResultAvailable(const gl::Context *context, bool *available)
{
ASSERT(available);
ContextMtl *contextMtl = mtl::GetImpl(context);
// glGetQueryObjectuiv implicitly flush any pending works related to the query
switch (getType())
{
case gl::QueryType::AnySamples:
case gl::QueryType::AnySamplesConservative:
ASSERT(mVisibilityResultBuffer);
if (mVisibilityResultBuffer->hasPendingWorks(contextMtl))
{
contextMtl->flushCommandBufer();
}
*available = !mVisibilityResultBuffer->isBeingUsedByGPU(contextMtl);
break;
case gl::QueryType::TransformFeedbackPrimitivesWritten:
*available = true;
break;
default:
UNIMPLEMENTED();
break;
}
return angle::Result::Continue;
}
angle::Result QueryMtl::getResult(const gl::Context *context, GLint *params)
{
return waitAndGetResult(context, params);
}
angle::Result QueryMtl::getResult(const gl::Context *context, GLuint *params)
{
return waitAndGetResult(context, params);
}
angle::Result QueryMtl::getResult(const gl::Context *context, GLint64 *params)
{
return waitAndGetResult(context, params);
}
angle::Result QueryMtl::getResult(const gl::Context *context, GLuint64 *params)
{
return waitAndGetResult(context, params);
}
void QueryMtl::resetVisibilityResult(ContextMtl *contextMtl)
{
// Occlusion query buffer must be allocated in QueryMtl::begin
ASSERT(mVisibilityResultBuffer);
// Fill the query's buffer with zeros
auto blitEncoder = contextMtl->getBlitCommandEncoder();
blitEncoder->fillBuffer(mVisibilityResultBuffer, NSMakeRange(0, mtl::kOcclusionQueryResultSize),
0);
mVisibilityResultBuffer->syncContent(contextMtl, blitEncoder);
}
void QueryMtl::onTransformFeedbackEnd(const gl::Context *context)
{
gl::TransformFeedback *transformFeedback = context->getState().getCurrentTransformFeedback();
if (transformFeedback)
{
mTransformFeedbackPrimitivesDrawn += transformFeedback->getPrimitivesDrawn();
}
}
}