gfx/angle/src/libGLESv2/renderer/VertexBuffer11.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 #include "precompiled.h"
michael@0 2 //
michael@0 3 // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved.
michael@0 4 // Use of this source code is governed by a BSD-style license that can be
michael@0 5 // found in the LICENSE file.
michael@0 6 //
michael@0 7
michael@0 8 // VertexBuffer11.cpp: Defines the D3D11 VertexBuffer implementation.
michael@0 9
michael@0 10 #include "libGLESv2/renderer/VertexBuffer11.h"
michael@0 11 #include "libGLESv2/renderer/BufferStorage.h"
michael@0 12
michael@0 13 #include "libGLESv2/Buffer.h"
michael@0 14 #include "libGLESv2/renderer/Renderer11.h"
michael@0 15 #include "libGLESv2/Context.h"
michael@0 16
michael@0 17 namespace rx
michael@0 18 {
michael@0 19
michael@0 20 VertexBuffer11::VertexBuffer11(rx::Renderer11 *const renderer) : mRenderer(renderer)
michael@0 21 {
michael@0 22 mBuffer = NULL;
michael@0 23 mBufferSize = 0;
michael@0 24 mDynamicUsage = false;
michael@0 25 }
michael@0 26
michael@0 27 VertexBuffer11::~VertexBuffer11()
michael@0 28 {
michael@0 29 if (mBuffer)
michael@0 30 {
michael@0 31 mBuffer->Release();
michael@0 32 mBuffer = NULL;
michael@0 33 }
michael@0 34 }
michael@0 35
michael@0 36 bool VertexBuffer11::initialize(unsigned int size, bool dynamicUsage)
michael@0 37 {
michael@0 38 if (mBuffer)
michael@0 39 {
michael@0 40 mBuffer->Release();
michael@0 41 mBuffer = NULL;
michael@0 42 }
michael@0 43
michael@0 44 updateSerial();
michael@0 45
michael@0 46 if (size > 0)
michael@0 47 {
michael@0 48 ID3D11Device* dxDevice = mRenderer->getDevice();
michael@0 49
michael@0 50 D3D11_BUFFER_DESC bufferDesc;
michael@0 51 bufferDesc.ByteWidth = size;
michael@0 52 bufferDesc.Usage = D3D11_USAGE_DYNAMIC;
michael@0 53 bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
michael@0 54 bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
michael@0 55 bufferDesc.MiscFlags = 0;
michael@0 56 bufferDesc.StructureByteStride = 0;
michael@0 57
michael@0 58 HRESULT result = dxDevice->CreateBuffer(&bufferDesc, NULL, &mBuffer);
michael@0 59 if (FAILED(result))
michael@0 60 {
michael@0 61 return false;
michael@0 62 }
michael@0 63 }
michael@0 64
michael@0 65 mBufferSize = size;
michael@0 66 mDynamicUsage = dynamicUsage;
michael@0 67 return true;
michael@0 68 }
michael@0 69
michael@0 70 VertexBuffer11 *VertexBuffer11::makeVertexBuffer11(VertexBuffer *vetexBuffer)
michael@0 71 {
michael@0 72 ASSERT(HAS_DYNAMIC_TYPE(VertexBuffer11*, vetexBuffer));
michael@0 73 return static_cast<VertexBuffer11*>(vetexBuffer);
michael@0 74 }
michael@0 75
michael@0 76 bool VertexBuffer11::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count,
michael@0 77 GLsizei instances, unsigned int offset)
michael@0 78 {
michael@0 79 if (mBuffer)
michael@0 80 {
michael@0 81 gl::Buffer *buffer = attrib.mBoundBuffer.get();
michael@0 82
michael@0 83 int inputStride = attrib.stride();
michael@0 84 const VertexConverter &converter = getVertexConversion(attrib);
michael@0 85
michael@0 86 ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext();
michael@0 87
michael@0 88 D3D11_MAPPED_SUBRESOURCE mappedResource;
michael@0 89 HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource);
michael@0 90 if (FAILED(result))
michael@0 91 {
michael@0 92 ERR("Vertex buffer map failed with error 0x%08x", result);
michael@0 93 return false;
michael@0 94 }
michael@0 95
michael@0 96 char* output = reinterpret_cast<char*>(mappedResource.pData) + offset;
michael@0 97
michael@0 98 const char *input = NULL;
michael@0 99 if (buffer)
michael@0 100 {
michael@0 101 BufferStorage *storage = buffer->getStorage();
michael@0 102 input = static_cast<const char*>(storage->getData()) + static_cast<int>(attrib.mOffset);
michael@0 103 }
michael@0 104 else
michael@0 105 {
michael@0 106 input = static_cast<const char*>(attrib.mPointer);
michael@0 107 }
michael@0 108
michael@0 109 if (instances == 0 || attrib.mDivisor == 0)
michael@0 110 {
michael@0 111 input += inputStride * start;
michael@0 112 }
michael@0 113
michael@0 114 converter.conversionFunc(input, inputStride, count, output);
michael@0 115
michael@0 116 dxContext->Unmap(mBuffer, 0);
michael@0 117
michael@0 118 return true;
michael@0 119 }
michael@0 120 else
michael@0 121 {
michael@0 122 ERR("Vertex buffer not initialized.");
michael@0 123 return false;
michael@0 124 }
michael@0 125 }
michael@0 126
michael@0 127 bool VertexBuffer11::storeRawData(const void* data, unsigned int size, unsigned int offset)
michael@0 128 {
michael@0 129 if (mBuffer)
michael@0 130 {
michael@0 131 ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext();
michael@0 132
michael@0 133 D3D11_MAPPED_SUBRESOURCE mappedResource;
michael@0 134 HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource);
michael@0 135 if (FAILED(result))
michael@0 136 {
michael@0 137 ERR("Vertex buffer map failed with error 0x%08x", result);
michael@0 138 return false;
michael@0 139 }
michael@0 140
michael@0 141 char* bufferData = static_cast<char*>(mappedResource.pData);
michael@0 142 memcpy(bufferData + offset, data, size);
michael@0 143
michael@0 144 dxContext->Unmap(mBuffer, 0);
michael@0 145
michael@0 146 return true;
michael@0 147 }
michael@0 148 else
michael@0 149 {
michael@0 150 ERR("Vertex buffer not initialized.");
michael@0 151 return false;
michael@0 152 }
michael@0 153 }
michael@0 154
michael@0 155 bool VertexBuffer11::getSpaceRequired(const gl::VertexAttribute &attrib, GLsizei count,
michael@0 156 GLsizei instances, unsigned int *outSpaceRequired) const
michael@0 157 {
michael@0 158 unsigned int elementSize = getVertexConversion(attrib).outputElementSize;
michael@0 159
michael@0 160 unsigned int elementCount = 0;
michael@0 161 if (instances == 0 || attrib.mDivisor == 0)
michael@0 162 {
michael@0 163 elementCount = count;
michael@0 164 }
michael@0 165 else
michael@0 166 {
michael@0 167 if (static_cast<unsigned int>(instances) < std::numeric_limits<unsigned int>::max() - (attrib.mDivisor - 1))
michael@0 168 {
michael@0 169 // Round up
michael@0 170 elementCount = (static_cast<unsigned int>(instances) + (attrib.mDivisor - 1)) / attrib.mDivisor;
michael@0 171 }
michael@0 172 else
michael@0 173 {
michael@0 174 elementCount = instances / attrib.mDivisor;
michael@0 175 }
michael@0 176 }
michael@0 177
michael@0 178 if (elementSize <= std::numeric_limits<unsigned int>::max() / elementCount)
michael@0 179 {
michael@0 180 if (outSpaceRequired)
michael@0 181 {
michael@0 182 *outSpaceRequired = elementSize * elementCount;
michael@0 183 }
michael@0 184 return true;
michael@0 185 }
michael@0 186 else
michael@0 187 {
michael@0 188 return false;
michael@0 189 }
michael@0 190 }
michael@0 191
michael@0 192 bool VertexBuffer11::requiresConversion(const gl::VertexAttribute &attrib) const
michael@0 193 {
michael@0 194 return !getVertexConversion(attrib).identity;
michael@0 195 }
michael@0 196
michael@0 197 unsigned int VertexBuffer11::getBufferSize() const
michael@0 198 {
michael@0 199 return mBufferSize;
michael@0 200 }
michael@0 201
michael@0 202 bool VertexBuffer11::setBufferSize(unsigned int size)
michael@0 203 {
michael@0 204 if (size > mBufferSize)
michael@0 205 {
michael@0 206 return initialize(size, mDynamicUsage);
michael@0 207 }
michael@0 208 else
michael@0 209 {
michael@0 210 return true;
michael@0 211 }
michael@0 212 }
michael@0 213
michael@0 214 bool VertexBuffer11::discard()
michael@0 215 {
michael@0 216 if (mBuffer)
michael@0 217 {
michael@0 218 ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext();
michael@0 219
michael@0 220 D3D11_MAPPED_SUBRESOURCE mappedResource;
michael@0 221 HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
michael@0 222 if (FAILED(result))
michael@0 223 {
michael@0 224 ERR("Vertex buffer map failed with error 0x%08x", result);
michael@0 225 return false;
michael@0 226 }
michael@0 227
michael@0 228 dxContext->Unmap(mBuffer, 0);
michael@0 229
michael@0 230 return true;
michael@0 231 }
michael@0 232 else
michael@0 233 {
michael@0 234 ERR("Vertex buffer not initialized.");
michael@0 235 return false;
michael@0 236 }
michael@0 237 }
michael@0 238
michael@0 239 unsigned int VertexBuffer11::getVertexSize(const gl::VertexAttribute &attrib) const
michael@0 240 {
michael@0 241 return getVertexConversion(attrib).outputElementSize;
michael@0 242 }
michael@0 243
michael@0 244 DXGI_FORMAT VertexBuffer11::getDXGIFormat(const gl::VertexAttribute &attrib) const
michael@0 245 {
michael@0 246 return getVertexConversion(attrib).dxgiFormat;
michael@0 247 }
michael@0 248
michael@0 249 ID3D11Buffer *VertexBuffer11::getBuffer() const
michael@0 250 {
michael@0 251 return mBuffer;
michael@0 252 }
michael@0 253
michael@0 254 template <typename T, unsigned int componentCount, bool widen, bool normalized>
michael@0 255 static void copyVertexData(const void *input, unsigned int stride, unsigned int count, void *output)
michael@0 256 {
michael@0 257 unsigned int attribSize = sizeof(T) * componentCount;
michael@0 258
michael@0 259 if (attribSize == stride && !widen)
michael@0 260 {
michael@0 261 memcpy(output, input, count * attribSize);
michael@0 262 }
michael@0 263 else
michael@0 264 {
michael@0 265 unsigned int outputStride = widen ? 4 : componentCount;
michael@0 266 T defaultVal = normalized ? std::numeric_limits<T>::max() : T(1);
michael@0 267
michael@0 268 for (unsigned int i = 0; i < count; i++)
michael@0 269 {
michael@0 270 const T *offsetInput = reinterpret_cast<const T*>(reinterpret_cast<const char*>(input) + i * stride);
michael@0 271 T *offsetOutput = reinterpret_cast<T*>(output) + i * outputStride;
michael@0 272
michael@0 273 for (unsigned int j = 0; j < componentCount; j++)
michael@0 274 {
michael@0 275 offsetOutput[j] = offsetInput[j];
michael@0 276 }
michael@0 277
michael@0 278 if (widen)
michael@0 279 {
michael@0 280 offsetOutput[3] = defaultVal;
michael@0 281 }
michael@0 282 }
michael@0 283 }
michael@0 284 }
michael@0 285
michael@0 286 template <unsigned int componentCount>
michael@0 287 static void copyFixedVertexData(const void* input, unsigned int stride, unsigned int count, void* output)
michael@0 288 {
michael@0 289 static const float divisor = 1.0f / (1 << 16);
michael@0 290
michael@0 291 for (unsigned int i = 0; i < count; i++)
michael@0 292 {
michael@0 293 const GLfixed* offsetInput = reinterpret_cast<const GLfixed*>(reinterpret_cast<const char*>(input) + stride * i);
michael@0 294 float* offsetOutput = reinterpret_cast<float*>(output) + i * componentCount;
michael@0 295
michael@0 296 for (unsigned int j = 0; j < componentCount; j++)
michael@0 297 {
michael@0 298 offsetOutput[j] = static_cast<float>(offsetInput[j]) * divisor;
michael@0 299 }
michael@0 300 }
michael@0 301 }
michael@0 302
michael@0 303 template <typename T, unsigned int componentCount, bool normalized>
michael@0 304 static void copyToFloatVertexData(const void* input, unsigned int stride, unsigned int count, void* output)
michael@0 305 {
michael@0 306 typedef std::numeric_limits<T> NL;
michael@0 307
michael@0 308 for (unsigned int i = 0; i < count; i++)
michael@0 309 {
michael@0 310 const T *offsetInput = reinterpret_cast<const T*>(reinterpret_cast<const char*>(input) + stride * i);
michael@0 311 float *offsetOutput = reinterpret_cast<float*>(output) + i * componentCount;
michael@0 312
michael@0 313 for (unsigned int j = 0; j < componentCount; j++)
michael@0 314 {
michael@0 315 if (normalized)
michael@0 316 {
michael@0 317 if (NL::is_signed)
michael@0 318 {
michael@0 319 const float divisor = 1.0f / (2 * static_cast<float>(NL::max()) + 1);
michael@0 320 offsetOutput[j] = (2 * static_cast<float>(offsetInput[j]) + 1) * divisor;
michael@0 321 }
michael@0 322 else
michael@0 323 {
michael@0 324 offsetOutput[j] = static_cast<float>(offsetInput[j]) / NL::max();
michael@0 325 }
michael@0 326 }
michael@0 327 else
michael@0 328 {
michael@0 329 offsetOutput[j] = static_cast<float>(offsetInput[j]);
michael@0 330 }
michael@0 331 }
michael@0 332 }
michael@0 333 }
michael@0 334
michael@0 335 const VertexBuffer11::VertexConverter VertexBuffer11::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] =
michael@0 336 {
michael@0 337 { // GL_BYTE
michael@0 338 { // unnormalized
michael@0 339 { &copyToFloatVertexData<GLbyte, 1, false>, false, DXGI_FORMAT_R32_FLOAT, 4 },
michael@0 340 { &copyToFloatVertexData<GLbyte, 2, false>, false, DXGI_FORMAT_R32G32_FLOAT, 8 },
michael@0 341 { &copyToFloatVertexData<GLbyte, 3, false>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 },
michael@0 342 { &copyToFloatVertexData<GLbyte, 4, false>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 },
michael@0 343 },
michael@0 344 { // normalized
michael@0 345 { &copyVertexData<GLbyte, 1, false, true>, true, DXGI_FORMAT_R8_SNORM, 1 },
michael@0 346 { &copyVertexData<GLbyte, 2, false, true>, true, DXGI_FORMAT_R8G8_SNORM, 2 },
michael@0 347 { &copyVertexData<GLbyte, 3, true, true>, false, DXGI_FORMAT_R8G8B8A8_SNORM, 4 },
michael@0 348 { &copyVertexData<GLbyte, 4, false, true>, true, DXGI_FORMAT_R8G8B8A8_SNORM, 4 },
michael@0 349 },
michael@0 350 },
michael@0 351 { // GL_UNSIGNED_BYTE
michael@0 352 { // unnormalized
michael@0 353 { &copyToFloatVertexData<GLubyte, 1, false>, false, DXGI_FORMAT_R32_FLOAT, 4 },
michael@0 354 { &copyToFloatVertexData<GLubyte, 2, false>, false, DXGI_FORMAT_R32G32_FLOAT, 8 },
michael@0 355 { &copyToFloatVertexData<GLubyte, 3, false>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 },
michael@0 356 { &copyToFloatVertexData<GLubyte, 4, false>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 },
michael@0 357 },
michael@0 358 { // normalized
michael@0 359 { &copyVertexData<GLubyte, 1, false, true>, true, DXGI_FORMAT_R8_UNORM, 1 },
michael@0 360 { &copyVertexData<GLubyte, 2, false, true>, true, DXGI_FORMAT_R8G8_UNORM, 2 },
michael@0 361 { &copyVertexData<GLubyte, 3, true, true>, false, DXGI_FORMAT_R8G8B8A8_UNORM, 4 },
michael@0 362 { &copyVertexData<GLubyte, 4, false, true>, true, DXGI_FORMAT_R8G8B8A8_UNORM, 4 },
michael@0 363 },
michael@0 364 },
michael@0 365 { // GL_SHORT
michael@0 366 { // unnormalized
michael@0 367 { &copyToFloatVertexData<GLshort, 1, false>, false, DXGI_FORMAT_R32_FLOAT, 4 },
michael@0 368 { &copyToFloatVertexData<GLshort, 2, false>, false, DXGI_FORMAT_R32G32_FLOAT, 8 },
michael@0 369 { &copyToFloatVertexData<GLshort, 3, false>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 },
michael@0 370 { &copyToFloatVertexData<GLshort, 4, false>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 },
michael@0 371 },
michael@0 372 { // normalized
michael@0 373 { &copyVertexData<GLshort, 1, false, true>, true, DXGI_FORMAT_R16_SNORM, 2 },
michael@0 374 { &copyVertexData<GLshort, 2, false, true>, true, DXGI_FORMAT_R16G16_SNORM, 4 },
michael@0 375 { &copyVertexData<GLshort, 3, true, true>, false, DXGI_FORMAT_R16G16B16A16_SNORM, 8 },
michael@0 376 { &copyVertexData<GLshort, 4, false, true>, true, DXGI_FORMAT_R16G16B16A16_SNORM, 8 },
michael@0 377 },
michael@0 378 },
michael@0 379 { // GL_UNSIGNED_SHORT
michael@0 380 { // unnormalized
michael@0 381 { &copyToFloatVertexData<GLushort, 1, false>, false, DXGI_FORMAT_R32_FLOAT, 4 },
michael@0 382 { &copyToFloatVertexData<GLushort, 2, false>, false, DXGI_FORMAT_R32G32_FLOAT, 8 },
michael@0 383 { &copyToFloatVertexData<GLushort, 3, false>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 },
michael@0 384 { &copyToFloatVertexData<GLushort, 4, false>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 },
michael@0 385 },
michael@0 386 { // normalized
michael@0 387 { &copyVertexData<GLushort, 1, false, true>, true, DXGI_FORMAT_R16_UNORM, 2 },
michael@0 388 { &copyVertexData<GLushort, 2, false, true>, true, DXGI_FORMAT_R16G16_UNORM, 4 },
michael@0 389 { &copyVertexData<GLushort, 3, true, true>, false, DXGI_FORMAT_R16G16B16A16_UNORM, 8 },
michael@0 390 { &copyVertexData<GLushort, 4, false, true>, true, DXGI_FORMAT_R16G16B16A16_UNORM, 8 },
michael@0 391 },
michael@0 392 },
michael@0 393 { // GL_FIXED
michael@0 394 { // unnormalized
michael@0 395 { &copyFixedVertexData<1>, false, DXGI_FORMAT_R32_FLOAT, 4 },
michael@0 396 { &copyFixedVertexData<2>, false, DXGI_FORMAT_R32G32_FLOAT, 8 },
michael@0 397 { &copyFixedVertexData<3>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 },
michael@0 398 { &copyFixedVertexData<4>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 },
michael@0 399 },
michael@0 400 { // normalized
michael@0 401 { &copyFixedVertexData<1>, false, DXGI_FORMAT_R32_FLOAT, 4 },
michael@0 402 { &copyFixedVertexData<2>, false, DXGI_FORMAT_R32G32_FLOAT, 8 },
michael@0 403 { &copyFixedVertexData<3>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 },
michael@0 404 { &copyFixedVertexData<4>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 },
michael@0 405 },
michael@0 406 },
michael@0 407 { // GL_FLOAT
michael@0 408 { // unnormalized
michael@0 409 { &copyVertexData<GLfloat, 1, false, false>, true, DXGI_FORMAT_R32_FLOAT, 4 },
michael@0 410 { &copyVertexData<GLfloat, 2, false, false>, true, DXGI_FORMAT_R32G32_FLOAT, 8 },
michael@0 411 { &copyVertexData<GLfloat, 3, false, false>, true, DXGI_FORMAT_R32G32B32_FLOAT, 12 },
michael@0 412 { &copyVertexData<GLfloat, 4, false, false>, true, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 },
michael@0 413 },
michael@0 414 { // normalized
michael@0 415 { &copyVertexData<GLfloat, 1, false, false>, true, DXGI_FORMAT_R32_FLOAT, 4 },
michael@0 416 { &copyVertexData<GLfloat, 2, false, false>, true, DXGI_FORMAT_R32G32_FLOAT, 8 },
michael@0 417 { &copyVertexData<GLfloat, 3, false, false>, true, DXGI_FORMAT_R32G32B32_FLOAT, 12 },
michael@0 418 { &copyVertexData<GLfloat, 4, false, false>, true, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 },
michael@0 419 },
michael@0 420 },
michael@0 421 };
michael@0 422
michael@0 423 const VertexBuffer11::VertexConverter &VertexBuffer11::getVertexConversion(const gl::VertexAttribute &attribute)
michael@0 424 {
michael@0 425 unsigned int typeIndex = 0;
michael@0 426 switch (attribute.mType)
michael@0 427 {
michael@0 428 case GL_BYTE: typeIndex = 0; break;
michael@0 429 case GL_UNSIGNED_BYTE: typeIndex = 1; break;
michael@0 430 case GL_SHORT: typeIndex = 2; break;
michael@0 431 case GL_UNSIGNED_SHORT: typeIndex = 3; break;
michael@0 432 case GL_FIXED: typeIndex = 4; break;
michael@0 433 case GL_FLOAT: typeIndex = 5; break;
michael@0 434 default: UNREACHABLE(); break;
michael@0 435 }
michael@0 436
michael@0 437 return mPossibleTranslations[typeIndex][attribute.mNormalized ? 1 : 0][attribute.mSize - 1];
michael@0 438 }
michael@0 439
michael@0 440 }

mercurial