michael@0: #include "precompiled.h" michael@0: // michael@0: // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: // VertexBuffer9.cpp: Defines the D3D9 VertexBuffer implementation. michael@0: michael@0: #include "libGLESv2/renderer/VertexBuffer9.h" michael@0: #include "libGLESv2/renderer/vertexconversion.h" michael@0: #include "libGLESv2/renderer/BufferStorage.h" michael@0: #include "libGLESv2/Context.h" michael@0: #include "libGLESv2/renderer/Renderer9.h" michael@0: michael@0: #include "libGLESv2/Buffer.h" michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: bool VertexBuffer9::mTranslationsInitialized = false; michael@0: VertexBuffer9::FormatConverter VertexBuffer9::mFormatConverters[NUM_GL_VERTEX_ATTRIB_TYPES][2][4]; michael@0: michael@0: VertexBuffer9::VertexBuffer9(rx::Renderer9 *const renderer) : mRenderer(renderer) michael@0: { michael@0: mVertexBuffer = NULL; michael@0: mBufferSize = 0; michael@0: mDynamicUsage = false; michael@0: michael@0: if (!mTranslationsInitialized) michael@0: { michael@0: initializeTranslations(renderer->getCapsDeclTypes()); michael@0: mTranslationsInitialized = true; michael@0: } michael@0: } michael@0: michael@0: VertexBuffer9::~VertexBuffer9() michael@0: { michael@0: if (mVertexBuffer) michael@0: { michael@0: mVertexBuffer->Release(); michael@0: mVertexBuffer = NULL; michael@0: } michael@0: } michael@0: michael@0: bool VertexBuffer9::initialize(unsigned int size, bool dynamicUsage) michael@0: { michael@0: if (mVertexBuffer) michael@0: { michael@0: mVertexBuffer->Release(); michael@0: mVertexBuffer = NULL; michael@0: } michael@0: michael@0: updateSerial(); michael@0: michael@0: if (size > 0) michael@0: { michael@0: DWORD flags = D3DUSAGE_WRITEONLY; michael@0: if (dynamicUsage) michael@0: { michael@0: flags |= D3DUSAGE_DYNAMIC; michael@0: } michael@0: michael@0: HRESULT result = mRenderer->createVertexBuffer(size, flags, &mVertexBuffer); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Out of memory allocating a vertex buffer of size %lu.", size); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: mBufferSize = size; michael@0: mDynamicUsage = dynamicUsage; michael@0: return true; michael@0: } michael@0: michael@0: VertexBuffer9 *VertexBuffer9::makeVertexBuffer9(VertexBuffer *vertexBuffer) michael@0: { michael@0: ASSERT(HAS_DYNAMIC_TYPE(VertexBuffer9*, vertexBuffer)); michael@0: return static_cast(vertexBuffer); michael@0: } michael@0: michael@0: bool VertexBuffer9::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, michael@0: GLsizei instances, unsigned int offset) michael@0: { michael@0: if (mVertexBuffer) michael@0: { michael@0: gl::Buffer *buffer = attrib.mBoundBuffer.get(); michael@0: michael@0: int inputStride = attrib.stride(); michael@0: int elementSize = attrib.typeSize(); michael@0: const FormatConverter &converter = formatConverter(attrib); michael@0: michael@0: DWORD lockFlags = mDynamicUsage ? D3DLOCK_NOOVERWRITE : 0; michael@0: michael@0: void *mapPtr = NULL; michael@0: michael@0: unsigned int mapSize; michael@0: if (!spaceRequired(attrib, count, instances, &mapSize)) michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: HRESULT result = mVertexBuffer->Lock(offset, mapSize, &mapPtr, lockFlags); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Lock failed with error 0x%08x", result); michael@0: return false; michael@0: } michael@0: michael@0: const char *input = NULL; michael@0: if (buffer) michael@0: { michael@0: BufferStorage *storage = buffer->getStorage(); michael@0: input = static_cast(storage->getData()) + static_cast(attrib.mOffset); michael@0: } michael@0: else michael@0: { michael@0: input = static_cast(attrib.mPointer); michael@0: } michael@0: michael@0: if (instances == 0 || attrib.mDivisor == 0) michael@0: { michael@0: input += inputStride * start; michael@0: } michael@0: michael@0: if (converter.identity && inputStride == elementSize) michael@0: { michael@0: memcpy(mapPtr, input, count * inputStride); michael@0: } michael@0: else michael@0: { michael@0: converter.convertArray(input, inputStride, count, mapPtr); michael@0: } michael@0: michael@0: mVertexBuffer->Unlock(); michael@0: michael@0: return true; michael@0: } michael@0: else michael@0: { michael@0: ERR("Vertex buffer not initialized."); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: bool VertexBuffer9::storeRawData(const void* data, unsigned int size, unsigned int offset) michael@0: { michael@0: if (mVertexBuffer) michael@0: { michael@0: DWORD lockFlags = mDynamicUsage ? D3DLOCK_NOOVERWRITE : 0; michael@0: michael@0: void *mapPtr = NULL; michael@0: HRESULT result = mVertexBuffer->Lock(offset, size, &mapPtr, lockFlags); michael@0: michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Lock failed with error 0x%08x", result); michael@0: return false; michael@0: } michael@0: michael@0: memcpy(mapPtr, data, size); michael@0: michael@0: mVertexBuffer->Unlock(); michael@0: michael@0: return true; michael@0: } michael@0: else michael@0: { michael@0: ERR("Vertex buffer not initialized."); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: bool VertexBuffer9::getSpaceRequired(const gl::VertexAttribute &attrib, GLsizei count, GLsizei instances, michael@0: unsigned int *outSpaceRequired) const michael@0: { michael@0: return spaceRequired(attrib, count, instances, outSpaceRequired); michael@0: } michael@0: michael@0: bool VertexBuffer9::requiresConversion(const gl::VertexAttribute &attrib) const michael@0: { michael@0: return formatConverter(attrib).identity; michael@0: } michael@0: michael@0: unsigned int VertexBuffer9::getVertexSize(const gl::VertexAttribute &attrib) const michael@0: { michael@0: unsigned int spaceRequired; michael@0: return getSpaceRequired(attrib, 1, 0, &spaceRequired) ? spaceRequired : 0; michael@0: } michael@0: michael@0: D3DDECLTYPE VertexBuffer9::getDeclType(const gl::VertexAttribute &attrib) const michael@0: { michael@0: return formatConverter(attrib).d3dDeclType; michael@0: } michael@0: michael@0: unsigned int VertexBuffer9::getBufferSize() const michael@0: { michael@0: return mBufferSize; michael@0: } michael@0: michael@0: bool VertexBuffer9::setBufferSize(unsigned int size) michael@0: { michael@0: if (size > mBufferSize) michael@0: { michael@0: return initialize(size, mDynamicUsage); michael@0: } michael@0: else michael@0: { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: bool VertexBuffer9::discard() michael@0: { michael@0: if (mVertexBuffer) michael@0: { michael@0: void *dummy; michael@0: HRESULT result; michael@0: michael@0: result = mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD); michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Discard lock failed with error 0x%08x", result); michael@0: return false; michael@0: } michael@0: michael@0: result = mVertexBuffer->Unlock(); michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Discard unlock failed with error 0x%08x", result); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: else michael@0: { michael@0: ERR("Vertex buffer not initialized."); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: IDirect3DVertexBuffer9 * VertexBuffer9::getBuffer() const michael@0: { michael@0: return mVertexBuffer; michael@0: } michael@0: michael@0: // Mapping from OpenGL-ES vertex attrib type to D3D decl type: michael@0: // michael@0: // BYTE SHORT (Cast) michael@0: // BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm) michael@0: // UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast) michael@0: // UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize) michael@0: // SHORT SHORT (Identity) michael@0: // SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize) michael@0: // UNSIGNED_SHORT FLOAT (Cast) michael@0: // UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize) michael@0: // FIXED (not in WebGL) FLOAT (FixedToFloat) michael@0: // FLOAT FLOAT (Identity) michael@0: michael@0: // GLToCType maps from GL type (as GLenum) to the C typedef. michael@0: template struct GLToCType { }; michael@0: michael@0: template <> struct GLToCType { typedef GLbyte type; }; michael@0: template <> struct GLToCType { typedef GLubyte type; }; michael@0: template <> struct GLToCType { typedef GLshort type; }; michael@0: template <> struct GLToCType { typedef GLushort type; }; michael@0: template <> struct GLToCType { typedef GLuint type; }; michael@0: template <> struct GLToCType { typedef GLfloat type; }; michael@0: michael@0: // This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.) michael@0: enum D3DVertexType michael@0: { michael@0: D3DVT_FLOAT, michael@0: D3DVT_SHORT, michael@0: D3DVT_SHORT_NORM, michael@0: D3DVT_UBYTE, michael@0: D3DVT_UBYTE_NORM, michael@0: D3DVT_USHORT_NORM michael@0: }; michael@0: michael@0: // D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type. michael@0: template struct D3DToCType { }; michael@0: michael@0: template <> struct D3DToCType { typedef float type; }; michael@0: template <> struct D3DToCType { typedef short type; }; michael@0: template <> struct D3DToCType { typedef short type; }; michael@0: template <> struct D3DToCType { typedef unsigned char type; }; michael@0: template <> struct D3DToCType { typedef unsigned char type; }; michael@0: template <> struct D3DToCType { typedef unsigned short type; }; michael@0: michael@0: // Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size. michael@0: template struct WidenRule { }; michael@0: michael@0: template struct WidenRule : NoWiden { }; michael@0: template struct WidenRule : WidenToEven { }; michael@0: template struct WidenRule : WidenToEven { }; michael@0: template struct WidenRule : WidenToFour { }; michael@0: template struct WidenRule : WidenToFour { }; michael@0: template struct WidenRule : WidenToEven { }; michael@0: michael@0: // VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination. michael@0: template struct VertexTypeFlags { }; michael@0: michael@0: template michael@0: struct VertexTypeFlagsHelper michael@0: { michael@0: enum { capflag = _capflag }; michael@0: enum { declflag = _declflag }; michael@0: }; michael@0: michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; michael@0: template <> struct VertexTypeFlags : VertexTypeFlagsHelper { }; michael@0: michael@0: michael@0: // VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums). michael@0: template struct VertexTypeMapping { }; michael@0: michael@0: template michael@0: struct VertexTypeMappingBase michael@0: { michael@0: enum { preferred = Preferred }; michael@0: enum { fallback = Fallback }; michael@0: }; michael@0: michael@0: template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Cast michael@0: template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Normalize michael@0: template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Identity, Cast michael@0: template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Identity, Normalize michael@0: template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Identity michael@0: template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Cast, Normalize michael@0: template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Cast michael@0: template <> struct VertexTypeMapping : VertexTypeMappingBase { }; // Cast, Normalize michael@0: template struct VertexTypeMapping : VertexTypeMappingBase { }; // FixedToFloat michael@0: template struct VertexTypeMapping : VertexTypeMappingBase { }; // Identity michael@0: michael@0: michael@0: // Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat). michael@0: // The conversion rules themselves are defined in vertexconversion.h. michael@0: michael@0: // Almost all cases are covered by Cast (including those that are actually Identity since Cast knows it's an identity mapping). michael@0: template michael@0: struct ConversionRule : Cast::type, typename D3DToCType::type> { }; michael@0: michael@0: // All conversions from normalized types to float use the Normalize operator. michael@0: template struct ConversionRule : Normalize::type> { }; michael@0: michael@0: // Use a full specialization for this so that it preferentially matches ahead of the generic normalize-to-float rules. michael@0: template <> struct ConversionRule : FixedToFloat { }; michael@0: template <> struct ConversionRule : FixedToFloat { }; michael@0: michael@0: // A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1) michael@0: // whether it is normalized or not. michael@0: template struct DefaultVertexValuesStage2 { }; michael@0: michael@0: template struct DefaultVertexValuesStage2 : NormalizedDefaultValues { }; michael@0: template struct DefaultVertexValuesStage2 : SimpleDefaultValues { }; michael@0: michael@0: // Work out the default value rule for a D3D type (expressed as the C type) and michael@0: template struct DefaultVertexValues : DefaultVertexValuesStage2 { }; michael@0: template struct DefaultVertexValues : SimpleDefaultValues { }; michael@0: michael@0: // Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion. michael@0: // The fallback conversion produces an output that all D3D9 devices must support. michael@0: template struct UsePreferred { enum { type = T::preferred }; }; michael@0: template struct UseFallback { enum { type = T::fallback }; }; michael@0: michael@0: // Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion, michael@0: // it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag michael@0: // and the D3DDECLTYPE member needed for the vertex declaration in declflag. michael@0: template class PreferenceRule> michael@0: struct Converter michael@0: : VertexDataConverter::type, michael@0: WidenRule >::type, size>, michael@0: ConversionRule >::type>, michael@0: DefaultVertexValues >::type>::type, normalized > > michael@0: { michael@0: private: michael@0: enum { d3dtype = PreferenceRule< VertexTypeMapping >::type }; michael@0: enum { d3dsize = WidenRule::finalWidth }; michael@0: michael@0: public: michael@0: enum { capflag = VertexTypeFlags::capflag }; michael@0: enum { declflag = VertexTypeFlags::declflag }; michael@0: }; michael@0: michael@0: // Initialize a TranslationInfo michael@0: #define TRANSLATION(type, norm, size, preferred) \ michael@0: { \ michael@0: Converter::identity, \ michael@0: Converter::finalSize, \ michael@0: Converter::convertArray, \ michael@0: static_cast(Converter::declflag) \ michael@0: } michael@0: michael@0: #define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \ michael@0: { \ michael@0: Converter::capflag, \ michael@0: TRANSLATION(type, norm, size, UsePreferred), \ michael@0: TRANSLATION(type, norm, size, UseFallback) \ michael@0: } michael@0: michael@0: #define TRANSLATIONS_FOR_TYPE(type) \ michael@0: { \ michael@0: { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \ michael@0: { TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 4) }, \ michael@0: } michael@0: michael@0: #define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \ michael@0: { \ michael@0: { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \ michael@0: { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \ michael@0: } michael@0: michael@0: const VertexBuffer9::TranslationDescription VertexBuffer9::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1] michael@0: { michael@0: TRANSLATIONS_FOR_TYPE(GL_BYTE), michael@0: TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE), michael@0: TRANSLATIONS_FOR_TYPE(GL_SHORT), michael@0: TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT), michael@0: TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED), michael@0: TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT) michael@0: }; michael@0: michael@0: void VertexBuffer9::initializeTranslations(DWORD declTypes) michael@0: { michael@0: for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++) michael@0: { michael@0: for (unsigned int j = 0; j < 2; j++) michael@0: { michael@0: for (unsigned int k = 0; k < 4; k++) michael@0: { michael@0: if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0) michael@0: { michael@0: mFormatConverters[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion; michael@0: } michael@0: else michael@0: { michael@0: mFormatConverters[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: unsigned int VertexBuffer9::typeIndex(GLenum type) michael@0: { michael@0: switch (type) michael@0: { michael@0: case GL_BYTE: return 0; michael@0: case GL_UNSIGNED_BYTE: return 1; michael@0: case GL_SHORT: return 2; michael@0: case GL_UNSIGNED_SHORT: return 3; michael@0: case GL_FIXED: return 4; michael@0: case GL_FLOAT: return 5; michael@0: michael@0: default: UNREACHABLE(); return 5; michael@0: } michael@0: } michael@0: michael@0: const VertexBuffer9::FormatConverter &VertexBuffer9::formatConverter(const gl::VertexAttribute &attribute) michael@0: { michael@0: return mFormatConverters[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1]; michael@0: } michael@0: michael@0: bool VertexBuffer9::spaceRequired(const gl::VertexAttribute &attrib, std::size_t count, GLsizei instances, michael@0: unsigned int *outSpaceRequired) michael@0: { michael@0: unsigned int elementSize = formatConverter(attrib).outputElementSize; michael@0: michael@0: if (instances == 0 || attrib.mDivisor == 0) michael@0: { michael@0: unsigned int elementCount = 0; michael@0: if (instances == 0 || attrib.mDivisor == 0) michael@0: { michael@0: elementCount = count; michael@0: } michael@0: else michael@0: { michael@0: if (static_cast(instances) < std::numeric_limits::max() - (attrib.mDivisor - 1)) michael@0: { michael@0: // Round up michael@0: elementCount = (static_cast(instances) + (attrib.mDivisor - 1)) / attrib.mDivisor; michael@0: } michael@0: else michael@0: { michael@0: elementCount = static_cast(instances) / attrib.mDivisor; michael@0: } michael@0: } michael@0: michael@0: if (elementSize <= std::numeric_limits::max() / elementCount) michael@0: { michael@0: if (outSpaceRequired) michael@0: { michael@0: *outSpaceRequired = elementSize * elementCount; michael@0: } michael@0: return true; michael@0: } michael@0: else michael@0: { michael@0: return false; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: const unsigned int elementSize = 4; michael@0: if (outSpaceRequired) michael@0: { michael@0: *outSpaceRequired = elementSize * 4; michael@0: } michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: }