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: // Indexffer9.cpp: Defines the D3D9 IndexBuffer implementation. michael@0: michael@0: #include "libGLESv2/renderer/IndexBuffer9.h" michael@0: #include "libGLESv2/renderer/Renderer9.h" michael@0: michael@0: namespace rx michael@0: { michael@0: michael@0: IndexBuffer9::IndexBuffer9(Renderer9 *const renderer) : mRenderer(renderer) michael@0: { michael@0: mIndexBuffer = NULL; michael@0: mBufferSize = 0; michael@0: mIndexType = 0; michael@0: mDynamic = false; michael@0: } michael@0: michael@0: IndexBuffer9::~IndexBuffer9() michael@0: { michael@0: if (mIndexBuffer) michael@0: { michael@0: mIndexBuffer->Release(); michael@0: mIndexBuffer = NULL; michael@0: } michael@0: } michael@0: michael@0: bool IndexBuffer9::initialize(unsigned int bufferSize, GLenum indexType, bool dynamic) michael@0: { michael@0: if (mIndexBuffer) michael@0: { michael@0: mIndexBuffer->Release(); michael@0: mIndexBuffer = NULL; michael@0: } michael@0: michael@0: updateSerial(); michael@0: michael@0: if (bufferSize > 0) michael@0: { michael@0: D3DFORMAT format; michael@0: if (indexType == GL_UNSIGNED_SHORT || indexType == GL_UNSIGNED_BYTE) michael@0: { michael@0: format = D3DFMT_INDEX16; michael@0: } michael@0: else if (indexType == GL_UNSIGNED_INT) michael@0: { michael@0: if (mRenderer->get32BitIndexSupport()) michael@0: { michael@0: format = D3DFMT_INDEX32; michael@0: } michael@0: else michael@0: { michael@0: ERR("Attempted to create a 32-bit index buffer but renderer does not support 32-bit indices."); michael@0: return false; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: ERR("Invalid index type %u.", indexType); michael@0: return false; michael@0: } michael@0: michael@0: DWORD usageFlags = D3DUSAGE_WRITEONLY; michael@0: if (dynamic) michael@0: { michael@0: usageFlags |= D3DUSAGE_DYNAMIC; michael@0: } michael@0: michael@0: HRESULT result = mRenderer->createIndexBuffer(bufferSize, usageFlags, format, &mIndexBuffer); michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Failed to create an index buffer of size %u, result: 0x%08x.", mBufferSize, result); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: mBufferSize = bufferSize; michael@0: mIndexType = indexType; michael@0: mDynamic = dynamic; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: IndexBuffer9 *IndexBuffer9::makeIndexBuffer9(IndexBuffer *indexBuffer) michael@0: { michael@0: ASSERT(HAS_DYNAMIC_TYPE(IndexBuffer9*, indexBuffer)); michael@0: return static_cast(indexBuffer); michael@0: } michael@0: michael@0: bool IndexBuffer9::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory) michael@0: { michael@0: if (mIndexBuffer) michael@0: { michael@0: DWORD lockFlags = mDynamic ? D3DLOCK_NOOVERWRITE : 0; michael@0: michael@0: void *mapPtr = NULL; michael@0: HRESULT result = mIndexBuffer->Lock(offset, size, &mapPtr, lockFlags); michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Index buffer lock failed with error 0x%08x", result); michael@0: return false; michael@0: } michael@0: michael@0: *outMappedMemory = mapPtr; michael@0: return true; michael@0: } michael@0: else michael@0: { michael@0: ERR("Index buffer not initialized."); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: bool IndexBuffer9::unmapBuffer() michael@0: { michael@0: if (mIndexBuffer) michael@0: { michael@0: HRESULT result = mIndexBuffer->Unlock(); michael@0: if (FAILED(result)) michael@0: { michael@0: ERR("Index buffer 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("Index buffer not initialized."); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: GLenum IndexBuffer9::getIndexType() const michael@0: { michael@0: return mIndexType; michael@0: } michael@0: michael@0: unsigned int IndexBuffer9::getBufferSize() const michael@0: { michael@0: return mBufferSize; michael@0: } michael@0: michael@0: bool IndexBuffer9::setSize(unsigned int bufferSize, GLenum indexType) michael@0: { michael@0: if (bufferSize > mBufferSize || indexType != mIndexType) michael@0: { michael@0: return initialize(bufferSize, indexType, mDynamic); michael@0: } michael@0: else michael@0: { michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: bool IndexBuffer9::discard() michael@0: { michael@0: if (mIndexBuffer) michael@0: { michael@0: void *dummy; michael@0: HRESULT result; michael@0: michael@0: result = mIndexBuffer->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 = mIndexBuffer->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("Index buffer not initialized."); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: D3DFORMAT IndexBuffer9::getIndexFormat() const michael@0: { michael@0: switch (mIndexType) michael@0: { michael@0: case GL_UNSIGNED_BYTE: return D3DFMT_INDEX16; michael@0: case GL_UNSIGNED_SHORT: return D3DFMT_INDEX16; michael@0: case GL_UNSIGNED_INT: return D3DFMT_INDEX32; michael@0: default: UNREACHABLE(); return D3DFMT_UNKNOWN; michael@0: } michael@0: } michael@0: michael@0: IDirect3DIndexBuffer9 * IndexBuffer9::getBuffer() const michael@0: { michael@0: return mIndexBuffer; michael@0: } michael@0: michael@0: }