1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/angle/src/libGLESv2/renderer/IndexBuffer9.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,207 @@ 1.4 +#include "precompiled.h" 1.5 +// 1.6 +// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. 1.7 +// Use of this source code is governed by a BSD-style license that can be 1.8 +// found in the LICENSE file. 1.9 +// 1.10 + 1.11 +// Indexffer9.cpp: Defines the D3D9 IndexBuffer implementation. 1.12 + 1.13 +#include "libGLESv2/renderer/IndexBuffer9.h" 1.14 +#include "libGLESv2/renderer/Renderer9.h" 1.15 + 1.16 +namespace rx 1.17 +{ 1.18 + 1.19 +IndexBuffer9::IndexBuffer9(Renderer9 *const renderer) : mRenderer(renderer) 1.20 +{ 1.21 + mIndexBuffer = NULL; 1.22 + mBufferSize = 0; 1.23 + mIndexType = 0; 1.24 + mDynamic = false; 1.25 +} 1.26 + 1.27 +IndexBuffer9::~IndexBuffer9() 1.28 +{ 1.29 + if (mIndexBuffer) 1.30 + { 1.31 + mIndexBuffer->Release(); 1.32 + mIndexBuffer = NULL; 1.33 + } 1.34 +} 1.35 + 1.36 +bool IndexBuffer9::initialize(unsigned int bufferSize, GLenum indexType, bool dynamic) 1.37 +{ 1.38 + if (mIndexBuffer) 1.39 + { 1.40 + mIndexBuffer->Release(); 1.41 + mIndexBuffer = NULL; 1.42 + } 1.43 + 1.44 + updateSerial(); 1.45 + 1.46 + if (bufferSize > 0) 1.47 + { 1.48 + D3DFORMAT format; 1.49 + if (indexType == GL_UNSIGNED_SHORT || indexType == GL_UNSIGNED_BYTE) 1.50 + { 1.51 + format = D3DFMT_INDEX16; 1.52 + } 1.53 + else if (indexType == GL_UNSIGNED_INT) 1.54 + { 1.55 + if (mRenderer->get32BitIndexSupport()) 1.56 + { 1.57 + format = D3DFMT_INDEX32; 1.58 + } 1.59 + else 1.60 + { 1.61 + ERR("Attempted to create a 32-bit index buffer but renderer does not support 32-bit indices."); 1.62 + return false; 1.63 + } 1.64 + } 1.65 + else 1.66 + { 1.67 + ERR("Invalid index type %u.", indexType); 1.68 + return false; 1.69 + } 1.70 + 1.71 + DWORD usageFlags = D3DUSAGE_WRITEONLY; 1.72 + if (dynamic) 1.73 + { 1.74 + usageFlags |= D3DUSAGE_DYNAMIC; 1.75 + } 1.76 + 1.77 + HRESULT result = mRenderer->createIndexBuffer(bufferSize, usageFlags, format, &mIndexBuffer); 1.78 + if (FAILED(result)) 1.79 + { 1.80 + ERR("Failed to create an index buffer of size %u, result: 0x%08x.", mBufferSize, result); 1.81 + return false; 1.82 + } 1.83 + } 1.84 + 1.85 + mBufferSize = bufferSize; 1.86 + mIndexType = indexType; 1.87 + mDynamic = dynamic; 1.88 + 1.89 + return true; 1.90 +} 1.91 + 1.92 +IndexBuffer9 *IndexBuffer9::makeIndexBuffer9(IndexBuffer *indexBuffer) 1.93 +{ 1.94 + ASSERT(HAS_DYNAMIC_TYPE(IndexBuffer9*, indexBuffer)); 1.95 + return static_cast<IndexBuffer9*>(indexBuffer); 1.96 +} 1.97 + 1.98 +bool IndexBuffer9::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory) 1.99 +{ 1.100 + if (mIndexBuffer) 1.101 + { 1.102 + DWORD lockFlags = mDynamic ? D3DLOCK_NOOVERWRITE : 0; 1.103 + 1.104 + void *mapPtr = NULL; 1.105 + HRESULT result = mIndexBuffer->Lock(offset, size, &mapPtr, lockFlags); 1.106 + if (FAILED(result)) 1.107 + { 1.108 + ERR("Index buffer lock failed with error 0x%08x", result); 1.109 + return false; 1.110 + } 1.111 + 1.112 + *outMappedMemory = mapPtr; 1.113 + return true; 1.114 + } 1.115 + else 1.116 + { 1.117 + ERR("Index buffer not initialized."); 1.118 + return false; 1.119 + } 1.120 +} 1.121 + 1.122 +bool IndexBuffer9::unmapBuffer() 1.123 +{ 1.124 + if (mIndexBuffer) 1.125 + { 1.126 + HRESULT result = mIndexBuffer->Unlock(); 1.127 + if (FAILED(result)) 1.128 + { 1.129 + ERR("Index buffer unlock failed with error 0x%08x", result); 1.130 + return false; 1.131 + } 1.132 + 1.133 + return true; 1.134 + } 1.135 + else 1.136 + { 1.137 + ERR("Index buffer not initialized."); 1.138 + return false; 1.139 + } 1.140 +} 1.141 + 1.142 +GLenum IndexBuffer9::getIndexType() const 1.143 +{ 1.144 + return mIndexType; 1.145 +} 1.146 + 1.147 +unsigned int IndexBuffer9::getBufferSize() const 1.148 +{ 1.149 + return mBufferSize; 1.150 +} 1.151 + 1.152 +bool IndexBuffer9::setSize(unsigned int bufferSize, GLenum indexType) 1.153 +{ 1.154 + if (bufferSize > mBufferSize || indexType != mIndexType) 1.155 + { 1.156 + return initialize(bufferSize, indexType, mDynamic); 1.157 + } 1.158 + else 1.159 + { 1.160 + return true; 1.161 + } 1.162 +} 1.163 + 1.164 +bool IndexBuffer9::discard() 1.165 +{ 1.166 + if (mIndexBuffer) 1.167 + { 1.168 + void *dummy; 1.169 + HRESULT result; 1.170 + 1.171 + result = mIndexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD); 1.172 + if (FAILED(result)) 1.173 + { 1.174 + ERR("Discard lock failed with error 0x%08x", result); 1.175 + return false; 1.176 + } 1.177 + 1.178 + result = mIndexBuffer->Unlock(); 1.179 + if (FAILED(result)) 1.180 + { 1.181 + ERR("Discard unlock failed with error 0x%08x", result); 1.182 + return false; 1.183 + } 1.184 + 1.185 + return true; 1.186 + } 1.187 + else 1.188 + { 1.189 + ERR("Index buffer not initialized."); 1.190 + return false; 1.191 + } 1.192 +} 1.193 + 1.194 +D3DFORMAT IndexBuffer9::getIndexFormat() const 1.195 +{ 1.196 + switch (mIndexType) 1.197 + { 1.198 + case GL_UNSIGNED_BYTE: return D3DFMT_INDEX16; 1.199 + case GL_UNSIGNED_SHORT: return D3DFMT_INDEX16; 1.200 + case GL_UNSIGNED_INT: return D3DFMT_INDEX32; 1.201 + default: UNREACHABLE(); return D3DFMT_UNKNOWN; 1.202 + } 1.203 +} 1.204 + 1.205 +IDirect3DIndexBuffer9 * IndexBuffer9::getBuffer() const 1.206 +{ 1.207 + return mIndexBuffer; 1.208 +} 1.209 + 1.210 +} 1.211 \ No newline at end of file