Home | History | Annotate | Download | only in d3d9
      1 #include "precompiled.h"
      2 //
      3 // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
      4 // Use of this source code is governed by a BSD-style license that can be
      5 // found in the LICENSE file.
      6 //
      7 
      8 // Indexffer9.cpp: Defines the D3D9 IndexBuffer implementation.
      9 
     10 #include "libGLESv2/renderer/d3d9/IndexBuffer9.h"
     11 #include "libGLESv2/renderer/d3d9/Renderer9.h"
     12 
     13 namespace rx
     14 {
     15 
     16 IndexBuffer9::IndexBuffer9(Renderer9 *const renderer) : mRenderer(renderer)
     17 {
     18     mIndexBuffer = NULL;
     19     mBufferSize = 0;
     20     mIndexType = 0;
     21     mDynamic = false;
     22 }
     23 
     24 IndexBuffer9::~IndexBuffer9()
     25 {
     26     SafeRelease(mIndexBuffer);
     27 }
     28 
     29 bool IndexBuffer9::initialize(unsigned int bufferSize, GLenum indexType, bool dynamic)
     30 {
     31     SafeRelease(mIndexBuffer);
     32 
     33     updateSerial();
     34 
     35     if (bufferSize > 0)
     36     {
     37         D3DFORMAT format;
     38         if (indexType == GL_UNSIGNED_SHORT || indexType == GL_UNSIGNED_BYTE)
     39         {
     40             format = D3DFMT_INDEX16;
     41         }
     42         else if (indexType == GL_UNSIGNED_INT)
     43         {
     44             if (mRenderer->get32BitIndexSupport())
     45             {
     46                 format = D3DFMT_INDEX32;
     47             }
     48             else
     49             {
     50                 ERR("Attempted to create a 32-bit index buffer but renderer does not support 32-bit indices.");
     51                 return false;
     52             }
     53         }
     54         else
     55         {
     56             ERR("Invalid index type %u.", indexType);
     57             return false;
     58         }
     59 
     60         DWORD usageFlags = D3DUSAGE_WRITEONLY;
     61         if (dynamic)
     62         {
     63             usageFlags |= D3DUSAGE_DYNAMIC;
     64         }
     65 
     66         HRESULT result = mRenderer->createIndexBuffer(bufferSize, usageFlags, format, &mIndexBuffer);
     67         if (FAILED(result))
     68         {
     69             ERR("Failed to create an index buffer of size %u, result: 0x%08x.", mBufferSize, result);
     70             return false;
     71         }
     72     }
     73 
     74     mBufferSize = bufferSize;
     75     mIndexType = indexType;
     76     mDynamic = dynamic;
     77 
     78     return true;
     79 }
     80 
     81 IndexBuffer9 *IndexBuffer9::makeIndexBuffer9(IndexBuffer *indexBuffer)
     82 {
     83     ASSERT(HAS_DYNAMIC_TYPE(IndexBuffer9*, indexBuffer));
     84     return static_cast<IndexBuffer9*>(indexBuffer);
     85 }
     86 
     87 bool IndexBuffer9::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory)
     88 {
     89     if (mIndexBuffer)
     90     {
     91         DWORD lockFlags = mDynamic ? D3DLOCK_NOOVERWRITE : 0;
     92 
     93         void *mapPtr = NULL;
     94         HRESULT result = mIndexBuffer->Lock(offset, size, &mapPtr, lockFlags);
     95         if (FAILED(result))
     96         {
     97             ERR("Index buffer lock failed with error 0x%08x", result);
     98             return false;
     99         }
    100 
    101         *outMappedMemory = mapPtr;
    102         return true;
    103     }
    104     else
    105     {
    106         ERR("Index buffer not initialized.");
    107         return false;
    108     }
    109 }
    110 
    111 bool IndexBuffer9::unmapBuffer()
    112 {
    113     if (mIndexBuffer)
    114     {
    115         HRESULT result = mIndexBuffer->Unlock();
    116         if (FAILED(result))
    117         {
    118             ERR("Index buffer unlock failed with error 0x%08x", result);
    119             return false;
    120         }
    121 
    122         return true;
    123     }
    124     else
    125     {
    126         ERR("Index buffer not initialized.");
    127         return false;
    128     }
    129 }
    130 
    131 GLenum IndexBuffer9::getIndexType() const
    132 {
    133     return mIndexType;
    134 }
    135 
    136 unsigned int IndexBuffer9::getBufferSize() const
    137 {
    138     return mBufferSize;
    139 }
    140 
    141 bool IndexBuffer9::setSize(unsigned int bufferSize, GLenum indexType)
    142 {
    143     if (bufferSize > mBufferSize || indexType != mIndexType)
    144     {
    145         return initialize(bufferSize, indexType, mDynamic);
    146     }
    147     else
    148     {
    149         return true;
    150     }
    151 }
    152 
    153 bool IndexBuffer9::discard()
    154 {
    155     if (mIndexBuffer)
    156     {
    157         void *dummy;
    158         HRESULT result;
    159 
    160         result = mIndexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD);
    161         if (FAILED(result))
    162         {
    163             ERR("Discard lock failed with error 0x%08x", result);
    164             return false;
    165         }
    166 
    167         result = mIndexBuffer->Unlock();
    168         if (FAILED(result))
    169         {
    170             ERR("Discard unlock failed with error 0x%08x", result);
    171             return false;
    172         }
    173 
    174         return true;
    175     }
    176     else
    177     {
    178         ERR("Index buffer not initialized.");
    179         return false;
    180     }
    181 }
    182 
    183 D3DFORMAT IndexBuffer9::getIndexFormat() const
    184 {
    185     switch (mIndexType)
    186     {
    187       case GL_UNSIGNED_BYTE:    return D3DFMT_INDEX16;
    188       case GL_UNSIGNED_SHORT:   return D3DFMT_INDEX16;
    189       case GL_UNSIGNED_INT:     return D3DFMT_INDEX32;
    190       default: UNREACHABLE();   return D3DFMT_UNKNOWN;
    191     }
    192 }
    193 
    194 IDirect3DIndexBuffer9 * IndexBuffer9::getBuffer() const
    195 {
    196     return mIndexBuffer;
    197 }
    198 
    199 }