Hash :
f1150231
Author :
Date :
2024-01-16T16:28:36
Track WebGL buffer type in gl::Buffer state. WebGL has special validation that disallows buffers from changing "WebGL buffer type". Once a buffer is marked as having index data it cannot be bound as any other type of buffer except the copy binding points to copy to other index buffers. This disallows any GPU writes to buffers used for index data. Use this rule to shadow index buffer data when the driver is unable to give us robust access. Index range computation can be done much faster when the buffer does not need to be read back. Bug: angleproject:8434 Change-Id: I059eff732bb2f43234f61d9ef5528289f7698b38 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5200242 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org>
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
//
// Copyright 2015 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.
//
// BufferGL.cpp: Implements the class methods for BufferGL.
#include "libANGLE/renderer/gl/BufferGL.h"
#include "common/debug.h"
#include "common/utilities.h"
#include "libANGLE/Context.h"
#include "libANGLE/angletypes.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/renderer/gl/ContextGL.h"
#include "libANGLE/renderer/gl/FunctionsGL.h"
#include "libANGLE/renderer/gl/StateManagerGL.h"
#include "libANGLE/renderer/gl/renderergl_utils.h"
namespace rx
{
namespace
{
bool KeepBufferShadowCopy(const gl::Context *context, gl::WebGLBufferType webglType)
{
// Always keep a shadow copy if the feature is enabled. This usually means there is no other way
// to read back the data.
const angle::FeaturesGL &features = GetFeaturesGL(context);
if (features.keepBufferShadowCopy.enabled)
{
return true;
}
// Shadow WebGL index buffers when the driver is unable to provide robust access.
// WebGL element array buffers cannot be bound to other binding points or written to on the GPU
// so the shadowed data will never be invalidated.
if (context->isWebGL() && context->isBufferAccessValidationEnabled() &&
webglType == gl::WebGLBufferType::ElementArray)
{
return true;
}
return false;
}
} // namespace
// Use the GL_COPY_READ_BUFFER binding when two buffers need to be bound simultaneously.
// GL_ELEMENT_ARRAY_BUFFER is supported on more versions but can modify the state of the currently
// bound VAO. Two simultaneous buffer bindings are only needed for glCopyBufferSubData which also
// adds the GL_COPY_READ_BUFFER binding.
static constexpr gl::BufferBinding SourceBufferOperationTarget = gl::BufferBinding::CopyRead;
// Use the GL_ELEMENT_ARRAY_BUFFER binding for most operations since it's available on all
// supported GL versions and doesn't affect any current state when it changes.
static constexpr gl::BufferBinding DestBufferOperationTarget = gl::BufferBinding::Array;
BufferGL::BufferGL(const gl::BufferState &state, GLuint buffer)
: BufferImpl(state),
mIsMapped(false),
mMapOffset(0),
mMapSize(0),
mShadowCopy(),
mBufferSize(0),
mBufferID(buffer)
{}
BufferGL::~BufferGL()
{
ASSERT(mBufferID == 0);
}
void BufferGL::destroy(const gl::Context *context)
{
StateManagerGL *stateManager = GetStateManagerGL(context);
stateManager->deleteBuffer(mBufferID);
mBufferID = 0;
}
angle::Result BufferGL::setData(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t size,
gl::BufferUsage usage)
{
ContextGL *contextGL = GetImplAs<ContextGL>(context);
const FunctionsGL *functions = GetFunctionsGL(context);
StateManagerGL *stateManager = GetStateManagerGL(context);
stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
ANGLE_GL_TRY(context, functions->bufferData(gl::ToGLenum(DestBufferOperationTarget), size, data,
ToGLenum(usage)));
// Initialize the shadow buffer if needed. Don't delete existing shadow data. WebGL allows users
// to bind as an element array buffer first and then copy source/dest later (but not the other
// way around).
if (KeepBufferShadowCopy(context, mState.getWebGLType()) && !mShadowCopy.has_value())
{
mShadowCopy = angle::MemoryBuffer();
}
if (mShadowCopy.has_value())
{
ANGLE_CHECK_GL_ALLOC(contextGL, mShadowCopy->resize(size));
if (size > 0 && data != nullptr)
{
memcpy(mShadowCopy->data(), data, size);
}
}
mBufferSize = size;
contextGL->markWorkSubmitted();
return angle::Result::Continue;
}
angle::Result BufferGL::setSubData(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t size,
size_t offset)
{
ContextGL *contextGL = GetImplAs<ContextGL>(context);
const FunctionsGL *functions = GetFunctionsGL(context);
StateManagerGL *stateManager = GetStateManagerGL(context);
stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
ANGLE_GL_TRY(context, functions->bufferSubData(gl::ToGLenum(DestBufferOperationTarget), offset,
size, data));
if (mShadowCopy.has_value() && size > 0)
{
memcpy(mShadowCopy->data() + offset, data, size);
}
contextGL->markWorkSubmitted();
return angle::Result::Continue;
}
angle::Result BufferGL::copySubData(const gl::Context *context,
BufferImpl *source,
GLintptr sourceOffset,
GLintptr destOffset,
GLsizeiptr size)
{
ContextGL *contextGL = GetImplAs<ContextGL>(context);
const FunctionsGL *functions = GetFunctionsGL(context);
StateManagerGL *stateManager = GetStateManagerGL(context);
BufferGL *sourceGL = GetAs<BufferGL>(source);
stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
stateManager->bindBuffer(SourceBufferOperationTarget, sourceGL->getBufferID());
ANGLE_GL_TRY(context, functions->copyBufferSubData(gl::ToGLenum(SourceBufferOperationTarget),
gl::ToGLenum(DestBufferOperationTarget),
sourceOffset, destOffset, size));
if (mShadowCopy.has_value() && size > 0)
{
// WebGL only allows copying between buffers that are marked as the same type. Both buffers
// would have to be element array buffers and have shadow data.
ASSERT(sourceGL->mShadowCopy.has_value());
ASSERT(sourceGL->mShadowCopy->size() >= static_cast<size_t>(sourceOffset + size));
memcpy(mShadowCopy->data() + destOffset, sourceGL->mShadowCopy->data() + sourceOffset,
size);
}
contextGL->markWorkSubmitted();
return angle::Result::Continue;
}
angle::Result BufferGL::map(const gl::Context *context, GLenum access, void **mapPtr)
{
ContextGL *contextGL = GetImplAs<ContextGL>(context);
const FunctionsGL *functions = GetFunctionsGL(context);
StateManagerGL *stateManager = GetStateManagerGL(context);
if (mShadowCopy.has_value())
{
*mapPtr = mShadowCopy->data();
}
else
{
stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
if (functions->mapBuffer)
{
*mapPtr = ANGLE_GL_TRY(
context, functions->mapBuffer(gl::ToGLenum(DestBufferOperationTarget), access));
}
else
{
ASSERT(functions->mapBufferRange && access == GL_WRITE_ONLY_OES);
*mapPtr = ANGLE_GL_TRY(
context, functions->mapBufferRange(gl::ToGLenum(DestBufferOperationTarget), 0,
mBufferSize, GL_MAP_WRITE_BIT));
}
// Unbind the mapped buffer from the array buffer binding. Some drivers generate errors if
// any mapped buffer is bound to array buffer bindings.
// crbug.com/1345777
stateManager->bindBuffer(DestBufferOperationTarget, 0);
}
mIsMapped = true;
mMapOffset = 0;
mMapSize = mBufferSize;
contextGL->markWorkSubmitted();
return angle::Result::Continue;
}
angle::Result BufferGL::mapRange(const gl::Context *context,
size_t offset,
size_t length,
GLbitfield access,
void **mapPtr)
{
ContextGL *contextGL = GetImplAs<ContextGL>(context);
const FunctionsGL *functions = GetFunctionsGL(context);
StateManagerGL *stateManager = GetStateManagerGL(context);
if (mShadowCopy.has_value())
{
*mapPtr = mShadowCopy->data() + offset;
}
else
{
stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
*mapPtr =
ANGLE_GL_TRY(context, functions->mapBufferRange(gl::ToGLenum(DestBufferOperationTarget),
offset, length, access));
// Unbind the mapped buffer from the array buffer binding. Some drivers generate errors if
// any mapped buffer is bound to array buffer bindings.
// crbug.com/1345777
stateManager->bindBuffer(DestBufferOperationTarget, 0);
}
mIsMapped = true;
mMapOffset = offset;
mMapSize = length;
contextGL->markWorkSubmitted();
return angle::Result::Continue;
}
angle::Result BufferGL::unmap(const gl::Context *context, GLboolean *result)
{
ContextGL *contextGL = GetImplAs<ContextGL>(context);
const FunctionsGL *functions = GetFunctionsGL(context);
StateManagerGL *stateManager = GetStateManagerGL(context);
ASSERT(result);
ASSERT(mIsMapped);
if (mShadowCopy.has_value())
{
stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
ANGLE_GL_TRY(context,
functions->bufferSubData(gl::ToGLenum(DestBufferOperationTarget), mMapOffset,
mMapSize, mShadowCopy->data() + mMapOffset));
*result = GL_TRUE;
}
else
{
stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
*result =
ANGLE_GL_TRY(context, functions->unmapBuffer(gl::ToGLenum(DestBufferOperationTarget)));
}
mIsMapped = false;
contextGL->markWorkSubmitted();
return angle::Result::Continue;
}
angle::Result BufferGL::getIndexRange(const gl::Context *context,
gl::DrawElementsType type,
size_t offset,
size_t count,
bool primitiveRestartEnabled,
gl::IndexRange *outRange)
{
ContextGL *contextGL = GetImplAs<ContextGL>(context);
const FunctionsGL *functions = GetFunctionsGL(context);
StateManagerGL *stateManager = GetStateManagerGL(context);
ASSERT(!mIsMapped);
if (mShadowCopy.has_value())
{
*outRange = gl::ComputeIndexRange(type, mShadowCopy->data() + offset, count,
primitiveRestartEnabled);
}
else
{
stateManager->bindBuffer(DestBufferOperationTarget, mBufferID);
const GLuint typeBytes = gl::GetDrawElementsTypeSize(type);
const uint8_t *bufferData =
MapBufferRangeWithFallback(functions, gl::ToGLenum(DestBufferOperationTarget), offset,
count * typeBytes, GL_MAP_READ_BIT);
if (bufferData)
{
*outRange = gl::ComputeIndexRange(type, bufferData, count, primitiveRestartEnabled);
ANGLE_GL_TRY(context, functions->unmapBuffer(gl::ToGLenum(DestBufferOperationTarget)));
}
else
{
// Workaround the null driver not having map support.
*outRange = gl::IndexRange(0, 0, 1);
}
}
contextGL->markWorkSubmitted();
return angle::Result::Continue;
}
size_t BufferGL::getBufferSize() const
{
return mBufferSize;
}
GLuint BufferGL::getBufferID() const
{
return mBufferID;
}
} // namespace rx