2 // Copyright (c) 2012-2014 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.
7 // renderer11_utils.h: Conversion functions and other utility routines
8 // specific to the D3D11 renderer.
10 #ifndef LIBGLESV2_RENDERER_RENDERER11_UTILS_H
11 #define LIBGLESV2_RENDERER_RENDERER11_UTILS_H
13 #include "libGLESv2/angletypes.h"
14 #include "libGLESv2/Caps.h"
20 class FramebufferAttachment;
30 D3D11_BLEND ConvertBlendFunc(GLenum glBlend, bool isAlpha);
31 D3D11_BLEND_OP ConvertBlendOp(GLenum glBlendOp);
32 UINT8 ConvertColorMask(bool maskRed, bool maskGreen, bool maskBlue, bool maskAlpha);
34 D3D11_CULL_MODE ConvertCullMode(bool cullEnabled, GLenum cullMode);
36 D3D11_COMPARISON_FUNC ConvertComparison(GLenum comparison);
37 D3D11_DEPTH_WRITE_MASK ConvertDepthMask(bool depthWriteEnabled);
38 UINT8 ConvertStencilMask(GLuint stencilmask);
39 D3D11_STENCIL_OP ConvertStencilOp(GLenum stencilOp);
41 D3D11_FILTER ConvertFilter(GLenum minFilter, GLenum magFilter, float maxAnisotropy, GLenum comparisonMode);
42 D3D11_TEXTURE_ADDRESS_MODE ConvertTextureWrap(GLenum wrap);
44 D3D11_QUERY ConvertQueryType(GLenum queryType);
51 void GenerateCaps(ID3D11Device *device, gl::Caps *caps, gl::TextureCapsMap *textureCapsMap, gl::Extensions *extensions);
58 void MakeValidSize(bool isImage, DXGI_FORMAT format, GLsizei *requestWidth, GLsizei *requestHeight, int *levelOffset);
60 void GenerateInitialTextureData(GLint internalFormat, GLuint width, GLuint height, GLuint depth,
61 GLuint mipLevels, std::vector<D3D11_SUBRESOURCE_DATA> *outSubresourceData,
62 std::vector< std::vector<BYTE> > *outData);
64 struct PositionTexCoordVertex
69 void SetPositionTexCoordVertex(PositionTexCoordVertex* vertex, float x, float y, float u, float v);
71 struct PositionLayerTexCoord3DVertex
77 void SetPositionLayerTexCoord3DVertex(PositionLayerTexCoord3DVertex* vertex, float x, float y,
78 unsigned int layer, float u, float v, float s);
81 struct PositionDepthColorVertex
88 void SetPositionDepthColorVertex(PositionDepthColorVertex<T>* vertex, float x, float y, float z,
89 const gl::Color<T> &color)
94 vertex->r = color.red;
95 vertex->g = color.green;
96 vertex->b = color.blue;
97 vertex->a = color.alpha;
100 HRESULT SetDebugName(ID3D11DeviceChild *resource, const char *name);
102 template <typename outType>
103 outType* DynamicCastComObject(IUnknown* object)
105 outType *outObject = NULL;
106 HRESULT result = object->QueryInterface(__uuidof(outType), reinterpret_cast<void**>(&outObject));
107 if (SUCCEEDED(result))
113 SafeRelease(outObject);
118 inline bool isDeviceLostError(HRESULT errorCode)
122 case DXGI_ERROR_DEVICE_HUNG:
123 case DXGI_ERROR_DEVICE_REMOVED:
124 case DXGI_ERROR_DEVICE_RESET:
125 case DXGI_ERROR_DRIVER_INTERNAL_ERROR:
126 case DXGI_ERROR_NOT_CURRENTLY_AVAILABLE:
133 template <unsigned int N>
134 inline ID3D11VertexShader *CompileVS(ID3D11Device *device, const BYTE (&byteCode)[N], const char *name)
136 ID3D11VertexShader *vs = NULL;
137 HRESULT result = device->CreateVertexShader(byteCode, N, NULL, &vs);
138 UNUSED_ASSERTION_VARIABLE(result);
139 ASSERT(SUCCEEDED(result));
140 SetDebugName(vs, name);
144 template <unsigned int N>
145 inline ID3D11GeometryShader *CompileGS(ID3D11Device *device, const BYTE (&byteCode)[N], const char *name)
147 ID3D11GeometryShader *gs = NULL;
148 HRESULT result = device->CreateGeometryShader(byteCode, N, NULL, &gs);
149 UNUSED_ASSERTION_VARIABLE(result);
150 ASSERT(SUCCEEDED(result));
151 SetDebugName(gs, name);
155 template <unsigned int N>
156 inline ID3D11PixelShader *CompilePS(ID3D11Device *device, const BYTE (&byteCode)[N], const char *name)
158 ID3D11PixelShader *ps = NULL;
159 HRESULT result = device->CreatePixelShader(byteCode, N, NULL, &ps);
160 UNUSED_ASSERTION_VARIABLE(result);
161 ASSERT(SUCCEEDED(result));
162 SetDebugName(ps, name);
166 // Copy data to small D3D11 buffers, such as for small constant buffers, which use one struct to
167 // represent an entire buffer.
169 inline void SetBufferData(ID3D11DeviceContext *context, ID3D11Buffer *constantBuffer, const T &value)
171 D3D11_MAPPED_SUBRESOURCE mappedResource;
172 context->Map(constantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
174 memcpy(mappedResource.pData, &value, sizeof(T));
176 context->Unmap(constantBuffer, 0);
179 RenderTarget11 *GetAttachmentRenderTarget(gl::FramebufferAttachment *attachment);
185 #endif // LIBGLESV2_RENDERER_RENDERER11_UTILS_H