Hash :
eae464dd
Author :
Date :
2019-03-22T15:15:04
D3D11: Fix reserved space with large dynamic buffers. We would end up with a large reserved space even though the allocation failed. Insead set the reserved space size after the allocation succedes. This was showing up as angle_end2end_tests failures on Windows 7 due to display reuse and buffer allocation. Bug: chromium:944454 Change-Id: Idb3bd530fe7b9cc2fce9a579787684e632b1a637 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1534684 Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Jamie Madill <jmadill@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
//
// Copyright (c) 2002-2012 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.
//
// VertexBuffer.cpp: Defines the abstract VertexBuffer class and VertexBufferInterface
// class with derivations, classes that perform graphics API agnostic vertex buffer operations.
#include "libANGLE/renderer/d3d/VertexBuffer.h"
#include "common/mathutil.h"
#include "libANGLE/Context.h"
#include "libANGLE/VertexAttribute.h"
#include "libANGLE/renderer/d3d/BufferD3D.h"
#include "libANGLE/renderer/d3d/ContextD3D.h"
namespace rx
{
// VertexBuffer Implementation
unsigned int VertexBuffer::mNextSerial = 1;
VertexBuffer::VertexBuffer() : mRefCount(1)
{
updateSerial();
}
VertexBuffer::~VertexBuffer() {}
void VertexBuffer::updateSerial()
{
mSerial = mNextSerial++;
}
unsigned int VertexBuffer::getSerial() const
{
return mSerial;
}
void VertexBuffer::addRef()
{
mRefCount++;
}
void VertexBuffer::release()
{
ASSERT(mRefCount > 0);
mRefCount--;
if (mRefCount == 0)
{
delete this;
}
}
// VertexBufferInterface Implementation
VertexBufferInterface::VertexBufferInterface(BufferFactoryD3D *factory, bool dynamic)
: mFactory(factory), mVertexBuffer(factory->createVertexBuffer()), mDynamic(dynamic)
{}
VertexBufferInterface::~VertexBufferInterface()
{
if (mVertexBuffer)
{
mVertexBuffer->release();
mVertexBuffer = nullptr;
}
}
unsigned int VertexBufferInterface::getSerial() const
{
ASSERT(mVertexBuffer);
return mVertexBuffer->getSerial();
}
unsigned int VertexBufferInterface::getBufferSize() const
{
ASSERT(mVertexBuffer);
return mVertexBuffer->getBufferSize();
}
angle::Result VertexBufferInterface::setBufferSize(const gl::Context *context, unsigned int size)
{
ASSERT(mVertexBuffer);
if (mVertexBuffer->getBufferSize() == 0)
{
return mVertexBuffer->initialize(context, size, mDynamic);
}
return mVertexBuffer->setBufferSize(context, size);
}
angle::Result VertexBufferInterface::getSpaceRequired(const gl::Context *context,
const gl::VertexAttribute &attrib,
const gl::VertexBinding &binding,
size_t count,
GLsizei instances,
unsigned int *spaceInBytesOut) const
{
unsigned int spaceRequired = 0;
ANGLE_TRY(mFactory->getVertexSpaceRequired(context, attrib, binding, count, instances,
&spaceRequired));
// Align to 16-byte boundary
unsigned int alignedSpaceRequired = roundUp(spaceRequired, 16u);
ANGLE_CHECK_GL_ALLOC(GetImplAs<ContextD3D>(context), alignedSpaceRequired >= spaceRequired);
*spaceInBytesOut = alignedSpaceRequired;
return angle::Result::Continue;
}
angle::Result VertexBufferInterface::discard(const gl::Context *context)
{
ASSERT(mVertexBuffer);
return mVertexBuffer->discard(context);
}
VertexBuffer *VertexBufferInterface::getVertexBuffer() const
{
ASSERT(mVertexBuffer);
return mVertexBuffer;
}
// StreamingVertexBufferInterface Implementation
StreamingVertexBufferInterface::StreamingVertexBufferInterface(BufferFactoryD3D *factory)
: VertexBufferInterface(factory, true), mWritePosition(0), mReservedSpace(0)
{}
angle::Result StreamingVertexBufferInterface::initialize(const gl::Context *context,
std::size_t initialSize)
{
return setBufferSize(context, static_cast<unsigned int>(initialSize));
}
void StreamingVertexBufferInterface::reset()
{
if (mVertexBuffer)
{
mVertexBuffer->release();
mVertexBuffer = mFactory->createVertexBuffer();
}
}
StreamingVertexBufferInterface::~StreamingVertexBufferInterface() {}
angle::Result StreamingVertexBufferInterface::reserveSpace(const gl::Context *context,
unsigned int size)
{
unsigned int curBufferSize = getBufferSize();
if (size > curBufferSize)
{
ANGLE_TRY(setBufferSize(context, std::max(size, 3 * curBufferSize / 2)));
mWritePosition = 0;
}
else if (mWritePosition + size > curBufferSize)
{
ANGLE_TRY(discard(context));
mWritePosition = 0;
}
mReservedSpace = size;
return angle::Result::Continue;
}
angle::Result StreamingVertexBufferInterface::storeDynamicAttribute(
const gl::Context *context,
const gl::VertexAttribute &attrib,
const gl::VertexBinding &binding,
gl::VertexAttribType currentValueType,
GLint start,
size_t count,
GLsizei instances,
unsigned int *outStreamOffset,
const uint8_t *sourceData)
{
unsigned int spaceRequired = 0;
ANGLE_TRY(getSpaceRequired(context, attrib, binding, count, instances, &spaceRequired));
// Protect against integer overflow
angle::CheckedNumeric<unsigned int> checkedPosition(mWritePosition);
checkedPosition += spaceRequired;
ANGLE_CHECK_GL_ALLOC(GetImplAs<ContextD3D>(context), checkedPosition.IsValid());
mReservedSpace = 0;
ANGLE_TRY(mVertexBuffer->storeVertexAttributes(context, attrib, binding, currentValueType,
start, count, instances, mWritePosition,
sourceData));
if (outStreamOffset)
{
*outStreamOffset = mWritePosition;
}
mWritePosition += spaceRequired;
return angle::Result::Continue;
}
angle::Result StreamingVertexBufferInterface::reserveVertexSpace(const gl::Context *context,
const gl::VertexAttribute &attrib,
const gl::VertexBinding &binding,
size_t count,
GLsizei instances)
{
unsigned int requiredSpace = 0;
ANGLE_TRY(mFactory->getVertexSpaceRequired(context, attrib, binding, count, instances,
&requiredSpace));
// Align to 16-byte boundary
auto alignedRequiredSpace = rx::CheckedRoundUp(requiredSpace, 16u);
alignedRequiredSpace += mReservedSpace;
// Protect against integer overflow
ANGLE_CHECK_GL_ALLOC(GetImplAs<ContextD3D>(context), alignedRequiredSpace.IsValid());
ANGLE_TRY(reserveSpace(context, alignedRequiredSpace.ValueOrDie()));
return angle::Result::Continue;
}
// StaticVertexBufferInterface Implementation
StaticVertexBufferInterface::AttributeSignature::AttributeSignature()
: type(gl::VertexAttribType::InvalidEnum),
size(0),
stride(0),
normalized(false),
pureInteger(false),
offset(0)
{}
bool StaticVertexBufferInterface::AttributeSignature::matchesAttribute(
const gl::VertexAttribute &attrib,
const gl::VertexBinding &binding) const
{
size_t attribStride = ComputeVertexAttributeStride(attrib, binding);
if (type != attrib.type || size != attrib.size || static_cast<GLuint>(stride) != attribStride ||
normalized != attrib.normalized || pureInteger != attrib.pureInteger)
{
return false;
}
size_t attribOffset =
(static_cast<size_t>(ComputeVertexAttributeOffset(attrib, binding)) % attribStride);
return (offset == attribOffset);
}
void StaticVertexBufferInterface::AttributeSignature::set(const gl::VertexAttribute &attrib,
const gl::VertexBinding &binding)
{
type = attrib.type;
size = attrib.size;
normalized = attrib.normalized;
pureInteger = attrib.pureInteger;
offset = stride = static_cast<GLuint>(ComputeVertexAttributeStride(attrib, binding));
offset = static_cast<size_t>(ComputeVertexAttributeOffset(attrib, binding)) %
ComputeVertexAttributeStride(attrib, binding);
}
StaticVertexBufferInterface::StaticVertexBufferInterface(BufferFactoryD3D *factory)
: VertexBufferInterface(factory, false)
{}
StaticVertexBufferInterface::~StaticVertexBufferInterface() {}
bool StaticVertexBufferInterface::matchesAttribute(const gl::VertexAttribute &attrib,
const gl::VertexBinding &binding) const
{
return mSignature.matchesAttribute(attrib, binding);
}
void StaticVertexBufferInterface::setAttribute(const gl::VertexAttribute &attrib,
const gl::VertexBinding &binding)
{
return mSignature.set(attrib, binding);
}
angle::Result StaticVertexBufferInterface::storeStaticAttribute(const gl::Context *context,
const gl::VertexAttribute &attrib,
const gl::VertexBinding &binding,
GLint start,
GLsizei count,
GLsizei instances,
const uint8_t *sourceData)
{
unsigned int spaceRequired = 0;
ANGLE_TRY(getSpaceRequired(context, attrib, binding, count, instances, &spaceRequired));
ANGLE_TRY(setBufferSize(context, spaceRequired));
ASSERT(attrib.enabled);
ANGLE_TRY(mVertexBuffer->storeVertexAttributes(context, attrib, binding,
gl::VertexAttribType::InvalidEnum, start, count,
instances, 0, sourceData));
mSignature.set(attrib, binding);
mVertexBuffer->hintUnmapResource();
return angle::Result::Continue;
}
} // namespace rx