daniel@transgaming.com | 11c2af5 | 2012-12-20 21:10:01 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // IndexBuffer11.cpp: Defines the D3D11 IndexBuffer implementation. |
| 8 | |
| 9 | #include "libGLESv2/renderer/IndexBuffer11.h" |
shannon.woods@transgaming.com | 486d9e9 | 2013-02-28 23:15:41 +0000 | [diff] [blame^] | 10 | #include "libGLESv2/renderer/Renderer11.h" |
daniel@transgaming.com | 11c2af5 | 2012-12-20 21:10:01 +0000 | [diff] [blame] | 11 | |
| 12 | namespace rx |
| 13 | { |
| 14 | |
| 15 | IndexBuffer11::IndexBuffer11(Renderer11 *const renderer) : mRenderer(renderer) |
| 16 | { |
| 17 | mBuffer = NULL; |
| 18 | mBufferSize = 0; |
| 19 | mDynamicUsage = false; |
| 20 | } |
| 21 | |
| 22 | IndexBuffer11::~IndexBuffer11() |
| 23 | { |
| 24 | if (mBuffer) |
| 25 | { |
| 26 | mBuffer->Release(); |
| 27 | mBuffer = NULL; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | bool IndexBuffer11::initialize(unsigned int bufferSize, GLenum indexType, bool dynamic) |
| 32 | { |
| 33 | if (mBuffer) |
| 34 | { |
| 35 | mBuffer->Release(); |
| 36 | mBuffer = NULL; |
| 37 | } |
| 38 | |
| 39 | updateSerial(); |
| 40 | |
| 41 | if (bufferSize > 0) |
| 42 | { |
| 43 | ID3D11Device* dxDevice = mRenderer->getDevice(); |
| 44 | |
| 45 | D3D11_BUFFER_DESC bufferDesc; |
| 46 | bufferDesc.ByteWidth = bufferSize; |
| 47 | bufferDesc.Usage = D3D11_USAGE_DYNAMIC; |
| 48 | bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; |
| 49 | bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; |
| 50 | bufferDesc.MiscFlags = 0; |
| 51 | bufferDesc.StructureByteStride = 0; |
| 52 | |
| 53 | HRESULT result = dxDevice->CreateBuffer(&bufferDesc, NULL, &mBuffer); |
| 54 | if (FAILED(result)) |
| 55 | { |
| 56 | return false; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | mBufferSize = bufferSize; |
| 61 | mIndexType = indexType; |
| 62 | mDynamicUsage = dynamic; |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | IndexBuffer11 *IndexBuffer11::makeIndexBuffer11(IndexBuffer *indexBuffer) |
| 68 | { |
apatrick@chromium.org | 8b400b1 | 2013-01-30 21:53:40 +0000 | [diff] [blame] | 69 | ASSERT(HAS_DYNAMIC_TYPE(IndexBuffer11*, indexBuffer)); |
daniel@transgaming.com | 11c2af5 | 2012-12-20 21:10:01 +0000 | [diff] [blame] | 70 | return static_cast<IndexBuffer11*>(indexBuffer); |
| 71 | } |
| 72 | |
| 73 | bool IndexBuffer11::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory) |
| 74 | { |
| 75 | if (mBuffer) |
| 76 | { |
| 77 | if (offset + size > mBufferSize) |
| 78 | { |
| 79 | ERR("Index buffer map range is not inside the buffer."); |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); |
| 84 | |
| 85 | D3D11_MAPPED_SUBRESOURCE mappedResource; |
| 86 | HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource); |
| 87 | if (FAILED(result)) |
| 88 | { |
| 89 | ERR("Index buffer map failed with error 0x%08x", result); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | *outMappedMemory = reinterpret_cast<char*>(mappedResource.pData) + offset; |
| 94 | return true; |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | ERR("Index buffer not initialized."); |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | bool IndexBuffer11::unmapBuffer() |
| 104 | { |
| 105 | if (mBuffer) |
| 106 | { |
| 107 | ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); |
| 108 | dxContext->Unmap(mBuffer, 0); |
| 109 | return true; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | ERR("Index buffer not initialized."); |
| 114 | return false; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | GLenum IndexBuffer11::getIndexType() const |
| 119 | { |
| 120 | return mIndexType; |
| 121 | } |
| 122 | |
| 123 | unsigned int IndexBuffer11::getBufferSize() const |
| 124 | { |
| 125 | return mBufferSize; |
| 126 | } |
| 127 | |
| 128 | bool IndexBuffer11::setSize(unsigned int bufferSize, GLenum indexType) |
| 129 | { |
| 130 | if (bufferSize > mBufferSize || indexType != mIndexType) |
| 131 | { |
| 132 | return initialize(bufferSize, indexType, mDynamicUsage); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | return true; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | bool IndexBuffer11::discard() |
| 141 | { |
| 142 | if (mBuffer) |
| 143 | { |
| 144 | ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); |
| 145 | |
| 146 | D3D11_MAPPED_SUBRESOURCE mappedResource; |
| 147 | HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); |
| 148 | if (FAILED(result)) |
| 149 | { |
| 150 | ERR("Index buffer map failed with error 0x%08x", result); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | dxContext->Unmap(mBuffer, 0); |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | ERR("Index buffer not initialized."); |
| 161 | return false; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | DXGI_FORMAT IndexBuffer11::getIndexFormat() const |
| 166 | { |
| 167 | switch (mIndexType) |
| 168 | { |
| 169 | case GL_UNSIGNED_BYTE: return DXGI_FORMAT_R16_UINT; |
| 170 | case GL_UNSIGNED_SHORT: return DXGI_FORMAT_R16_UINT; |
| 171 | case GL_UNSIGNED_INT: return DXGI_FORMAT_R32_UINT; |
| 172 | default: UNREACHABLE(); return DXGI_FORMAT_UNKNOWN; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | ID3D11Buffer *IndexBuffer11::getBuffer() const |
| 177 | { |
| 178 | return mBuffer; |
| 179 | } |
| 180 | |
| 181 | } |