Home | History | Annotate | Download | only in d3d11
      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 // renderer11_utils.h: Conversion functions and other utility routines
      8 // specific to the D3D11 renderer.
      9 
     10 #ifndef LIBGLESV2_RENDERER_RENDERER11_UTILS_H
     11 #define LIBGLESV2_RENDERER_RENDERER11_UTILS_H
     12 
     13 #include "libGLESv2/angletypes.h"
     14 
     15 namespace rx
     16 {
     17 
     18 namespace gl_d3d11
     19 {
     20 
     21 D3D11_BLEND ConvertBlendFunc(GLenum glBlend, bool isAlpha);
     22 D3D11_BLEND_OP ConvertBlendOp(GLenum glBlendOp);
     23 UINT8 ConvertColorMask(bool maskRed, bool maskGreen, bool maskBlue, bool maskAlpha);
     24 
     25 D3D11_CULL_MODE ConvertCullMode(bool cullEnabled, GLenum cullMode);
     26 
     27 D3D11_COMPARISON_FUNC ConvertComparison(GLenum comparison);
     28 D3D11_DEPTH_WRITE_MASK ConvertDepthMask(bool depthWriteEnabled);
     29 UINT8 ConvertStencilMask(GLuint stencilmask);
     30 D3D11_STENCIL_OP ConvertStencilOp(GLenum stencilOp);
     31 
     32 D3D11_FILTER ConvertFilter(GLenum minFilter, GLenum magFilter, float maxAnisotropy, GLenum comparisonMode);
     33 D3D11_TEXTURE_ADDRESS_MODE ConvertTextureWrap(GLenum wrap);
     34 
     35 D3D11_QUERY ConvertQueryType(GLenum queryType);
     36 
     37 }
     38 
     39 namespace d3d11
     40 {
     41 
     42 void GenerateInitialTextureData(GLint internalFormat, GLuint clientVersion, GLuint width, GLuint height, GLuint depth,
     43                                 GLuint mipLevels, std::vector<D3D11_SUBRESOURCE_DATA> *outSubresourceData,
     44                                 std::vector< std::vector<BYTE> > *outData);
     45 
     46 struct PositionTexCoordVertex
     47 {
     48     float x, y;
     49     float u, v;
     50 };
     51 void SetPositionTexCoordVertex(PositionTexCoordVertex* vertex, float x, float y, float u, float v);
     52 
     53 struct PositionLayerTexCoord3DVertex
     54 {
     55     float x, y;
     56     unsigned int l;
     57     float u, v, s;
     58 };
     59 void SetPositionLayerTexCoord3DVertex(PositionLayerTexCoord3DVertex* vertex, float x, float y,
     60                                       unsigned int layer, float u, float v, float s);
     61 
     62 template <typename T>
     63 struct PositionDepthColorVertex
     64 {
     65     float x, y, z;
     66     T r, g, b, a;
     67 };
     68 
     69 template <typename T>
     70 void SetPositionDepthColorVertex(PositionDepthColorVertex<T>* vertex, float x, float y, float z,
     71                                  const gl::Color<T> &color)
     72 {
     73     vertex->x = x;
     74     vertex->y = y;
     75     vertex->z = z;
     76     vertex->r = color.red;
     77     vertex->g = color.green;
     78     vertex->b = color.blue;
     79     vertex->a = color.alpha;
     80 }
     81 
     82 HRESULT SetDebugName(ID3D11DeviceChild *resource, const char *name);
     83 
     84 template <typename outType>
     85 outType* DynamicCastComObject(IUnknown* object)
     86 {
     87     outType *outObject = NULL;
     88     HRESULT result = object->QueryInterface(__uuidof(outType), reinterpret_cast<void**>(&outObject));
     89     if (SUCCEEDED(result))
     90     {
     91         return outObject;
     92     }
     93     else
     94     {
     95         SafeRelease(outObject);
     96         return NULL;
     97     }
     98 }
     99 
    100 inline bool isDeviceLostError(HRESULT errorCode)
    101 {
    102     switch (errorCode)
    103     {
    104       case DXGI_ERROR_DEVICE_HUNG:
    105       case DXGI_ERROR_DEVICE_REMOVED:
    106       case DXGI_ERROR_DEVICE_RESET:
    107       case DXGI_ERROR_DRIVER_INTERNAL_ERROR:
    108       case DXGI_ERROR_NOT_CURRENTLY_AVAILABLE:
    109         return true;
    110       default:
    111         return false;
    112     }
    113 }
    114 
    115 template <unsigned int N>
    116 inline ID3D11VertexShader *CompileVS(ID3D11Device *device, const BYTE (&byteCode)[N], const char *name)
    117 {
    118     ID3D11VertexShader *vs = NULL;
    119     HRESULT result = device->CreateVertexShader(byteCode, N, NULL, &vs);
    120     UNUSED_ASSERTION_VARIABLE(result);
    121     ASSERT(SUCCEEDED(result));
    122     SetDebugName(vs, name);
    123     return vs;
    124 }
    125 
    126 template <unsigned int N>
    127 inline ID3D11GeometryShader *CompileGS(ID3D11Device *device, const BYTE (&byteCode)[N], const char *name)
    128 {
    129     ID3D11GeometryShader *gs = NULL;
    130     HRESULT result = device->CreateGeometryShader(byteCode, N, NULL, &gs);
    131     UNUSED_ASSERTION_VARIABLE(result);
    132     ASSERT(SUCCEEDED(result));
    133     SetDebugName(gs, name);
    134     return gs;
    135 }
    136 
    137 template <unsigned int N>
    138 inline ID3D11PixelShader *CompilePS(ID3D11Device *device, const BYTE (&byteCode)[N], const char *name)
    139 {
    140     ID3D11PixelShader *ps = NULL;
    141     HRESULT result = device->CreatePixelShader(byteCode, N, NULL, &ps);
    142     UNUSED_ASSERTION_VARIABLE(result);
    143     ASSERT(SUCCEEDED(result));
    144     SetDebugName(ps, name);
    145     return ps;
    146 }
    147 
    148 // Copy data to small D3D11 buffers, such as for small constant buffers, which use one struct to
    149 // represent an entire buffer.
    150 template <class T>
    151 inline void SetBufferData(ID3D11DeviceContext *context, ID3D11Buffer *constantBuffer, const T &value)
    152 {
    153     D3D11_MAPPED_SUBRESOURCE mappedResource;
    154     context->Map(constantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
    155 
    156     memcpy(mappedResource.pData, &value, sizeof(T));
    157 
    158     context->Unmap(constantBuffer, 0);
    159 }
    160 
    161 }
    162 
    163 }
    164 
    165 #endif // LIBGLESV2_RENDERER_RENDERER11_UTILS_H
    166