code
stringlengths
1
2.06M
language
stringclasses
1 value
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // ShaderCache: Defines rx::ShaderCache, a cache of Direct3D shader objects // keyed by their byte code. #ifndef LIBGLESV2_RENDERER_SHADER_CACHE_H_ #define LIBGLESV2_RENDERER_SHADER_CACHE_H_ #include "common/debug.h" namespace rx { template <typename ShaderObject> class ShaderCache { public: ShaderCache() : mDevice(NULL) { } ~ShaderCache() { // Call clear while the device is still valid. ASSERT(mMap.empty()); } void initialize(IDirect3DDevice9* device) { mDevice = device; } ShaderObject *create(const DWORD *function, size_t length) { std::string key(reinterpret_cast<const char*>(function), length); typename Map::iterator it = mMap.find(key); if (it != mMap.end()) { it->second->AddRef(); return it->second; } ShaderObject *shader; HRESULT result = createShader(function, &shader); if (FAILED(result)) { return NULL; } // Random eviction policy. if (mMap.size() >= kMaxMapSize) { mMap.begin()->second->Release(); mMap.erase(mMap.begin()); } shader->AddRef(); mMap[key] = shader; return shader; } void clear() { for (typename Map::iterator it = mMap.begin(); it != mMap.end(); ++it) { it->second->Release(); } mMap.clear(); } private: DISALLOW_COPY_AND_ASSIGN(ShaderCache); const static size_t kMaxMapSize = 100; HRESULT createShader(const DWORD *function, IDirect3DVertexShader9 **shader) { return mDevice->CreateVertexShader(function, shader); } HRESULT createShader(const DWORD *function, IDirect3DPixelShader9 **shader) { return mDevice->CreatePixelShader(function, shader); } #ifndef HASH_MAP # ifdef _MSC_VER # define HASH_MAP stdext::hash_map # else # define HASH_MAP std::unordered_map # endif #endif typedef HASH_MAP<std::string, ShaderObject*> Map; Map mMap; IDirect3DDevice9 *mDevice; }; typedef ShaderCache<IDirect3DVertexShader9> VertexShaderCache; typedef ShaderCache<IDirect3DPixelShader9> PixelShaderCache; } #endif // LIBGLESV2_RENDERER_SHADER_CACHE_H_
C++
// // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // renderer9_utils.h: Conversion functions and other utility routines // specific to the D3D9 renderer #ifndef LIBGLESV2_RENDERER_RENDERER9_UTILS_H #define LIBGLESV2_RENDERER_RENDERER9_UTILS_H #include "libGLESv2/utilities.h" const D3DFORMAT D3DFMT_INTZ = ((D3DFORMAT)(MAKEFOURCC('I','N','T','Z'))); const D3DFORMAT D3DFMT_NULL = ((D3DFORMAT)(MAKEFOURCC('N','U','L','L'))); namespace gl_d3d9 { D3DCMPFUNC ConvertComparison(GLenum comparison); D3DCOLOR ConvertColor(gl::Color color); D3DBLEND ConvertBlendFunc(GLenum blend); D3DBLENDOP ConvertBlendOp(GLenum blendOp); D3DSTENCILOP ConvertStencilOp(GLenum stencilOp); D3DTEXTUREADDRESS ConvertTextureWrap(GLenum wrap); D3DCULL ConvertCullMode(GLenum cullFace, GLenum frontFace); D3DCUBEMAP_FACES ConvertCubeFace(GLenum cubeFace); DWORD ConvertColorMask(bool red, bool green, bool blue, bool alpha); D3DTEXTUREFILTERTYPE ConvertMagFilter(GLenum magFilter, float maxAnisotropy); void ConvertMinFilter(GLenum minFilter, D3DTEXTUREFILTERTYPE *d3dMinFilter, D3DTEXTUREFILTERTYPE *d3dMipFilter, float maxAnisotropy); D3DFORMAT ConvertRenderbufferFormat(GLenum format); D3DMULTISAMPLE_TYPE GetMultisampleTypeFromSamples(GLsizei samples); } namespace d3d9_gl { GLuint GetAlphaSize(D3DFORMAT colorFormat); GLuint GetStencilSize(D3DFORMAT stencilFormat); GLsizei GetSamplesFromMultisampleType(D3DMULTISAMPLE_TYPE type); bool IsFormatChannelEquivalent(D3DFORMAT d3dformat, GLenum format); GLenum ConvertBackBufferFormat(D3DFORMAT format); GLenum ConvertDepthStencilFormat(D3DFORMAT format); GLenum ConvertRenderTargetFormat(D3DFORMAT format); GLenum GetEquivalentFormat(D3DFORMAT format); } namespace d3d9 { bool IsCompressedFormat(D3DFORMAT format); size_t ComputeRowSize(D3DFORMAT format, unsigned int width); inline bool isDeviceLostError(HRESULT errorCode) { switch (errorCode) { case D3DERR_DRIVERINTERNALERROR: case D3DERR_DEVICELOST: case D3DERR_DEVICEHUNG: case D3DERR_DEVICEREMOVED: return true; default: return false; } } } #endif // LIBGLESV2_RENDERER_RENDERER9_UTILS_H
C++
#include "precompiled.h" // // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // VertexBuffer11.cpp: Defines the D3D11 VertexBuffer implementation. #include "libGLESv2/renderer/VertexBuffer11.h" #include "libGLESv2/renderer/BufferStorage.h" #include "libGLESv2/Buffer.h" #include "libGLESv2/renderer/Renderer11.h" #include "libGLESv2/Context.h" namespace rx { VertexBuffer11::VertexBuffer11(rx::Renderer11 *const renderer) : mRenderer(renderer) { mBuffer = NULL; mBufferSize = 0; mDynamicUsage = false; } VertexBuffer11::~VertexBuffer11() { if (mBuffer) { mBuffer->Release(); mBuffer = NULL; } } bool VertexBuffer11::initialize(unsigned int size, bool dynamicUsage) { if (mBuffer) { mBuffer->Release(); mBuffer = NULL; } updateSerial(); if (size > 0) { ID3D11Device* dxDevice = mRenderer->getDevice(); D3D11_BUFFER_DESC bufferDesc; bufferDesc.ByteWidth = size; bufferDesc.Usage = D3D11_USAGE_DYNAMIC; bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; bufferDesc.MiscFlags = 0; bufferDesc.StructureByteStride = 0; HRESULT result = dxDevice->CreateBuffer(&bufferDesc, NULL, &mBuffer); if (FAILED(result)) { return false; } } mBufferSize = size; mDynamicUsage = dynamicUsage; return true; } VertexBuffer11 *VertexBuffer11::makeVertexBuffer11(VertexBuffer *vetexBuffer) { ASSERT(HAS_DYNAMIC_TYPE(VertexBuffer11*, vetexBuffer)); return static_cast<VertexBuffer11*>(vetexBuffer); } bool VertexBuffer11::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances, unsigned int offset) { if (mBuffer) { gl::Buffer *buffer = attrib.mBoundBuffer.get(); int inputStride = attrib.stride(); const VertexConverter &converter = getVertexConversion(attrib); ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource); if (FAILED(result)) { ERR("Vertex buffer map failed with error 0x%08x", result); return false; } char* output = reinterpret_cast<char*>(mappedResource.pData) + offset; const char *input = NULL; if (buffer) { BufferStorage *storage = buffer->getStorage(); input = static_cast<const char*>(storage->getData()) + static_cast<int>(attrib.mOffset); } else { input = static_cast<const char*>(attrib.mPointer); } if (instances == 0 || attrib.mDivisor == 0) { input += inputStride * start; } converter.conversionFunc(input, inputStride, count, output); dxContext->Unmap(mBuffer, 0); return true; } else { ERR("Vertex buffer not initialized."); return false; } } bool VertexBuffer11::storeRawData(const void* data, unsigned int size, unsigned int offset) { if (mBuffer) { ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource); if (FAILED(result)) { ERR("Vertex buffer map failed with error 0x%08x", result); return false; } char* bufferData = static_cast<char*>(mappedResource.pData); memcpy(bufferData + offset, data, size); dxContext->Unmap(mBuffer, 0); return true; } else { ERR("Vertex buffer not initialized."); return false; } } unsigned int VertexBuffer11::getSpaceRequired(const gl::VertexAttribute &attrib, GLsizei count, GLsizei instances) const { unsigned int elementSize = getVertexConversion(attrib).outputElementSize; if (instances == 0 || attrib.mDivisor == 0) { return elementSize * count; } else { return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor); } } bool VertexBuffer11::requiresConversion(const gl::VertexAttribute &attrib) const { return !getVertexConversion(attrib).identity; } unsigned int VertexBuffer11::getBufferSize() const { return mBufferSize; } bool VertexBuffer11::setBufferSize(unsigned int size) { if (size > mBufferSize) { return initialize(size, mDynamicUsage); } else { return true; } } bool VertexBuffer11::discard() { if (mBuffer) { ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { ERR("Vertex buffer map failed with error 0x%08x", result); return false; } dxContext->Unmap(mBuffer, 0); return true; } else { ERR("Vertex buffer not initialized."); return false; } } unsigned int VertexBuffer11::getVertexSize(const gl::VertexAttribute &attrib) const { return getVertexConversion(attrib).outputElementSize; } DXGI_FORMAT VertexBuffer11::getDXGIFormat(const gl::VertexAttribute &attrib) const { return getVertexConversion(attrib).dxgiFormat; } ID3D11Buffer *VertexBuffer11::getBuffer() const { return mBuffer; } template <typename T, unsigned int componentCount, bool widen, bool normalized> static void copyVertexData(const void *input, unsigned int stride, unsigned int count, void *output) { unsigned int attribSize = sizeof(T) * componentCount; if (attribSize == stride && !widen) { memcpy(output, input, count * attribSize); } else { unsigned int outputStride = widen ? 4 : componentCount; T defaultVal = normalized ? std::numeric_limits<T>::max() : T(1); for (unsigned int i = 0; i < count; i++) { const T *offsetInput = reinterpret_cast<const T*>(reinterpret_cast<const char*>(input) + i * stride); T *offsetOutput = reinterpret_cast<T*>(output) + i * outputStride; for (unsigned int j = 0; j < componentCount; j++) { offsetOutput[j] = offsetInput[j]; } if (widen) { offsetOutput[3] = defaultVal; } } } } template <unsigned int componentCount> static void copyFixedVertexData(const void* input, unsigned int stride, unsigned int count, void* output) { static const float divisor = 1.0f / (1 << 16); for (unsigned int i = 0; i < count; i++) { const GLfixed* offsetInput = reinterpret_cast<const GLfixed*>(reinterpret_cast<const char*>(input) + stride * i); float* offsetOutput = reinterpret_cast<float*>(output) + i * componentCount; for (unsigned int j = 0; j < componentCount; j++) { offsetOutput[j] = static_cast<float>(offsetInput[j]) * divisor; } } } template <typename T, unsigned int componentCount, bool normalized> static void copyToFloatVertexData(const void* input, unsigned int stride, unsigned int count, void* output) { typedef std::numeric_limits<T> NL; for (unsigned int i = 0; i < count; i++) { const T *offsetInput = reinterpret_cast<const T*>(reinterpret_cast<const char*>(input) + stride * i); float *offsetOutput = reinterpret_cast<float*>(output) + i * componentCount; for (unsigned int j = 0; j < componentCount; j++) { if (normalized) { if (NL::is_signed) { const float divisor = 1.0f / (2 * static_cast<float>(NL::max()) + 1); offsetOutput[j] = (2 * static_cast<float>(offsetInput[j]) + 1) * divisor; } else { offsetOutput[j] = static_cast<float>(offsetInput[j]) / NL::max(); } } else { offsetOutput[j] = static_cast<float>(offsetInput[j]); } } } } const VertexBuffer11::VertexConverter VertexBuffer11::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = { { // GL_BYTE { // unnormalized { &copyToFloatVertexData<GLbyte, 1, false>, false, DXGI_FORMAT_R32_FLOAT, 4 }, { &copyToFloatVertexData<GLbyte, 2, false>, false, DXGI_FORMAT_R32G32_FLOAT, 8 }, { &copyToFloatVertexData<GLbyte, 3, false>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 }, { &copyToFloatVertexData<GLbyte, 4, false>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 }, }, { // normalized { &copyVertexData<GLbyte, 1, false, true>, true, DXGI_FORMAT_R8_SNORM, 1 }, { &copyVertexData<GLbyte, 2, false, true>, true, DXGI_FORMAT_R8G8_SNORM, 2 }, { &copyVertexData<GLbyte, 3, true, true>, false, DXGI_FORMAT_R8G8B8A8_SNORM, 4 }, { &copyVertexData<GLbyte, 4, false, true>, true, DXGI_FORMAT_R8G8B8A8_SNORM, 4 }, }, }, { // GL_UNSIGNED_BYTE { // unnormalized { &copyToFloatVertexData<GLubyte, 1, false>, false, DXGI_FORMAT_R32_FLOAT, 4 }, { &copyToFloatVertexData<GLubyte, 2, false>, false, DXGI_FORMAT_R32G32_FLOAT, 8 }, { &copyToFloatVertexData<GLubyte, 3, false>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 }, { &copyToFloatVertexData<GLubyte, 4, false>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 }, }, { // normalized { &copyVertexData<GLubyte, 1, false, true>, true, DXGI_FORMAT_R8_UNORM, 1 }, { &copyVertexData<GLubyte, 2, false, true>, true, DXGI_FORMAT_R8G8_UNORM, 2 }, { &copyVertexData<GLubyte, 3, true, true>, false, DXGI_FORMAT_R8G8B8A8_UNORM, 4 }, { &copyVertexData<GLubyte, 4, false, true>, true, DXGI_FORMAT_R8G8B8A8_UNORM, 4 }, }, }, { // GL_SHORT { // unnormalized { &copyToFloatVertexData<GLshort, 1, false>, false, DXGI_FORMAT_R32_FLOAT, 4 }, { &copyToFloatVertexData<GLshort, 2, false>, false, DXGI_FORMAT_R32G32_FLOAT, 8 }, { &copyToFloatVertexData<GLshort, 3, false>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 }, { &copyToFloatVertexData<GLshort, 4, false>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 }, }, { // normalized { &copyVertexData<GLshort, 1, false, true>, true, DXGI_FORMAT_R16_SNORM, 2 }, { &copyVertexData<GLshort, 2, false, true>, true, DXGI_FORMAT_R16G16_SNORM, 4 }, { &copyVertexData<GLshort, 3, true, true>, false, DXGI_FORMAT_R16G16B16A16_SNORM, 8 }, { &copyVertexData<GLshort, 4, false, true>, true, DXGI_FORMAT_R16G16B16A16_SNORM, 8 }, }, }, { // GL_UNSIGNED_SHORT { // unnormalized { &copyToFloatVertexData<GLushort, 1, false>, false, DXGI_FORMAT_R32_FLOAT, 4 }, { &copyToFloatVertexData<GLushort, 2, false>, false, DXGI_FORMAT_R32G32_FLOAT, 8 }, { &copyToFloatVertexData<GLushort, 3, false>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 }, { &copyToFloatVertexData<GLushort, 4, false>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 }, }, { // normalized { &copyVertexData<GLushort, 1, false, true>, true, DXGI_FORMAT_R16_UNORM, 2 }, { &copyVertexData<GLushort, 2, false, true>, true, DXGI_FORMAT_R16G16_UNORM, 4 }, { &copyVertexData<GLushort, 3, true, true>, false, DXGI_FORMAT_R16G16B16A16_UNORM, 8 }, { &copyVertexData<GLushort, 4, false, true>, true, DXGI_FORMAT_R16G16B16A16_UNORM, 8 }, }, }, { // GL_FIXED { // unnormalized { &copyFixedVertexData<1>, false, DXGI_FORMAT_R32_FLOAT, 4 }, { &copyFixedVertexData<2>, false, DXGI_FORMAT_R32G32_FLOAT, 8 }, { &copyFixedVertexData<3>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 }, { &copyFixedVertexData<4>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 }, }, { // normalized { &copyFixedVertexData<1>, false, DXGI_FORMAT_R32_FLOAT, 4 }, { &copyFixedVertexData<2>, false, DXGI_FORMAT_R32G32_FLOAT, 8 }, { &copyFixedVertexData<3>, false, DXGI_FORMAT_R32G32B32_FLOAT, 12 }, { &copyFixedVertexData<4>, false, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 }, }, }, { // GL_FLOAT { // unnormalized { &copyVertexData<GLfloat, 1, false, false>, true, DXGI_FORMAT_R32_FLOAT, 4 }, { &copyVertexData<GLfloat, 2, false, false>, true, DXGI_FORMAT_R32G32_FLOAT, 8 }, { &copyVertexData<GLfloat, 3, false, false>, true, DXGI_FORMAT_R32G32B32_FLOAT, 12 }, { &copyVertexData<GLfloat, 4, false, false>, true, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 }, }, { // normalized { &copyVertexData<GLfloat, 1, false, false>, true, DXGI_FORMAT_R32_FLOAT, 4 }, { &copyVertexData<GLfloat, 2, false, false>, true, DXGI_FORMAT_R32G32_FLOAT, 8 }, { &copyVertexData<GLfloat, 3, false, false>, true, DXGI_FORMAT_R32G32B32_FLOAT, 12 }, { &copyVertexData<GLfloat, 4, false, false>, true, DXGI_FORMAT_R32G32B32A32_FLOAT, 16 }, }, }, }; const VertexBuffer11::VertexConverter &VertexBuffer11::getVertexConversion(const gl::VertexAttribute &attribute) { unsigned int typeIndex = 0; switch (attribute.mType) { case GL_BYTE: typeIndex = 0; break; case GL_UNSIGNED_BYTE: typeIndex = 1; break; case GL_SHORT: typeIndex = 2; break; case GL_UNSIGNED_SHORT: typeIndex = 3; break; case GL_FIXED: typeIndex = 4; break; case GL_FLOAT: typeIndex = 5; break; default: UNREACHABLE(); break; } return mPossibleTranslations[typeIndex][attribute.mNormalized ? 1 : 0][attribute.mSize - 1]; } }
C++
#include "precompiled.h" // // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Renderer11.cpp: Implements a back-end specific class for the D3D11 renderer. #include "libGLESv2/main.h" #include "libGLESv2/utilities.h" #include "libGLESv2/Buffer.h" #include "libGLESv2/ProgramBinary.h" #include "libGLESv2/Framebuffer.h" #include "libGLESv2/RenderBuffer.h" #include "libGLESv2/renderer/Renderer11.h" #include "libGLESv2/renderer/RenderTarget11.h" #include "libGLESv2/renderer/renderer11_utils.h" #include "libGLESv2/renderer/ShaderExecutable11.h" #include "libGLESv2/renderer/SwapChain11.h" #include "libGLESv2/renderer/Image11.h" #include "libGLESv2/renderer/VertexBuffer11.h" #include "libGLESv2/renderer/IndexBuffer11.h" #include "libGLESv2/renderer/BufferStorage11.h" #include "libGLESv2/renderer/VertexDataManager.h" #include "libGLESv2/renderer/IndexDataManager.h" #include "libGLESv2/renderer/TextureStorage11.h" #include "libGLESv2/renderer/Query11.h" #include "libGLESv2/renderer/Fence11.h" #include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h" #include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h" #include "libGLESv2/renderer/shaders/compiled/passthroughrgb11ps.h" #include "libGLESv2/renderer/shaders/compiled/passthroughlum11ps.h" #include "libGLESv2/renderer/shaders/compiled/passthroughlumalpha11ps.h" #include "libGLESv2/renderer/shaders/compiled/clear11vs.h" #include "libGLESv2/renderer/shaders/compiled/clearsingle11ps.h" #include "libGLESv2/renderer/shaders/compiled/clearmultiple11ps.h" #include "libEGL/Display.h" #ifdef _DEBUG // this flag enables suppressing some spurious warnings that pop up in certain WebGL samples // and conformance tests. to enable all warnings, remove this define. #define ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS 1 #endif namespace rx { static const DXGI_FORMAT RenderTargetFormats[] = { DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_R8G8B8A8_UNORM }; static const DXGI_FORMAT DepthStencilFormats[] = { DXGI_FORMAT_UNKNOWN, DXGI_FORMAT_D24_UNORM_S8_UINT, DXGI_FORMAT_D16_UNORM }; enum { MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 = 16 }; Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(hDc) { mVertexDataManager = NULL; mIndexDataManager = NULL; mLineLoopIB = NULL; mTriangleFanIB = NULL; mCopyResourcesInitialized = false; mCopyVB = NULL; mCopySampler = NULL; mCopyIL = NULL; mCopyVS = NULL; mCopyRGBAPS = NULL; mCopyRGBPS = NULL; mCopyLumPS = NULL; mCopyLumAlphaPS = NULL; mClearResourcesInitialized = false; mClearVB = NULL; mClearIL = NULL; mClearVS = NULL; mClearSinglePS = NULL; mClearMultiplePS = NULL; mClearScissorRS = NULL; mClearNoScissorRS = NULL; mSyncQuery = NULL; mD3d11Module = NULL; mDxgiModule = NULL; mDeviceLost = false; mMaxSupportedSamples = 0; mDevice = NULL; mDeviceContext = NULL; mDxgiAdapter = NULL; mDxgiFactory = NULL; mDriverConstantBufferVS = NULL; mDriverConstantBufferPS = NULL; mBGRATextureSupport = false; mIsGeometryShaderActive = false; } Renderer11::~Renderer11() { release(); } Renderer11 *Renderer11::makeRenderer11(Renderer *renderer) { ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer11*, renderer)); return static_cast<rx::Renderer11*>(renderer); } #ifndef __d3d11_1_h__ #define D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET ((D3D11_MESSAGE_ID)3146081) #endif EGLint Renderer11::initialize() { if (!initializeCompiler()) { return EGL_NOT_INITIALIZED; } mDxgiModule = LoadLibrary(TEXT("dxgi.dll")); mD3d11Module = LoadLibrary(TEXT("d3d11.dll")); if (mD3d11Module == NULL || mDxgiModule == NULL) { ERR("Could not load D3D11 or DXGI library - aborting!\n"); return EGL_NOT_INITIALIZED; } // create the D3D11 device ASSERT(mDevice == NULL); PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice"); if (D3D11CreateDevice == NULL) { ERR("Could not retrieve D3D11CreateDevice address - aborting!\n"); return EGL_NOT_INITIALIZED; } D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0, }; HRESULT result = S_OK; #ifdef _DEBUG result = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, featureLevels, ArraySize(featureLevels), D3D11_SDK_VERSION, &mDevice, &mFeatureLevel, &mDeviceContext); if (!mDevice || FAILED(result)) { ERR("Failed creating Debug D3D11 device - falling back to release runtime.\n"); } if (!mDevice || FAILED(result)) #endif { result = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, featureLevels, ArraySize(featureLevels), D3D11_SDK_VERSION, &mDevice, &mFeatureLevel, &mDeviceContext); if (!mDevice || FAILED(result)) { ERR("Could not create D3D11 device - aborting!\n"); return EGL_NOT_INITIALIZED; // Cleanup done by destructor through glDestroyRenderer } } IDXGIDevice *dxgiDevice = NULL; result = mDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&dxgiDevice); if (FAILED(result)) { ERR("Could not query DXGI device - aborting!\n"); return EGL_NOT_INITIALIZED; } result = dxgiDevice->GetParent(__uuidof(IDXGIAdapter), (void**)&mDxgiAdapter); if (FAILED(result)) { ERR("Could not retrieve DXGI adapter - aborting!\n"); return EGL_NOT_INITIALIZED; } dxgiDevice->Release(); mDxgiAdapter->GetDesc(&mAdapterDescription); memset(mDescription, 0, sizeof(mDescription)); wcstombs(mDescription, mAdapterDescription.Description, sizeof(mDescription) - 1); result = mDxgiAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&mDxgiFactory); if (!mDxgiFactory || FAILED(result)) { ERR("Could not create DXGI factory - aborting!\n"); return EGL_NOT_INITIALIZED; } // Disable some spurious D3D11 debug warnings to prevent them from flooding the output log #if defined(ANGLE_SUPPRESS_D3D11_HAZARD_WARNINGS) && defined(_DEBUG) ID3D11InfoQueue *infoQueue; result = mDevice->QueryInterface(__uuidof(ID3D11InfoQueue), (void **)&infoQueue); if (SUCCEEDED(result)) { D3D11_MESSAGE_ID hideMessages[] = { D3D11_MESSAGE_ID_DEVICE_OMSETRENDERTARGETS_HAZARD, D3D11_MESSAGE_ID_DEVICE_PSSETSHADERRESOURCES_HAZARD, D3D11_MESSAGE_ID_DEVICE_DRAW_RENDERTARGETVIEW_NOT_SET }; D3D11_INFO_QUEUE_FILTER filter = {0}; filter.DenyList.NumIDs = ArraySize(hideMessages); filter.DenyList.pIDList = hideMessages; infoQueue->AddStorageFilterEntries(&filter); infoQueue->Release(); } #endif unsigned int maxSupportedSamples = 0; unsigned int rtFormatCount = ArraySize(RenderTargetFormats); unsigned int dsFormatCount = ArraySize(DepthStencilFormats); for (unsigned int i = 0; i < rtFormatCount + dsFormatCount; ++i) { DXGI_FORMAT format = (i < rtFormatCount) ? RenderTargetFormats[i] : DepthStencilFormats[i - rtFormatCount]; if (format != DXGI_FORMAT_UNKNOWN) { UINT formatSupport; result = mDevice->CheckFormatSupport(format, &formatSupport); if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_MULTISAMPLE_RENDERTARGET)) { MultisampleSupportInfo supportInfo; for (unsigned int j = 1; j <= D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; j++) { result = mDevice->CheckMultisampleQualityLevels(format, j, &supportInfo.qualityLevels[j - 1]); if (SUCCEEDED(result) && supportInfo.qualityLevels[j - 1] > 0) { maxSupportedSamples = std::max(j, maxSupportedSamples); } else { supportInfo.qualityLevels[j - 1] = 0; } } mMultisampleSupportMap.insert(std::make_pair(format, supportInfo)); } } } mMaxSupportedSamples = maxSupportedSamples; initializeDevice(); // BGRA texture support is optional in feature levels 10 and 10_1 UINT formatSupport; result = mDevice->CheckFormatSupport(DXGI_FORMAT_B8G8R8A8_UNORM, &formatSupport); if (FAILED(result)) { ERR("Error checking BGRA format support: 0x%08X", result); } else { const int flags = (D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_RENDER_TARGET); mBGRATextureSupport = (formatSupport & flags) == flags; } // Check floating point texture support static const unsigned int requiredTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D | D3D11_FORMAT_SUPPORT_TEXTURECUBE; static const unsigned int requiredRenderableFlags = D3D11_FORMAT_SUPPORT_RENDER_TARGET; static const unsigned int requiredFilterFlags = D3D11_FORMAT_SUPPORT_SHADER_SAMPLE; DXGI_FORMAT float16Formats[] = { DXGI_FORMAT_R16_FLOAT, DXGI_FORMAT_R16G16_FLOAT, DXGI_FORMAT_R16G16B16A16_FLOAT, }; DXGI_FORMAT float32Formats[] = { DXGI_FORMAT_R32_FLOAT, DXGI_FORMAT_R32G32_FLOAT, DXGI_FORMAT_R32G32B32_FLOAT, DXGI_FORMAT_R32G32B32A32_FLOAT, }; mFloat16TextureSupport = true; mFloat16FilterSupport = true; mFloat16RenderSupport = true; for (unsigned int i = 0; i < ArraySize(float16Formats); i++) { if (SUCCEEDED(mDevice->CheckFormatSupport(float16Formats[i], &formatSupport))) { mFloat16TextureSupport = mFloat16TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags; mFloat16FilterSupport = mFloat16FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags; mFloat16RenderSupport = mFloat16RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags; } else { mFloat16TextureSupport = false; mFloat16RenderSupport = false; mFloat16FilterSupport = false; } } mFloat32TextureSupport = true; mFloat32FilterSupport = true; mFloat32RenderSupport = true; for (unsigned int i = 0; i < ArraySize(float32Formats); i++) { if (SUCCEEDED(mDevice->CheckFormatSupport(float32Formats[i], &formatSupport))) { mFloat32TextureSupport = mFloat32TextureSupport && (formatSupport & requiredTextureFlags) == requiredTextureFlags; mFloat32FilterSupport = mFloat32FilterSupport && (formatSupport & requiredFilterFlags) == requiredFilterFlags; mFloat32RenderSupport = mFloat32RenderSupport && (formatSupport & requiredRenderableFlags) == requiredRenderableFlags; } else { mFloat32TextureSupport = false; mFloat32FilterSupport = false; mFloat32RenderSupport = false; } } // Check compressed texture support const unsigned int requiredCompressedTextureFlags = D3D11_FORMAT_SUPPORT_TEXTURE2D; if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC1_UNORM, &formatSupport))) { mDXT1TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags; } else { mDXT1TextureSupport = false; } if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC3_UNORM, &formatSupport))) { mDXT3TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags; } else { mDXT3TextureSupport = false; } if (SUCCEEDED(mDevice->CheckFormatSupport(DXGI_FORMAT_BC5_UNORM, &formatSupport))) { mDXT5TextureSupport = (formatSupport & requiredCompressedTextureFlags) == requiredCompressedTextureFlags; } else { mDXT5TextureSupport = false; } // Check depth texture support DXGI_FORMAT depthTextureFormats[] = { DXGI_FORMAT_D16_UNORM, DXGI_FORMAT_D24_UNORM_S8_UINT, }; static const unsigned int requiredDepthTextureFlags = D3D11_FORMAT_SUPPORT_DEPTH_STENCIL | D3D11_FORMAT_SUPPORT_TEXTURE2D; mDepthTextureSupport = true; for (unsigned int i = 0; i < ArraySize(depthTextureFormats); i++) { if (SUCCEEDED(mDevice->CheckFormatSupport(depthTextureFormats[i], &formatSupport))) { mDepthTextureSupport = mDepthTextureSupport && ((formatSupport & requiredDepthTextureFlags) == requiredDepthTextureFlags); } else { mDepthTextureSupport = false; } } return EGL_SUCCESS; } // do any one-time device initialization // NOTE: this is also needed after a device lost/reset // to reset the scene status and ensure the default states are reset. void Renderer11::initializeDevice() { mStateCache.initialize(mDevice); mInputLayoutCache.initialize(mDevice, mDeviceContext); ASSERT(!mVertexDataManager && !mIndexDataManager); mVertexDataManager = new VertexDataManager(this); mIndexDataManager = new IndexDataManager(this); markAllStateDirty(); } int Renderer11::generateConfigs(ConfigDesc **configDescList) { unsigned int numRenderFormats = ArraySize(RenderTargetFormats); unsigned int numDepthFormats = ArraySize(DepthStencilFormats); (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats]; int numConfigs = 0; for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++) { for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++) { DXGI_FORMAT renderTargetFormat = RenderTargetFormats[formatIndex]; UINT formatSupport = 0; HRESULT result = mDevice->CheckFormatSupport(renderTargetFormat, &formatSupport); if (SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_RENDER_TARGET)) { DXGI_FORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex]; bool depthStencilFormatOK = true; if (depthStencilFormat != DXGI_FORMAT_UNKNOWN) { UINT formatSupport = 0; result = mDevice->CheckFormatSupport(depthStencilFormat, &formatSupport); depthStencilFormatOK = SUCCEEDED(result) && (formatSupport & D3D11_FORMAT_SUPPORT_DEPTH_STENCIL); } if (depthStencilFormatOK) { ConfigDesc newConfig; newConfig.renderTargetFormat = d3d11_gl::ConvertBackBufferFormat(renderTargetFormat); newConfig.depthStencilFormat = d3d11_gl::ConvertDepthStencilFormat(depthStencilFormat); newConfig.multiSample = 0; // FIXME: enumerate multi-sampling newConfig.fastConfig = true; // Assume all DX11 format conversions to be fast (*configDescList)[numConfigs++] = newConfig; } } } } return numConfigs; } void Renderer11::deleteConfigs(ConfigDesc *configDescList) { delete [] (configDescList); } void Renderer11::sync(bool block) { if (block) { HRESULT result; if (!mSyncQuery) { D3D11_QUERY_DESC queryDesc; queryDesc.Query = D3D11_QUERY_EVENT; queryDesc.MiscFlags = 0; result = mDevice->CreateQuery(&queryDesc, &mSyncQuery); ASSERT(SUCCEEDED(result)); } mDeviceContext->End(mSyncQuery); mDeviceContext->Flush(); do { result = mDeviceContext->GetData(mSyncQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH); // Keep polling, but allow other threads to do something useful first Sleep(0); if (testDeviceLost(true)) { return; } } while (result == S_FALSE); } else { mDeviceContext->Flush(); } } SwapChain *Renderer11::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat) { return new rx::SwapChain11(this, window, shareHandle, backBufferFormat, depthBufferFormat); } void Renderer11::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState) { if (type == gl::SAMPLER_PIXEL) { if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS) { ERR("Pixel shader sampler index %i is not valid.", index); return; } if (mForceSetPixelSamplerStates[index] || memcmp(&samplerState, &mCurPixelSamplerStates[index], sizeof(gl::SamplerState)) != 0) { ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState); if (!dxSamplerState) { ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default" "sampler state for pixel shaders at slot %i.", index); } mDeviceContext->PSSetSamplers(index, 1, &dxSamplerState); mCurPixelSamplerStates[index] = samplerState; } mForceSetPixelSamplerStates[index] = false; } else if (type == gl::SAMPLER_VERTEX) { if (index < 0 || index >= (int)getMaxVertexTextureImageUnits()) { ERR("Vertex shader sampler index %i is not valid.", index); return; } if (mForceSetVertexSamplerStates[index] || memcmp(&samplerState, &mCurVertexSamplerStates[index], sizeof(gl::SamplerState)) != 0) { ID3D11SamplerState *dxSamplerState = mStateCache.getSamplerState(samplerState); if (!dxSamplerState) { ERR("NULL sampler state returned by RenderStateCache::getSamplerState, setting the default" "sampler state for vertex shaders at slot %i.", index); } mDeviceContext->VSSetSamplers(index, 1, &dxSamplerState); mCurVertexSamplerStates[index] = samplerState; } mForceSetVertexSamplerStates[index] = false; } else UNREACHABLE(); } void Renderer11::setTexture(gl::SamplerType type, int index, gl::Texture *texture) { ID3D11ShaderResourceView *textureSRV = NULL; unsigned int serial = 0; bool forceSetTexture = false; if (texture) { TextureStorageInterface *texStorage = texture->getNativeTexture(); if (texStorage) { TextureStorage11 *storage11 = TextureStorage11::makeTextureStorage11(texStorage->getStorageInstance()); textureSRV = storage11->getSRV(); } // If we get NULL back from getSRV here, something went wrong in the texture class and we're unexpectedly // missing the shader resource view ASSERT(textureSRV != NULL); serial = texture->getTextureSerial(); forceSetTexture = texture->hasDirtyImages(); } if (type == gl::SAMPLER_PIXEL) { if (index < 0 || index >= gl::MAX_TEXTURE_IMAGE_UNITS) { ERR("Pixel shader sampler index %i is not valid.", index); return; } if (forceSetTexture || mCurPixelTextureSerials[index] != serial) { mDeviceContext->PSSetShaderResources(index, 1, &textureSRV); } mCurPixelTextureSerials[index] = serial; } else if (type == gl::SAMPLER_VERTEX) { if (index < 0 || index >= (int)getMaxVertexTextureImageUnits()) { ERR("Vertex shader sampler index %i is not valid.", index); return; } if (forceSetTexture || mCurVertexTextureSerials[index] != serial) { mDeviceContext->VSSetShaderResources(index, 1, &textureSRV); } mCurVertexTextureSerials[index] = serial; } else UNREACHABLE(); } void Renderer11::setRasterizerState(const gl::RasterizerState &rasterState) { if (mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0) { ID3D11RasterizerState *dxRasterState = mStateCache.getRasterizerState(rasterState, mScissorEnabled, mCurDepthSize); if (!dxRasterState) { ERR("NULL rasterizer state returned by RenderStateCache::getRasterizerState, setting the default" "rasterizer state."); } mDeviceContext->RSSetState(dxRasterState); mCurRasterState = rasterState; } mForceSetRasterState = false; } void Renderer11::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask) { if (mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0 || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0 || sampleMask != mCurSampleMask) { ID3D11BlendState *dxBlendState = mStateCache.getBlendState(blendState); if (!dxBlendState) { ERR("NULL blend state returned by RenderStateCache::getBlendState, setting the default " "blend state."); } float blendColors[4] = {0.0f}; if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) { blendColors[0] = blendColor.red; blendColors[1] = blendColor.green; blendColors[2] = blendColor.blue; blendColors[3] = blendColor.alpha; } else { blendColors[0] = blendColor.alpha; blendColors[1] = blendColor.alpha; blendColors[2] = blendColor.alpha; blendColors[3] = blendColor.alpha; } mDeviceContext->OMSetBlendState(dxBlendState, blendColors, sampleMask); mCurBlendState = blendState; mCurBlendColor = blendColor; mCurSampleMask = sampleMask; } mForceSetBlendState = false; } void Renderer11::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, int stencilBackRef, bool frontFaceCCW) { if (mForceSetDepthStencilState || memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0 || stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef) { if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask || stencilRef != stencilBackRef || depthStencilState.stencilMask != depthStencilState.stencilBackMask) { ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are " "invalid under WebGL."); return gl::error(GL_INVALID_OPERATION); } ID3D11DepthStencilState *dxDepthStencilState = mStateCache.getDepthStencilState(depthStencilState); if (!dxDepthStencilState) { ERR("NULL depth stencil state returned by RenderStateCache::getDepthStencilState, " "setting the default depth stencil state."); } mDeviceContext->OMSetDepthStencilState(dxDepthStencilState, static_cast<UINT>(stencilRef)); mCurDepthStencilState = depthStencilState; mCurStencilRef = stencilRef; mCurStencilBackRef = stencilBackRef; } mForceSetDepthStencilState = false; } void Renderer11::setScissorRectangle(const gl::Rectangle &scissor, bool enabled) { if (mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 || enabled != mScissorEnabled) { if (enabled) { D3D11_RECT rect; rect.left = std::max(0, scissor.x); rect.top = std::max(0, scissor.y); rect.right = scissor.x + std::max(0, scissor.width); rect.bottom = scissor.y + std::max(0, scissor.height); mDeviceContext->RSSetScissorRects(1, &rect); } if (enabled != mScissorEnabled) { mForceSetRasterState = true; } mCurScissor = scissor; mScissorEnabled = enabled; } mForceSetScissor = false; } bool Renderer11::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace, bool ignoreViewport) { gl::Rectangle actualViewport = viewport; float actualZNear = gl::clamp01(zNear); float actualZFar = gl::clamp01(zFar); if (ignoreViewport) { actualViewport.x = 0; actualViewport.y = 0; actualViewport.width = mRenderTargetDesc.width; actualViewport.height = mRenderTargetDesc.height; actualZNear = 0.0f; actualZFar = 1.0f; } // Get D3D viewport bounds, which depends on the feature level const Range& viewportBounds = getViewportBounds(); // Clamp width and height first to the gl maximum, then clamp further if we extend past the D3D maximum bounds D3D11_VIEWPORT dxViewport; dxViewport.TopLeftX = gl::clamp(actualViewport.x, viewportBounds.start, viewportBounds.end); dxViewport.TopLeftY = gl::clamp(actualViewport.y, viewportBounds.start, viewportBounds.end); dxViewport.Width = gl::clamp(actualViewport.width, 0, getMaxViewportDimension()); dxViewport.Height = gl::clamp(actualViewport.height, 0, getMaxViewportDimension()); dxViewport.Width = std::min((int)dxViewport.Width, viewportBounds.end - static_cast<int>(dxViewport.TopLeftX)); dxViewport.Height = std::min((int)dxViewport.Height, viewportBounds.end - static_cast<int>(dxViewport.TopLeftY)); dxViewport.MinDepth = actualZNear; dxViewport.MaxDepth = actualZFar; if (dxViewport.Width <= 0 || dxViewport.Height <= 0) { return false; // Nothing to render } bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 || actualZNear != mCurNear || actualZFar != mCurFar; if (viewportChanged) { mDeviceContext->RSSetViewports(1, &dxViewport); mCurViewport = actualViewport; mCurNear = actualZNear; mCurFar = actualZFar; mPixelConstants.viewCoords[0] = actualViewport.width * 0.5f; mPixelConstants.viewCoords[1] = actualViewport.height * 0.5f; mPixelConstants.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f); mPixelConstants.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f); mPixelConstants.depthFront[0] = (actualZFar - actualZNear) * 0.5f; mPixelConstants.depthFront[1] = (actualZNear + actualZFar) * 0.5f; mVertexConstants.depthRange[0] = actualZNear; mVertexConstants.depthRange[1] = actualZFar; mVertexConstants.depthRange[2] = actualZFar - actualZNear; mPixelConstants.depthRange[0] = actualZNear; mPixelConstants.depthRange[1] = actualZFar; mPixelConstants.depthRange[2] = actualZFar - actualZNear; } mForceSetViewport = false; return true; } bool Renderer11::applyPrimitiveType(GLenum mode, GLsizei count) { D3D11_PRIMITIVE_TOPOLOGY primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_UNDEFINED; switch (mode) { case GL_POINTS: primitiveTopology = D3D11_PRIMITIVE_TOPOLOGY_POINTLIST; break; case GL_LINES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break; case GL_LINE_LOOP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break; case GL_LINE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break; case GL_TRIANGLES: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break; case GL_TRIANGLE_STRIP: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break; // emulate fans via rewriting index buffer case GL_TRIANGLE_FAN: primitiveTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break; default: return gl::error(GL_INVALID_ENUM, false); } mDeviceContext->IASetPrimitiveTopology(primitiveTopology); return count > 0; } bool Renderer11::applyRenderTarget(gl::Framebuffer *framebuffer) { // Get the color render buffer and serial // Also extract the render target dimensions and view unsigned int renderTargetWidth = 0; unsigned int renderTargetHeight = 0; GLenum renderTargetFormat = 0; unsigned int renderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {0}; ID3D11RenderTargetView* framebufferRTVs[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL}; bool missingColorRenderTarget = true; for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++) { const GLenum drawBufferState = framebuffer->getDrawBufferState(colorAttachment); if (framebuffer->getColorbufferType(colorAttachment) != GL_NONE && drawBufferState != GL_NONE) { // the draw buffer must be either "none", "back" for the default buffer or the same index as this color (in order) ASSERT(drawBufferState == GL_BACK || drawBufferState == (GL_COLOR_ATTACHMENT0_EXT + colorAttachment)); gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(colorAttachment); if (!colorbuffer) { ERR("render target pointer unexpectedly null."); return false; } // check for zero-sized default framebuffer, which is a special case. // in this case we do not wish to modify any state and just silently return false. // this will not report any gl error but will cause the calling method to return. if (colorbuffer->getWidth() == 0 || colorbuffer->getHeight() == 0) { return false; } renderTargetSerials[colorAttachment] = colorbuffer->getSerial(); // Extract the render target dimensions and view RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); if (!renderTarget) { ERR("render target pointer unexpectedly null."); return false; } framebufferRTVs[colorAttachment] = renderTarget->getRenderTargetView(); if (!framebufferRTVs[colorAttachment]) { ERR("render target view pointer unexpectedly null."); return false; } if (missingColorRenderTarget) { renderTargetWidth = colorbuffer->getWidth(); renderTargetHeight = colorbuffer->getHeight(); renderTargetFormat = colorbuffer->getActualFormat(); missingColorRenderTarget = false; } } } // Get the depth stencil render buffer and serials gl::Renderbuffer *depthStencil = NULL; unsigned int depthbufferSerial = 0; unsigned int stencilbufferSerial = 0; if (framebuffer->getDepthbufferType() != GL_NONE) { depthStencil = framebuffer->getDepthbuffer(); if (!depthStencil) { ERR("Depth stencil pointer unexpectedly null."); SafeRelease(framebufferRTVs); return false; } depthbufferSerial = depthStencil->getSerial(); } else if (framebuffer->getStencilbufferType() != GL_NONE) { depthStencil = framebuffer->getStencilbuffer(); if (!depthStencil) { ERR("Depth stencil pointer unexpectedly null."); SafeRelease(framebufferRTVs); return false; } stencilbufferSerial = depthStencil->getSerial(); } // Extract the depth stencil sizes and view unsigned int depthSize = 0; unsigned int stencilSize = 0; ID3D11DepthStencilView* framebufferDSV = NULL; if (depthStencil) { RenderTarget11 *depthStencilRenderTarget = RenderTarget11::makeRenderTarget11(depthStencil->getDepthStencil()); if (!depthStencilRenderTarget) { ERR("render target pointer unexpectedly null."); SafeRelease(framebufferRTVs); return false; } framebufferDSV = depthStencilRenderTarget->getDepthStencilView(); if (!framebufferDSV) { ERR("depth stencil view pointer unexpectedly null."); SafeRelease(framebufferRTVs); return false; } // If there is no render buffer, the width, height and format values come from // the depth stencil if (missingColorRenderTarget) { renderTargetWidth = depthStencil->getWidth(); renderTargetHeight = depthStencil->getHeight(); renderTargetFormat = depthStencil->getActualFormat(); } depthSize = depthStencil->getDepthSize(); stencilSize = depthStencil->getStencilSize(); } // Apply the render target and depth stencil if (!mRenderTargetDescInitialized || !mDepthStencilInitialized || memcmp(renderTargetSerials, mAppliedRenderTargetSerials, sizeof(renderTargetSerials)) != 0 || depthbufferSerial != mAppliedDepthbufferSerial || stencilbufferSerial != mAppliedStencilbufferSerial) { mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), framebufferRTVs, framebufferDSV); mRenderTargetDesc.width = renderTargetWidth; mRenderTargetDesc.height = renderTargetHeight; mRenderTargetDesc.format = renderTargetFormat; mForceSetViewport = true; mForceSetScissor = true; if (!mDepthStencilInitialized || depthSize != mCurDepthSize) { mCurDepthSize = depthSize; mForceSetRasterState = true; } mCurStencilSize = stencilSize; for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++) { mAppliedRenderTargetSerials[rtIndex] = renderTargetSerials[rtIndex]; } mAppliedDepthbufferSerial = depthbufferSerial; mAppliedStencilbufferSerial = stencilbufferSerial; mRenderTargetDescInitialized = true; mDepthStencilInitialized = true; } SafeRelease(framebufferRTVs); SafeRelease(framebufferDSV); return true; } GLenum Renderer11::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances) { TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS]; GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances); if (err != GL_NO_ERROR) { return err; } return mInputLayoutCache.applyVertexBuffers(attributes, programBinary); } GLenum Renderer11::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo) { GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo); if (err == GL_NO_ERROR) { if (indexInfo->storage) { if (indexInfo->serial != mAppliedStorageIBSerial || indexInfo->startOffset != mAppliedIBOffset) { BufferStorage11 *storage = BufferStorage11::makeBufferStorage11(indexInfo->storage); IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer); mDeviceContext->IASetIndexBuffer(storage->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset); mAppliedIBSerial = 0; mAppliedStorageIBSerial = storage->getSerial(); mAppliedIBOffset = indexInfo->startOffset; } } else if (indexInfo->serial != mAppliedIBSerial || indexInfo->startOffset != mAppliedIBOffset) { IndexBuffer11* indexBuffer = IndexBuffer11::makeIndexBuffer11(indexInfo->indexBuffer); mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexInfo->startOffset); mAppliedIBSerial = indexInfo->serial; mAppliedStorageIBSerial = 0; mAppliedIBOffset = indexInfo->startOffset; } } return err; } void Renderer11::drawArrays(GLenum mode, GLsizei count, GLsizei instances) { if (mode == GL_LINE_LOOP) { drawLineLoop(count, GL_NONE, NULL, 0, NULL); } else if (mode == GL_TRIANGLE_FAN) { drawTriangleFan(count, GL_NONE, NULL, 0, NULL, instances); } else if (instances > 0) { mDeviceContext->DrawInstanced(count, instances, 0, 0); } else { mDeviceContext->Draw(count, 0); } } void Renderer11::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances) { if (mode == GL_LINE_LOOP) { drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer); } else if (mode == GL_TRIANGLE_FAN) { drawTriangleFan(count, type, indices, indexInfo.minIndex, elementArrayBuffer, instances); } else if (instances > 0) { mDeviceContext->DrawIndexedInstanced(count, instances, 0, -static_cast<int>(indexInfo.minIndex), 0); } else { mDeviceContext->DrawIndexed(count, 0, -static_cast<int>(indexInfo.minIndex)); } } void Renderer11::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer) { // Get the raw indices for an indexed draw if (type != GL_NONE && elementArrayBuffer) { gl::Buffer *indexBuffer = elementArrayBuffer; BufferStorage *storage = indexBuffer->getStorage(); intptr_t offset = reinterpret_cast<intptr_t>(indices); indices = static_cast<const GLubyte*>(storage->getData()) + offset; } if (!mLineLoopIB) { mLineLoopIB = new StreamingIndexBufferInterface(this); if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) { delete mLineLoopIB; mLineLoopIB = NULL; ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } } const int spaceNeeded = (count + 1) * sizeof(unsigned int); if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT)) { ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } void* mappedMemory = NULL; int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory); if (offset == -1 || mappedMemory == NULL) { ERR("Could not map index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); unsigned int indexBufferOffset = static_cast<unsigned int>(offset); switch (type) { case GL_NONE: // Non-indexed draw for (int i = 0; i < count; i++) { data[i] = i; } data[count] = 0; break; case GL_UNSIGNED_BYTE: for (int i = 0; i < count; i++) { data[i] = static_cast<const GLubyte*>(indices)[i]; } data[count] = static_cast<const GLubyte*>(indices)[0]; break; case GL_UNSIGNED_SHORT: for (int i = 0; i < count; i++) { data[i] = static_cast<const GLushort*>(indices)[i]; } data[count] = static_cast<const GLushort*>(indices)[0]; break; case GL_UNSIGNED_INT: for (int i = 0; i < count; i++) { data[i] = static_cast<const GLuint*>(indices)[i]; } data[count] = static_cast<const GLuint*>(indices)[0]; break; default: UNREACHABLE(); } if (!mLineLoopIB->unmapBuffer()) { ERR("Could not unmap index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } if (mAppliedIBSerial != mLineLoopIB->getSerial() || mAppliedIBOffset != indexBufferOffset) { IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mLineLoopIB->getIndexBuffer()); mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset); mAppliedIBSerial = mLineLoopIB->getSerial(); mAppliedStorageIBSerial = 0; mAppliedIBOffset = indexBufferOffset; } mDeviceContext->DrawIndexed(count + 1, 0, -minIndex); } void Renderer11::drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances) { // Get the raw indices for an indexed draw if (type != GL_NONE && elementArrayBuffer) { gl::Buffer *indexBuffer = elementArrayBuffer; BufferStorage *storage = indexBuffer->getStorage(); intptr_t offset = reinterpret_cast<intptr_t>(indices); indices = static_cast<const GLubyte*>(storage->getData()) + offset; } if (!mTriangleFanIB) { mTriangleFanIB = new StreamingIndexBufferInterface(this); if (!mTriangleFanIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) { delete mTriangleFanIB; mTriangleFanIB = NULL; ERR("Could not create a scratch index buffer for GL_TRIANGLE_FAN."); return gl::error(GL_OUT_OF_MEMORY); } } const int numTris = count - 2; const int spaceNeeded = (numTris * 3) * sizeof(unsigned int); if (!mTriangleFanIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT)) { ERR("Could not reserve enough space in scratch index buffer for GL_TRIANGLE_FAN."); return gl::error(GL_OUT_OF_MEMORY); } void* mappedMemory = NULL; int offset = mTriangleFanIB->mapBuffer(spaceNeeded, &mappedMemory); if (offset == -1 || mappedMemory == NULL) { ERR("Could not map scratch index buffer for GL_TRIANGLE_FAN."); return gl::error(GL_OUT_OF_MEMORY); } unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); unsigned int indexBufferOffset = static_cast<unsigned int>(offset); switch (type) { case GL_NONE: // Non-indexed draw for (int i = 0; i < numTris; i++) { data[i*3 + 0] = 0; data[i*3 + 1] = i + 1; data[i*3 + 2] = i + 2; } break; case GL_UNSIGNED_BYTE: for (int i = 0; i < numTris; i++) { data[i*3 + 0] = static_cast<const GLubyte*>(indices)[0]; data[i*3 + 1] = static_cast<const GLubyte*>(indices)[i + 1]; data[i*3 + 2] = static_cast<const GLubyte*>(indices)[i + 2]; } break; case GL_UNSIGNED_SHORT: for (int i = 0; i < numTris; i++) { data[i*3 + 0] = static_cast<const GLushort*>(indices)[0]; data[i*3 + 1] = static_cast<const GLushort*>(indices)[i + 1]; data[i*3 + 2] = static_cast<const GLushort*>(indices)[i + 2]; } break; case GL_UNSIGNED_INT: for (int i = 0; i < numTris; i++) { data[i*3 + 0] = static_cast<const GLuint*>(indices)[0]; data[i*3 + 1] = static_cast<const GLuint*>(indices)[i + 1]; data[i*3 + 2] = static_cast<const GLuint*>(indices)[i + 2]; } break; default: UNREACHABLE(); } if (!mTriangleFanIB->unmapBuffer()) { ERR("Could not unmap scratch index buffer for GL_TRIANGLE_FAN."); return gl::error(GL_OUT_OF_MEMORY); } if (mAppliedIBSerial != mTriangleFanIB->getSerial() || mAppliedIBOffset != indexBufferOffset) { IndexBuffer11 *indexBuffer = IndexBuffer11::makeIndexBuffer11(mTriangleFanIB->getIndexBuffer()); mDeviceContext->IASetIndexBuffer(indexBuffer->getBuffer(), indexBuffer->getIndexFormat(), indexBufferOffset); mAppliedIBSerial = mTriangleFanIB->getSerial(); mAppliedStorageIBSerial = 0; mAppliedIBOffset = indexBufferOffset; } if (instances > 0) { mDeviceContext->DrawIndexedInstanced(numTris * 3, instances, 0, -minIndex, 0); } else { mDeviceContext->DrawIndexed(numTris * 3, 0, -minIndex); } } void Renderer11::applyShaders(gl::ProgramBinary *programBinary) { unsigned int programBinarySerial = programBinary->getSerial(); const bool updateProgramState = (programBinarySerial != mAppliedProgramBinarySerial); if (updateProgramState) { ShaderExecutable *vertexExe = programBinary->getVertexExecutable(); ShaderExecutable *pixelExe = programBinary->getPixelExecutable(); ID3D11VertexShader *vertexShader = NULL; if (vertexExe) vertexShader = ShaderExecutable11::makeShaderExecutable11(vertexExe)->getVertexShader(); ID3D11PixelShader *pixelShader = NULL; if (pixelExe) pixelShader = ShaderExecutable11::makeShaderExecutable11(pixelExe)->getPixelShader(); mDeviceContext->PSSetShader(pixelShader, NULL, 0); mDeviceContext->VSSetShader(vertexShader, NULL, 0); programBinary->dirtyAllUniforms(); mAppliedProgramBinarySerial = programBinarySerial; } // Only use the geometry shader currently for point sprite drawing const bool usesGeometryShader = (programBinary->usesGeometryShader() && mCurRasterState.pointDrawMode); if (updateProgramState || usesGeometryShader != mIsGeometryShaderActive) { if (usesGeometryShader) { ShaderExecutable *geometryExe = programBinary->getGeometryExecutable(); ID3D11GeometryShader *geometryShader = ShaderExecutable11::makeShaderExecutable11(geometryExe)->getGeometryShader(); mDeviceContext->GSSetShader(geometryShader, NULL, 0); } else { mDeviceContext->GSSetShader(NULL, NULL, 0); } mIsGeometryShaderActive = usesGeometryShader; } } void Renderer11::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray) { ShaderExecutable11 *vertexExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getVertexExecutable()); ShaderExecutable11 *pixelExecutable = ShaderExecutable11::makeShaderExecutable11(programBinary->getPixelExecutable()); unsigned int totalRegisterCountVS = 0; unsigned int totalRegisterCountPS = 0; bool vertexUniformsDirty = false; bool pixelUniformsDirty = false; for (gl::UniformArray::const_iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++) { const gl::Uniform *uniform = *uniform_iterator; if (uniform->vsRegisterIndex >= 0) { totalRegisterCountVS += uniform->registerCount; vertexUniformsDirty = vertexUniformsDirty || uniform->dirty; } if (uniform->psRegisterIndex >= 0) { totalRegisterCountPS += uniform->registerCount; pixelUniformsDirty = pixelUniformsDirty || uniform->dirty; } } ID3D11Buffer *vertexConstantBuffer = vertexExecutable->getConstantBuffer(mDevice, totalRegisterCountVS); ID3D11Buffer *pixelConstantBuffer = pixelExecutable->getConstantBuffer(mDevice, totalRegisterCountPS); float (*mapVS)[4] = NULL; float (*mapPS)[4] = NULL; if (totalRegisterCountVS > 0 && vertexUniformsDirty) { D3D11_MAPPED_SUBRESOURCE map = {0}; HRESULT result = mDeviceContext->Map(vertexConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map); ASSERT(SUCCEEDED(result)); mapVS = (float(*)[4])map.pData; } if (totalRegisterCountPS > 0 && pixelUniformsDirty) { D3D11_MAPPED_SUBRESOURCE map = {0}; HRESULT result = mDeviceContext->Map(pixelConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &map); ASSERT(SUCCEEDED(result)); mapPS = (float(*)[4])map.pData; } for (gl::UniformArray::iterator uniform_iterator = uniformArray->begin(); uniform_iterator != uniformArray->end(); uniform_iterator++) { gl::Uniform *uniform = *uniform_iterator; if (uniform->type != GL_SAMPLER_2D && uniform->type != GL_SAMPLER_CUBE) { if (uniform->vsRegisterIndex >= 0 && mapVS) { memcpy(mapVS + uniform->vsRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4])); } if (uniform->psRegisterIndex >= 0 && mapPS) { memcpy(mapPS + uniform->psRegisterIndex, uniform->data, uniform->registerCount * sizeof(float[4])); } } uniform->dirty = false; } if (mapVS) { mDeviceContext->Unmap(vertexConstantBuffer, 0); } if (mapPS) { mDeviceContext->Unmap(pixelConstantBuffer, 0); } mDeviceContext->VSSetConstantBuffers(0, 1, &vertexConstantBuffer); mDeviceContext->PSSetConstantBuffers(0, 1, &pixelConstantBuffer); // Driver uniforms if (!mDriverConstantBufferVS) { D3D11_BUFFER_DESC constantBufferDescription = {0}; constantBufferDescription.ByteWidth = sizeof(dx_VertexConstants); constantBufferDescription.Usage = D3D11_USAGE_DEFAULT; constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER; constantBufferDescription.CPUAccessFlags = 0; constantBufferDescription.MiscFlags = 0; constantBufferDescription.StructureByteStride = 0; HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferVS); ASSERT(SUCCEEDED(result)); mDeviceContext->VSSetConstantBuffers(1, 1, &mDriverConstantBufferVS); } if (!mDriverConstantBufferPS) { D3D11_BUFFER_DESC constantBufferDescription = {0}; constantBufferDescription.ByteWidth = sizeof(dx_PixelConstants); constantBufferDescription.Usage = D3D11_USAGE_DEFAULT; constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER; constantBufferDescription.CPUAccessFlags = 0; constantBufferDescription.MiscFlags = 0; constantBufferDescription.StructureByteStride = 0; HRESULT result = mDevice->CreateBuffer(&constantBufferDescription, NULL, &mDriverConstantBufferPS); ASSERT(SUCCEEDED(result)); mDeviceContext->PSSetConstantBuffers(1, 1, &mDriverConstantBufferPS); } if (memcmp(&mVertexConstants, &mAppliedVertexConstants, sizeof(dx_VertexConstants)) != 0) { mDeviceContext->UpdateSubresource(mDriverConstantBufferVS, 0, NULL, &mVertexConstants, 16, 0); memcpy(&mAppliedVertexConstants, &mVertexConstants, sizeof(dx_VertexConstants)); } if (memcmp(&mPixelConstants, &mAppliedPixelConstants, sizeof(dx_PixelConstants)) != 0) { mDeviceContext->UpdateSubresource(mDriverConstantBufferPS, 0, NULL, &mPixelConstants, 16, 0); memcpy(&mAppliedPixelConstants, &mPixelConstants, sizeof(dx_PixelConstants)); } // needed for the point sprite geometry shader mDeviceContext->GSSetConstantBuffers(0, 1, &mDriverConstantBufferPS); } void Renderer11::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer) { bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha; bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) && !(clearParams.colorMaskRed && clearParams.colorMaskGreen && clearParams.colorMaskBlue && alphaUnmasked); unsigned int stencilUnmasked = 0x0; if (frameBuffer->hasStencil()) { unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat()); stencilUnmasked = (0x1 << stencilSize) - 1; } bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) && (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked; bool needScissoredClear = mScissorEnabled && (mCurScissor.x > 0 || mCurScissor.y > 0 || mCurScissor.x + mCurScissor.width < mRenderTargetDesc.width || mCurScissor.y + mCurScissor.height < mRenderTargetDesc.height); if (needMaskedColorClear || needMaskedStencilClear || needScissoredClear) { maskedClear(clearParams, frameBuffer->usingExtendedDrawBuffers()); } else { if (clearParams.mask & GL_COLOR_BUFFER_BIT) { for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++) { if (frameBuffer->isEnabledColorAttachment(colorAttachment)) { gl::Renderbuffer *renderbufferObject = frameBuffer->getColorbuffer(colorAttachment); if (renderbufferObject) { RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getRenderTarget()); if (!renderTarget) { ERR("render target pointer unexpectedly null."); return; } ID3D11RenderTargetView *framebufferRTV = renderTarget->getRenderTargetView(); if (!framebufferRTV) { ERR("render target view pointer unexpectedly null."); return; } const float clearValues[4] = { clearParams.colorClearValue.red, clearParams.colorClearValue.green, clearParams.colorClearValue.blue, clearParams.colorClearValue.alpha }; mDeviceContext->ClearRenderTargetView(framebufferRTV, clearValues); framebufferRTV->Release(); } } } } if (clearParams.mask & GL_DEPTH_BUFFER_BIT || clearParams.mask & GL_STENCIL_BUFFER_BIT) { gl::Renderbuffer *renderbufferObject = frameBuffer->getDepthOrStencilbuffer(); if (renderbufferObject) { RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(renderbufferObject->getDepthStencil()); if (!renderTarget) { ERR("render target pointer unexpectedly null."); return; } ID3D11DepthStencilView *framebufferDSV = renderTarget->getDepthStencilView(); if (!framebufferDSV) { ERR("depth stencil view pointer unexpectedly null."); return; } UINT clearFlags = 0; if (clearParams.mask & GL_DEPTH_BUFFER_BIT) { clearFlags |= D3D11_CLEAR_DEPTH; } if (clearParams.mask & GL_STENCIL_BUFFER_BIT) { clearFlags |= D3D11_CLEAR_STENCIL; } float depthClear = gl::clamp01(clearParams.depthClearValue); UINT8 stencilClear = clearParams.stencilClearValue & 0x000000FF; mDeviceContext->ClearDepthStencilView(framebufferDSV, clearFlags, depthClear, stencilClear); framebufferDSV->Release(); } } } } void Renderer11::maskedClear(const gl::ClearParameters &clearParams, bool usingExtendedDrawBuffers) { HRESULT result; if (!mClearResourcesInitialized) { ASSERT(!mClearVB && !mClearVS && !mClearSinglePS && !mClearMultiplePS && !mClearScissorRS && !mClearNoScissorRS); D3D11_BUFFER_DESC vbDesc; vbDesc.ByteWidth = sizeof(d3d11::PositionDepthColorVertex) * 4; vbDesc.Usage = D3D11_USAGE_DYNAMIC; vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; vbDesc.MiscFlags = 0; vbDesc.StructureByteStride = 0; result = mDevice->CreateBuffer(&vbDesc, NULL, &mClearVB); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mClearVB, "Renderer11 masked clear vertex buffer"); D3D11_INPUT_ELEMENT_DESC quadLayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Clear, sizeof(g_VS_Clear), &mClearIL); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mClearIL, "Renderer11 masked clear input layout"); result = mDevice->CreateVertexShader(g_VS_Clear, sizeof(g_VS_Clear), NULL, &mClearVS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mClearVS, "Renderer11 masked clear vertex shader"); result = mDevice->CreatePixelShader(g_PS_ClearSingle, sizeof(g_PS_ClearSingle), NULL, &mClearSinglePS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mClearSinglePS, "Renderer11 masked clear pixel shader (1 RT)"); result = mDevice->CreatePixelShader(g_PS_ClearMultiple, sizeof(g_PS_ClearMultiple), NULL, &mClearMultiplePS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mClearMultiplePS, "Renderer11 masked clear pixel shader (MRT)"); D3D11_RASTERIZER_DESC rsScissorDesc; rsScissorDesc.FillMode = D3D11_FILL_SOLID; rsScissorDesc.CullMode = D3D11_CULL_NONE; rsScissorDesc.FrontCounterClockwise = FALSE; rsScissorDesc.DepthBias = 0; rsScissorDesc.DepthBiasClamp = 0.0f; rsScissorDesc.SlopeScaledDepthBias = 0.0f; rsScissorDesc.DepthClipEnable = FALSE; rsScissorDesc.ScissorEnable = TRUE; rsScissorDesc.MultisampleEnable = FALSE; rsScissorDesc.AntialiasedLineEnable = FALSE; result = mDevice->CreateRasterizerState(&rsScissorDesc, &mClearScissorRS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mClearScissorRS, "Renderer11 masked clear scissor rasterizer state"); D3D11_RASTERIZER_DESC rsNoScissorDesc; rsNoScissorDesc.FillMode = D3D11_FILL_SOLID; rsNoScissorDesc.CullMode = D3D11_CULL_NONE; rsNoScissorDesc.FrontCounterClockwise = FALSE; rsNoScissorDesc.DepthBias = 0; rsNoScissorDesc.DepthBiasClamp = 0.0f; rsNoScissorDesc.SlopeScaledDepthBias = 0.0f; rsNoScissorDesc.DepthClipEnable = FALSE; rsNoScissorDesc.ScissorEnable = FALSE; rsNoScissorDesc.MultisampleEnable = FALSE; rsNoScissorDesc.AntialiasedLineEnable = FALSE; result = mDevice->CreateRasterizerState(&rsNoScissorDesc, &mClearNoScissorRS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mClearNoScissorRS, "Renderer11 masked clear no scissor rasterizer state"); mClearResourcesInitialized = true; } // Prepare the depth stencil state to write depth values if the depth should be cleared // and stencil values if the stencil should be cleared gl::DepthStencilState glDSState; glDSState.depthTest = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0; glDSState.depthFunc = GL_ALWAYS; glDSState.depthMask = (clearParams.mask & GL_DEPTH_BUFFER_BIT) != 0; glDSState.stencilTest = (clearParams.mask & GL_STENCIL_BUFFER_BIT) != 0; glDSState.stencilFunc = GL_ALWAYS; glDSState.stencilMask = 0; glDSState.stencilFail = GL_REPLACE; glDSState.stencilPassDepthFail = GL_REPLACE; glDSState.stencilPassDepthPass = GL_REPLACE; glDSState.stencilWritemask = clearParams.stencilWriteMask; glDSState.stencilBackFunc = GL_ALWAYS; glDSState.stencilBackMask = 0; glDSState.stencilBackFail = GL_REPLACE; glDSState.stencilBackPassDepthFail = GL_REPLACE; glDSState.stencilBackPassDepthPass = GL_REPLACE; glDSState.stencilBackWritemask = clearParams.stencilWriteMask; int stencilClear = clearParams.stencilClearValue & 0x000000FF; ID3D11DepthStencilState *dsState = mStateCache.getDepthStencilState(glDSState); // Prepare the blend state to use a write mask if the color buffer should be cleared gl::BlendState glBlendState; glBlendState.blend = false; glBlendState.sourceBlendRGB = GL_ONE; glBlendState.destBlendRGB = GL_ZERO; glBlendState.sourceBlendAlpha = GL_ONE; glBlendState.destBlendAlpha = GL_ZERO; glBlendState.blendEquationRGB = GL_FUNC_ADD; glBlendState.blendEquationAlpha = GL_FUNC_ADD; glBlendState.colorMaskRed = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskRed : false; glBlendState.colorMaskGreen = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskGreen : false; glBlendState.colorMaskBlue = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskBlue : false; glBlendState.colorMaskAlpha = (clearParams.mask & GL_COLOR_BUFFER_BIT) ? clearParams.colorMaskAlpha : false; glBlendState.sampleAlphaToCoverage = false; glBlendState.dither = false; static const float blendFactors[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; static const UINT sampleMask = 0xFFFFFFFF; ID3D11BlendState *blendState = mStateCache.getBlendState(glBlendState); // Set the vertices D3D11_MAPPED_SUBRESOURCE mappedResource; result = mDeviceContext->Map(mClearVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { ERR("Failed to map masked clear vertex buffer, HRESULT: 0x%X.", result); return; } d3d11::PositionDepthColorVertex *vertices = reinterpret_cast<d3d11::PositionDepthColorVertex*>(mappedResource.pData); float depthClear = gl::clamp01(clearParams.depthClearValue); d3d11::SetPositionDepthColorVertex(&vertices[0], -1.0f, 1.0f, depthClear, clearParams.colorClearValue); d3d11::SetPositionDepthColorVertex(&vertices[1], -1.0f, -1.0f, depthClear, clearParams.colorClearValue); d3d11::SetPositionDepthColorVertex(&vertices[2], 1.0f, 1.0f, depthClear, clearParams.colorClearValue); d3d11::SetPositionDepthColorVertex(&vertices[3], 1.0f, -1.0f, depthClear, clearParams.colorClearValue); mDeviceContext->Unmap(mClearVB, 0); // Apply state mDeviceContext->OMSetBlendState(blendState, blendFactors, sampleMask); mDeviceContext->OMSetDepthStencilState(dsState, stencilClear); mDeviceContext->RSSetState(mScissorEnabled ? mClearScissorRS : mClearNoScissorRS); // Apply shaders ID3D11PixelShader *pixelShader = usingExtendedDrawBuffers ? mClearMultiplePS : mClearSinglePS; mDeviceContext->IASetInputLayout(mClearIL); mDeviceContext->VSSetShader(mClearVS, NULL, 0); mDeviceContext->PSSetShader(pixelShader, NULL, 0); mDeviceContext->GSSetShader(NULL, NULL, 0); // Apply vertex buffer static UINT stride = sizeof(d3d11::PositionDepthColorVertex); static UINT startIdx = 0; mDeviceContext->IASetVertexBuffers(0, 1, &mClearVB, &stride, &startIdx); mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); // Draw the clear quad mDeviceContext->Draw(4, 0); // Clean up markAllStateDirty(); } void Renderer11::markAllStateDirty() { for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++) { mAppliedRenderTargetSerials[rtIndex] = 0; } mAppliedDepthbufferSerial = 0; mAppliedStencilbufferSerial = 0; mDepthStencilInitialized = false; mRenderTargetDescInitialized = false; for (int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++) { mForceSetVertexSamplerStates[i] = true; mCurVertexTextureSerials[i] = 0; } for (int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++) { mForceSetPixelSamplerStates[i] = true; mCurPixelTextureSerials[i] = 0; } mForceSetBlendState = true; mForceSetRasterState = true; mForceSetDepthStencilState = true; mForceSetScissor = true; mForceSetViewport = true; mAppliedIBSerial = 0; mAppliedStorageIBSerial = 0; mAppliedIBOffset = 0; mAppliedProgramBinarySerial = 0; memset(&mAppliedVertexConstants, 0, sizeof(dx_VertexConstants)); memset(&mAppliedPixelConstants, 0, sizeof(dx_PixelConstants)); } void Renderer11::releaseDeviceResources() { mStateCache.clear(); mInputLayoutCache.clear(); delete mVertexDataManager; mVertexDataManager = NULL; delete mIndexDataManager; mIndexDataManager = NULL; delete mLineLoopIB; mLineLoopIB = NULL; delete mTriangleFanIB; mTriangleFanIB = NULL; SafeRelease(mCopyVB); SafeRelease(mCopySampler); SafeRelease(mCopyIL); SafeRelease(mCopyIL); SafeRelease(mCopyVS); SafeRelease(mCopyRGBAPS); SafeRelease(mCopyRGBPS); SafeRelease(mCopyLumPS); SafeRelease(mCopyLumAlphaPS); mCopyResourcesInitialized = false; SafeRelease(mClearVB); SafeRelease(mClearIL); SafeRelease(mClearVS); SafeRelease(mClearSinglePS); SafeRelease(mClearMultiplePS); SafeRelease(mClearScissorRS); SafeRelease(mClearNoScissorRS); mClearResourcesInitialized = false; SafeRelease(mDriverConstantBufferVS); SafeRelease(mDriverConstantBufferPS); SafeRelease(mSyncQuery); } void Renderer11::notifyDeviceLost() { mDeviceLost = true; mDisplay->notifyDeviceLost(); } bool Renderer11::isDeviceLost() { return mDeviceLost; } // set notify to true to broadcast a message to all contexts of the device loss bool Renderer11::testDeviceLost(bool notify) { bool isLost = false; // GetRemovedReason is used to test if the device is removed HRESULT result = mDevice->GetDeviceRemovedReason(); isLost = d3d11::isDeviceLostError(result); if (isLost) { // Log error if this is a new device lost event if (mDeviceLost == false) { ERR("The D3D11 device was removed: 0x%08X", result); } // ensure we note the device loss -- // we'll probably get this done again by notifyDeviceLost // but best to remember it! // Note that we don't want to clear the device loss status here // -- this needs to be done by resetDevice mDeviceLost = true; if (notify) { notifyDeviceLost(); } } return isLost; } bool Renderer11::testDeviceResettable() { // determine if the device is resettable by creating a dummy device PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice"); if (D3D11CreateDevice == NULL) { return false; } D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0, }; ID3D11Device* dummyDevice; D3D_FEATURE_LEVEL dummyFeatureLevel; ID3D11DeviceContext* dummyContext; HRESULT result = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, #if defined(_DEBUG) D3D11_CREATE_DEVICE_DEBUG, #else 0, #endif featureLevels, ArraySize(featureLevels), D3D11_SDK_VERSION, &dummyDevice, &dummyFeatureLevel, &dummyContext); if (!mDevice || FAILED(result)) { return false; } dummyContext->Release(); dummyDevice->Release(); return true; } void Renderer11::release() { releaseDeviceResources(); if (mDxgiFactory) { mDxgiFactory->Release(); mDxgiFactory = NULL; } if (mDxgiAdapter) { mDxgiAdapter->Release(); mDxgiAdapter = NULL; } if (mDeviceContext) { mDeviceContext->ClearState(); mDeviceContext->Flush(); mDeviceContext->Release(); mDeviceContext = NULL; } if (mDevice) { mDevice->Release(); mDevice = NULL; } if (mD3d11Module) { FreeLibrary(mD3d11Module); mD3d11Module = NULL; } if (mDxgiModule) { FreeLibrary(mDxgiModule); mDxgiModule = NULL; } } bool Renderer11::resetDevice() { // recreate everything release(); EGLint result = initialize(); if (result != EGL_SUCCESS) { ERR("Could not reinitialize D3D11 device: %08X", result); return false; } mDeviceLost = false; return true; } DWORD Renderer11::getAdapterVendor() const { return mAdapterDescription.VendorId; } std::string Renderer11::getRendererDescription() const { std::ostringstream rendererString; rendererString << mDescription; rendererString << " Direct3D11"; rendererString << " vs_" << getMajorShaderModel() << "_" << getMinorShaderModel(); rendererString << " ps_" << getMajorShaderModel() << "_" << getMinorShaderModel(); return rendererString.str(); } GUID Renderer11::getAdapterIdentifier() const { // Use the adapter LUID as our adapter ID // This number is local to a machine is only guaranteed to be unique between restarts META_ASSERT(sizeof(LUID) <= sizeof(GUID)); GUID adapterId = {0}; memcpy(&adapterId, &mAdapterDescription.AdapterLuid, sizeof(LUID)); return adapterId; } bool Renderer11::getBGRATextureSupport() const { return mBGRATextureSupport; } bool Renderer11::getDXT1TextureSupport() { return mDXT1TextureSupport; } bool Renderer11::getDXT3TextureSupport() { return mDXT3TextureSupport; } bool Renderer11::getDXT5TextureSupport() { return mDXT5TextureSupport; } bool Renderer11::getDepthTextureSupport() const { return mDepthTextureSupport; } bool Renderer11::getFloat32TextureSupport(bool *filtering, bool *renderable) { *renderable = mFloat32RenderSupport; *filtering = mFloat32FilterSupport; return mFloat32TextureSupport; } bool Renderer11::getFloat16TextureSupport(bool *filtering, bool *renderable) { *renderable = mFloat16RenderSupport; *filtering = mFloat16FilterSupport; return mFloat16TextureSupport; } bool Renderer11::getLuminanceTextureSupport() { return false; } bool Renderer11::getLuminanceAlphaTextureSupport() { return false; } bool Renderer11::getTextureFilterAnisotropySupport() const { return true; } float Renderer11::getTextureMaxAnisotropy() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: return D3D11_MAX_MAXANISOTROPY; case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return D3D10_MAX_MAXANISOTROPY; default: UNREACHABLE(); return 0; } } bool Renderer11::getEventQuerySupport() { return true; } Range Renderer11::getViewportBounds() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: return Range(D3D11_VIEWPORT_BOUNDS_MIN, D3D11_VIEWPORT_BOUNDS_MAX); case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return Range(D3D10_VIEWPORT_BOUNDS_MIN, D3D10_VIEWPORT_BOUNDS_MAX); default: UNREACHABLE(); return Range(0, 0); } } unsigned int Renderer11::getMaxVertexTextureImageUnits() const { META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM4 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS); switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return MAX_TEXTURE_IMAGE_UNITS_VTF_SM4; default: UNREACHABLE(); return 0; } } unsigned int Renderer11::getMaxCombinedTextureImageUnits() const { return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits(); } unsigned int Renderer11::getReservedVertexUniformVectors() const { return 0; // Driver uniforms are stored in a separate constant buffer } unsigned int Renderer11::getReservedFragmentUniformVectors() const { return 0; // Driver uniforms are stored in a separate constant buffer } unsigned int Renderer11::getMaxVertexUniformVectors() const { META_ASSERT(MAX_VERTEX_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT); ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0); return MAX_VERTEX_UNIFORM_VECTORS_D3D11; } unsigned int Renderer11::getMaxFragmentUniformVectors() const { META_ASSERT(MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 <= D3D10_REQ_CONSTANT_BUFFER_ELEMENT_COUNT); ASSERT(mFeatureLevel >= D3D_FEATURE_LEVEL_10_0); return MAX_FRAGMENT_UNIFORM_VECTORS_D3D11; } unsigned int Renderer11::getMaxVaryingVectors() const { META_ASSERT(gl::IMPLEMENTATION_MAX_VARYING_VECTORS == D3D11_VS_OUTPUT_REGISTER_COUNT); switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: return D3D11_VS_OUTPUT_REGISTER_COUNT; case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return D3D10_VS_OUTPUT_REGISTER_COUNT; default: UNREACHABLE(); return 0; } } bool Renderer11::getNonPower2TextureSupport() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return true; default: UNREACHABLE(); return false; } } bool Renderer11::getOcclusionQuerySupport() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return true; default: UNREACHABLE(); return false; } } bool Renderer11::getInstancingSupport() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return true; default: UNREACHABLE(); return false; } } bool Renderer11::getShareHandleSupport() const { // We only currently support share handles with BGRA surfaces, because // chrome needs BGRA. Once chrome fixes this, we should always support them. // PIX doesn't seem to support using share handles, so disable them. return getBGRATextureSupport() && !gl::perfActive(); } bool Renderer11::getDerivativeInstructionSupport() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return true; default: UNREACHABLE(); return false; } } bool Renderer11::getPostSubBufferSupport() const { // D3D11 does not support present with dirty rectangles until D3D11.1 and DXGI 1.2. return false; } int Renderer11::getMajorShaderModel() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MAJOR_VERSION; // 5 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MAJOR_VERSION; // 4 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MAJOR_VERSION; // 4 default: UNREACHABLE(); return 0; } } int Renderer11::getMinorShaderModel() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: return D3D11_SHADER_MINOR_VERSION; // 0 case D3D_FEATURE_LEVEL_10_1: return D3D10_1_SHADER_MINOR_VERSION; // 1 case D3D_FEATURE_LEVEL_10_0: return D3D10_SHADER_MINOR_VERSION; // 0 default: UNREACHABLE(); return 0; } } float Renderer11::getMaxPointSize() const { // choose a reasonable maximum. we enforce this in the shader. // (nb: on a Radeon 2600xt, DX9 reports a 256 max point size) return 1024.0f; } int Renderer11::getMaxViewportDimension() const { // Maximum viewport size must be at least as large as the largest render buffer (or larger). // In our case return the maximum texture size, which is the maximum render buffer size. META_ASSERT(D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D11_VIEWPORT_BOUNDS_MAX); META_ASSERT(D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION * 2 - 1 <= D3D10_VIEWPORT_BOUNDS_MAX); switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 default: UNREACHABLE(); return 0; } } int Renderer11::getMaxTextureWidth() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 default: UNREACHABLE(); return 0; } } int Renderer11::getMaxTextureHeight() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: return D3D11_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 16384 case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_TEXTURE2D_U_OR_V_DIMENSION; // 8192 default: UNREACHABLE(); return 0; } } bool Renderer11::get32BitIndexSupport() const { switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return D3D10_REQ_DRAWINDEXED_INDEX_COUNT_2_TO_EXP >= 32; // true default: UNREACHABLE(); return false; } } int Renderer11::getMinSwapInterval() const { return 0; } int Renderer11::getMaxSwapInterval() const { return 4; } int Renderer11::getMaxSupportedSamples() const { return mMaxSupportedSamples; } int Renderer11::getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const { if (requested == 0) { return 0; } MultisampleSupportMap::const_iterator iter = mMultisampleSupportMap.find(format); if (iter != mMultisampleSupportMap.end()) { const MultisampleSupportInfo& info = iter->second; for (unsigned int i = requested - 1; i < D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT; i++) { if (info.qualityLevels[i] > 0) { return i + 1; } } } return -1; } unsigned int Renderer11::getMaxRenderTargets() const { META_ASSERT(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS); META_ASSERT(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT <= gl::IMPLEMENTATION_MAX_DRAW_BUFFERS); switch (mFeatureLevel) { case D3D_FEATURE_LEVEL_11_0: return D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8 case D3D_FEATURE_LEVEL_10_1: case D3D_FEATURE_LEVEL_10_0: return D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; // 8 default: UNREACHABLE(); return 1; } } bool Renderer11::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source) { if (source && dest) { TextureStorage11_2D *source11 = TextureStorage11_2D::makeTextureStorage11_2D(source->getStorageInstance()); TextureStorage11_2D *dest11 = TextureStorage11_2D::makeTextureStorage11_2D(dest->getStorageInstance()); mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture()); return true; } return false; } bool Renderer11::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source) { if (source && dest) { TextureStorage11_Cube *source11 = TextureStorage11_Cube::makeTextureStorage11_Cube(source->getStorageInstance()); TextureStorage11_Cube *dest11 = TextureStorage11_Cube::makeTextureStorage11_Cube(dest->getStorageInstance()); mDeviceContext->CopyResource(dest11->getBaseTexture(), source11->getBaseTexture()); return true; } return false; } bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level) { gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer(); if (!colorbuffer) { ERR("Failed to retrieve the color buffer from the frame buffer."); return gl::error(GL_OUT_OF_MEMORY, false); } RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); if (!sourceRenderTarget) { ERR("Failed to retrieve the render target from the frame buffer."); return gl::error(GL_OUT_OF_MEMORY, false); } ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView(); if (!source) { ERR("Failed to retrieve the render target view from the render target."); return gl::error(GL_OUT_OF_MEMORY, false); } TextureStorage11_2D *storage11 = TextureStorage11_2D::makeTextureStorage11_2D(storage->getStorageInstance()); if (!storage11) { source->Release(); ERR("Failed to retrieve the texture storage from the destination."); return gl::error(GL_OUT_OF_MEMORY, false); } RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(level)); if (!destRenderTarget) { source->Release(); ERR("Failed to retrieve the render target from the destination storage."); return gl::error(GL_OUT_OF_MEMORY, false); } ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView(); if (!dest) { source->Release(); ERR("Failed to retrieve the render target view from the destination render target."); return gl::error(GL_OUT_OF_MEMORY, false); } gl::Rectangle destRect; destRect.x = xoffset; destRect.y = yoffset; destRect.width = sourceRect.width; destRect.height = sourceRect.height; bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat); source->Release(); dest->Release(); return ret; } bool Renderer11::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level) { gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer(); if (!colorbuffer) { ERR("Failed to retrieve the color buffer from the frame buffer."); return gl::error(GL_OUT_OF_MEMORY, false); } RenderTarget11 *sourceRenderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); if (!sourceRenderTarget) { ERR("Failed to retrieve the render target from the frame buffer."); return gl::error(GL_OUT_OF_MEMORY, false); } ID3D11ShaderResourceView *source = sourceRenderTarget->getShaderResourceView(); if (!source) { ERR("Failed to retrieve the render target view from the render target."); return gl::error(GL_OUT_OF_MEMORY, false); } TextureStorage11_Cube *storage11 = TextureStorage11_Cube::makeTextureStorage11_Cube(storage->getStorageInstance()); if (!storage11) { source->Release(); ERR("Failed to retrieve the texture storage from the destination."); return gl::error(GL_OUT_OF_MEMORY, false); } RenderTarget11 *destRenderTarget = RenderTarget11::makeRenderTarget11(storage11->getRenderTarget(target, level)); if (!destRenderTarget) { source->Release(); ERR("Failed to retrieve the render target from the destination storage."); return gl::error(GL_OUT_OF_MEMORY, false); } ID3D11RenderTargetView *dest = destRenderTarget->getRenderTargetView(); if (!dest) { source->Release(); ERR("Failed to retrieve the render target view from the destination render target."); return gl::error(GL_OUT_OF_MEMORY, false); } gl::Rectangle destRect; destRect.x = xoffset; destRect.y = yoffset; destRect.width = sourceRect.width; destRect.height = sourceRect.height; bool ret = copyTexture(source, sourceRect, sourceRenderTarget->getWidth(), sourceRenderTarget->getHeight(), dest, destRect, destRenderTarget->getWidth(), destRenderTarget->getHeight(), destFormat); source->Release(); dest->Release(); return ret; } bool Renderer11::copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight, ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat) { HRESULT result; if (!mCopyResourcesInitialized) { ASSERT(!mCopyVB && !mCopySampler && !mCopyIL && !mCopyVS && !mCopyRGBAPS && !mCopyRGBPS && !mCopyLumPS && !mCopyLumAlphaPS); D3D11_BUFFER_DESC vbDesc; vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4; vbDesc.Usage = D3D11_USAGE_DYNAMIC; vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; vbDesc.MiscFlags = 0; vbDesc.StructureByteStride = 0; result = mDevice->CreateBuffer(&vbDesc, NULL, &mCopyVB); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mCopyVB, "Renderer11 copy texture vertex buffer"); D3D11_SAMPLER_DESC samplerDesc; samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.MipLODBias = 0.0f; samplerDesc.MaxAnisotropy = 0; samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; samplerDesc.BorderColor[0] = 0.0f; samplerDesc.BorderColor[1] = 0.0f; samplerDesc.BorderColor[2] = 0.0f; samplerDesc.BorderColor[3] = 0.0f; samplerDesc.MinLOD = 0.0f; samplerDesc.MaxLOD = 0.0f; result = mDevice->CreateSamplerState(&samplerDesc, &mCopySampler); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mCopySampler, "Renderer11 copy sampler"); D3D11_INPUT_ELEMENT_DESC quadLayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; result = mDevice->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mCopyIL); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mCopyIL, "Renderer11 copy texture input layout"); result = mDevice->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mCopyVS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mCopyVS, "Renderer11 copy texture vertex shader"); result = mDevice->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mCopyRGBAPS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mCopyRGBAPS, "Renderer11 copy texture RGBA pixel shader"); result = mDevice->CreatePixelShader(g_PS_PassthroughRGB, sizeof(g_PS_PassthroughRGB), NULL, &mCopyRGBPS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mCopyRGBPS, "Renderer11 copy texture RGB pixel shader"); result = mDevice->CreatePixelShader(g_PS_PassthroughLum, sizeof(g_PS_PassthroughLum), NULL, &mCopyLumPS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mCopyLumPS, "Renderer11 copy texture luminance pixel shader"); result = mDevice->CreatePixelShader(g_PS_PassthroughLumAlpha, sizeof(g_PS_PassthroughLumAlpha), NULL, &mCopyLumAlphaPS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mCopyLumAlphaPS, "Renderer11 copy texture luminance alpha pixel shader"); mCopyResourcesInitialized = true; } // Verify the source and destination area sizes if (sourceArea.x < 0 || sourceArea.x + sourceArea.width > static_cast<int>(sourceWidth) || sourceArea.y < 0 || sourceArea.y + sourceArea.height > static_cast<int>(sourceHeight) || destArea.x < 0 || destArea.x + destArea.width > static_cast<int>(destWidth) || destArea.y < 0 || destArea.y + destArea.height > static_cast<int>(destHeight)) { return gl::error(GL_INVALID_VALUE, false); } // Set vertices D3D11_MAPPED_SUBRESOURCE mappedResource; result = mDeviceContext->Map(mCopyVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result); return gl::error(GL_OUT_OF_MEMORY, false); } d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData); // Create a quad in homogeneous coordinates float x1 = (destArea.x / float(destWidth)) * 2.0f - 1.0f; float y1 = ((destHeight - destArea.y - destArea.height) / float(destHeight)) * 2.0f - 1.0f; float x2 = ((destArea.x + destArea.width) / float(destWidth)) * 2.0f - 1.0f; float y2 = ((destHeight - destArea.y) / float(destHeight)) * 2.0f - 1.0f; float u1 = sourceArea.x / float(sourceWidth); float v1 = sourceArea.y / float(sourceHeight); float u2 = (sourceArea.x + sourceArea.width) / float(sourceWidth); float v2 = (sourceArea.y + sourceArea.height) / float(sourceHeight); d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2); d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1); d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2); d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1); mDeviceContext->Unmap(mCopyVB, 0); static UINT stride = sizeof(d3d11::PositionTexCoordVertex); static UINT startIdx = 0; mDeviceContext->IASetVertexBuffers(0, 1, &mCopyVB, &stride, &startIdx); // Apply state mDeviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF); mDeviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF); mDeviceContext->RSSetState(NULL); // Apply shaders mDeviceContext->IASetInputLayout(mCopyIL); mDeviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); mDeviceContext->VSSetShader(mCopyVS, NULL, 0); ID3D11PixelShader *ps = NULL; switch(destFormat) { case GL_RGBA: ps = mCopyRGBAPS; break; case GL_RGB: ps = mCopyRGBPS; break; case GL_ALPHA: ps = mCopyRGBAPS; break; case GL_BGRA_EXT: ps = mCopyRGBAPS; break; case GL_LUMINANCE: ps = mCopyLumPS; break; case GL_LUMINANCE_ALPHA: ps = mCopyLumAlphaPS; break; default: UNREACHABLE(); ps = NULL; break; } mDeviceContext->PSSetShader(ps, NULL, 0); mDeviceContext->GSSetShader(NULL, NULL, 0); // Unset the currently bound shader resource to avoid conflicts static ID3D11ShaderResourceView *const nullSRV = NULL; mDeviceContext->PSSetShaderResources(0, 1, &nullSRV); // Apply render target setOneTimeRenderTarget(dest); // Set the viewport D3D11_VIEWPORT viewport; viewport.TopLeftX = 0; viewport.TopLeftY = 0; viewport.Width = destWidth; viewport.Height = destHeight; viewport.MinDepth = 0.0f; viewport.MaxDepth = 1.0f; mDeviceContext->RSSetViewports(1, &viewport); // Apply textures mDeviceContext->PSSetShaderResources(0, 1, &source); mDeviceContext->PSSetSamplers(0, 1, &mCopySampler); // Draw the quad mDeviceContext->Draw(4, 0); // Unbind textures and render targets and vertex buffer mDeviceContext->PSSetShaderResources(0, 1, &nullSRV); unapplyRenderTargets(); UINT zero = 0; ID3D11Buffer *const nullBuffer = NULL; mDeviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero); markAllStateDirty(); return true; } void Renderer11::unapplyRenderTargets() { setOneTimeRenderTarget(NULL); } void Renderer11::setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView) { ID3D11RenderTargetView *rtvArray[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS] = {NULL}; rtvArray[0] = renderTargetView; mDeviceContext->OMSetRenderTargets(getMaxRenderTargets(), rtvArray, NULL); // Do not preserve the serial for this one-time-use render target for (unsigned int rtIndex = 0; rtIndex < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; rtIndex++) { mAppliedRenderTargetSerials[rtIndex] = 0; } } RenderTarget *Renderer11::createRenderTarget(SwapChain *swapChain, bool depth) { SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain); RenderTarget11 *renderTarget = NULL; if (depth) { // Note: depth stencil may be NULL for 0 sized surfaces renderTarget = new RenderTarget11(this, swapChain11->getDepthStencil(), swapChain11->getDepthStencilTexture(), NULL, swapChain11->getWidth(), swapChain11->getHeight()); } else { // Note: render target may be NULL for 0 sized surfaces renderTarget = new RenderTarget11(this, swapChain11->getRenderTarget(), swapChain11->getOffscreenTexture(), swapChain11->getRenderTargetShaderResource(), swapChain11->getWidth(), swapChain11->getHeight()); } return renderTarget; } RenderTarget *Renderer11::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth) { RenderTarget11 *renderTarget = new RenderTarget11(this, width, height, format, samples, depth); return renderTarget; } ShaderExecutable *Renderer11::loadExecutable(const void *function, size_t length, rx::ShaderType type) { ShaderExecutable11 *executable = NULL; switch (type) { case rx::SHADER_VERTEX: { ID3D11VertexShader *vshader = NULL; HRESULT result = mDevice->CreateVertexShader(function, length, NULL, &vshader); ASSERT(SUCCEEDED(result)); if (vshader) { executable = new ShaderExecutable11(function, length, vshader); } } break; case rx::SHADER_PIXEL: { ID3D11PixelShader *pshader = NULL; HRESULT result = mDevice->CreatePixelShader(function, length, NULL, &pshader); ASSERT(SUCCEEDED(result)); if (pshader) { executable = new ShaderExecutable11(function, length, pshader); } } break; case rx::SHADER_GEOMETRY: { ID3D11GeometryShader *gshader = NULL; HRESULT result = mDevice->CreateGeometryShader(function, length, NULL, &gshader); ASSERT(SUCCEEDED(result)); if (gshader) { executable = new ShaderExecutable11(function, length, gshader); } } break; default: UNREACHABLE(); break; } return executable; } ShaderExecutable *Renderer11::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type) { const char *profile = NULL; switch (type) { case rx::SHADER_VERTEX: profile = "vs_4_0"; break; case rx::SHADER_PIXEL: profile = "ps_4_0"; break; case rx::SHADER_GEOMETRY: profile = "gs_4_0"; break; default: UNREACHABLE(); return NULL; } ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, D3DCOMPILE_OPTIMIZATION_LEVEL0, false); if (!binary) return NULL; ShaderExecutable *executable = loadExecutable((DWORD *)binary->GetBufferPointer(), binary->GetBufferSize(), type); binary->Release(); return executable; } VertexBuffer *Renderer11::createVertexBuffer() { return new VertexBuffer11(this); } IndexBuffer *Renderer11::createIndexBuffer() { return new IndexBuffer11(this); } BufferStorage *Renderer11::createBufferStorage() { return new BufferStorage11(this); } QueryImpl *Renderer11::createQuery(GLenum type) { return new Query11(this, type); } FenceImpl *Renderer11::createFence() { return new Fence11(this); } bool Renderer11::getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource) { ASSERT(colorbuffer != NULL); RenderTarget11 *renderTarget = RenderTarget11::makeRenderTarget11(colorbuffer->getRenderTarget()); if (renderTarget) { *subresourceIndex = renderTarget->getSubresourceIndex(); ID3D11RenderTargetView *colorBufferRTV = renderTarget->getRenderTargetView(); if (colorBufferRTV) { ID3D11Resource *textureResource = NULL; colorBufferRTV->GetResource(&textureResource); colorBufferRTV->Release(); if (textureResource) { HRESULT result = textureResource->QueryInterface(IID_ID3D11Texture2D, (void**)resource); textureResource->Release(); if (SUCCEEDED(result)) { return true; } else { ERR("Failed to extract the ID3D11Texture2D from the render target resource, " "HRESULT: 0x%X.", result); } } } } return false; } bool Renderer11::blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect, bool blitRenderTarget, bool blitDepthStencil) { if (blitRenderTarget) { gl::Renderbuffer *readBuffer = readTarget->getReadColorbuffer(); if (!readBuffer) { ERR("Failed to retrieve the read buffer from the read framebuffer."); return gl::error(GL_OUT_OF_MEMORY, false); } RenderTarget *readRenderTarget = readBuffer->getRenderTarget(); for (unsigned int colorAttachment = 0; colorAttachment < gl::IMPLEMENTATION_MAX_DRAW_BUFFERS; colorAttachment++) { if (drawTarget->isEnabledColorAttachment(colorAttachment)) { gl::Renderbuffer *drawBuffer = drawTarget->getColorbuffer(colorAttachment); if (!drawBuffer) { ERR("Failed to retrieve the draw buffer from the draw framebuffer."); return gl::error(GL_OUT_OF_MEMORY, false); } RenderTarget *drawRenderTarget = drawBuffer->getRenderTarget(); if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, false)) { return false; } } } } if (blitDepthStencil) { gl::Renderbuffer *readBuffer = readTarget->getDepthOrStencilbuffer(); gl::Renderbuffer *drawBuffer = drawTarget->getDepthOrStencilbuffer(); if (!readBuffer) { ERR("Failed to retrieve the read depth-stencil buffer from the read framebuffer."); return gl::error(GL_OUT_OF_MEMORY, false); } if (!drawBuffer) { ERR("Failed to retrieve the draw depth-stencil buffer from the draw framebuffer."); return gl::error(GL_OUT_OF_MEMORY, false); } RenderTarget *readRenderTarget = readBuffer->getDepthStencil(); RenderTarget *drawRenderTarget = drawBuffer->getDepthStencil(); if (!blitRenderbufferRect(readRect, drawRect, readRenderTarget, drawRenderTarget, true)) { return false; } } return true; } void Renderer11::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels) { ID3D11Texture2D *colorBufferTexture = NULL; unsigned int subresourceIndex = 0; gl::Renderbuffer *colorbuffer = framebuffer->getReadColorbuffer(); if (colorbuffer && getRenderTargetResource(colorbuffer, &subresourceIndex, &colorBufferTexture)) { gl::Rectangle area; area.x = x; area.y = y; area.width = width; area.height = height; readTextureData(colorBufferTexture, subresourceIndex, area, format, type, outputPitch, packReverseRowOrder, packAlignment, pixels); colorBufferTexture->Release(); colorBufferTexture = NULL; } } Image *Renderer11::createImage() { return new Image11(); } void Renderer11::generateMipmap(Image *dest, Image *src) { Image11 *dest11 = Image11::makeImage11(dest); Image11 *src11 = Image11::makeImage11(src); Image11::generateMipmap(dest11, src11); } TextureStorage *Renderer11::createTextureStorage2D(SwapChain *swapChain) { SwapChain11 *swapChain11 = SwapChain11::makeSwapChain11(swapChain); return new TextureStorage11_2D(this, swapChain11); } TextureStorage *Renderer11::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) { return new TextureStorage11_2D(this, levels, internalformat, usage, forceRenderable, width, height); } TextureStorage *Renderer11::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) { return new TextureStorage11_Cube(this, levels, internalformat, usage, forceRenderable, size); } static inline unsigned int getFastPixelCopySize(DXGI_FORMAT sourceFormat, GLenum destFormat, GLenum destType) { if (sourceFormat == DXGI_FORMAT_A8_UNORM && destFormat == GL_ALPHA && destType == GL_UNSIGNED_BYTE) { return 1; } else if (sourceFormat == DXGI_FORMAT_R8G8B8A8_UNORM && destFormat == GL_RGBA && destType == GL_UNSIGNED_BYTE) { return 4; } else if (sourceFormat == DXGI_FORMAT_B8G8R8A8_UNORM && destFormat == GL_BGRA_EXT && destType == GL_UNSIGNED_BYTE) { return 4; } else if (sourceFormat == DXGI_FORMAT_R16G16B16A16_FLOAT && destFormat == GL_RGBA && destType == GL_HALF_FLOAT_OES) { return 8; } else if (sourceFormat == DXGI_FORMAT_R32G32B32_FLOAT && destFormat == GL_RGB && destType == GL_FLOAT) { return 12; } else if (sourceFormat == DXGI_FORMAT_R32G32B32A32_FLOAT && destFormat == GL_RGBA && destType == GL_FLOAT) { return 16; } else { return 0; } } static inline void readPixelColor(const unsigned char *data, DXGI_FORMAT format, unsigned int x, unsigned int y, int inputPitch, gl::Color *outColor) { switch (format) { case DXGI_FORMAT_R8G8B8A8_UNORM: { unsigned int rgba = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch); outColor->red = (rgba & 0x000000FF) * (1.0f / 0x000000FF); outColor->green = (rgba & 0x0000FF00) * (1.0f / 0x0000FF00); outColor->blue = (rgba & 0x00FF0000) * (1.0f / 0x00FF0000); outColor->alpha = (rgba & 0xFF000000) * (1.0f / 0xFF000000); } break; case DXGI_FORMAT_A8_UNORM: { outColor->red = 0.0f; outColor->green = 0.0f; outColor->blue = 0.0f; outColor->alpha = *(data + x + y * inputPitch) / 255.0f; } break; case DXGI_FORMAT_R32G32B32A32_FLOAT: { outColor->red = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 0); outColor->green = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 1); outColor->blue = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 2); outColor->alpha = *(reinterpret_cast<const float*>(data + 16 * x + y * inputPitch) + 3); } break; case DXGI_FORMAT_R32G32B32_FLOAT: { outColor->red = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 0); outColor->green = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 1); outColor->blue = *(reinterpret_cast<const float*>(data + 12 * x + y * inputPitch) + 2); outColor->alpha = 1.0f; } break; case DXGI_FORMAT_R16G16B16A16_FLOAT: { outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 0)); outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 1)); outColor->blue = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 2)); outColor->alpha = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 8 * x + y * inputPitch) + 3)); } break; case DXGI_FORMAT_B8G8R8A8_UNORM: { unsigned int bgra = *reinterpret_cast<const unsigned int*>(data + 4 * x + y * inputPitch); outColor->red = (bgra & 0x00FF0000) * (1.0f / 0x00FF0000); outColor->blue = (bgra & 0x000000FF) * (1.0f / 0x000000FF); outColor->green = (bgra & 0x0000FF00) * (1.0f / 0x0000FF00); outColor->alpha = (bgra & 0xFF000000) * (1.0f / 0xFF000000); } break; case DXGI_FORMAT_R8_UNORM: { outColor->red = *(data + x + y * inputPitch) / 255.0f; outColor->green = 0.0f; outColor->blue = 0.0f; outColor->alpha = 1.0f; } break; case DXGI_FORMAT_R8G8_UNORM: { unsigned short rg = *reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch); outColor->red = (rg & 0xFF00) * (1.0f / 0xFF00); outColor->green = (rg & 0x00FF) * (1.0f / 0x00FF); outColor->blue = 0.0f; outColor->alpha = 1.0f; } break; case DXGI_FORMAT_R16_FLOAT: { outColor->red = gl::float16ToFloat32(*reinterpret_cast<const unsigned short*>(data + 2 * x + y * inputPitch)); outColor->green = 0.0f; outColor->blue = 0.0f; outColor->alpha = 1.0f; } break; case DXGI_FORMAT_R16G16_FLOAT: { outColor->red = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 0)); outColor->green = gl::float16ToFloat32(*(reinterpret_cast<const unsigned short*>(data + 4 * x + y * inputPitch) + 1)); outColor->blue = 0.0f; outColor->alpha = 1.0f; } break; default: ERR("ReadPixelColor not implemented for DXGI format %u.", format); UNIMPLEMENTED(); break; } } static inline void writePixelColor(const gl::Color &color, GLenum format, GLenum type, unsigned int x, unsigned int y, int outputPitch, void *outData) { unsigned char* byteData = reinterpret_cast<unsigned char*>(outData); unsigned short* shortData = reinterpret_cast<unsigned short*>(outData); switch (format) { case GL_RGBA: switch (type) { case GL_UNSIGNED_BYTE: byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f); byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f); byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f); break; default: ERR("WritePixelColor not implemented for format GL_RGBA and type 0x%X.", type); UNIMPLEMENTED(); break; } break; case GL_BGRA_EXT: switch (type) { case GL_UNSIGNED_BYTE: byteData[4 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.blue + 0.5f); byteData[4 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); byteData[4 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.red + 0.5f); byteData[4 * x + y * outputPitch + 3] = static_cast<unsigned char>(255 * color.alpha + 0.5f); break; case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section // this type is packed as follows: // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 // -------------------------------------------------------------------------------- // | 4th | 3rd | 2nd | 1st component | // -------------------------------------------------------------------------------- // in the case of BGRA_EXT, B is the first component, G the second, and so forth. shortData[x + y * outputPitch / sizeof(unsigned short)] = (static_cast<unsigned short>(15 * color.alpha + 0.5f) << 12) | (static_cast<unsigned short>(15 * color.red + 0.5f) << 8) | (static_cast<unsigned short>(15 * color.green + 0.5f) << 4) | (static_cast<unsigned short>(15 * color.blue + 0.5f) << 0); break; case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section // this type is packed as follows: // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 // -------------------------------------------------------------------------------- // | 4th | 3rd | 2nd | 1st component | // -------------------------------------------------------------------------------- // in the case of BGRA_EXT, B is the first component, G the second, and so forth. shortData[x + y * outputPitch / sizeof(unsigned short)] = (static_cast<unsigned short>( color.alpha + 0.5f) << 15) | (static_cast<unsigned short>(31 * color.red + 0.5f) << 10) | (static_cast<unsigned short>(31 * color.green + 0.5f) << 5) | (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0); break; default: ERR("WritePixelColor not implemented for format GL_BGRA_EXT and type 0x%X.", type); UNIMPLEMENTED(); break; } break; case GL_RGB: switch (type) { case GL_UNSIGNED_SHORT_5_6_5: shortData[x + y * outputPitch / sizeof(unsigned short)] = (static_cast<unsigned short>(31 * color.blue + 0.5f) << 0) | (static_cast<unsigned short>(63 * color.green + 0.5f) << 5) | (static_cast<unsigned short>(31 * color.red + 0.5f) << 11); break; case GL_UNSIGNED_BYTE: byteData[3 * x + y * outputPitch + 0] = static_cast<unsigned char>(255 * color.red + 0.5f); byteData[3 * x + y * outputPitch + 1] = static_cast<unsigned char>(255 * color.green + 0.5f); byteData[3 * x + y * outputPitch + 2] = static_cast<unsigned char>(255 * color.blue + 0.5f); break; default: ERR("WritePixelColor not implemented for format GL_RGB and type 0x%X.", type); UNIMPLEMENTED(); break; } break; default: ERR("WritePixelColor not implemented for format 0x%X.", format); UNIMPLEMENTED(); break; } } void Renderer11::readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void *pixels) { D3D11_TEXTURE2D_DESC textureDesc; texture->GetDesc(&textureDesc); D3D11_TEXTURE2D_DESC stagingDesc; stagingDesc.Width = area.width; stagingDesc.Height = area.height; stagingDesc.MipLevels = 1; stagingDesc.ArraySize = 1; stagingDesc.Format = textureDesc.Format; stagingDesc.SampleDesc.Count = 1; stagingDesc.SampleDesc.Quality = 0; stagingDesc.Usage = D3D11_USAGE_STAGING; stagingDesc.BindFlags = 0; stagingDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ; stagingDesc.MiscFlags = 0; ID3D11Texture2D* stagingTex = NULL; HRESULT result = mDevice->CreateTexture2D(&stagingDesc, NULL, &stagingTex); if (FAILED(result)) { ERR("Failed to create staging texture for readPixels, HRESULT: 0x%X.", result); return; } ID3D11Texture2D* srcTex = NULL; if (textureDesc.SampleDesc.Count > 1) { D3D11_TEXTURE2D_DESC resolveDesc; resolveDesc.Width = textureDesc.Width; resolveDesc.Height = textureDesc.Height; resolveDesc.MipLevels = 1; resolveDesc.ArraySize = 1; resolveDesc.Format = textureDesc.Format; resolveDesc.SampleDesc.Count = 1; resolveDesc.SampleDesc.Quality = 0; resolveDesc.Usage = D3D11_USAGE_DEFAULT; resolveDesc.BindFlags = 0; resolveDesc.CPUAccessFlags = 0; resolveDesc.MiscFlags = 0; result = mDevice->CreateTexture2D(&resolveDesc, NULL, &srcTex); if (FAILED(result)) { ERR("Failed to create resolve texture for readPixels, HRESULT: 0x%X.", result); stagingTex->Release(); return; } mDeviceContext->ResolveSubresource(srcTex, 0, texture, subResource, textureDesc.Format); subResource = 0; } else { srcTex = texture; srcTex->AddRef(); } D3D11_BOX srcBox; srcBox.left = area.x; srcBox.right = area.x + area.width; srcBox.top = area.y; srcBox.bottom = area.y + area.height; srcBox.front = 0; srcBox.back = 1; mDeviceContext->CopySubresourceRegion(stagingTex, 0, 0, 0, 0, srcTex, subResource, &srcBox); srcTex->Release(); srcTex = NULL; D3D11_MAPPED_SUBRESOURCE mapping; mDeviceContext->Map(stagingTex, 0, D3D11_MAP_READ, 0, &mapping); unsigned char *source; int inputPitch; if (packReverseRowOrder) { source = static_cast<unsigned char*>(mapping.pData) + mapping.RowPitch * (area.height - 1); inputPitch = -static_cast<int>(mapping.RowPitch); } else { source = static_cast<unsigned char*>(mapping.pData); inputPitch = static_cast<int>(mapping.RowPitch); } unsigned int fastPixelSize = getFastPixelCopySize(textureDesc.Format, format, type); if (fastPixelSize != 0) { unsigned char *dest = static_cast<unsigned char*>(pixels); for (int j = 0; j < area.height; j++) { memcpy(dest + j * outputPitch, source + j * inputPitch, area.width * fastPixelSize); } } else if (textureDesc.Format == DXGI_FORMAT_B8G8R8A8_UNORM && format == GL_RGBA && type == GL_UNSIGNED_BYTE) { // Fast path for swapping red with blue unsigned char *dest = static_cast<unsigned char*>(pixels); for (int j = 0; j < area.height; j++) { for (int i = 0; i < area.width; i++) { unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); *(unsigned int*)(dest + 4 * i + j * outputPitch) = (argb & 0xFF00FF00) | // Keep alpha and green (argb & 0x00FF0000) >> 16 | // Move red to blue (argb & 0x000000FF) << 16; // Move blue to red } } } else { gl::Color pixelColor; for (int j = 0; j < area.height; j++) { for (int i = 0; i < area.width; i++) { readPixelColor(source, textureDesc.Format, i, j, inputPitch, &pixelColor); writePixelColor(pixelColor, format, type, i, j, outputPitch, pixels); } } } mDeviceContext->Unmap(stagingTex, 0); stagingTex->Release(); stagingTex = NULL; } bool Renderer11::blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget, RenderTarget *drawRenderTarget, bool wholeBufferCopy) { ASSERT(readRect.width == drawRect.width && readRect.height == drawRect.height); RenderTarget11 *readRenderTarget11 = RenderTarget11::makeRenderTarget11(readRenderTarget); if (!readRenderTarget) { ERR("Failed to retrieve the read render target from the read framebuffer."); return gl::error(GL_OUT_OF_MEMORY, false); } ID3D11Texture2D *readTexture = NULL; unsigned int readSubresource = 0; if (readRenderTarget->getSamples() > 0) { ID3D11Texture2D *unresolvedTexture = readRenderTarget11->getTexture(); readTexture = resolveMultisampledTexture(unresolvedTexture, readRenderTarget11->getSubresourceIndex()); readSubresource = 0; unresolvedTexture->Release(); } else { readTexture = readRenderTarget11->getTexture(); readSubresource = readRenderTarget11->getSubresourceIndex(); } if (!readTexture) { ERR("Failed to retrieve the read render target view from the read render target."); return gl::error(GL_OUT_OF_MEMORY, false); } RenderTarget11 *drawRenderTarget11 = RenderTarget11::makeRenderTarget11(drawRenderTarget); if (!drawRenderTarget) { readTexture->Release(); ERR("Failed to retrieve the draw render target from the draw framebuffer."); return gl::error(GL_OUT_OF_MEMORY, false); } ID3D11Texture2D *drawTexture = drawRenderTarget11->getTexture(); unsigned int drawSubresource = drawRenderTarget11->getSubresourceIndex(); D3D11_BOX readBox; readBox.left = readRect.x; readBox.right = readRect.x + readRect.width; readBox.top = readRect.y; readBox.bottom = readRect.y + readRect.height; readBox.front = 0; readBox.back = 1; // D3D11 needs depth-stencil CopySubresourceRegions to have a NULL pSrcBox // We also require complete framebuffer copies for depth-stencil blit. D3D11_BOX *pSrcBox = wholeBufferCopy ? NULL : &readBox; mDeviceContext->CopySubresourceRegion(drawTexture, drawSubresource, drawRect.x, drawRect.y, 0, readTexture, readSubresource, pSrcBox); readTexture->Release(); drawTexture->Release(); return true; } ID3D11Texture2D *Renderer11::resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource) { D3D11_TEXTURE2D_DESC textureDesc; source->GetDesc(&textureDesc); if (textureDesc.SampleDesc.Count > 1) { D3D11_TEXTURE2D_DESC resolveDesc; resolveDesc.Width = textureDesc.Width; resolveDesc.Height = textureDesc.Height; resolveDesc.MipLevels = 1; resolveDesc.ArraySize = 1; resolveDesc.Format = textureDesc.Format; resolveDesc.SampleDesc.Count = 1; resolveDesc.SampleDesc.Quality = 0; resolveDesc.Usage = textureDesc.Usage; resolveDesc.BindFlags = textureDesc.BindFlags; resolveDesc.CPUAccessFlags = 0; resolveDesc.MiscFlags = 0; ID3D11Texture2D *resolveTexture = NULL; HRESULT result = mDevice->CreateTexture2D(&resolveDesc, NULL, &resolveTexture); if (FAILED(result)) { ERR("Failed to create a multisample resolve texture, HRESULT: 0x%X.", result); return NULL; } mDeviceContext->ResolveSubresource(resolveTexture, 0, source, subresource, textureDesc.Format); return resolveTexture; } else { source->AddRef(); return source; } } bool Renderer11::getLUID(LUID *adapterLuid) const { adapterLuid->HighPart = 0; adapterLuid->LowPart = 0; if (!mDxgiAdapter) { return false; } DXGI_ADAPTER_DESC adapterDesc; if (FAILED(mDxgiAdapter->GetDesc(&adapterDesc))) { return false; } *adapterLuid = adapterDesc.AdapterLuid; return true; } }
C++
#include "precompiled.h" // // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // RenderTarget11.cpp: Implements a DX11-specific wrapper for ID3D11View pointers // retained by Renderbuffers. #include "libGLESv2/renderer/RenderTarget11.h" #include "libGLESv2/renderer/Renderer11.h" #include "libGLESv2/renderer/renderer11_utils.h" #include "libGLESv2/main.h" namespace rx { static unsigned int getRTVSubresourceIndex(ID3D11Texture2D *texture, ID3D11RenderTargetView *view) { D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; view->GetDesc(&rtvDesc); D3D11_TEXTURE2D_DESC texDesc; texture->GetDesc(&texDesc); unsigned int mipSlice = 0; unsigned int arraySlice = 0; unsigned int mipLevels = texDesc.MipLevels; switch (rtvDesc.ViewDimension) { case D3D11_RTV_DIMENSION_TEXTURE1D: mipSlice = rtvDesc.Texture1D.MipSlice; arraySlice = 0; break; case D3D11_RTV_DIMENSION_TEXTURE1DARRAY: mipSlice = rtvDesc.Texture1DArray.MipSlice; arraySlice = rtvDesc.Texture1DArray.FirstArraySlice; break; case D3D11_RTV_DIMENSION_TEXTURE2D: mipSlice = rtvDesc.Texture2D.MipSlice; arraySlice = 0; break; case D3D11_RTV_DIMENSION_TEXTURE2DARRAY: mipSlice = rtvDesc.Texture2DArray.MipSlice; arraySlice = rtvDesc.Texture2DArray.FirstArraySlice; break; case D3D11_RTV_DIMENSION_TEXTURE2DMS: mipSlice = 0; arraySlice = 0; break; case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY: mipSlice = 0; arraySlice = rtvDesc.Texture2DMSArray.FirstArraySlice; break; case D3D11_RTV_DIMENSION_TEXTURE3D: mipSlice = rtvDesc.Texture3D.MipSlice; arraySlice = 0; break; case D3D11_RTV_DIMENSION_UNKNOWN: case D3D11_RTV_DIMENSION_BUFFER: UNIMPLEMENTED(); break; default: UNREACHABLE(); break; } return D3D11CalcSubresource(mipSlice, arraySlice, mipLevels); } static unsigned int getDSVSubresourceIndex(ID3D11Texture2D *texture, ID3D11DepthStencilView *view) { D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc; view->GetDesc(&dsvDesc); D3D11_TEXTURE2D_DESC texDesc; texture->GetDesc(&texDesc); unsigned int mipSlice = 0; unsigned int arraySlice = 0; unsigned int mipLevels = texDesc.MipLevels; switch (dsvDesc.ViewDimension) { case D3D11_DSV_DIMENSION_TEXTURE1D: mipSlice = dsvDesc.Texture1D.MipSlice; arraySlice = 0; break; case D3D11_DSV_DIMENSION_TEXTURE1DARRAY: mipSlice = dsvDesc.Texture1DArray.MipSlice; arraySlice = dsvDesc.Texture1DArray.FirstArraySlice; break; case D3D11_DSV_DIMENSION_TEXTURE2D: mipSlice = dsvDesc.Texture2D.MipSlice; arraySlice = 0; break; case D3D11_DSV_DIMENSION_TEXTURE2DARRAY: mipSlice = dsvDesc.Texture2DArray.MipSlice; arraySlice = dsvDesc.Texture2DArray.FirstArraySlice; break; case D3D11_DSV_DIMENSION_TEXTURE2DMS: mipSlice = 0; arraySlice = 0; break; case D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY: mipSlice = 0; arraySlice = dsvDesc.Texture2DMSArray.FirstArraySlice; break; case D3D11_RTV_DIMENSION_UNKNOWN: UNIMPLEMENTED(); break; default: UNREACHABLE(); break; } return D3D11CalcSubresource(mipSlice, arraySlice, mipLevels); } RenderTarget11::RenderTarget11(Renderer *renderer, ID3D11RenderTargetView *rtv, ID3D11Texture2D *tex, ID3D11ShaderResourceView *srv, GLsizei width, GLsizei height) { mRenderer = Renderer11::makeRenderer11(renderer); mTexture = tex; mRenderTarget = rtv; mDepthStencil = NULL; mShaderResource = srv; mSubresourceIndex = 0; if (mRenderTarget && mTexture) { D3D11_RENDER_TARGET_VIEW_DESC desc; mRenderTarget->GetDesc(&desc); D3D11_TEXTURE2D_DESC texDesc; mTexture->GetDesc(&texDesc); mSubresourceIndex = getRTVSubresourceIndex(mTexture, mRenderTarget); mWidth = width; mHeight = height; mSamples = (texDesc.SampleDesc.Count > 1) ? texDesc.SampleDesc.Count : 0; mInternalFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format); mActualFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format); } } RenderTarget11::RenderTarget11(Renderer *renderer, ID3D11DepthStencilView *dsv, ID3D11Texture2D *tex, ID3D11ShaderResourceView *srv, GLsizei width, GLsizei height) { mRenderer = Renderer11::makeRenderer11(renderer); mTexture = tex; mRenderTarget = NULL; mDepthStencil = dsv; mShaderResource = srv; mSubresourceIndex = 0; if (mDepthStencil && mTexture) { D3D11_DEPTH_STENCIL_VIEW_DESC desc; mDepthStencil->GetDesc(&desc); D3D11_TEXTURE2D_DESC texDesc; mTexture->GetDesc(&texDesc); mSubresourceIndex = getDSVSubresourceIndex(mTexture, mDepthStencil); mWidth = width; mHeight = height; mSamples = (texDesc.SampleDesc.Count > 1) ? texDesc.SampleDesc.Count : 0; mInternalFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format); mActualFormat = d3d11_gl::ConvertTextureInternalFormat(desc.Format); } } RenderTarget11::RenderTarget11(Renderer *renderer, GLsizei width, GLsizei height, GLenum format, GLsizei samples, bool depth) { mRenderer = Renderer11::makeRenderer11(renderer); mTexture = NULL; mRenderTarget = NULL; mDepthStencil = NULL; mShaderResource = NULL; DXGI_FORMAT requestedFormat = gl_d3d11::ConvertRenderbufferFormat(format); int supportedSamples = mRenderer->getNearestSupportedSamples(requestedFormat, samples); if (supportedSamples < 0) { gl::error(GL_OUT_OF_MEMORY); return; } if (width > 0 && height > 0) { // Create texture resource D3D11_TEXTURE2D_DESC desc; desc.Width = width; desc.Height = height; desc.MipLevels = 1; desc.ArraySize = 1; desc.Format = requestedFormat; desc.SampleDesc.Count = (supportedSamples == 0) ? 1 : supportedSamples; desc.SampleDesc.Quality = 0; desc.Usage = D3D11_USAGE_DEFAULT; desc.CPUAccessFlags = 0; desc.MiscFlags = 0; desc.BindFlags = (depth ? D3D11_BIND_DEPTH_STENCIL : (D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE)); ID3D11Device *device = mRenderer->getDevice(); HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); if (result == E_OUTOFMEMORY) { gl::error(GL_OUT_OF_MEMORY); return; } ASSERT(SUCCEEDED(result)); if (depth) { D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc; dsvDesc.Format = requestedFormat; dsvDesc.ViewDimension = (supportedSamples == 0) ? D3D11_DSV_DIMENSION_TEXTURE2D : D3D11_DSV_DIMENSION_TEXTURE2DMS; dsvDesc.Texture2D.MipSlice = 0; dsvDesc.Flags = 0; result = device->CreateDepthStencilView(mTexture, &dsvDesc, &mDepthStencil); if (result == E_OUTOFMEMORY) { mTexture->Release(); mTexture = NULL; gl::error(GL_OUT_OF_MEMORY); } ASSERT(SUCCEEDED(result)); } else { D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; rtvDesc.Format = requestedFormat; rtvDesc.ViewDimension = (supportedSamples == 0) ? D3D11_RTV_DIMENSION_TEXTURE2D : D3D11_RTV_DIMENSION_TEXTURE2DMS; rtvDesc.Texture2D.MipSlice = 0; result = device->CreateRenderTargetView(mTexture, &rtvDesc, &mRenderTarget); if (result == E_OUTOFMEMORY) { mTexture->Release(); mTexture = NULL; gl::error(GL_OUT_OF_MEMORY); return; } ASSERT(SUCCEEDED(result)); D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; srvDesc.Format = requestedFormat; srvDesc.ViewDimension = (supportedSamples == 0) ? D3D11_SRV_DIMENSION_TEXTURE2D : D3D11_SRV_DIMENSION_TEXTURE2DMS; srvDesc.Texture2D.MostDetailedMip = 0; srvDesc.Texture2D.MipLevels = 1; result = device->CreateShaderResourceView(mTexture, &srvDesc, &mShaderResource); if (result == E_OUTOFMEMORY) { mTexture->Release(); mTexture = NULL; mRenderTarget->Release(); mRenderTarget = NULL; gl::error(GL_OUT_OF_MEMORY); return; } ASSERT(SUCCEEDED(result)); } } mWidth = width; mHeight = height; mInternalFormat = format; mSamples = supportedSamples; mActualFormat = d3d11_gl::ConvertTextureInternalFormat(requestedFormat); mSubresourceIndex = D3D11CalcSubresource(0, 0, 1); } RenderTarget11::~RenderTarget11() { if (mTexture) { mTexture->Release(); mTexture = NULL; } if (mRenderTarget) { mRenderTarget->Release(); mRenderTarget = NULL; } if (mDepthStencil) { mDepthStencil->Release(); mDepthStencil = NULL; } if (mShaderResource) { mShaderResource->Release(); mShaderResource = NULL; } } RenderTarget11 *RenderTarget11::makeRenderTarget11(RenderTarget *target) { ASSERT(HAS_DYNAMIC_TYPE(rx::RenderTarget11*, target)); return static_cast<rx::RenderTarget11*>(target); } ID3D11Texture2D *RenderTarget11::getTexture() const { if (mTexture) { mTexture->AddRef(); } return mTexture; } // Adds reference, caller must call Release ID3D11RenderTargetView *RenderTarget11::getRenderTargetView() const { if (mRenderTarget) { mRenderTarget->AddRef(); } return mRenderTarget; } // Adds reference, caller must call Release ID3D11DepthStencilView *RenderTarget11::getDepthStencilView() const { if (mDepthStencil) { mDepthStencil->AddRef(); } return mDepthStencil; } // Adds reference, caller must call Release ID3D11ShaderResourceView *RenderTarget11::getShaderResourceView() const { if (mShaderResource) { mShaderResource->AddRef(); } return mShaderResource; } unsigned int RenderTarget11::getSubresourceIndex() const { return mSubresourceIndex; } }
C++
// // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // generatemip.h: Defines the GenerateMip function, templated on the format // type of the image for which mip levels are being generated. #ifndef LIBGLESV2_RENDERER_GENERATEMIP_H_ #define LIBGLESV2_RENDERER_GENERATEMIP_H_ #include "libGLESv2/mathutil.h" namespace rx { struct L8 { unsigned char L; static void average(L8 *dst, const L8 *src1, const L8 *src2) { dst->L = ((src1->L ^ src2->L) >> 1) + (src1->L & src2->L); } }; typedef L8 R8; // R8 type is functionally equivalent for mip purposes typedef L8 A8; // A8 type is functionally equivalent for mip purposes struct A8L8 { unsigned char L; unsigned char A; static void average(A8L8 *dst, const A8L8 *src1, const A8L8 *src2) { *(unsigned short*)dst = (((*(unsigned short*)src1 ^ *(unsigned short*)src2) & 0xFEFE) >> 1) + (*(unsigned short*)src1 & *(unsigned short*)src2); } }; typedef A8L8 R8G8; // R8G8 type is functionally equivalent for mip purposes struct A8R8G8B8 { unsigned char B; unsigned char G; unsigned char R; unsigned char A; static void average(A8R8G8B8 *dst, const A8R8G8B8 *src1, const A8R8G8B8 *src2) { *(unsigned int*)dst = (((*(unsigned int*)src1 ^ *(unsigned int*)src2) & 0xFEFEFEFE) >> 1) + (*(unsigned int*)src1 & *(unsigned int*)src2); } }; typedef A8R8G8B8 R8G8B8A8; // R8G8B8A8 type is functionally equivalent for mip purposes struct A16B16G16R16F { unsigned short R; unsigned short G; unsigned short B; unsigned short A; static void average(A16B16G16R16F *dst, const A16B16G16R16F *src1, const A16B16G16R16F *src2) { dst->R = gl::float32ToFloat16((gl::float16ToFloat32(src1->R) + gl::float16ToFloat32(src2->R)) * 0.5f); dst->G = gl::float32ToFloat16((gl::float16ToFloat32(src1->G) + gl::float16ToFloat32(src2->G)) * 0.5f); dst->B = gl::float32ToFloat16((gl::float16ToFloat32(src1->B) + gl::float16ToFloat32(src2->B)) * 0.5f); dst->A = gl::float32ToFloat16((gl::float16ToFloat32(src1->A) + gl::float16ToFloat32(src2->A)) * 0.5f); } }; struct R16F { unsigned short R; static void average(R16F *dst, const R16F *src1, const R16F *src2) { dst->R = gl::float32ToFloat16((gl::float16ToFloat32(src1->R) + gl::float16ToFloat32(src2->R)) * 0.5f); } }; struct R16G16F { unsigned short R; unsigned short G; static void average(R16G16F *dst, const R16G16F *src1, const R16G16F *src2) { dst->R = gl::float32ToFloat16((gl::float16ToFloat32(src1->R) + gl::float16ToFloat32(src2->R)) * 0.5f); dst->G = gl::float32ToFloat16((gl::float16ToFloat32(src1->G) + gl::float16ToFloat32(src2->G)) * 0.5f); } }; struct A32B32G32R32F { float R; float G; float B; float A; static void average(A32B32G32R32F *dst, const A32B32G32R32F *src1, const A32B32G32R32F *src2) { dst->R = (src1->R + src2->R) * 0.5f; dst->G = (src1->G + src2->G) * 0.5f; dst->B = (src1->B + src2->B) * 0.5f; dst->A = (src1->A + src2->A) * 0.5f; } }; struct R32F { float R; static void average(R32F *dst, const R32F *src1, const R32F *src2) { dst->R = (src1->R + src2->R) * 0.5f; } }; struct R32G32F { float R; float G; static void average(R32G32F *dst, const R32G32F *src1, const R32G32F *src2) { dst->R = (src1->R + src2->R) * 0.5f; dst->G = (src1->G + src2->G) * 0.5f; } }; struct R32G32B32F { float R; float G; float B; static void average(R32G32B32F *dst, const R32G32B32F *src1, const R32G32B32F *src2) { dst->R = (src1->R + src2->R) * 0.5f; dst->G = (src1->G + src2->G) * 0.5f; dst->B = (src1->B + src2->B) * 0.5f; } }; template <typename T> static void GenerateMip(unsigned int sourceWidth, unsigned int sourceHeight, const unsigned char *sourceData, int sourcePitch, unsigned char *destData, int destPitch) { unsigned int mipWidth = std::max(1U, sourceWidth >> 1); unsigned int mipHeight = std::max(1U, sourceHeight >> 1); if (sourceHeight == 1) { ASSERT(sourceWidth != 1); const T *src = (const T*)sourceData; T *dst = (T*)destData; for (unsigned int x = 0; x < mipWidth; x++) { T::average(&dst[x], &src[x * 2], &src[x * 2 + 1]); } } else if (sourceWidth == 1) { ASSERT(sourceHeight != 1); for (unsigned int y = 0; y < mipHeight; y++) { const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch); const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch); T *dst = (T*)(destData + y * destPitch); T::average(dst, src0, src1); } } else { for (unsigned int y = 0; y < mipHeight; y++) { const T *src0 = (const T*)(sourceData + y * 2 * sourcePitch); const T *src1 = (const T*)(sourceData + y * 2 * sourcePitch + sourcePitch); T *dst = (T*)(destData + y * destPitch); for (unsigned int x = 0; x < mipWidth; x++) { T tmp0; T tmp1; T::average(&tmp0, &src0[x * 2], &src0[x * 2 + 1]); T::average(&tmp1, &src1[x * 2], &src1[x * 2 + 1]); T::average(&dst[x], &tmp0, &tmp1); } } } } } #endif // LIBGLESV2_RENDERER_GENERATEMIP_H_
C++
#include "precompiled.h" // // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Indexffer9.cpp: Defines the D3D9 IndexBuffer implementation. #include "libGLESv2/renderer/IndexBuffer9.h" #include "libGLESv2/renderer/Renderer9.h" namespace rx { IndexBuffer9::IndexBuffer9(Renderer9 *const renderer) : mRenderer(renderer) { mIndexBuffer = NULL; mBufferSize = 0; mIndexType = 0; mDynamic = false; } IndexBuffer9::~IndexBuffer9() { if (mIndexBuffer) { mIndexBuffer->Release(); mIndexBuffer = NULL; } } bool IndexBuffer9::initialize(unsigned int bufferSize, GLenum indexType, bool dynamic) { if (mIndexBuffer) { mIndexBuffer->Release(); mIndexBuffer = NULL; } updateSerial(); if (bufferSize > 0) { D3DFORMAT format; if (indexType == GL_UNSIGNED_SHORT || indexType == GL_UNSIGNED_BYTE) { format = D3DFMT_INDEX16; } else if (indexType == GL_UNSIGNED_INT) { if (mRenderer->get32BitIndexSupport()) { format = D3DFMT_INDEX32; } else { ERR("Attempted to create a 32-bit index buffer but renderer does not support 32-bit indices."); return false; } } else { ERR("Invalid index type %u.", indexType); return false; } DWORD usageFlags = D3DUSAGE_WRITEONLY; if (dynamic) { usageFlags |= D3DUSAGE_DYNAMIC; } HRESULT result = mRenderer->createIndexBuffer(bufferSize, usageFlags, format, &mIndexBuffer); if (FAILED(result)) { ERR("Failed to create an index buffer of size %u, result: 0x%08x.", mBufferSize, result); return false; } } mBufferSize = bufferSize; mIndexType = indexType; mDynamic = dynamic; return true; } IndexBuffer9 *IndexBuffer9::makeIndexBuffer9(IndexBuffer *indexBuffer) { ASSERT(HAS_DYNAMIC_TYPE(IndexBuffer9*, indexBuffer)); return static_cast<IndexBuffer9*>(indexBuffer); } bool IndexBuffer9::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory) { if (mIndexBuffer) { DWORD lockFlags = mDynamic ? D3DLOCK_NOOVERWRITE : 0; void *mapPtr = NULL; HRESULT result = mIndexBuffer->Lock(offset, size, &mapPtr, lockFlags); if (FAILED(result)) { ERR("Index buffer lock failed with error 0x%08x", result); return false; } *outMappedMemory = mapPtr; return true; } else { ERR("Index buffer not initialized."); return false; } } bool IndexBuffer9::unmapBuffer() { if (mIndexBuffer) { HRESULT result = mIndexBuffer->Unlock(); if (FAILED(result)) { ERR("Index buffer unlock failed with error 0x%08x", result); return false; } return true; } else { ERR("Index buffer not initialized."); return false; } } GLenum IndexBuffer9::getIndexType() const { return mIndexType; } unsigned int IndexBuffer9::getBufferSize() const { return mBufferSize; } bool IndexBuffer9::setSize(unsigned int bufferSize, GLenum indexType) { if (bufferSize > mBufferSize || indexType != mIndexType) { return initialize(bufferSize, indexType, mDynamic); } else { return true; } } bool IndexBuffer9::discard() { if (mIndexBuffer) { void *dummy; HRESULT result; result = mIndexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD); if (FAILED(result)) { ERR("Discard lock failed with error 0x%08x", result); return false; } result = mIndexBuffer->Unlock(); if (FAILED(result)) { ERR("Discard unlock failed with error 0x%08x", result); return false; } return true; } else { ERR("Index buffer not initialized."); return false; } } D3DFORMAT IndexBuffer9::getIndexFormat() const { switch (mIndexType) { case GL_UNSIGNED_BYTE: return D3DFMT_INDEX16; case GL_UNSIGNED_SHORT: return D3DFMT_INDEX16; case GL_UNSIGNED_INT: return D3DFMT_INDEX32; default: UNREACHABLE(); return D3DFMT_UNKNOWN; } } IDirect3DIndexBuffer9 * IndexBuffer9::getBuffer() const { return mIndexBuffer; } }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Image11.h: Defines the rx::Image11 class, which acts as the interface to // the actual underlying resources of a Texture #ifndef LIBGLESV2_RENDERER_IMAGE11_H_ #define LIBGLESV2_RENDERER_IMAGE11_H_ #include "libGLESv2/renderer/Image.h" #include "common/debug.h" namespace gl { class Framebuffer; } namespace rx { class Renderer; class Renderer11; class TextureStorageInterface2D; class TextureStorageInterfaceCube; class Image11 : public Image { public: Image11(); virtual ~Image11(); static Image11 *makeImage11(Image *img); static void generateMipmap(Image11 *dest, Image11 *src); virtual bool isDirty() const; virtual bool updateSurface(TextureStorageInterface2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height); virtual bool updateSurface(TextureStorageInterfaceCube *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height); virtual bool redefine(Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease); virtual bool isRenderableFormat() const; DXGI_FORMAT getDXGIFormat() const; virtual void loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLint unpackAlignment, const void *input); virtual void loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, const void *input); virtual void copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source); protected: HRESULT map(D3D11_MAPPED_SUBRESOURCE *map); void unmap(); private: DISALLOW_COPY_AND_ASSIGN(Image11); ID3D11Texture2D *getStagingTexture(); unsigned int getStagingSubresource(); void createStagingTexture(); Renderer11 *mRenderer; DXGI_FORMAT mDXGIFormat; ID3D11Texture2D *mStagingTexture; unsigned int mStagingSubresource; }; } #endif // LIBGLESV2_RENDERER_IMAGE11_H_
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // TextureStorage9.h: Defines the abstract rx::TextureStorage9 class and its concrete derived // classes TextureStorage9_2D and TextureStorage9_Cube, which act as the interface to the // D3D9 texture. #ifndef LIBGLESV2_RENDERER_TEXTURESTORAGE9_H_ #define LIBGLESV2_RENDERER_TEXTURESTORAGE9_H_ #include "libGLESv2/renderer/TextureStorage.h" #include "common/debug.h" namespace rx { class Renderer9; class SwapChain9; class RenderTarget; class RenderTarget9; class Blit; class TextureStorage9 : public TextureStorage { public: TextureStorage9(Renderer *renderer, DWORD usage); virtual ~TextureStorage9(); static TextureStorage9 *makeTextureStorage9(TextureStorage *storage); static DWORD GetTextureUsage(D3DFORMAT d3dfmt, GLenum glusage, bool forceRenderable); static bool IsTextureFormatRenderable(D3DFORMAT format); D3DPOOL getPool() const; DWORD getUsage() const; virtual IDirect3DBaseTexture9 *getBaseTexture() const = 0; virtual RenderTarget *getRenderTarget() { return NULL; } virtual RenderTarget *getRenderTarget(GLenum faceTarget) { return NULL; } virtual void generateMipmap(int level) {}; virtual void generateMipmap(int face, int level) {}; virtual int getLodOffset() const; virtual bool isRenderTarget() const; virtual bool isManaged() const; virtual int levelCount(); protected: int mLodOffset; Renderer9 *mRenderer; private: DISALLOW_COPY_AND_ASSIGN(TextureStorage9); const DWORD mD3DUsage; const D3DPOOL mD3DPool; }; class TextureStorage9_2D : public TextureStorage9 { public: TextureStorage9_2D(Renderer *renderer, SwapChain9 *swapchain); TextureStorage9_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height); virtual ~TextureStorage9_2D(); static TextureStorage9_2D *makeTextureStorage9_2D(TextureStorage *storage); IDirect3DSurface9 *getSurfaceLevel(int level, bool dirty); virtual RenderTarget *getRenderTarget(); virtual IDirect3DBaseTexture9 *getBaseTexture() const; virtual void generateMipmap(int level); private: DISALLOW_COPY_AND_ASSIGN(TextureStorage9_2D); void initializeRenderTarget(); IDirect3DTexture9 *mTexture; RenderTarget9 *mRenderTarget; }; class TextureStorage9_Cube : public TextureStorage9 { public: TextureStorage9_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size); virtual ~TextureStorage9_Cube(); static TextureStorage9_Cube *makeTextureStorage9_Cube(TextureStorage *storage); IDirect3DSurface9 *getCubeMapSurface(GLenum faceTarget, int level, bool dirty); virtual RenderTarget *getRenderTarget(GLenum faceTarget); virtual IDirect3DBaseTexture9 *getBaseTexture() const; virtual void generateMipmap(int face, int level); private: DISALLOW_COPY_AND_ASSIGN(TextureStorage9_Cube); void initializeRenderTarget(); IDirect3DCubeTexture9 *mTexture; RenderTarget9 *mRenderTarget[6]; }; } #endif // LIBGLESV2_RENDERER_TEXTURESTORAGE9_H_
C++
#include "precompiled.h" // // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Blit.cpp: Surface copy utility class. #include "libGLESv2/renderer/Blit.h" #include "libGLESv2/main.h" #include "libGLESv2/renderer/renderer9_utils.h" #include "libGLESv2/renderer/TextureStorage9.h" #include "libGLESv2/renderer/RenderTarget9.h" #include "libGLESv2/renderer/Renderer9.h" #include "libGLESv2/Framebuffer.h" #include "libGLESv2/Renderbuffer.h" namespace { #include "libGLESv2/renderer/shaders/compiled/standardvs.h" #include "libGLESv2/renderer/shaders/compiled/flipyvs.h" #include "libGLESv2/renderer/shaders/compiled/passthroughps.h" #include "libGLESv2/renderer/shaders/compiled/luminanceps.h" #include "libGLESv2/renderer/shaders/compiled/componentmaskps.h" const BYTE* const g_shaderCode[] = { g_vs20_standardvs, g_vs20_flipyvs, g_ps20_passthroughps, g_ps20_luminanceps, g_ps20_componentmaskps }; const size_t g_shaderSize[] = { sizeof(g_vs20_standardvs), sizeof(g_vs20_flipyvs), sizeof(g_ps20_passthroughps), sizeof(g_ps20_luminanceps), sizeof(g_ps20_componentmaskps) }; } namespace rx { Blit::Blit(rx::Renderer9 *renderer) : mRenderer(renderer), mQuadVertexBuffer(NULL), mQuadVertexDeclaration(NULL), mSavedStateBlock(NULL), mSavedRenderTarget(NULL), mSavedDepthStencil(NULL) { initGeometry(); memset(mCompiledShaders, 0, sizeof(mCompiledShaders)); } Blit::~Blit() { if (mSavedStateBlock) mSavedStateBlock->Release(); if (mQuadVertexBuffer) mQuadVertexBuffer->Release(); if (mQuadVertexDeclaration) mQuadVertexDeclaration->Release(); for (int i = 0; i < SHADER_COUNT; i++) { if (mCompiledShaders[i]) { mCompiledShaders[i]->Release(); } } } void Blit::initGeometry() { static const float quad[] = { -1, -1, -1, 1, 1, -1, 1, 1 }; IDirect3DDevice9 *device = mRenderer->getDevice(); HRESULT result = device->CreateVertexBuffer(sizeof(quad), D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &mQuadVertexBuffer, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); return gl::error(GL_OUT_OF_MEMORY); } void *lockPtr = NULL; result = mQuadVertexBuffer->Lock(0, 0, &lockPtr, 0); if (FAILED(result) || lockPtr == NULL) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); return gl::error(GL_OUT_OF_MEMORY); } memcpy(lockPtr, quad, sizeof(quad)); mQuadVertexBuffer->Unlock(); static const D3DVERTEXELEMENT9 elements[] = { { 0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() }; result = device->CreateVertexDeclaration(elements, &mQuadVertexDeclaration); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); return gl::error(GL_OUT_OF_MEMORY); } } template <class D3DShaderType> bool Blit::setShader(ShaderId source, const char *profile, D3DShaderType *(rx::Renderer9::*createShader)(const DWORD *, size_t length), HRESULT (WINAPI IDirect3DDevice9::*setShader)(D3DShaderType*)) { IDirect3DDevice9 *device = mRenderer->getDevice(); D3DShaderType *shader; if (mCompiledShaders[source] != NULL) { shader = static_cast<D3DShaderType*>(mCompiledShaders[source]); } else { const BYTE* shaderCode = g_shaderCode[source]; size_t shaderSize = g_shaderSize[source]; shader = (mRenderer->*createShader)(reinterpret_cast<const DWORD*>(shaderCode), shaderSize); if (!shader) { ERR("Failed to create shader for blit operation"); return false; } mCompiledShaders[source] = shader; } HRESULT hr = (device->*setShader)(shader); if (FAILED(hr)) { ERR("Failed to set shader for blit operation"); return false; } return true; } bool Blit::setVertexShader(ShaderId shader) { return setShader<IDirect3DVertexShader9>(shader, "vs_2_0", &rx::Renderer9::createVertexShader, &IDirect3DDevice9::SetVertexShader); } bool Blit::setPixelShader(ShaderId shader) { return setShader<IDirect3DPixelShader9>(shader, "ps_2_0", &rx::Renderer9::createPixelShader, &IDirect3DDevice9::SetPixelShader); } RECT Blit::getSurfaceRect(IDirect3DSurface9 *surface) const { D3DSURFACE_DESC desc; surface->GetDesc(&desc); RECT rect; rect.left = 0; rect.top = 0; rect.right = desc.Width; rect.bottom = desc.Height; return rect; } bool Blit::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) { IDirect3DTexture9 *texture = copySurfaceToTexture(source, getSurfaceRect(source)); if (!texture) { return false; } IDirect3DDevice9 *device = mRenderer->getDevice(); saveState(); device->SetTexture(0, texture); device->SetRenderTarget(0, dest); setVertexShader(SHADER_VS_STANDARD); setPixelShader(SHADER_PS_PASSTHROUGH); setCommonBlitState(); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); setViewport(getSurfaceRect(dest), 0, 0); render(); texture->Release(); restoreState(); return true; } bool Blit::copy(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level) { RenderTarget9 *renderTarget = NULL; IDirect3DSurface9 *source = NULL; gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0); if (colorbuffer) { renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget()); } if (renderTarget) { source = renderTarget->getSurface(); } if (!source) { ERR("Failed to retrieve the render target."); return gl::error(GL_OUT_OF_MEMORY, false); } TextureStorage9_2D *storage9 = TextureStorage9_2D::makeTextureStorage9_2D(storage->getStorageInstance()); IDirect3DSurface9 *destSurface = storage9->getSurfaceLevel(level, true); bool result = false; if (destSurface) { result = copy(source, sourceRect, destFormat, xoffset, yoffset, destSurface); destSurface->Release(); } source->Release(); return result; } bool Blit::copy(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level) { RenderTarget9 *renderTarget = NULL; IDirect3DSurface9 *source = NULL; gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0); if (colorbuffer) { renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget()); } if (renderTarget) { source = renderTarget->getSurface(); } if (!source) { ERR("Failed to retrieve the render target."); return gl::error(GL_OUT_OF_MEMORY, false); } TextureStorage9_Cube *storage9 = TextureStorage9_Cube::makeTextureStorage9_Cube(storage->getStorageInstance()); IDirect3DSurface9 *destSurface = storage9->getCubeMapSurface(target, level, true); bool result = false; if (destSurface) { result = copy(source, sourceRect, destFormat, xoffset, yoffset, destSurface); destSurface->Release(); } source->Release(); return result; } bool Blit::copy(IDirect3DSurface9 *source, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, IDirect3DSurface9 *dest) { if (!dest) { return false; } IDirect3DDevice9 *device = mRenderer->getDevice(); D3DSURFACE_DESC sourceDesc; D3DSURFACE_DESC destDesc; source->GetDesc(&sourceDesc); dest->GetDesc(&destDesc); if (sourceDesc.Format == destDesc.Format && destDesc.Usage & D3DUSAGE_RENDERTARGET && d3d9_gl::IsFormatChannelEquivalent(destDesc.Format, destFormat)) // Can use StretchRect { RECT destRect = {xoffset, yoffset, xoffset + (sourceRect.right - sourceRect.left), yoffset + (sourceRect.bottom - sourceRect.top)}; HRESULT result = device->StretchRect(source, &sourceRect, dest, &destRect, D3DTEXF_POINT); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); return gl::error(GL_OUT_OF_MEMORY, false); } } else { return formatConvert(source, sourceRect, destFormat, xoffset, yoffset, dest); } return true; } bool Blit::formatConvert(IDirect3DSurface9 *source, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, IDirect3DSurface9 *dest) { IDirect3DTexture9 *texture = copySurfaceToTexture(source, sourceRect); if (!texture) { return false; } IDirect3DDevice9 *device = mRenderer->getDevice(); saveState(); device->SetTexture(0, texture); device->SetRenderTarget(0, dest); setViewport(sourceRect, xoffset, yoffset); setCommonBlitState(); if (setFormatConvertShaders(destFormat)) { render(); } texture->Release(); restoreState(); return true; } bool Blit::setFormatConvertShaders(GLenum destFormat) { bool okay = setVertexShader(SHADER_VS_STANDARD); switch (destFormat) { default: UNREACHABLE(); case GL_RGBA: case GL_BGRA_EXT: case GL_RGB: case GL_ALPHA: okay = okay && setPixelShader(SHADER_PS_COMPONENTMASK); break; case GL_LUMINANCE: case GL_LUMINANCE_ALPHA: okay = okay && setPixelShader(SHADER_PS_LUMINANCE); break; } if (!okay) { return false; } enum { X = 0, Y = 1, Z = 2, W = 3 }; // The meaning of this constant depends on the shader that was selected. // See the shader assembly code above for details. float psConst0[4] = { 0, 0, 0, 0 }; switch (destFormat) { default: UNREACHABLE(); case GL_RGBA: case GL_BGRA_EXT: psConst0[X] = 1; psConst0[Z] = 1; break; case GL_RGB: psConst0[X] = 1; psConst0[W] = 1; break; case GL_ALPHA: psConst0[Z] = 1; break; case GL_LUMINANCE: psConst0[Y] = 1; break; case GL_LUMINANCE_ALPHA: psConst0[X] = 1; break; } mRenderer->getDevice()->SetPixelShaderConstantF(0, psConst0, 1); return true; } IDirect3DTexture9 *Blit::copySurfaceToTexture(IDirect3DSurface9 *surface, const RECT &sourceRect) { if (!surface) { return NULL; } IDirect3DDevice9 *device = mRenderer->getDevice(); D3DSURFACE_DESC sourceDesc; surface->GetDesc(&sourceDesc); // Copy the render target into a texture IDirect3DTexture9 *texture; HRESULT result = device->CreateTexture(sourceRect.right - sourceRect.left, sourceRect.bottom - sourceRect.top, 1, D3DUSAGE_RENDERTARGET, sourceDesc.Format, D3DPOOL_DEFAULT, &texture, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); return gl::error(GL_OUT_OF_MEMORY, (IDirect3DTexture9*)NULL); } IDirect3DSurface9 *textureSurface; result = texture->GetSurfaceLevel(0, &textureSurface); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); texture->Release(); return gl::error(GL_OUT_OF_MEMORY, (IDirect3DTexture9*)NULL); } mRenderer->endScene(); result = device->StretchRect(surface, &sourceRect, textureSurface, NULL, D3DTEXF_NONE); textureSurface->Release(); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); texture->Release(); return gl::error(GL_OUT_OF_MEMORY, (IDirect3DTexture9*)NULL); } return texture; } void Blit::setViewport(const RECT &sourceRect, GLint xoffset, GLint yoffset) { IDirect3DDevice9 *device = mRenderer->getDevice(); D3DVIEWPORT9 vp; vp.X = xoffset; vp.Y = yoffset; vp.Width = sourceRect.right - sourceRect.left; vp.Height = sourceRect.bottom - sourceRect.top; vp.MinZ = 0.0f; vp.MaxZ = 1.0f; device->SetViewport(&vp); float halfPixelAdjust[4] = { -1.0f/vp.Width, 1.0f/vp.Height, 0, 0 }; device->SetVertexShaderConstantF(0, halfPixelAdjust, 1); } void Blit::setCommonBlitState() { IDirect3DDevice9 *device = mRenderer->getDevice(); device->SetDepthStencilSurface(NULL); device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED); device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, FALSE); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); RECT scissorRect = {0}; // Scissoring is disabled for flipping, but we need this to capture and restore the old rectangle device->SetScissorRect(&scissorRect); for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { device->SetStreamSourceFreq(i, 1); } } void Blit::render() { IDirect3DDevice9 *device = mRenderer->getDevice(); HRESULT hr = device->SetStreamSource(0, mQuadVertexBuffer, 0, 2 * sizeof(float)); hr = device->SetVertexDeclaration(mQuadVertexDeclaration); mRenderer->startScene(); hr = device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); } void Blit::saveState() { IDirect3DDevice9 *device = mRenderer->getDevice(); HRESULT hr; device->GetDepthStencilSurface(&mSavedDepthStencil); device->GetRenderTarget(0, &mSavedRenderTarget); if (mSavedStateBlock == NULL) { hr = device->BeginStateBlock(); ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); setCommonBlitState(); static const float dummyConst[4] = { 0, 0, 0, 0 }; device->SetVertexShader(NULL); device->SetVertexShaderConstantF(0, dummyConst, 1); device->SetPixelShader(NULL); device->SetPixelShaderConstantF(0, dummyConst, 1); D3DVIEWPORT9 dummyVp; dummyVp.X = 0; dummyVp.Y = 0; dummyVp.Width = 1; dummyVp.Height = 1; dummyVp.MinZ = 0; dummyVp.MaxZ = 1; device->SetViewport(&dummyVp); device->SetTexture(0, NULL); device->SetStreamSource(0, mQuadVertexBuffer, 0, 0); device->SetVertexDeclaration(mQuadVertexDeclaration); hr = device->EndStateBlock(&mSavedStateBlock); ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); } ASSERT(mSavedStateBlock != NULL); if (mSavedStateBlock != NULL) { hr = mSavedStateBlock->Capture(); ASSERT(SUCCEEDED(hr)); } } void Blit::restoreState() { IDirect3DDevice9 *device = mRenderer->getDevice(); device->SetDepthStencilSurface(mSavedDepthStencil); if (mSavedDepthStencil != NULL) { mSavedDepthStencil->Release(); mSavedDepthStencil = NULL; } device->SetRenderTarget(0, mSavedRenderTarget); if (mSavedRenderTarget != NULL) { mSavedRenderTarget->Release(); mSavedRenderTarget = NULL; } ASSERT(mSavedStateBlock != NULL); if (mSavedStateBlock != NULL) { mSavedStateBlock->Apply(); } } }
C++
// // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Query9.h: Defines the rx::Query9 class which implements rx::QueryImpl. #ifndef LIBGLESV2_RENDERER_QUERY9_H_ #define LIBGLESV2_RENDERER_QUERY9_H_ #include "libGLESv2/renderer/QueryImpl.h" namespace rx { class Renderer9; class Query9 : public QueryImpl { public: Query9(rx::Renderer9 *renderer, GLenum type); virtual ~Query9(); void begin(); void end(); GLuint getResult(); GLboolean isResultAvailable(); private: DISALLOW_COPY_AND_ASSIGN(Query9); GLboolean testQuery(); rx::Renderer9 *mRenderer; IDirect3DQuery9 *mQuery; }; } #endif // LIBGLESV2_RENDERER_QUERY9_H_
C++
#include "precompiled.h" // // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // ImageSSE2.cpp: Implements SSE2-based functions of rx::Image class. It's // in a separated file for GCC, which can enable SSE usage only per-file, // not for code blocks that use SSE2 explicitly. #include "libGLESv2/Texture.h" #include "libGLESv2/renderer/Image.h" namespace rx { void Image::loadRGBAUByteDataToBGRASSE2(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output) { const unsigned int *source = NULL; unsigned int *dest = NULL; __m128i brMask = _mm_set1_epi32(0x00ff00ff); for (int y = 0; y < height; y++) { source = reinterpret_cast<const unsigned int*>(static_cast<const unsigned char*>(input) + y * inputPitch); dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + y * outputPitch); int x = 0; // Make output writes aligned for (x = 0; ((reinterpret_cast<intptr_t>(&dest[x]) & 15) != 0) && x < width; x++) { unsigned int rgba = source[x]; dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00); } for (; x + 3 < width; x += 4) { __m128i sourceData = _mm_loadu_si128(reinterpret_cast<const __m128i*>(&source[x])); // Mask out g and a, which don't change __m128i gaComponents = _mm_andnot_si128(brMask, sourceData); // Mask out b and r __m128i brComponents = _mm_and_si128(sourceData, brMask); // Swap b and r __m128i brSwapped = _mm_shufflehi_epi16(_mm_shufflelo_epi16(brComponents, _MM_SHUFFLE(2, 3, 0, 1)), _MM_SHUFFLE(2, 3, 0, 1)); __m128i result = _mm_or_si128(gaComponents, brSwapped); _mm_store_si128(reinterpret_cast<__m128i*>(&dest[x]), result); } // Perform leftover writes for (; x < width; x++) { unsigned int rgba = source[x]; dest[x] = (_rotl(rgba, 16) & 0x00ff00ff) | (rgba & 0xff00ff00); } } } void Image::loadAlphaDataToBGRASSE2(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output) { const unsigned char *source = NULL; unsigned int *dest = NULL; __m128i zeroWide = _mm_setzero_si128(); for (int y = 0; y < height; y++) { source = static_cast<const unsigned char*>(input) + y * inputPitch; dest = reinterpret_cast<unsigned int*>(static_cast<unsigned char*>(output) + y * outputPitch); int x; // Make output writes aligned for (x = 0; ((reinterpret_cast<intptr_t>(&dest[x]) & 0xF) != 0 && x < width); x++) { dest[x] = static_cast<unsigned int>(source[x]) << 24; } for (; x + 7 < width; x += 8) { __m128i sourceData = _mm_loadl_epi64(reinterpret_cast<const __m128i*>(&source[x])); // Interleave each byte to 16bit, make the lower byte to zero sourceData = _mm_unpacklo_epi8(zeroWide, sourceData); // Interleave each 16bit to 32bit, make the lower 16bit to zero __m128i lo = _mm_unpacklo_epi16(zeroWide, sourceData); __m128i hi = _mm_unpackhi_epi16(zeroWide, sourceData); _mm_store_si128(reinterpret_cast<__m128i*>(&dest[x]), lo); _mm_store_si128(reinterpret_cast<__m128i*>(&dest[x + 4]), hi); } // Handle the remainder for (; x < width; x++) { dest[x] = static_cast<unsigned int>(source[x]) << 24; } } } }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // SwapChain.h: Defines a back-end specific class that hides the details of the // implementation-specific swapchain. #ifndef LIBGLESV2_RENDERER_SWAPCHAIN_H_ #define LIBGLESV2_RENDERER_SWAPCHAIN_H_ #include "common/angleutils.h" namespace rx { class SwapChain { public: SwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat) : mWindow(window), mShareHandle(shareHandle), mBackBufferFormat(backBufferFormat), mDepthBufferFormat(depthBufferFormat) { } virtual ~SwapChain() {}; virtual EGLint resize(EGLint backbufferWidth, EGLint backbufferSize) = 0; virtual EGLint reset(EGLint backbufferWidth, EGLint backbufferHeight, EGLint swapInterval) = 0; virtual EGLint swapRect(EGLint x, EGLint y, EGLint width, EGLint height) = 0; virtual void recreate() = 0; virtual HANDLE getShareHandle() {return mShareHandle;}; protected: const HWND mWindow; // Window that the surface is created for. const GLenum mBackBufferFormat; const GLenum mDepthBufferFormat; HANDLE mShareHandle; }; } #endif // LIBGLESV2_RENDERER_SWAPCHAIN_H_
C++
#include "precompiled.h" // // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // RenderTarget9.cpp: Implements a D3D9-specific wrapper for IDirect3DSurface9 // pointers retained by renderbuffers. #include "libGLESv2/renderer/RenderTarget9.h" #include "libGLESv2/renderer/Renderer9.h" #include "libGLESv2/renderer/renderer9_utils.h" #include "libGLESv2/main.h" namespace rx { RenderTarget9::RenderTarget9(Renderer *renderer, IDirect3DSurface9 *surface) { mRenderer = Renderer9::makeRenderer9(renderer); mRenderTarget = surface; if (mRenderTarget) { D3DSURFACE_DESC description; mRenderTarget->GetDesc(&description); mWidth = description.Width; mHeight = description.Height; mInternalFormat = d3d9_gl::GetEquivalentFormat(description.Format); mActualFormat = d3d9_gl::GetEquivalentFormat(description.Format); mSamples = d3d9_gl::GetSamplesFromMultisampleType(description.MultiSampleType); } } RenderTarget9::RenderTarget9(Renderer *renderer, GLsizei width, GLsizei height, GLenum format, GLsizei samples) { mRenderer = Renderer9::makeRenderer9(renderer); mRenderTarget = NULL; D3DFORMAT requestedFormat = gl_d3d9::ConvertRenderbufferFormat(format); int supportedSamples = mRenderer->getNearestSupportedSamples(requestedFormat, samples); if (supportedSamples == -1) { gl::error(GL_OUT_OF_MEMORY); return; } HRESULT result = D3DERR_INVALIDCALL; if (width > 0 && height > 0) { if (requestedFormat == D3DFMT_D24S8) { result = mRenderer->getDevice()->CreateDepthStencilSurface(width, height, requestedFormat, gl_d3d9::GetMultisampleTypeFromSamples(supportedSamples), 0, FALSE, &mRenderTarget, NULL); } else { result = mRenderer->getDevice()->CreateRenderTarget(width, height, requestedFormat, gl_d3d9::GetMultisampleTypeFromSamples(supportedSamples), 0, FALSE, &mRenderTarget, NULL); } if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) { gl::error(GL_OUT_OF_MEMORY); return; } ASSERT(SUCCEEDED(result)); } mWidth = width; mHeight = height; mInternalFormat = format; mSamples = supportedSamples; mActualFormat = d3d9_gl::GetEquivalentFormat(requestedFormat); } RenderTarget9::~RenderTarget9() { if (mRenderTarget) { mRenderTarget->Release(); } } RenderTarget9 *RenderTarget9::makeRenderTarget9(RenderTarget *target) { ASSERT(HAS_DYNAMIC_TYPE(rx::RenderTarget9*, target)); return static_cast<rx::RenderTarget9*>(target); } IDirect3DSurface9 *RenderTarget9::getSurface() { // Caller is responsible for releasing the returned surface reference. if (mRenderTarget) { mRenderTarget->AddRef(); } return mRenderTarget; } }
C++
#include "precompiled.h" // // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // TextureStorage11.cpp: Implements the abstract rx::TextureStorage11 class and its concrete derived // classes TextureStorage11_2D and TextureStorage11_Cube, which act as the interface to the D3D11 texture. #include "libGLESv2/renderer/TextureStorage11.h" #include "libGLESv2/renderer/Renderer11.h" #include "libGLESv2/renderer/RenderTarget11.h" #include "libGLESv2/renderer/SwapChain11.h" #include "libGLESv2/renderer/renderer11_utils.h" #include "libGLESv2/utilities.h" #include "libGLESv2/main.h" namespace rx { TextureStorage11::TextureStorage11(Renderer *renderer, UINT bindFlags) : mBindFlags(bindFlags), mLodOffset(0), mMipLevels(0), mTexture(NULL), mTextureFormat(DXGI_FORMAT_UNKNOWN), mShaderResourceFormat(DXGI_FORMAT_UNKNOWN), mRenderTargetFormat(DXGI_FORMAT_UNKNOWN), mDepthStencilFormat(DXGI_FORMAT_UNKNOWN), mSRV(NULL), mTextureWidth(0), mTextureHeight(0) { mRenderer = Renderer11::makeRenderer11(renderer); } TextureStorage11::~TextureStorage11() { } TextureStorage11 *TextureStorage11::makeTextureStorage11(TextureStorage *storage) { ASSERT(HAS_DYNAMIC_TYPE(TextureStorage11*, storage)); return static_cast<TextureStorage11*>(storage); } DWORD TextureStorage11::GetTextureBindFlags(DXGI_FORMAT format, GLenum glusage, bool forceRenderable) { UINT bindFlags = D3D11_BIND_SHADER_RESOURCE; if (d3d11::IsDepthStencilFormat(format)) { bindFlags |= D3D11_BIND_DEPTH_STENCIL; } else if(forceRenderable || (TextureStorage11::IsTextureFormatRenderable(format) && (glusage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE))) { bindFlags |= D3D11_BIND_RENDER_TARGET; } return bindFlags; } bool TextureStorage11::IsTextureFormatRenderable(DXGI_FORMAT format) { switch(format) { case DXGI_FORMAT_R8G8B8A8_UNORM: case DXGI_FORMAT_A8_UNORM: case DXGI_FORMAT_R32G32B32A32_FLOAT: case DXGI_FORMAT_R32G32B32_FLOAT: case DXGI_FORMAT_R16G16B16A16_FLOAT: case DXGI_FORMAT_B8G8R8A8_UNORM: case DXGI_FORMAT_R8_UNORM: case DXGI_FORMAT_R8G8_UNORM: case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16G16_FLOAT: return true; case DXGI_FORMAT_BC1_UNORM: case DXGI_FORMAT_BC2_UNORM: case DXGI_FORMAT_BC3_UNORM: return false; default: UNREACHABLE(); return false; } } UINT TextureStorage11::getBindFlags() const { return mBindFlags; } ID3D11Texture2D *TextureStorage11::getBaseTexture() const { return mTexture; } int TextureStorage11::getLodOffset() const { return mLodOffset; } bool TextureStorage11::isRenderTarget() const { return (mBindFlags & (D3D11_BIND_RENDER_TARGET | D3D11_BIND_DEPTH_STENCIL)) != 0; } bool TextureStorage11::isManaged() const { return false; } int TextureStorage11::levelCount() { int levels = 0; if (getBaseTexture()) { levels = mMipLevels - getLodOffset(); } return levels; } UINT TextureStorage11::getSubresourceIndex(int level, int faceIndex) { UINT index = 0; if (getBaseTexture()) { index = D3D11CalcSubresource(level, faceIndex, mMipLevels); } return index; } bool TextureStorage11::updateSubresourceLevel(ID3D11Texture2D *srcTexture, unsigned int sourceSubresource, int level, int face, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) { if (srcTexture) { // Round up the width and height to the nearest multiple of dimension alignment unsigned int dimensionAlignment = d3d11::GetTextureFormatDimensionAlignment(mTextureFormat); width = width + dimensionAlignment - 1 - (width - 1) % dimensionAlignment; height = height + dimensionAlignment - 1 - (height - 1) % dimensionAlignment; D3D11_BOX srcBox; srcBox.left = xoffset; srcBox.top = yoffset; srcBox.right = xoffset + width; srcBox.bottom = yoffset + height; srcBox.front = 0; srcBox.back = 1; ID3D11DeviceContext *context = mRenderer->getDeviceContext(); ASSERT(getBaseTexture()); context->CopySubresourceRegion(getBaseTexture(), getSubresourceIndex(level + mLodOffset, face), xoffset, yoffset, 0, srcTexture, sourceSubresource, &srcBox); return true; } return false; } void TextureStorage11::generateMipmapLayer(RenderTarget11 *source, RenderTarget11 *dest) { if (source && dest) { ID3D11ShaderResourceView *sourceSRV = source->getShaderResourceView(); ID3D11RenderTargetView *destRTV = dest->getRenderTargetView(); if (sourceSRV && destRTV) { gl::Rectangle sourceArea; sourceArea.x = 0; sourceArea.y = 0; sourceArea.width = source->getWidth(); sourceArea.height = source->getHeight(); gl::Rectangle destArea; destArea.x = 0; destArea.y = 0; destArea.width = dest->getWidth(); destArea.height = dest->getHeight(); mRenderer->copyTexture(sourceSRV, sourceArea, source->getWidth(), source->getHeight(), destRTV, destArea, dest->getWidth(), dest->getHeight(), GL_RGBA); } if (sourceSRV) { sourceSRV->Release(); } if (destRTV) { destRTV->Release(); } } } TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, SwapChain11 *swapchain) : TextureStorage11(renderer, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE) { mTexture = swapchain->getOffscreenTexture(); mSRV = swapchain->getRenderTargetShaderResource(); for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) { mRenderTarget[i] = NULL; } D3D11_TEXTURE2D_DESC texDesc; mTexture->GetDesc(&texDesc); mMipLevels = texDesc.MipLevels; mTextureFormat = texDesc.Format; mTextureWidth = texDesc.Width; mTextureHeight = texDesc.Height; D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; mSRV->GetDesc(&srvDesc); mShaderResourceFormat = srvDesc.Format; ID3D11RenderTargetView* offscreenRTV = swapchain->getRenderTarget(); D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; offscreenRTV->GetDesc(&rtvDesc); mRenderTargetFormat = rtvDesc.Format; offscreenRTV->Release(); mDepthStencilFormat = DXGI_FORMAT_UNKNOWN; } TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable)) { for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) { mRenderTarget[i] = NULL; } DXGI_FORMAT convertedFormat = gl_d3d11::ConvertTextureFormat(internalformat); if (d3d11::IsDepthStencilFormat(convertedFormat)) { mTextureFormat = d3d11::GetDepthTextureFormat(convertedFormat); mShaderResourceFormat = d3d11::GetDepthShaderResourceFormat(convertedFormat); mDepthStencilFormat = convertedFormat; mRenderTargetFormat = DXGI_FORMAT_UNKNOWN; } else { mTextureFormat = convertedFormat; mShaderResourceFormat = convertedFormat; mDepthStencilFormat = DXGI_FORMAT_UNKNOWN; mRenderTargetFormat = convertedFormat; } // if the width or height is not positive this should be treated as an incomplete texture // we handle that here by skipping the d3d texture creation if (width > 0 && height > 0) { // adjust size if needed for compressed textures gl::MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset); ID3D11Device *device = mRenderer->getDevice(); D3D11_TEXTURE2D_DESC desc; desc.Width = width; // Compressed texture size constraints? desc.Height = height; desc.MipLevels = (levels > 0) ? levels + mLodOffset : 0; desc.ArraySize = 1; desc.Format = mTextureFormat; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Usage = D3D11_USAGE_DEFAULT; desc.BindFlags = getBindFlags(); desc.CPUAccessFlags = 0; desc.MiscFlags = 0; HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); // this can happen from windows TDR if (d3d11::isDeviceLostError(result)) { mRenderer->notifyDeviceLost(); gl::error(GL_OUT_OF_MEMORY); } else if (FAILED(result)) { ASSERT(result == E_OUTOFMEMORY); ERR("Creating image failed."); gl::error(GL_OUT_OF_MEMORY); } else { mTexture->GetDesc(&desc); mMipLevels = desc.MipLevels; mTextureWidth = desc.Width; mTextureHeight = desc.Height; } } } TextureStorage11_2D::~TextureStorage11_2D() { if (mTexture) { mTexture->Release(); mTexture = NULL; } if (mSRV) { mSRV->Release(); mSRV = NULL; } for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) { delete mRenderTarget[i]; mRenderTarget[i] = NULL; } } TextureStorage11_2D *TextureStorage11_2D::makeTextureStorage11_2D(TextureStorage *storage) { ASSERT(HAS_DYNAMIC_TYPE(TextureStorage11_2D*, storage)); return static_cast<TextureStorage11_2D*>(storage); } RenderTarget *TextureStorage11_2D::getRenderTarget(int level) { if (level >= 0 && level < static_cast<int>(mMipLevels)) { if (!mRenderTarget[level]) { ID3D11Device *device = mRenderer->getDevice(); HRESULT result; D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; srvDesc.Format = mShaderResourceFormat; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MostDetailedMip = level; srvDesc.Texture2D.MipLevels = 1; ID3D11ShaderResourceView *srv; result = device->CreateShaderResourceView(mTexture, &srvDesc, &srv); if (result == E_OUTOFMEMORY) { return gl::error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); } ASSERT(SUCCEEDED(result)); if (mRenderTargetFormat != DXGI_FORMAT_UNKNOWN) { D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; rtvDesc.Format = mRenderTargetFormat; rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; rtvDesc.Texture2D.MipSlice = level; ID3D11RenderTargetView *rtv; result = device->CreateRenderTargetView(mTexture, &rtvDesc, &rtv); if (result == E_OUTOFMEMORY) { srv->Release(); return gl::error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); } ASSERT(SUCCEEDED(result)); // RenderTarget11 expects to be the owner of the resources it is given but TextureStorage11 // also needs to keep a reference to the texture. mTexture->AddRef(); mRenderTarget[level] = new RenderTarget11(mRenderer, rtv, mTexture, srv, std::max(mTextureWidth >> level, 1U), std::max(mTextureHeight >> level, 1U)); } else if (mDepthStencilFormat != DXGI_FORMAT_UNKNOWN) { D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc; dsvDesc.Format = mDepthStencilFormat; dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; dsvDesc.Texture2D.MipSlice = level; dsvDesc.Flags = 0; ID3D11DepthStencilView *dsv; result = device->CreateDepthStencilView(mTexture, &dsvDesc, &dsv); if (result == E_OUTOFMEMORY) { srv->Release(); return gl::error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); } ASSERT(SUCCEEDED(result)); // RenderTarget11 expects to be the owner of the resources it is given but TextureStorage11 // also needs to keep a reference to the texture. mTexture->AddRef(); mRenderTarget[level] = new RenderTarget11(mRenderer, dsv, mTexture, srv, std::max(mTextureWidth >> level, 1U), std::max(mTextureHeight >> level, 1U)); } else { UNREACHABLE(); } } return mRenderTarget[level]; } else { return NULL; } } ID3D11ShaderResourceView *TextureStorage11_2D::getSRV() { if (!mSRV) { ID3D11Device *device = mRenderer->getDevice(); D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; srvDesc.Format = mShaderResourceFormat; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D; srvDesc.Texture2D.MipLevels = (mMipLevels == 0 ? -1 : mMipLevels); srvDesc.Texture2D.MostDetailedMip = 0; HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV); if (result == E_OUTOFMEMORY) { return gl::error(GL_OUT_OF_MEMORY, static_cast<ID3D11ShaderResourceView*>(NULL)); } ASSERT(SUCCEEDED(result)); } return mSRV; } void TextureStorage11_2D::generateMipmap(int level) { RenderTarget11 *source = RenderTarget11::makeRenderTarget11(getRenderTarget(level - 1)); RenderTarget11 *dest = RenderTarget11::makeRenderTarget11(getRenderTarget(level)); generateMipmapLayer(source, dest); } TextureStorage11_Cube::TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable)) { for (unsigned int i = 0; i < 6; i++) { for (unsigned int j = 0; j < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; j++) { mRenderTarget[i][j] = NULL; } } DXGI_FORMAT convertedFormat = gl_d3d11::ConvertTextureFormat(internalformat); if (d3d11::IsDepthStencilFormat(convertedFormat)) { mTextureFormat = d3d11::GetDepthTextureFormat(convertedFormat); mShaderResourceFormat = d3d11::GetDepthShaderResourceFormat(convertedFormat); mDepthStencilFormat = convertedFormat; mRenderTargetFormat = DXGI_FORMAT_UNKNOWN; } else { mTextureFormat = convertedFormat; mShaderResourceFormat = convertedFormat; mDepthStencilFormat = DXGI_FORMAT_UNKNOWN; mRenderTargetFormat = convertedFormat; } // if the size is not positive this should be treated as an incomplete texture // we handle that here by skipping the d3d texture creation if (size > 0) { // adjust size if needed for compressed textures int height = size; gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset); ID3D11Device *device = mRenderer->getDevice(); D3D11_TEXTURE2D_DESC desc; desc.Width = size; desc.Height = size; desc.MipLevels = (levels > 0) ? levels + mLodOffset : 0; desc.ArraySize = 6; desc.Format = mTextureFormat; desc.SampleDesc.Count = 1; desc.SampleDesc.Quality = 0; desc.Usage = D3D11_USAGE_DEFAULT; desc.BindFlags = getBindFlags(); desc.CPUAccessFlags = 0; desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE; HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); if (FAILED(result)) { ASSERT(result == E_OUTOFMEMORY); ERR("Creating image failed."); gl::error(GL_OUT_OF_MEMORY); } else { mTexture->GetDesc(&desc); mMipLevels = desc.MipLevels; mTextureWidth = desc.Width; mTextureHeight = desc.Height; } } } TextureStorage11_Cube::~TextureStorage11_Cube() { if (mTexture) { mTexture->Release(); mTexture = NULL; } if (mSRV) { mSRV->Release(); mSRV = NULL; } for (unsigned int i = 0; i < 6; i++) { for (unsigned int j = 0; j < gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS; j++) { delete mRenderTarget[i][j]; mRenderTarget[i][j] = NULL; } } } TextureStorage11_Cube *TextureStorage11_Cube::makeTextureStorage11_Cube(TextureStorage *storage) { ASSERT(HAS_DYNAMIC_TYPE(TextureStorage11_Cube*, storage)); return static_cast<TextureStorage11_Cube*>(storage); } RenderTarget *TextureStorage11_Cube::getRenderTarget(GLenum faceTarget, int level) { unsigned int faceIdx = gl::TextureCubeMap::faceIndex(faceTarget); if (level >= 0 && level < static_cast<int>(mMipLevels)) { if (!mRenderTarget[faceIdx][level]) { ID3D11Device *device = mRenderer->getDevice(); HRESULT result; D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; srvDesc.Format = mShaderResourceFormat; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; srvDesc.Texture2DArray.MostDetailedMip = level; srvDesc.Texture2DArray.MipLevels = 1; srvDesc.Texture2DArray.FirstArraySlice = faceIdx; srvDesc.Texture2DArray.ArraySize = 1; ID3D11ShaderResourceView *srv; result = device->CreateShaderResourceView(mTexture, &srvDesc, &srv); if (result == E_OUTOFMEMORY) { return gl::error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); } ASSERT(SUCCEEDED(result)); if (mRenderTargetFormat != DXGI_FORMAT_UNKNOWN) { D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; rtvDesc.Format = mRenderTargetFormat; rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY; rtvDesc.Texture2DArray.MipSlice = level; rtvDesc.Texture2DArray.FirstArraySlice = faceIdx; rtvDesc.Texture2DArray.ArraySize = 1; ID3D11RenderTargetView *rtv; result = device->CreateRenderTargetView(mTexture, &rtvDesc, &rtv); if (result == E_OUTOFMEMORY) { srv->Release(); return gl::error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); } ASSERT(SUCCEEDED(result)); // RenderTarget11 expects to be the owner of the resources it is given but TextureStorage11 // also needs to keep a reference to the texture. mTexture->AddRef(); mRenderTarget[faceIdx][level] = new RenderTarget11(mRenderer, rtv, mTexture, srv, std::max(mTextureWidth >> level, 1U), std::max(mTextureHeight >> level, 1U)); } else if (mDepthStencilFormat != DXGI_FORMAT_UNKNOWN) { D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc; dsvDesc.Format = mRenderTargetFormat; dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2DARRAY; dsvDesc.Texture2DArray.MipSlice = level; dsvDesc.Texture2DArray.FirstArraySlice = faceIdx; dsvDesc.Texture2DArray.ArraySize = 1; ID3D11DepthStencilView *dsv; result = device->CreateDepthStencilView(mTexture, &dsvDesc, &dsv); if (result == E_OUTOFMEMORY) { srv->Release(); return gl::error(GL_OUT_OF_MEMORY, static_cast<RenderTarget*>(NULL)); } ASSERT(SUCCEEDED(result)); // RenderTarget11 expects to be the owner of the resources it is given but TextureStorage11 // also needs to keep a reference to the texture. mTexture->AddRef(); mRenderTarget[faceIdx][level] = new RenderTarget11(mRenderer, dsv, mTexture, srv, std::max(mTextureWidth >> level, 1U), std::max(mTextureHeight >> level, 1U)); } else { UNREACHABLE(); } } return mRenderTarget[faceIdx][level]; } else { return NULL; } } ID3D11ShaderResourceView *TextureStorage11_Cube::getSRV() { if (!mSRV) { ID3D11Device *device = mRenderer->getDevice(); D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc; srvDesc.Format = mShaderResourceFormat; srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE; srvDesc.TextureCube.MipLevels = (mMipLevels == 0 ? -1 : mMipLevels); srvDesc.TextureCube.MostDetailedMip = 0; HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV); if (result == E_OUTOFMEMORY) { return gl::error(GL_OUT_OF_MEMORY, static_cast<ID3D11ShaderResourceView*>(NULL)); } ASSERT(SUCCEEDED(result)); } return mSRV; } void TextureStorage11_Cube::generateMipmap(int face, int level) { RenderTarget11 *source = RenderTarget11::makeRenderTarget11(getRenderTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level - 1)); RenderTarget11 *dest = RenderTarget11::makeRenderTarget11(getRenderTarget(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level)); generateMipmapLayer(source, dest); } }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // SwapChain9.h: Defines a back-end specific class for the D3D9 swap chain. #ifndef LIBGLESV2_RENDERER_SWAPCHAIN9_H_ #define LIBGLESV2_RENDERER_SWAPCHAIN9_H_ #include "common/angleutils.h" #include "libGLESv2/renderer/SwapChain.h" namespace rx { class Renderer9; class SwapChain9 : public SwapChain { public: SwapChain9(Renderer9 *renderer, HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat); virtual ~SwapChain9(); EGLint resize(EGLint backbufferWidth, EGLint backbufferHeight); virtual EGLint reset(EGLint backbufferWidth, EGLint backbufferHeight, EGLint swapInterval); virtual EGLint swapRect(EGLint x, EGLint y, EGLint width, EGLint height); virtual void recreate(); virtual IDirect3DSurface9 *getRenderTarget(); virtual IDirect3DSurface9 *getDepthStencil(); virtual IDirect3DTexture9 *getOffscreenTexture(); static SwapChain9 *makeSwapChain9(SwapChain *swapChain); private: DISALLOW_COPY_AND_ASSIGN(SwapChain9); void release(); Renderer9 *mRenderer; EGLint mHeight; EGLint mWidth; EGLint mSwapInterval; IDirect3DSwapChain9 *mSwapChain; IDirect3DSurface9 *mBackBuffer; IDirect3DSurface9 *mRenderTarget; IDirect3DSurface9 *mDepthStencil; IDirect3DTexture9* mOffscreenTexture; }; } #endif // LIBGLESV2_RENDERER_SWAPCHAIN9_H_
C++
#include "precompiled.h" // // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // ShaderExecutable11.cpp: Implements a D3D11-specific class to contain shader // executable implementation details. #include "libGLESv2/renderer/ShaderExecutable11.h" #include "common/debug.h" namespace rx { ShaderExecutable11::ShaderExecutable11(const void *function, size_t length, ID3D11PixelShader *executable) : ShaderExecutable(function, length) { mPixelExecutable = executable; mVertexExecutable = NULL; mGeometryExecutable = NULL; mConstantBuffer = NULL; } ShaderExecutable11::ShaderExecutable11(const void *function, size_t length, ID3D11VertexShader *executable) : ShaderExecutable(function, length) { mVertexExecutable = executable; mPixelExecutable = NULL; mGeometryExecutable = NULL; mConstantBuffer = NULL; } ShaderExecutable11::ShaderExecutable11(const void *function, size_t length, ID3D11GeometryShader *executable) : ShaderExecutable(function, length) { mGeometryExecutable = executable; mVertexExecutable = NULL; mPixelExecutable = NULL; mConstantBuffer = NULL; } ShaderExecutable11::~ShaderExecutable11() { if (mVertexExecutable) { mVertexExecutable->Release(); } if (mPixelExecutable) { mPixelExecutable->Release(); } if (mGeometryExecutable) { mGeometryExecutable->Release(); } if (mConstantBuffer) { mConstantBuffer->Release(); } } ShaderExecutable11 *ShaderExecutable11::makeShaderExecutable11(ShaderExecutable *executable) { ASSERT(HAS_DYNAMIC_TYPE(ShaderExecutable11*, executable)); return static_cast<ShaderExecutable11*>(executable); } ID3D11VertexShader *ShaderExecutable11::getVertexShader() const { return mVertexExecutable; } ID3D11PixelShader *ShaderExecutable11::getPixelShader() const { return mPixelExecutable; } ID3D11GeometryShader *ShaderExecutable11::getGeometryShader() const { return mGeometryExecutable; } ID3D11Buffer *ShaderExecutable11::getConstantBuffer(ID3D11Device *device, unsigned int registerCount) { if (!mConstantBuffer && registerCount > 0) { D3D11_BUFFER_DESC constantBufferDescription = {0}; constantBufferDescription.ByteWidth = registerCount * sizeof(float[4]); constantBufferDescription.Usage = D3D11_USAGE_DYNAMIC; constantBufferDescription.BindFlags = D3D11_BIND_CONSTANT_BUFFER; constantBufferDescription.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; constantBufferDescription.MiscFlags = 0; constantBufferDescription.StructureByteStride = 0; HRESULT result = device->CreateBuffer(&constantBufferDescription, NULL, &mConstantBuffer); ASSERT(SUCCEEDED(result)); } return mConstantBuffer; } }
C++
#include "precompiled.h" // // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // BufferStorage.cpp Defines the abstract BufferStorage class. #include "libGLESv2/renderer/BufferStorage.h" namespace rx { unsigned int BufferStorage::mNextSerial = 1; BufferStorage::BufferStorage() { updateSerial(); } BufferStorage::~BufferStorage() { } unsigned int BufferStorage::getSerial() const { return mSerial; } void BufferStorage::updateSerial() { mSerial = mNextSerial++; } void BufferStorage::markBufferUsage() { } }
C++
// // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Query11.h: Defines the rx::Query11 class which implements rx::QueryImpl. #ifndef LIBGLESV2_RENDERER_QUERY11_H_ #define LIBGLESV2_RENDERER_QUERY11_H_ #include "libGLESv2/renderer/QueryImpl.h" namespace rx { class Renderer11; class Query11 : public QueryImpl { public: Query11(rx::Renderer11 *renderer, GLenum type); virtual ~Query11(); void begin(); void end(); GLuint getResult(); GLboolean isResultAvailable(); private: DISALLOW_COPY_AND_ASSIGN(Query11); GLboolean testQuery(); rx::Renderer11 *mRenderer; ID3D11Query *mQuery; }; } #endif // LIBGLESV2_RENDERER_QUERY11_H_
C++
#include "precompiled.h" // // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // TextureStorage9.cpp: Implements the abstract rx::TextureStorage9 class and its concrete derived // classes TextureStorage9_2D and TextureStorage9_Cube, which act as the interface to the // D3D9 texture. #include "libGLESv2/main.h" #include "libGLESv2/renderer/Renderer9.h" #include "libGLESv2/renderer/TextureStorage9.h" #include "libGLESv2/renderer/SwapChain9.h" #include "libGLESv2/renderer/RenderTarget9.h" #include "libGLESv2/renderer/renderer9_utils.h" #include "libGLESv2/Texture.h" namespace rx { TextureStorage9::TextureStorage9(Renderer *renderer, DWORD usage) : mLodOffset(0), mRenderer(Renderer9::makeRenderer9(renderer)), mD3DUsage(usage), mD3DPool(mRenderer->getTexturePool(usage)) { } TextureStorage9::~TextureStorage9() { } TextureStorage9 *TextureStorage9::makeTextureStorage9(TextureStorage *storage) { ASSERT(HAS_DYNAMIC_TYPE(TextureStorage9*, storage)); return static_cast<TextureStorage9*>(storage); } DWORD TextureStorage9::GetTextureUsage(D3DFORMAT d3dfmt, GLenum glusage, bool forceRenderable) { DWORD d3dusage = 0; if (d3dfmt == D3DFMT_INTZ) { d3dusage |= D3DUSAGE_DEPTHSTENCIL; } else if(forceRenderable || (TextureStorage9::IsTextureFormatRenderable(d3dfmt) && (glusage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE))) { d3dusage |= D3DUSAGE_RENDERTARGET; } return d3dusage; } bool TextureStorage9::IsTextureFormatRenderable(D3DFORMAT format) { if (format == D3DFMT_INTZ) { return true; } switch(format) { case D3DFMT_L8: case D3DFMT_A8L8: case D3DFMT_DXT1: case D3DFMT_DXT3: case D3DFMT_DXT5: return false; case D3DFMT_A8R8G8B8: case D3DFMT_X8R8G8B8: case D3DFMT_A16B16G16R16F: case D3DFMT_A32B32G32R32F: return true; default: UNREACHABLE(); } return false; } bool TextureStorage9::isRenderTarget() const { return (mD3DUsage & (D3DUSAGE_RENDERTARGET | D3DUSAGE_DEPTHSTENCIL)) != 0; } bool TextureStorage9::isManaged() const { return (mD3DPool == D3DPOOL_MANAGED); } D3DPOOL TextureStorage9::getPool() const { return mD3DPool; } DWORD TextureStorage9::getUsage() const { return mD3DUsage; } int TextureStorage9::getLodOffset() const { return mLodOffset; } int TextureStorage9::levelCount() { return getBaseTexture() ? getBaseTexture()->GetLevelCount() - getLodOffset() : 0; } TextureStorage9_2D::TextureStorage9_2D(Renderer *renderer, SwapChain9 *swapchain) : TextureStorage9(renderer, D3DUSAGE_RENDERTARGET) { IDirect3DTexture9 *surfaceTexture = swapchain->getOffscreenTexture(); mTexture = surfaceTexture; mRenderTarget = NULL; initializeRenderTarget(); } TextureStorage9_2D::TextureStorage9_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) : TextureStorage9(renderer, GetTextureUsage(Renderer9::makeRenderer9(renderer)->ConvertTextureInternalFormat(internalformat), usage, forceRenderable)) { mTexture = NULL; mRenderTarget = NULL; // if the width or height is not positive this should be treated as an incomplete texture // we handle that here by skipping the d3d texture creation if (width > 0 && height > 0) { IDirect3DDevice9 *device = mRenderer->getDevice(); gl::MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset); HRESULT result = device->CreateTexture(width, height, levels ? levels + mLodOffset : 0, getUsage(), mRenderer->ConvertTextureInternalFormat(internalformat), getPool(), &mTexture, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); gl::error(GL_OUT_OF_MEMORY); } } initializeRenderTarget(); } TextureStorage9_2D::~TextureStorage9_2D() { if (mTexture) { mTexture->Release(); } delete mRenderTarget; } TextureStorage9_2D *TextureStorage9_2D::makeTextureStorage9_2D(TextureStorage *storage) { ASSERT(HAS_DYNAMIC_TYPE(TextureStorage9_2D*, storage)); return static_cast<TextureStorage9_2D*>(storage); } // Increments refcount on surface. // caller must Release() the returned surface IDirect3DSurface9 *TextureStorage9_2D::getSurfaceLevel(int level, bool dirty) { IDirect3DSurface9 *surface = NULL; if (mTexture) { HRESULT result = mTexture->GetSurfaceLevel(level + mLodOffset, &surface); ASSERT(SUCCEEDED(result)); // With managed textures the driver needs to be informed of updates to the lower mipmap levels if (level + mLodOffset != 0 && isManaged() && dirty) { mTexture->AddDirtyRect(NULL); } } return surface; } RenderTarget *TextureStorage9_2D::getRenderTarget() { return mRenderTarget; } void TextureStorage9_2D::generateMipmap(int level) { IDirect3DSurface9 *upper = getSurfaceLevel(level - 1, false); IDirect3DSurface9 *lower = getSurfaceLevel(level, true); if (upper != NULL && lower != NULL) { mRenderer->boxFilter(upper, lower); } if (upper != NULL) upper->Release(); if (lower != NULL) lower->Release(); } IDirect3DBaseTexture9 *TextureStorage9_2D::getBaseTexture() const { return mTexture; } void TextureStorage9_2D::initializeRenderTarget() { ASSERT(mRenderTarget == NULL); if (mTexture != NULL && isRenderTarget()) { IDirect3DSurface9 *surface = getSurfaceLevel(0, false); mRenderTarget = new RenderTarget9(mRenderer, surface); } } TextureStorage9_Cube::TextureStorage9_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) : TextureStorage9(renderer, GetTextureUsage(Renderer9::makeRenderer9(renderer)->ConvertTextureInternalFormat(internalformat), usage, forceRenderable)) { mTexture = NULL; for (int i = 0; i < 6; ++i) { mRenderTarget[i] = NULL; } // if the size is not positive this should be treated as an incomplete texture // we handle that here by skipping the d3d texture creation if (size > 0) { IDirect3DDevice9 *device = mRenderer->getDevice(); int height = size; gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset); HRESULT result = device->CreateCubeTexture(size, levels ? levels + mLodOffset : 0, getUsage(), mRenderer->ConvertTextureInternalFormat(internalformat), getPool(), &mTexture, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); gl::error(GL_OUT_OF_MEMORY); } } initializeRenderTarget(); } TextureStorage9_Cube::~TextureStorage9_Cube() { if (mTexture) { mTexture->Release(); } for (int i = 0; i < 6; ++i) { delete mRenderTarget[i]; } } TextureStorage9_Cube *TextureStorage9_Cube::makeTextureStorage9_Cube(TextureStorage *storage) { ASSERT(HAS_DYNAMIC_TYPE(TextureStorage9_Cube*, storage)); return static_cast<TextureStorage9_Cube*>(storage); } // Increments refcount on surface. // caller must Release() the returned surface IDirect3DSurface9 *TextureStorage9_Cube::getCubeMapSurface(GLenum faceTarget, int level, bool dirty) { IDirect3DSurface9 *surface = NULL; if (mTexture) { D3DCUBEMAP_FACES face = gl_d3d9::ConvertCubeFace(faceTarget); HRESULT result = mTexture->GetCubeMapSurface(face, level + mLodOffset, &surface); ASSERT(SUCCEEDED(result)); // With managed textures the driver needs to be informed of updates to the lower mipmap levels if (level != 0 && isManaged() && dirty) { mTexture->AddDirtyRect(face, NULL); } } return surface; } RenderTarget *TextureStorage9_Cube::getRenderTarget(GLenum faceTarget) { return mRenderTarget[gl::TextureCubeMap::faceIndex(faceTarget)]; } void TextureStorage9_Cube::generateMipmap(int face, int level) { IDirect3DSurface9 *upper = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level - 1, false); IDirect3DSurface9 *lower = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true); if (upper != NULL && lower != NULL) { mRenderer->boxFilter(upper, lower); } if (upper != NULL) upper->Release(); if (lower != NULL) lower->Release(); } IDirect3DBaseTexture9 *TextureStorage9_Cube::getBaseTexture() const { return mTexture; } void TextureStorage9_Cube::initializeRenderTarget() { if (mTexture != NULL && isRenderTarget()) { IDirect3DSurface9 *surface = NULL; for (int i = 0; i < 6; ++i) { ASSERT(mRenderTarget[i] == NULL); surface = getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, false); mRenderTarget[i] = new RenderTarget9(mRenderer, surface); } } } }
C++
#include "precompiled.h" // // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // renderer11_utils.cpp: Conversion functions and other utility routines // specific to the D3D11 renderer. #include "libGLESv2/renderer/renderer11_utils.h" #include "common/debug.h" namespace gl_d3d11 { D3D11_BLEND ConvertBlendFunc(GLenum glBlend, bool isAlpha) { D3D11_BLEND d3dBlend = D3D11_BLEND_ZERO; switch (glBlend) { case GL_ZERO: d3dBlend = D3D11_BLEND_ZERO; break; case GL_ONE: d3dBlend = D3D11_BLEND_ONE; break; case GL_SRC_COLOR: d3dBlend = (isAlpha ? D3D11_BLEND_SRC_ALPHA : D3D11_BLEND_SRC_COLOR); break; case GL_ONE_MINUS_SRC_COLOR: d3dBlend = (isAlpha ? D3D11_BLEND_INV_SRC_ALPHA : D3D11_BLEND_INV_SRC_COLOR); break; case GL_DST_COLOR: d3dBlend = (isAlpha ? D3D11_BLEND_DEST_ALPHA : D3D11_BLEND_DEST_COLOR); break; case GL_ONE_MINUS_DST_COLOR: d3dBlend = (isAlpha ? D3D11_BLEND_INV_DEST_ALPHA : D3D11_BLEND_INV_DEST_COLOR); break; case GL_SRC_ALPHA: d3dBlend = D3D11_BLEND_SRC_ALPHA; break; case GL_ONE_MINUS_SRC_ALPHA: d3dBlend = D3D11_BLEND_INV_SRC_ALPHA; break; case GL_DST_ALPHA: d3dBlend = D3D11_BLEND_DEST_ALPHA; break; case GL_ONE_MINUS_DST_ALPHA: d3dBlend = D3D11_BLEND_INV_DEST_ALPHA; break; case GL_CONSTANT_COLOR: d3dBlend = D3D11_BLEND_BLEND_FACTOR; break; case GL_ONE_MINUS_CONSTANT_COLOR: d3dBlend = D3D11_BLEND_INV_BLEND_FACTOR; break; case GL_CONSTANT_ALPHA: d3dBlend = D3D11_BLEND_BLEND_FACTOR; break; case GL_ONE_MINUS_CONSTANT_ALPHA: d3dBlend = D3D11_BLEND_INV_BLEND_FACTOR; break; case GL_SRC_ALPHA_SATURATE: d3dBlend = D3D11_BLEND_SRC_ALPHA_SAT; break; default: UNREACHABLE(); } return d3dBlend; } D3D11_BLEND_OP ConvertBlendOp(GLenum glBlendOp) { D3D11_BLEND_OP d3dBlendOp = D3D11_BLEND_OP_ADD; switch (glBlendOp) { case GL_FUNC_ADD: d3dBlendOp = D3D11_BLEND_OP_ADD; break; case GL_FUNC_SUBTRACT: d3dBlendOp = D3D11_BLEND_OP_SUBTRACT; break; case GL_FUNC_REVERSE_SUBTRACT: d3dBlendOp = D3D11_BLEND_OP_REV_SUBTRACT; break; default: UNREACHABLE(); } return d3dBlendOp; } UINT8 ConvertColorMask(bool red, bool green, bool blue, bool alpha) { UINT8 mask = 0; if (red) { mask |= D3D11_COLOR_WRITE_ENABLE_RED; } if (green) { mask |= D3D11_COLOR_WRITE_ENABLE_GREEN; } if (blue) { mask |= D3D11_COLOR_WRITE_ENABLE_BLUE; } if (alpha) { mask |= D3D11_COLOR_WRITE_ENABLE_ALPHA; } return mask; } D3D11_CULL_MODE ConvertCullMode(bool cullEnabled, GLenum cullMode) { D3D11_CULL_MODE cull = D3D11_CULL_NONE; if (cullEnabled) { switch (cullMode) { case GL_FRONT: cull = D3D11_CULL_FRONT; break; case GL_BACK: cull = D3D11_CULL_BACK; break; case GL_FRONT_AND_BACK: cull = D3D11_CULL_NONE; break; default: UNREACHABLE(); } } else { cull = D3D11_CULL_NONE; } return cull; } D3D11_COMPARISON_FUNC ConvertComparison(GLenum comparison) { D3D11_COMPARISON_FUNC d3dComp = D3D11_COMPARISON_NEVER; switch (comparison) { case GL_NEVER: d3dComp = D3D11_COMPARISON_NEVER; break; case GL_ALWAYS: d3dComp = D3D11_COMPARISON_ALWAYS; break; case GL_LESS: d3dComp = D3D11_COMPARISON_LESS; break; case GL_LEQUAL: d3dComp = D3D11_COMPARISON_LESS_EQUAL; break; case GL_EQUAL: d3dComp = D3D11_COMPARISON_EQUAL; break; case GL_GREATER: d3dComp = D3D11_COMPARISON_GREATER; break; case GL_GEQUAL: d3dComp = D3D11_COMPARISON_GREATER_EQUAL; break; case GL_NOTEQUAL: d3dComp = D3D11_COMPARISON_NOT_EQUAL; break; default: UNREACHABLE(); } return d3dComp; } D3D11_DEPTH_WRITE_MASK ConvertDepthMask(bool depthWriteEnabled) { return depthWriteEnabled ? D3D11_DEPTH_WRITE_MASK_ALL : D3D11_DEPTH_WRITE_MASK_ZERO; } UINT8 ConvertStencilMask(GLuint stencilmask) { return static_cast<UINT8>(stencilmask); } D3D11_STENCIL_OP ConvertStencilOp(GLenum stencilOp) { D3D11_STENCIL_OP d3dStencilOp = D3D11_STENCIL_OP_KEEP; switch (stencilOp) { case GL_ZERO: d3dStencilOp = D3D11_STENCIL_OP_ZERO; break; case GL_KEEP: d3dStencilOp = D3D11_STENCIL_OP_KEEP; break; case GL_REPLACE: d3dStencilOp = D3D11_STENCIL_OP_REPLACE; break; case GL_INCR: d3dStencilOp = D3D11_STENCIL_OP_INCR_SAT; break; case GL_DECR: d3dStencilOp = D3D11_STENCIL_OP_DECR_SAT; break; case GL_INVERT: d3dStencilOp = D3D11_STENCIL_OP_INVERT; break; case GL_INCR_WRAP: d3dStencilOp = D3D11_STENCIL_OP_INCR; break; case GL_DECR_WRAP: d3dStencilOp = D3D11_STENCIL_OP_DECR; break; default: UNREACHABLE(); } return d3dStencilOp; } D3D11_FILTER ConvertFilter(GLenum minFilter, GLenum magFilter, float maxAnisotropy) { if (maxAnisotropy > 1.0f) { return D3D11_ENCODE_ANISOTROPIC_FILTER(false); } else { D3D11_FILTER_TYPE dxMin = D3D11_FILTER_TYPE_POINT; D3D11_FILTER_TYPE dxMip = D3D11_FILTER_TYPE_POINT; switch (minFilter) { case GL_NEAREST: dxMin = D3D11_FILTER_TYPE_POINT; dxMip = D3D11_FILTER_TYPE_POINT; break; case GL_LINEAR: dxMin = D3D11_FILTER_TYPE_LINEAR; dxMip = D3D11_FILTER_TYPE_POINT; break; case GL_NEAREST_MIPMAP_NEAREST: dxMin = D3D11_FILTER_TYPE_POINT; dxMip = D3D11_FILTER_TYPE_POINT; break; case GL_LINEAR_MIPMAP_NEAREST: dxMin = D3D11_FILTER_TYPE_LINEAR; dxMip = D3D11_FILTER_TYPE_POINT; break; case GL_NEAREST_MIPMAP_LINEAR: dxMin = D3D11_FILTER_TYPE_POINT; dxMip = D3D11_FILTER_TYPE_LINEAR; break; case GL_LINEAR_MIPMAP_LINEAR: dxMin = D3D11_FILTER_TYPE_LINEAR; dxMip = D3D11_FILTER_TYPE_LINEAR; break; default: UNREACHABLE(); } D3D11_FILTER_TYPE dxMag = D3D11_FILTER_TYPE_POINT; switch (magFilter) { case GL_NEAREST: dxMag = D3D11_FILTER_TYPE_POINT; break; case GL_LINEAR: dxMag = D3D11_FILTER_TYPE_LINEAR; break; default: UNREACHABLE(); } return D3D11_ENCODE_BASIC_FILTER(dxMin, dxMag, dxMip, false); } } D3D11_TEXTURE_ADDRESS_MODE ConvertTextureWrap(GLenum wrap) { switch (wrap) { case GL_REPEAT: return D3D11_TEXTURE_ADDRESS_WRAP; case GL_CLAMP_TO_EDGE: return D3D11_TEXTURE_ADDRESS_CLAMP; case GL_MIRRORED_REPEAT: return D3D11_TEXTURE_ADDRESS_MIRROR; default: UNREACHABLE(); } return D3D11_TEXTURE_ADDRESS_WRAP; } FLOAT ConvertMinLOD(GLenum minFilter, unsigned int lodOffset) { return (minFilter == GL_NEAREST || minFilter == GL_LINEAR) ? static_cast<float>(lodOffset) : -FLT_MAX; } FLOAT ConvertMaxLOD(GLenum minFilter, unsigned int lodOffset) { return (minFilter == GL_NEAREST || minFilter == GL_LINEAR) ? static_cast<float>(lodOffset) : FLT_MAX; } } namespace d3d11_gl { GLenum ConvertBackBufferFormat(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_R8G8B8A8_UNORM: return GL_RGBA8_OES; case DXGI_FORMAT_B8G8R8A8_UNORM: return GL_BGRA8_EXT; default: UNREACHABLE(); } return GL_RGBA8_OES; } GLenum ConvertDepthStencilFormat(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_UNKNOWN: return GL_NONE; case DXGI_FORMAT_D16_UNORM: return GL_DEPTH_COMPONENT16; case DXGI_FORMAT_D24_UNORM_S8_UINT: return GL_DEPTH24_STENCIL8_OES; default: UNREACHABLE(); } return GL_DEPTH24_STENCIL8_OES; } GLenum ConvertRenderbufferFormat(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_B8G8R8A8_UNORM: return GL_BGRA8_EXT; case DXGI_FORMAT_R8G8B8A8_UNORM: return GL_RGBA8_OES; case DXGI_FORMAT_D16_UNORM: return GL_DEPTH_COMPONENT16; case DXGI_FORMAT_D24_UNORM_S8_UINT: return GL_DEPTH24_STENCIL8_OES; default: UNREACHABLE(); } return GL_RGBA8_OES; } GLenum ConvertTextureInternalFormat(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_R8G8B8A8_UNORM: return GL_RGBA8_OES; case DXGI_FORMAT_A8_UNORM: return GL_ALPHA8_EXT; case DXGI_FORMAT_BC1_UNORM: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; case DXGI_FORMAT_BC2_UNORM: return GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE; case DXGI_FORMAT_BC3_UNORM: return GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE; case DXGI_FORMAT_R32G32B32A32_FLOAT: return GL_RGBA32F_EXT; case DXGI_FORMAT_R32G32B32_FLOAT: return GL_RGB32F_EXT; case DXGI_FORMAT_R16G16B16A16_FLOAT: return GL_RGBA16F_EXT; case DXGI_FORMAT_B8G8R8A8_UNORM: return GL_BGRA8_EXT; case DXGI_FORMAT_R8_UNORM: return GL_R8_EXT; case DXGI_FORMAT_R8G8_UNORM: return GL_RG8_EXT; case DXGI_FORMAT_R16_FLOAT: return GL_R16F_EXT; case DXGI_FORMAT_R16G16_FLOAT: return GL_RG16F_EXT; case DXGI_FORMAT_D16_UNORM: return GL_DEPTH_COMPONENT16; case DXGI_FORMAT_D24_UNORM_S8_UINT: return GL_DEPTH24_STENCIL8_OES; case DXGI_FORMAT_UNKNOWN: return GL_NONE; default: UNREACHABLE(); } return GL_RGBA8_OES; } } namespace gl_d3d11 { DXGI_FORMAT ConvertRenderbufferFormat(GLenum format) { switch (format) { case GL_RGBA4: case GL_RGB5_A1: case GL_RGBA8_OES: case GL_RGB565: case GL_RGB8_OES: return DXGI_FORMAT_R8G8B8A8_UNORM; case GL_BGRA8_EXT: return DXGI_FORMAT_B8G8R8A8_UNORM; case GL_DEPTH_COMPONENT16: return DXGI_FORMAT_D16_UNORM; case GL_STENCIL_INDEX8: case GL_DEPTH24_STENCIL8_OES: return DXGI_FORMAT_D24_UNORM_S8_UINT; default: UNREACHABLE(); } return DXGI_FORMAT_R8G8B8A8_UNORM; } DXGI_FORMAT ConvertTextureFormat(GLenum internalformat) { switch (internalformat) { case GL_RGB565: case GL_RGBA4: case GL_RGB5_A1: case GL_RGB8_OES: case GL_RGBA8_OES: case GL_LUMINANCE8_EXT: case GL_LUMINANCE8_ALPHA8_EXT: return DXGI_FORMAT_R8G8B8A8_UNORM; case GL_ALPHA8_EXT: return DXGI_FORMAT_A8_UNORM; case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return DXGI_FORMAT_BC1_UNORM; case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: return DXGI_FORMAT_BC2_UNORM; case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: return DXGI_FORMAT_BC3_UNORM; case GL_RGBA32F_EXT: case GL_ALPHA32F_EXT: case GL_LUMINANCE_ALPHA32F_EXT: return DXGI_FORMAT_R32G32B32A32_FLOAT; case GL_RGB32F_EXT: case GL_LUMINANCE32F_EXT: return DXGI_FORMAT_R32G32B32_FLOAT; case GL_RGBA16F_EXT: case GL_ALPHA16F_EXT: case GL_LUMINANCE_ALPHA16F_EXT: case GL_RGB16F_EXT: case GL_LUMINANCE16F_EXT: return DXGI_FORMAT_R16G16B16A16_FLOAT; case GL_BGRA8_EXT: return DXGI_FORMAT_B8G8R8A8_UNORM; case GL_R8_EXT: return DXGI_FORMAT_R8_UNORM; case GL_RG8_EXT: return DXGI_FORMAT_R8G8_UNORM; case GL_R16F_EXT: return DXGI_FORMAT_R16_FLOAT; case GL_RG16F_EXT: return DXGI_FORMAT_R16G16_FLOAT; case GL_DEPTH_COMPONENT16: return DXGI_FORMAT_D16_UNORM; case GL_DEPTH_COMPONENT32_OES: case GL_DEPTH24_STENCIL8_OES: return DXGI_FORMAT_D24_UNORM_S8_UINT; case GL_NONE: return DXGI_FORMAT_UNKNOWN; default: UNREACHABLE(); } return DXGI_FORMAT_R8G8B8A8_UNORM; } } namespace d3d11 { void SetPositionTexCoordVertex(PositionTexCoordVertex* vertex, float x, float y, float u, float v) { vertex->x = x; vertex->y = y; vertex->u = u; vertex->v = v; } void SetPositionDepthColorVertex(PositionDepthColorVertex* vertex, float x, float y, float z, const gl::Color &color) { vertex->x = x; vertex->y = y; vertex->z = z; vertex->r = color.red; vertex->g = color.green; vertex->b = color.blue; vertex->a = color.alpha; } size_t ComputePixelSizeBits(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_R1_UNORM: return 1; case DXGI_FORMAT_A8_UNORM: case DXGI_FORMAT_R8_SINT: case DXGI_FORMAT_R8_SNORM: case DXGI_FORMAT_R8_TYPELESS: case DXGI_FORMAT_R8_UINT: case DXGI_FORMAT_R8_UNORM: return 8; case DXGI_FORMAT_B5G5R5A1_UNORM: case DXGI_FORMAT_B5G6R5_UNORM: case DXGI_FORMAT_D16_UNORM: case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16_SINT: case DXGI_FORMAT_R16_SNORM: case DXGI_FORMAT_R16_TYPELESS: case DXGI_FORMAT_R16_UINT: case DXGI_FORMAT_R16_UNORM: case DXGI_FORMAT_R8G8_SINT: case DXGI_FORMAT_R8G8_SNORM: case DXGI_FORMAT_R8G8_TYPELESS: case DXGI_FORMAT_R8G8_UINT: case DXGI_FORMAT_R8G8_UNORM: return 16; case DXGI_FORMAT_B8G8R8X8_TYPELESS: case DXGI_FORMAT_B8G8R8X8_UNORM: case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB: case DXGI_FORMAT_D24_UNORM_S8_UINT: case DXGI_FORMAT_D32_FLOAT: case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: case DXGI_FORMAT_G8R8_G8B8_UNORM: case DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM: case DXGI_FORMAT_R10G10B10A2_TYPELESS: case DXGI_FORMAT_R10G10B10A2_UINT: case DXGI_FORMAT_R10G10B10A2_UNORM: case DXGI_FORMAT_R11G11B10_FLOAT: case DXGI_FORMAT_R16G16_FLOAT: case DXGI_FORMAT_R16G16_SINT: case DXGI_FORMAT_R16G16_SNORM: case DXGI_FORMAT_R16G16_TYPELESS: case DXGI_FORMAT_R16G16_UINT: case DXGI_FORMAT_R16G16_UNORM: case DXGI_FORMAT_R24_UNORM_X8_TYPELESS: case DXGI_FORMAT_R24G8_TYPELESS: case DXGI_FORMAT_R32_FLOAT: case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS: case DXGI_FORMAT_R32_SINT: case DXGI_FORMAT_R32_TYPELESS: case DXGI_FORMAT_R32_UINT: case DXGI_FORMAT_R8G8_B8G8_UNORM: case DXGI_FORMAT_R8G8B8A8_SINT: case DXGI_FORMAT_R8G8B8A8_SNORM: case DXGI_FORMAT_R8G8B8A8_TYPELESS: case DXGI_FORMAT_R8G8B8A8_UINT: case DXGI_FORMAT_R8G8B8A8_UNORM: case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: case DXGI_FORMAT_B8G8R8A8_TYPELESS: case DXGI_FORMAT_B8G8R8A8_UNORM: case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB: case DXGI_FORMAT_R9G9B9E5_SHAREDEXP: case DXGI_FORMAT_X24_TYPELESS_G8_UINT: case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT: return 32; case DXGI_FORMAT_R16G16B16A16_FLOAT: case DXGI_FORMAT_R16G16B16A16_SINT: case DXGI_FORMAT_R16G16B16A16_SNORM: case DXGI_FORMAT_R16G16B16A16_TYPELESS: case DXGI_FORMAT_R16G16B16A16_UINT: case DXGI_FORMAT_R16G16B16A16_UNORM: case DXGI_FORMAT_R32G32_FLOAT: case DXGI_FORMAT_R32G32_SINT: case DXGI_FORMAT_R32G32_TYPELESS: case DXGI_FORMAT_R32G32_UINT: case DXGI_FORMAT_R32G8X24_TYPELESS: return 64; case DXGI_FORMAT_R32G32B32_FLOAT: case DXGI_FORMAT_R32G32B32_SINT: case DXGI_FORMAT_R32G32B32_TYPELESS: case DXGI_FORMAT_R32G32B32_UINT: return 96; case DXGI_FORMAT_R32G32B32A32_FLOAT: case DXGI_FORMAT_R32G32B32A32_SINT: case DXGI_FORMAT_R32G32B32A32_TYPELESS: case DXGI_FORMAT_R32G32B32A32_UINT: return 128; case DXGI_FORMAT_BC1_TYPELESS: case DXGI_FORMAT_BC1_UNORM: case DXGI_FORMAT_BC1_UNORM_SRGB: case DXGI_FORMAT_BC4_SNORM: case DXGI_FORMAT_BC4_TYPELESS: case DXGI_FORMAT_BC4_UNORM: return 4; case DXGI_FORMAT_BC2_TYPELESS: case DXGI_FORMAT_BC2_UNORM: case DXGI_FORMAT_BC2_UNORM_SRGB: case DXGI_FORMAT_BC3_TYPELESS: case DXGI_FORMAT_BC3_UNORM: case DXGI_FORMAT_BC3_UNORM_SRGB: case DXGI_FORMAT_BC5_SNORM: case DXGI_FORMAT_BC5_TYPELESS: case DXGI_FORMAT_BC5_UNORM: case DXGI_FORMAT_BC6H_SF16: case DXGI_FORMAT_BC6H_TYPELESS: case DXGI_FORMAT_BC6H_UF16: case DXGI_FORMAT_BC7_TYPELESS: case DXGI_FORMAT_BC7_UNORM: case DXGI_FORMAT_BC7_UNORM_SRGB: return 8; default: return 0; } } size_t ComputeBlockSizeBits(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_BC1_TYPELESS: case DXGI_FORMAT_BC1_UNORM: case DXGI_FORMAT_BC1_UNORM_SRGB: case DXGI_FORMAT_BC4_SNORM: case DXGI_FORMAT_BC4_TYPELESS: case DXGI_FORMAT_BC4_UNORM: case DXGI_FORMAT_BC2_TYPELESS: case DXGI_FORMAT_BC2_UNORM: case DXGI_FORMAT_BC2_UNORM_SRGB: case DXGI_FORMAT_BC3_TYPELESS: case DXGI_FORMAT_BC3_UNORM: case DXGI_FORMAT_BC3_UNORM_SRGB: case DXGI_FORMAT_BC5_SNORM: case DXGI_FORMAT_BC5_TYPELESS: case DXGI_FORMAT_BC5_UNORM: case DXGI_FORMAT_BC6H_SF16: case DXGI_FORMAT_BC6H_TYPELESS: case DXGI_FORMAT_BC6H_UF16: case DXGI_FORMAT_BC7_TYPELESS: case DXGI_FORMAT_BC7_UNORM: case DXGI_FORMAT_BC7_UNORM_SRGB: return ComputePixelSizeBits(format) * 16; default: UNREACHABLE(); return 0; } } bool IsCompressed(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_BC1_TYPELESS: case DXGI_FORMAT_BC1_UNORM: case DXGI_FORMAT_BC1_UNORM_SRGB: case DXGI_FORMAT_BC4_SNORM: case DXGI_FORMAT_BC4_TYPELESS: case DXGI_FORMAT_BC4_UNORM: case DXGI_FORMAT_BC2_TYPELESS: case DXGI_FORMAT_BC2_UNORM: case DXGI_FORMAT_BC2_UNORM_SRGB: case DXGI_FORMAT_BC3_TYPELESS: case DXGI_FORMAT_BC3_UNORM: case DXGI_FORMAT_BC3_UNORM_SRGB: case DXGI_FORMAT_BC5_SNORM: case DXGI_FORMAT_BC5_TYPELESS: case DXGI_FORMAT_BC5_UNORM: case DXGI_FORMAT_BC6H_SF16: case DXGI_FORMAT_BC6H_TYPELESS: case DXGI_FORMAT_BC6H_UF16: case DXGI_FORMAT_BC7_TYPELESS: case DXGI_FORMAT_BC7_UNORM: case DXGI_FORMAT_BC7_UNORM_SRGB: return true; case DXGI_FORMAT_UNKNOWN: UNREACHABLE(); return false; default: return false; } } unsigned int GetTextureFormatDimensionAlignment(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_BC1_TYPELESS: case DXGI_FORMAT_BC1_UNORM: case DXGI_FORMAT_BC1_UNORM_SRGB: case DXGI_FORMAT_BC4_SNORM: case DXGI_FORMAT_BC4_TYPELESS: case DXGI_FORMAT_BC4_UNORM: case DXGI_FORMAT_BC2_TYPELESS: case DXGI_FORMAT_BC2_UNORM: case DXGI_FORMAT_BC2_UNORM_SRGB: case DXGI_FORMAT_BC3_TYPELESS: case DXGI_FORMAT_BC3_UNORM: case DXGI_FORMAT_BC3_UNORM_SRGB: case DXGI_FORMAT_BC5_SNORM: case DXGI_FORMAT_BC5_TYPELESS: case DXGI_FORMAT_BC5_UNORM: case DXGI_FORMAT_BC6H_SF16: case DXGI_FORMAT_BC6H_TYPELESS: case DXGI_FORMAT_BC6H_UF16: case DXGI_FORMAT_BC7_TYPELESS: case DXGI_FORMAT_BC7_UNORM: case DXGI_FORMAT_BC7_UNORM_SRGB: return 4; case DXGI_FORMAT_UNKNOWN: UNREACHABLE(); return 1; default: return 1; } } bool IsDepthStencilFormat(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: case DXGI_FORMAT_D32_FLOAT: case DXGI_FORMAT_D24_UNORM_S8_UINT: case DXGI_FORMAT_D16_UNORM: return true; default: return false; } } DXGI_FORMAT GetDepthTextureFormat(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: return DXGI_FORMAT_R32G8X24_TYPELESS; case DXGI_FORMAT_D32_FLOAT: return DXGI_FORMAT_R32_TYPELESS; case DXGI_FORMAT_D24_UNORM_S8_UINT: return DXGI_FORMAT_R24G8_TYPELESS; case DXGI_FORMAT_D16_UNORM: return DXGI_FORMAT_R16_TYPELESS; default: UNREACHABLE(); return DXGI_FORMAT_UNKNOWN; } } DXGI_FORMAT GetDepthShaderResourceFormat(DXGI_FORMAT format) { switch (format) { case DXGI_FORMAT_D32_FLOAT_S8X24_UINT: return DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS; case DXGI_FORMAT_D32_FLOAT: return DXGI_FORMAT_R32_UINT; case DXGI_FORMAT_D24_UNORM_S8_UINT: return DXGI_FORMAT_R24_UNORM_X8_TYPELESS; case DXGI_FORMAT_D16_UNORM: return DXGI_FORMAT_R16_UNORM; default: UNREACHABLE(); return DXGI_FORMAT_UNKNOWN; } } HRESULT SetDebugName(ID3D11DeviceChild *resource, const char *name) { #if defined(_DEBUG) return resource->SetPrivateData(WKPDID_D3DDebugObjectName, strlen(name), name); #else return S_OK; #endif } }
C++
// // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // RenderStateCache.h: Defines rx::RenderStateCache, a cache of Direct3D render // state objects. #ifndef LIBGLESV2_RENDERER_RENDERSTATECACHE_H_ #define LIBGLESV2_RENDERER_RENDERSTATECACHE_H_ #include "libGLESv2/angletypes.h" #include "common/angleutils.h" namespace rx { class RenderStateCache { public: RenderStateCache(); virtual ~RenderStateCache(); void initialize(ID3D11Device *device); void clear(); // Increments refcount on the returned blend state, Release() must be called. ID3D11BlendState *getBlendState(const gl::BlendState &blendState); ID3D11RasterizerState *getRasterizerState(const gl::RasterizerState &rasterState, bool scissorEnabled, unsigned int depthSize); ID3D11DepthStencilState *getDepthStencilState(const gl::DepthStencilState &dsState); ID3D11SamplerState *getSamplerState(const gl::SamplerState &samplerState); private: DISALLOW_COPY_AND_ASSIGN(RenderStateCache); unsigned long long mCounter; // Blend state cache static std::size_t hashBlendState(const gl::BlendState &blendState); static bool compareBlendStates(const gl::BlendState &a, const gl::BlendState &b); static const unsigned int kMaxBlendStates; typedef std::size_t (*BlendStateHashFunction)(const gl::BlendState &); typedef bool (*BlendStateEqualityFunction)(const gl::BlendState &, const gl::BlendState &); typedef std::pair<ID3D11BlendState*, unsigned long long> BlendStateCounterPair; typedef std::unordered_map<gl::BlendState, BlendStateCounterPair, BlendStateHashFunction, BlendStateEqualityFunction> BlendStateMap; BlendStateMap mBlendStateCache; // Rasterizer state cache struct RasterizerStateKey { gl::RasterizerState rasterizerState; bool scissorEnabled; unsigned int depthSize; }; static std::size_t hashRasterizerState(const RasterizerStateKey &rasterState); static bool compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b); static const unsigned int kMaxRasterizerStates; typedef std::size_t (*RasterizerStateHashFunction)(const RasterizerStateKey &); typedef bool (*RasterizerStateEqualityFunction)(const RasterizerStateKey &, const RasterizerStateKey &); typedef std::pair<ID3D11RasterizerState*, unsigned long long> RasterizerStateCounterPair; typedef std::unordered_map<RasterizerStateKey, RasterizerStateCounterPair, RasterizerStateHashFunction, RasterizerStateEqualityFunction> RasterizerStateMap; RasterizerStateMap mRasterizerStateCache; // Depth stencil state cache static std::size_t hashDepthStencilState(const gl::DepthStencilState &dsState); static bool compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b); static const unsigned int kMaxDepthStencilStates; typedef std::size_t (*DepthStencilStateHashFunction)(const gl::DepthStencilState &); typedef bool (*DepthStencilStateEqualityFunction)(const gl::DepthStencilState &, const gl::DepthStencilState &); typedef std::pair<ID3D11DepthStencilState*, unsigned long long> DepthStencilStateCounterPair; typedef std::unordered_map<gl::DepthStencilState, DepthStencilStateCounterPair, DepthStencilStateHashFunction, DepthStencilStateEqualityFunction> DepthStencilStateMap; DepthStencilStateMap mDepthStencilStateCache; // Sample state cache static std::size_t hashSamplerState(const gl::SamplerState &samplerState); static bool compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b); static const unsigned int kMaxSamplerStates; typedef std::size_t (*SamplerStateHashFunction)(const gl::SamplerState &); typedef bool (*SamplerStateEqualityFunction)(const gl::SamplerState &, const gl::SamplerState &); typedef std::pair<ID3D11SamplerState*, unsigned long long> SamplerStateCounterPair; typedef std::unordered_map<gl::SamplerState, SamplerStateCounterPair, SamplerStateHashFunction, SamplerStateEqualityFunction> SamplerStateMap; SamplerStateMap mSamplerStateCache; ID3D11Device *mDevice; }; } #endif // LIBGLESV2_RENDERER_RENDERSTATECACHE_H_
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // renderer11_utils.h: Conversion functions and other utility routines // specific to the D3D11 renderer. #ifndef LIBGLESV2_RENDERER_RENDERER11_UTILS_H #define LIBGLESV2_RENDERER_RENDERER11_UTILS_H #include "libGLESv2/angletypes.h" namespace gl_d3d11 { D3D11_BLEND ConvertBlendFunc(GLenum glBlend, bool isAlpha); D3D11_BLEND_OP ConvertBlendOp(GLenum glBlendOp); UINT8 ConvertColorMask(bool maskRed, bool maskGreen, bool maskBlue, bool maskAlpha); D3D11_CULL_MODE ConvertCullMode(bool cullEnabled, GLenum cullMode); D3D11_COMPARISON_FUNC ConvertComparison(GLenum comparison); D3D11_DEPTH_WRITE_MASK ConvertDepthMask(bool depthWriteEnabled); UINT8 ConvertStencilMask(GLuint stencilmask); D3D11_STENCIL_OP ConvertStencilOp(GLenum stencilOp); D3D11_FILTER ConvertFilter(GLenum minFilter, GLenum magFilter, float maxAnisotropy); D3D11_TEXTURE_ADDRESS_MODE ConvertTextureWrap(GLenum wrap); FLOAT ConvertMinLOD(GLenum minFilter, unsigned int lodOffset); FLOAT ConvertMaxLOD(GLenum minFilter, unsigned int lodOffset); DXGI_FORMAT ConvertRenderbufferFormat(GLenum format); DXGI_FORMAT ConvertTextureFormat(GLenum format); } namespace d3d11_gl { GLenum ConvertBackBufferFormat(DXGI_FORMAT format); GLenum ConvertDepthStencilFormat(DXGI_FORMAT format); GLenum ConvertRenderbufferFormat(DXGI_FORMAT format); GLenum ConvertTextureInternalFormat(DXGI_FORMAT format); } namespace d3d11 { struct PositionTexCoordVertex { float x, y; float u, v; }; void SetPositionTexCoordVertex(PositionTexCoordVertex* vertex, float x, float y, float u, float v); struct PositionDepthColorVertex { float x, y, z; float r, g, b, a; }; void SetPositionDepthColorVertex(PositionDepthColorVertex* vertex, float x, float y, float z, const gl::Color &color); size_t ComputePixelSizeBits(DXGI_FORMAT format); size_t ComputeBlockSizeBits(DXGI_FORMAT format); bool IsCompressed(DXGI_FORMAT format); unsigned int GetTextureFormatDimensionAlignment(DXGI_FORMAT format); bool IsDepthStencilFormat(DXGI_FORMAT format); DXGI_FORMAT GetDepthTextureFormat(DXGI_FORMAT format); DXGI_FORMAT GetDepthShaderResourceFormat(DXGI_FORMAT format); HRESULT SetDebugName(ID3D11DeviceChild *resource, const char *name); inline bool isDeviceLostError(HRESULT errorCode) { switch (errorCode) { case DXGI_ERROR_DEVICE_HUNG: case DXGI_ERROR_DEVICE_REMOVED: case DXGI_ERROR_DEVICE_RESET: case DXGI_ERROR_DRIVER_INTERNAL_ERROR: case DXGI_ERROR_NOT_CURRENTLY_AVAILABLE: return true; default: return false; } } } #endif // LIBGLESV2_RENDERER_RENDERER11_UTILS_H
C++
// // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // QueryImpl.h: Defines the abstract rx::QueryImpl class. #ifndef LIBGLESV2_RENDERER_QUERYIMPL_H_ #define LIBGLESV2_RENDERER_QUERYIMPL_H_ #include "common/angleutils.h" namespace rx { class QueryImpl { public: explicit QueryImpl(GLenum type) : mType(type), mStatus(GL_FALSE), mResult(0) { } virtual ~QueryImpl() { } virtual void begin() = 0; virtual void end() = 0; virtual GLuint getResult() = 0; virtual GLboolean isResultAvailable() = 0; GLenum getType() const { return mType; } protected: GLuint mResult; GLboolean mStatus; private: DISALLOW_COPY_AND_ASSIGN(QueryImpl); GLenum mType; }; } #endif // LIBGLESV2_RENDERER_QUERYIMPL_H_
C++
#include "precompiled.h" // // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // TextureStorage.cpp: Implements the abstract rx::TextureStorageInterface class and its concrete derived // classes TextureStorageInterface2D and TextureStorageInterfaceCube, which act as the interface to the // GPU-side texture. #include "libGLESv2/renderer/TextureStorage.h" #include "libGLESv2/renderer/Renderer.h" #include "libGLESv2/Renderbuffer.h" #include "libGLESv2/Texture.h" #include "common/debug.h" namespace rx { unsigned int TextureStorageInterface::mCurrentTextureSerial = 1; TextureStorageInterface::TextureStorageInterface() : mTextureSerial(issueTextureSerial()), mInstance(NULL) { } TextureStorageInterface::~TextureStorageInterface() { delete mInstance; } bool TextureStorageInterface::isRenderTarget() const { return mInstance->isRenderTarget(); } bool TextureStorageInterface::isManaged() const { return mInstance->isManaged(); } unsigned int TextureStorageInterface::getTextureSerial() const { return mTextureSerial; } unsigned int TextureStorageInterface::issueTextureSerial() { return mCurrentTextureSerial++; } int TextureStorageInterface::getLodOffset() const { return mInstance->getLodOffset(); } int TextureStorageInterface::levelCount() { return mInstance->levelCount(); } TextureStorageInterface2D::TextureStorageInterface2D(Renderer *renderer, SwapChain *swapchain) : mRenderTargetSerial(gl::RenderbufferStorage::issueSerial()) { mInstance = renderer->createTextureStorage2D(swapchain); } TextureStorageInterface2D::TextureStorageInterface2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) : mRenderTargetSerial(gl::RenderbufferStorage::issueSerial()) { mInstance = renderer->createTextureStorage2D(levels, internalformat, usage, forceRenderable, width, height); } TextureStorageInterface2D::~TextureStorageInterface2D() { } RenderTarget *TextureStorageInterface2D::getRenderTarget() const { return mInstance->getRenderTarget(); } void TextureStorageInterface2D::generateMipmap(int level) { mInstance->generateMipmap(level); } unsigned int TextureStorageInterface2D::getRenderTargetSerial(GLenum target) const { return mRenderTargetSerial; } TextureStorageInterfaceCube::TextureStorageInterfaceCube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) : mFirstRenderTargetSerial(gl::RenderbufferStorage::issueCubeSerials()) { mInstance = renderer->createTextureStorageCube(levels, internalformat, usage, forceRenderable, size); } TextureStorageInterfaceCube::~TextureStorageInterfaceCube() { } RenderTarget *TextureStorageInterfaceCube::getRenderTarget(GLenum faceTarget) const { return mInstance->getRenderTarget(faceTarget); } void TextureStorageInterfaceCube::generateMipmap(int face, int level) { mInstance->generateMipmap(face, level); } unsigned int TextureStorageInterfaceCube::getRenderTargetSerial(GLenum target) const { return mFirstRenderTargetSerial + gl::TextureCubeMap::faceIndex(target); } }
C++
#include "precompiled.h" // // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Fence9.cpp: Defines the rx::Fence9 class. #include "libGLESv2/renderer/Fence9.h" #include "libGLESv2/main.h" #include "libGLESv2/renderer/renderer9_utils.h" #include "libGLESv2/renderer/Renderer9.h" namespace rx { Fence9::Fence9(rx::Renderer9 *renderer) { mRenderer = renderer; mQuery = NULL; } Fence9::~Fence9() { if (mQuery) { mRenderer->freeEventQuery(mQuery); mQuery = NULL; } } GLboolean Fence9::isFence() { // GL_NV_fence spec: // A name returned by GenFencesNV, but not yet set via SetFenceNV, is not the name of an existing fence. return mQuery != NULL; } void Fence9::setFence(GLenum condition) { if (!mQuery) { mQuery = mRenderer->allocateEventQuery(); if (!mQuery) { return gl::error(GL_OUT_OF_MEMORY); } } HRESULT result = mQuery->Issue(D3DISSUE_END); ASSERT(SUCCEEDED(result)); setCondition(condition); setStatus(GL_FALSE); } GLboolean Fence9::testFence() { if (mQuery == NULL) { return gl::error(GL_INVALID_OPERATION, GL_TRUE); } HRESULT result = mQuery->GetData(NULL, 0, D3DGETDATA_FLUSH); if (d3d9::isDeviceLostError(result)) { mRenderer->notifyDeviceLost(); return gl::error(GL_OUT_OF_MEMORY, GL_TRUE); } ASSERT(result == S_OK || result == S_FALSE); setStatus(result == S_OK); return getStatus(); } void Fence9::finishFence() { if (mQuery == NULL) { return gl::error(GL_INVALID_OPERATION); } while (!testFence()) { Sleep(0); } } void Fence9::getFenceiv(GLenum pname, GLint *params) { if (mQuery == NULL) { return gl::error(GL_INVALID_OPERATION); } switch (pname) { case GL_FENCE_STATUS_NV: { // GL_NV_fence spec: // Once the status of a fence has been finished (via FinishFenceNV) or tested and the returned status is TRUE (via either TestFenceNV // or GetFenceivNV querying the FENCE_STATUS_NV), the status remains TRUE until the next SetFenceNV of the fence. if (getStatus()) { params[0] = GL_TRUE; return; } HRESULT result = mQuery->GetData(NULL, 0, 0); if (d3d9::isDeviceLostError(result)) { params[0] = GL_TRUE; mRenderer->notifyDeviceLost(); return gl::error(GL_OUT_OF_MEMORY); } ASSERT(result == S_OK || result == S_FALSE); setStatus(result == S_OK); params[0] = getStatus(); break; } case GL_FENCE_CONDITION_NV: params[0] = getCondition(); break; default: return gl::error(GL_INVALID_ENUM); break; } } }
C++
#include "precompiled.h" // // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // renderer9_utils.cpp: Conversion functions and other utility routines // specific to the D3D9 renderer. #include "libGLESv2/renderer/renderer9_utils.h" #include "libGLESv2/mathutil.h" #include "libGLESv2/Context.h" #include "common/debug.h" namespace gl_d3d9 { D3DCMPFUNC ConvertComparison(GLenum comparison) { D3DCMPFUNC d3dComp = D3DCMP_ALWAYS; switch (comparison) { case GL_NEVER: d3dComp = D3DCMP_NEVER; break; case GL_ALWAYS: d3dComp = D3DCMP_ALWAYS; break; case GL_LESS: d3dComp = D3DCMP_LESS; break; case GL_LEQUAL: d3dComp = D3DCMP_LESSEQUAL; break; case GL_EQUAL: d3dComp = D3DCMP_EQUAL; break; case GL_GREATER: d3dComp = D3DCMP_GREATER; break; case GL_GEQUAL: d3dComp = D3DCMP_GREATEREQUAL; break; case GL_NOTEQUAL: d3dComp = D3DCMP_NOTEQUAL; break; default: UNREACHABLE(); } return d3dComp; } D3DCOLOR ConvertColor(gl::Color color) { return D3DCOLOR_RGBA(gl::unorm<8>(color.red), gl::unorm<8>(color.green), gl::unorm<8>(color.blue), gl::unorm<8>(color.alpha)); } D3DBLEND ConvertBlendFunc(GLenum blend) { D3DBLEND d3dBlend = D3DBLEND_ZERO; switch (blend) { case GL_ZERO: d3dBlend = D3DBLEND_ZERO; break; case GL_ONE: d3dBlend = D3DBLEND_ONE; break; case GL_SRC_COLOR: d3dBlend = D3DBLEND_SRCCOLOR; break; case GL_ONE_MINUS_SRC_COLOR: d3dBlend = D3DBLEND_INVSRCCOLOR; break; case GL_DST_COLOR: d3dBlend = D3DBLEND_DESTCOLOR; break; case GL_ONE_MINUS_DST_COLOR: d3dBlend = D3DBLEND_INVDESTCOLOR; break; case GL_SRC_ALPHA: d3dBlend = D3DBLEND_SRCALPHA; break; case GL_ONE_MINUS_SRC_ALPHA: d3dBlend = D3DBLEND_INVSRCALPHA; break; case GL_DST_ALPHA: d3dBlend = D3DBLEND_DESTALPHA; break; case GL_ONE_MINUS_DST_ALPHA: d3dBlend = D3DBLEND_INVDESTALPHA; break; case GL_CONSTANT_COLOR: d3dBlend = D3DBLEND_BLENDFACTOR; break; case GL_ONE_MINUS_CONSTANT_COLOR: d3dBlend = D3DBLEND_INVBLENDFACTOR; break; case GL_CONSTANT_ALPHA: d3dBlend = D3DBLEND_BLENDFACTOR; break; case GL_ONE_MINUS_CONSTANT_ALPHA: d3dBlend = D3DBLEND_INVBLENDFACTOR; break; case GL_SRC_ALPHA_SATURATE: d3dBlend = D3DBLEND_SRCALPHASAT; break; default: UNREACHABLE(); } return d3dBlend; } D3DBLENDOP ConvertBlendOp(GLenum blendOp) { D3DBLENDOP d3dBlendOp = D3DBLENDOP_ADD; switch (blendOp) { case GL_FUNC_ADD: d3dBlendOp = D3DBLENDOP_ADD; break; case GL_FUNC_SUBTRACT: d3dBlendOp = D3DBLENDOP_SUBTRACT; break; case GL_FUNC_REVERSE_SUBTRACT: d3dBlendOp = D3DBLENDOP_REVSUBTRACT; break; default: UNREACHABLE(); } return d3dBlendOp; } D3DSTENCILOP ConvertStencilOp(GLenum stencilOp) { D3DSTENCILOP d3dStencilOp = D3DSTENCILOP_KEEP; switch (stencilOp) { case GL_ZERO: d3dStencilOp = D3DSTENCILOP_ZERO; break; case GL_KEEP: d3dStencilOp = D3DSTENCILOP_KEEP; break; case GL_REPLACE: d3dStencilOp = D3DSTENCILOP_REPLACE; break; case GL_INCR: d3dStencilOp = D3DSTENCILOP_INCRSAT; break; case GL_DECR: d3dStencilOp = D3DSTENCILOP_DECRSAT; break; case GL_INVERT: d3dStencilOp = D3DSTENCILOP_INVERT; break; case GL_INCR_WRAP: d3dStencilOp = D3DSTENCILOP_INCR; break; case GL_DECR_WRAP: d3dStencilOp = D3DSTENCILOP_DECR; break; default: UNREACHABLE(); } return d3dStencilOp; } D3DTEXTUREADDRESS ConvertTextureWrap(GLenum wrap) { D3DTEXTUREADDRESS d3dWrap = D3DTADDRESS_WRAP; switch (wrap) { case GL_REPEAT: d3dWrap = D3DTADDRESS_WRAP; break; case GL_CLAMP_TO_EDGE: d3dWrap = D3DTADDRESS_CLAMP; break; case GL_MIRRORED_REPEAT: d3dWrap = D3DTADDRESS_MIRROR; break; default: UNREACHABLE(); } return d3dWrap; } D3DCULL ConvertCullMode(GLenum cullFace, GLenum frontFace) { D3DCULL cull = D3DCULL_CCW; switch (cullFace) { case GL_FRONT: cull = (frontFace == GL_CCW ? D3DCULL_CW : D3DCULL_CCW); break; case GL_BACK: cull = (frontFace == GL_CCW ? D3DCULL_CCW : D3DCULL_CW); break; case GL_FRONT_AND_BACK: cull = D3DCULL_NONE; // culling will be handled during draw break; default: UNREACHABLE(); } return cull; } D3DCUBEMAP_FACES ConvertCubeFace(GLenum cubeFace) { D3DCUBEMAP_FACES face = D3DCUBEMAP_FACE_POSITIVE_X; switch (cubeFace) { case GL_TEXTURE_CUBE_MAP_POSITIVE_X: face = D3DCUBEMAP_FACE_POSITIVE_X; break; case GL_TEXTURE_CUBE_MAP_NEGATIVE_X: face = D3DCUBEMAP_FACE_NEGATIVE_X; break; case GL_TEXTURE_CUBE_MAP_POSITIVE_Y: face = D3DCUBEMAP_FACE_POSITIVE_Y; break; case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: face = D3DCUBEMAP_FACE_NEGATIVE_Y; break; case GL_TEXTURE_CUBE_MAP_POSITIVE_Z: face = D3DCUBEMAP_FACE_POSITIVE_Z; break; case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: face = D3DCUBEMAP_FACE_NEGATIVE_Z; break; default: UNREACHABLE(); } return face; } DWORD ConvertColorMask(bool red, bool green, bool blue, bool alpha) { return (red ? D3DCOLORWRITEENABLE_RED : 0) | (green ? D3DCOLORWRITEENABLE_GREEN : 0) | (blue ? D3DCOLORWRITEENABLE_BLUE : 0) | (alpha ? D3DCOLORWRITEENABLE_ALPHA : 0); } D3DTEXTUREFILTERTYPE ConvertMagFilter(GLenum magFilter, float maxAnisotropy) { if (maxAnisotropy > 1.0f) { return D3DTEXF_ANISOTROPIC; } D3DTEXTUREFILTERTYPE d3dMagFilter = D3DTEXF_POINT; switch (magFilter) { case GL_NEAREST: d3dMagFilter = D3DTEXF_POINT; break; case GL_LINEAR: d3dMagFilter = D3DTEXF_LINEAR; break; default: UNREACHABLE(); } return d3dMagFilter; } void ConvertMinFilter(GLenum minFilter, D3DTEXTUREFILTERTYPE *d3dMinFilter, D3DTEXTUREFILTERTYPE *d3dMipFilter, float maxAnisotropy) { switch (minFilter) { case GL_NEAREST: *d3dMinFilter = D3DTEXF_POINT; *d3dMipFilter = D3DTEXF_NONE; break; case GL_LINEAR: *d3dMinFilter = D3DTEXF_LINEAR; *d3dMipFilter = D3DTEXF_NONE; break; case GL_NEAREST_MIPMAP_NEAREST: *d3dMinFilter = D3DTEXF_POINT; *d3dMipFilter = D3DTEXF_POINT; break; case GL_LINEAR_MIPMAP_NEAREST: *d3dMinFilter = D3DTEXF_LINEAR; *d3dMipFilter = D3DTEXF_POINT; break; case GL_NEAREST_MIPMAP_LINEAR: *d3dMinFilter = D3DTEXF_POINT; *d3dMipFilter = D3DTEXF_LINEAR; break; case GL_LINEAR_MIPMAP_LINEAR: *d3dMinFilter = D3DTEXF_LINEAR; *d3dMipFilter = D3DTEXF_LINEAR; break; default: *d3dMinFilter = D3DTEXF_POINT; *d3dMipFilter = D3DTEXF_NONE; UNREACHABLE(); } if (maxAnisotropy > 1.0f) { *d3dMinFilter = D3DTEXF_ANISOTROPIC; } } D3DFORMAT ConvertRenderbufferFormat(GLenum format) { switch (format) { case GL_NONE: return D3DFMT_NULL; case GL_RGBA4: case GL_RGB5_A1: case GL_RGBA8_OES: return D3DFMT_A8R8G8B8; case GL_RGB565: return D3DFMT_R5G6B5; case GL_RGB8_OES: return D3DFMT_X8R8G8B8; case GL_DEPTH_COMPONENT16: case GL_STENCIL_INDEX8: case GL_DEPTH24_STENCIL8_OES: return D3DFMT_D24S8; default: UNREACHABLE(); return D3DFMT_A8R8G8B8; } } D3DMULTISAMPLE_TYPE GetMultisampleTypeFromSamples(GLsizei samples) { if (samples <= 1) return D3DMULTISAMPLE_NONE; else return (D3DMULTISAMPLE_TYPE)samples; } } namespace d3d9_gl { unsigned int GetStencilSize(D3DFORMAT stencilFormat) { if (stencilFormat == D3DFMT_INTZ) { return 8; } switch(stencilFormat) { case D3DFMT_D24FS8: case D3DFMT_D24S8: return 8; case D3DFMT_D24X4S4: return 4; case D3DFMT_D15S1: return 1; case D3DFMT_D16_LOCKABLE: case D3DFMT_D32: case D3DFMT_D24X8: case D3DFMT_D32F_LOCKABLE: case D3DFMT_D16: return 0; //case D3DFMT_D32_LOCKABLE: return 0; // DirectX 9Ex only //case D3DFMT_S8_LOCKABLE: return 8; // DirectX 9Ex only default: return 0; } } unsigned int GetAlphaSize(D3DFORMAT colorFormat) { switch (colorFormat) { case D3DFMT_A16B16G16R16F: return 16; case D3DFMT_A32B32G32R32F: return 32; case D3DFMT_A2R10G10B10: return 2; case D3DFMT_A8R8G8B8: return 8; case D3DFMT_A1R5G5B5: return 1; case D3DFMT_X8R8G8B8: case D3DFMT_R5G6B5: return 0; default: return 0; } } GLsizei GetSamplesFromMultisampleType(D3DMULTISAMPLE_TYPE type) { if (type == D3DMULTISAMPLE_NONMASKABLE) return 0; else return type; } bool IsFormatChannelEquivalent(D3DFORMAT d3dformat, GLenum format) { switch (d3dformat) { case D3DFMT_L8: return (format == GL_LUMINANCE); case D3DFMT_A8L8: return (format == GL_LUMINANCE_ALPHA); case D3DFMT_DXT1: return (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT || format == GL_COMPRESSED_RGB_S3TC_DXT1_EXT); case D3DFMT_DXT3: return (format == GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE); case D3DFMT_DXT5: return (format == GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE); case D3DFMT_A8R8G8B8: case D3DFMT_A16B16G16R16F: case D3DFMT_A32B32G32R32F: return (format == GL_RGBA || format == GL_BGRA_EXT); case D3DFMT_X8R8G8B8: return (format == GL_RGB); default: if (d3dformat == D3DFMT_INTZ && gl::IsDepthTexture(format)) return true; return false; } } GLenum ConvertBackBufferFormat(D3DFORMAT format) { switch (format) { case D3DFMT_A4R4G4B4: return GL_RGBA4; case D3DFMT_A8R8G8B8: return GL_RGBA8_OES; case D3DFMT_A1R5G5B5: return GL_RGB5_A1; case D3DFMT_R5G6B5: return GL_RGB565; case D3DFMT_X8R8G8B8: return GL_RGB8_OES; default: UNREACHABLE(); } return GL_RGBA4; } GLenum ConvertDepthStencilFormat(D3DFORMAT format) { if (format == D3DFMT_INTZ) { return GL_DEPTH24_STENCIL8_OES; } switch (format) { case D3DFMT_D16: case D3DFMT_D24X8: return GL_DEPTH_COMPONENT16; case D3DFMT_D24S8: return GL_DEPTH24_STENCIL8_OES; case D3DFMT_UNKNOWN: return GL_NONE; default: UNREACHABLE(); } return GL_DEPTH24_STENCIL8_OES; } GLenum ConvertRenderTargetFormat(D3DFORMAT format) { if (format == D3DFMT_INTZ) { return GL_DEPTH24_STENCIL8_OES; } switch (format) { case D3DFMT_A4R4G4B4: return GL_RGBA4; case D3DFMT_A8R8G8B8: return GL_RGBA8_OES; case D3DFMT_A1R5G5B5: return GL_RGB5_A1; case D3DFMT_R5G6B5: return GL_RGB565; case D3DFMT_X8R8G8B8: return GL_RGB8_OES; case D3DFMT_D16: case D3DFMT_D24X8: return GL_DEPTH_COMPONENT16; case D3DFMT_D24S8: return GL_DEPTH24_STENCIL8_OES; case D3DFMT_UNKNOWN: return GL_NONE; default: UNREACHABLE(); } return GL_RGBA4; } GLenum GetEquivalentFormat(D3DFORMAT format) { if (format == D3DFMT_INTZ) return GL_DEPTH24_STENCIL8_OES; if (format == D3DFMT_NULL) return GL_NONE; switch (format) { case D3DFMT_A4R4G4B4: return GL_RGBA4; case D3DFMT_A8R8G8B8: return GL_RGBA8_OES; case D3DFMT_A1R5G5B5: return GL_RGB5_A1; case D3DFMT_R5G6B5: return GL_RGB565; case D3DFMT_X8R8G8B8: return GL_RGB8_OES; case D3DFMT_D16: return GL_DEPTH_COMPONENT16; case D3DFMT_D24S8: return GL_DEPTH24_STENCIL8_OES; case D3DFMT_UNKNOWN: return GL_NONE; case D3DFMT_DXT1: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; case D3DFMT_DXT3: return GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE; case D3DFMT_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE; case D3DFMT_A32B32G32R32F: return GL_RGBA32F_EXT; case D3DFMT_A16B16G16R16F: return GL_RGBA16F_EXT; case D3DFMT_L8: return GL_LUMINANCE8_EXT; case D3DFMT_A8L8: return GL_LUMINANCE8_ALPHA8_EXT; default: UNREACHABLE(); return GL_NONE; } } } namespace d3d9 { bool IsCompressedFormat(D3DFORMAT surfaceFormat) { switch(surfaceFormat) { case D3DFMT_DXT1: case D3DFMT_DXT2: case D3DFMT_DXT3: case D3DFMT_DXT4: case D3DFMT_DXT5: return true; default: return false; } } size_t ComputeRowSize(D3DFORMAT format, unsigned int width) { if (format == D3DFMT_INTZ) { return 4 * width; } switch (format) { case D3DFMT_L8: return 1 * width; case D3DFMT_A8L8: return 2 * width; case D3DFMT_X8R8G8B8: case D3DFMT_A8R8G8B8: return 4 * width; case D3DFMT_A16B16G16R16F: return 8 * width; case D3DFMT_A32B32G32R32F: return 16 * width; case D3DFMT_DXT1: return 8 * ((width + 3) / 4); case D3DFMT_DXT3: case D3DFMT_DXT5: return 16 * ((width + 3) / 4); default: UNREACHABLE(); return 0; } } }
C++
#include "precompiled.h" // // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // IndexDataManager.cpp: Defines the IndexDataManager, a class that // runs the Buffer translation process for index buffers. #include "libGLESv2/renderer/IndexDataManager.h" #include "libGLESv2/renderer/BufferStorage.h" #include "libGLESv2/Buffer.h" #include "libGLESv2/main.h" #include "libGLESv2/renderer/IndexBuffer.h" namespace rx { IndexDataManager::IndexDataManager(Renderer *renderer) : mRenderer(renderer) { mStreamingBufferShort = new StreamingIndexBufferInterface(mRenderer); if (!mStreamingBufferShort->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT)) { delete mStreamingBufferShort; mStreamingBufferShort = NULL; } mStreamingBufferInt = new StreamingIndexBufferInterface(mRenderer); if (!mStreamingBufferInt->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) { delete mStreamingBufferInt; mStreamingBufferInt = NULL; } if (!mStreamingBufferShort) { // Make sure both buffers are deleted. delete mStreamingBufferInt; mStreamingBufferInt = NULL; ERR("Failed to allocate the streaming index buffer(s)."); } mCountingBuffer = NULL; } IndexDataManager::~IndexDataManager() { delete mStreamingBufferShort; delete mStreamingBufferInt; delete mCountingBuffer; } static unsigned int indexTypeSize(GLenum type) { switch (type) { case GL_UNSIGNED_INT: return sizeof(GLuint); case GL_UNSIGNED_SHORT: return sizeof(GLushort); case GL_UNSIGNED_BYTE: return sizeof(GLubyte); default: UNREACHABLE(); return sizeof(GLushort); } } static void convertIndices(GLenum type, const void *input, GLsizei count, void *output) { if (type == GL_UNSIGNED_BYTE) { const GLubyte *in = static_cast<const GLubyte*>(input); GLushort *out = static_cast<GLushort*>(output); for (GLsizei i = 0; i < count; i++) { out[i] = in[i]; } } else if (type == GL_UNSIGNED_INT) { memcpy(output, input, count * sizeof(GLuint)); } else if (type == GL_UNSIGNED_SHORT) { memcpy(output, input, count * sizeof(GLushort)); } else UNREACHABLE(); } template <class IndexType> static void computeRange(const IndexType *indices, GLsizei count, GLuint *minIndex, GLuint *maxIndex) { *minIndex = indices[0]; *maxIndex = indices[0]; for (GLsizei i = 0; i < count; i++) { if (*minIndex > indices[i]) *minIndex = indices[i]; if (*maxIndex < indices[i]) *maxIndex = indices[i]; } } static void computeRange(GLenum type, const GLvoid *indices, GLsizei count, GLuint *minIndex, GLuint *maxIndex) { if (type == GL_UNSIGNED_BYTE) { computeRange(static_cast<const GLubyte*>(indices), count, minIndex, maxIndex); } else if (type == GL_UNSIGNED_INT) { computeRange(static_cast<const GLuint*>(indices), count, minIndex, maxIndex); } else if (type == GL_UNSIGNED_SHORT) { computeRange(static_cast<const GLushort*>(indices), count, minIndex, maxIndex); } else UNREACHABLE(); } GLenum IndexDataManager::prepareIndexData(GLenum type, GLsizei count, gl::Buffer *buffer, const GLvoid *indices, TranslatedIndexData *translated) { if (!mStreamingBufferShort) { return GL_OUT_OF_MEMORY; } GLenum destinationIndexType = (type == GL_UNSIGNED_INT) ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT; intptr_t offset = reinterpret_cast<intptr_t>(indices); bool alignedOffset = false; BufferStorage *storage = NULL; if (buffer != NULL) { storage = buffer->getStorage(); switch (type) { case GL_UNSIGNED_BYTE: alignedOffset = (offset % sizeof(GLubyte) == 0); break; case GL_UNSIGNED_SHORT: alignedOffset = (offset % sizeof(GLushort) == 0); break; case GL_UNSIGNED_INT: alignedOffset = (offset % sizeof(GLuint) == 0); break; default: UNREACHABLE(); alignedOffset = false; } if (indexTypeSize(type) * count + offset > storage->getSize()) { return GL_INVALID_OPERATION; } indices = static_cast<const GLubyte*>(storage->getData()) + offset; } StreamingIndexBufferInterface *streamingBuffer = (type == GL_UNSIGNED_INT) ? mStreamingBufferInt : mStreamingBufferShort; StaticIndexBufferInterface *staticBuffer = buffer ? buffer->getStaticIndexBuffer() : NULL; IndexBufferInterface *indexBuffer = streamingBuffer; bool directStorage = alignedOffset && storage && storage->supportsDirectBinding() && destinationIndexType == type; UINT streamOffset = 0; if (directStorage) { indexBuffer = streamingBuffer; streamOffset = offset; storage->markBufferUsage(); computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); } else if (staticBuffer && staticBuffer->getBufferSize() != 0 && staticBuffer->getIndexType() == type && alignedOffset) { indexBuffer = staticBuffer; streamOffset = staticBuffer->lookupRange(offset, count, &translated->minIndex, &translated->maxIndex); if (streamOffset == -1) { streamOffset = (offset / indexTypeSize(type)) * indexTypeSize(destinationIndexType); computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); staticBuffer->addRange(offset, count, translated->minIndex, translated->maxIndex, streamOffset); } } else { int convertCount = count; if (staticBuffer) { if (staticBuffer->getBufferSize() == 0 && alignedOffset) { indexBuffer = staticBuffer; convertCount = storage->getSize() / indexTypeSize(type); } else { buffer->invalidateStaticData(); staticBuffer = NULL; } } if (!indexBuffer) { ERR("No valid index buffer."); return GL_INVALID_OPERATION; } unsigned int bufferSizeRequired = convertCount * indexTypeSize(destinationIndexType); indexBuffer->reserveBufferSpace(bufferSizeRequired, type); void* output = NULL; streamOffset = indexBuffer->mapBuffer(bufferSizeRequired, &output); if (streamOffset == -1 || output == NULL) { ERR("Failed to map index buffer."); return GL_OUT_OF_MEMORY; } convertIndices(type, staticBuffer ? storage->getData() : indices, convertCount, output); if (!indexBuffer->unmapBuffer()) { ERR("Failed to unmap index buffer."); return GL_OUT_OF_MEMORY; } computeRange(type, indices, count, &translated->minIndex, &translated->maxIndex); if (staticBuffer) { streamOffset = (offset / indexTypeSize(type)) * indexTypeSize(destinationIndexType); staticBuffer->addRange(offset, count, translated->minIndex, translated->maxIndex, streamOffset); } } translated->storage = directStorage ? storage : NULL; translated->indexBuffer = indexBuffer->getIndexBuffer(); translated->serial = directStorage ? storage->getSerial() : indexBuffer->getSerial(); translated->startIndex = streamOffset / indexTypeSize(destinationIndexType); translated->startOffset = streamOffset; if (buffer) { buffer->promoteStaticUsage(count * indexTypeSize(type)); } return GL_NO_ERROR; } StaticIndexBufferInterface *IndexDataManager::getCountingIndices(GLsizei count) { if (count <= 65536) // 16-bit indices { const unsigned int spaceNeeded = count * sizeof(unsigned short); if (!mCountingBuffer || mCountingBuffer->getBufferSize() < spaceNeeded) { delete mCountingBuffer; mCountingBuffer = new StaticIndexBufferInterface(mRenderer); mCountingBuffer->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT); void* mappedMemory = NULL; if (mCountingBuffer->mapBuffer(spaceNeeded, &mappedMemory) == -1 || mappedMemory == NULL) { ERR("Failed to map counting buffer."); return NULL; } unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory); for(int i = 0; i < count; i++) { data[i] = i; } if (!mCountingBuffer->unmapBuffer()) { ERR("Failed to unmap counting buffer."); return NULL; } } } else if (mStreamingBufferInt) // 32-bit indices supported { const unsigned int spaceNeeded = count * sizeof(unsigned int); if (!mCountingBuffer || mCountingBuffer->getBufferSize() < spaceNeeded) { delete mCountingBuffer; mCountingBuffer = new StaticIndexBufferInterface(mRenderer); mCountingBuffer->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT); void* mappedMemory = NULL; if (mCountingBuffer->mapBuffer(spaceNeeded, &mappedMemory) == -1 || mappedMemory == NULL) { ERR("Failed to map counting buffer."); return NULL; } unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); for(int i = 0; i < count; i++) { data[i] = i; } if (!mCountingBuffer->unmapBuffer()) { ERR("Failed to unmap counting buffer."); return NULL; } } } else { return NULL; } return mCountingBuffer; } }
C++
#include "precompiled.h" // // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // IndexBuffer.cpp: Defines the abstract IndexBuffer class and IndexBufferInterface // class with derivations, classes that perform graphics API agnostic index buffer operations. #include "libGLESv2/renderer/IndexBuffer.h" #include "libGLESv2/renderer/Renderer.h" namespace rx { unsigned int IndexBuffer::mNextSerial = 1; IndexBuffer::IndexBuffer() { updateSerial(); } IndexBuffer::~IndexBuffer() { } unsigned int IndexBuffer::getSerial() const { return mSerial; } void IndexBuffer::updateSerial() { mSerial = mNextSerial++; } IndexBufferInterface::IndexBufferInterface(Renderer *renderer, bool dynamic) : mRenderer(renderer) { mIndexBuffer = renderer->createIndexBuffer(); mDynamic = dynamic; mWritePosition = 0; } IndexBufferInterface::~IndexBufferInterface() { if (mIndexBuffer) { delete mIndexBuffer; } } GLenum IndexBufferInterface::getIndexType() const { return mIndexBuffer->getIndexType(); } unsigned int IndexBufferInterface::getBufferSize() const { return mIndexBuffer->getBufferSize(); } unsigned int IndexBufferInterface::getSerial() const { return mIndexBuffer->getSerial(); } int IndexBufferInterface::mapBuffer(unsigned int size, void** outMappedMemory) { if (!mIndexBuffer->mapBuffer(mWritePosition, size, outMappedMemory)) { *outMappedMemory = NULL; return -1; } int oldWritePos = static_cast<int>(mWritePosition); mWritePosition += size; return oldWritePos; } bool IndexBufferInterface::unmapBuffer() { return mIndexBuffer->unmapBuffer(); } IndexBuffer * IndexBufferInterface::getIndexBuffer() const { return mIndexBuffer; } unsigned int IndexBufferInterface::getWritePosition() const { return mWritePosition; } void IndexBufferInterface::setWritePosition(unsigned int writePosition) { mWritePosition = writePosition; } bool IndexBufferInterface::discard() { return mIndexBuffer->discard(); } bool IndexBufferInterface::setBufferSize(unsigned int bufferSize, GLenum indexType) { if (mIndexBuffer->getBufferSize() == 0) { return mIndexBuffer->initialize(bufferSize, indexType, mDynamic); } else { return mIndexBuffer->setSize(bufferSize, indexType); } } StreamingIndexBufferInterface::StreamingIndexBufferInterface(Renderer *renderer) : IndexBufferInterface(renderer, true) { } StreamingIndexBufferInterface::~StreamingIndexBufferInterface() { } bool StreamingIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType) { bool result = true; unsigned int curBufferSize = getBufferSize(); if (size > curBufferSize) { result = setBufferSize(std::max(size, 2 * curBufferSize), indexType); setWritePosition(0); } else if (getWritePosition() + size > curBufferSize) { if (!discard()) { return false; } setWritePosition(0); } return result; } StaticIndexBufferInterface::StaticIndexBufferInterface(Renderer *renderer) : IndexBufferInterface(renderer, false) { } StaticIndexBufferInterface::~StaticIndexBufferInterface() { } bool StaticIndexBufferInterface::reserveBufferSpace(unsigned int size, GLenum indexType) { unsigned int curSize = getBufferSize(); if (curSize == 0) { return setBufferSize(size, indexType); } else if (curSize >= size && indexType == getIndexType()) { return true; } else { ERR("Static index buffers can't be resized"); UNREACHABLE(); return false; } } unsigned int StaticIndexBufferInterface::lookupRange(intptr_t offset, GLsizei count, unsigned int *minIndex, unsigned int *maxIndex) { IndexRange range = {offset, count}; std::map<IndexRange, IndexResult>::iterator res = mCache.find(range); if (res == mCache.end()) { return -1; } *minIndex = res->second.minIndex; *maxIndex = res->second.maxIndex; return res->second.streamOffset; } void StaticIndexBufferInterface::addRange(intptr_t offset, GLsizei count, unsigned int minIndex, unsigned int maxIndex, unsigned int streamOffset) { IndexRange indexRange = {offset, count}; IndexResult indexResult = {minIndex, maxIndex, streamOffset}; mCache[indexRange] = indexResult; } }
C++
// // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Image.h: Defines the rx::Image class, an abstract base class for the // renderer-specific classes which will define the interface to the underlying // surfaces or resources. #ifndef LIBGLESV2_RENDERER_IMAGE_H_ #define LIBGLESV2_RENDERER_IMAGE_H_ #include "common/debug.h" namespace gl { class Framebuffer; } namespace rx { class Renderer; class TextureStorageInterface2D; class TextureStorageInterfaceCube; class Image { public: Image(); virtual ~Image() {}; GLsizei getWidth() const { return mWidth; } GLsizei getHeight() const { return mHeight; } GLenum getInternalFormat() const { return mInternalFormat; } GLenum getActualFormat() const { return mActualFormat; } void markDirty() {mDirty = true;} void markClean() {mDirty = false;} virtual bool isDirty() const = 0; virtual void setManagedSurface(TextureStorageInterface2D *storage, int level) {}; virtual void setManagedSurface(TextureStorageInterfaceCube *storage, int face, int level) {}; virtual bool updateSurface(TextureStorageInterface2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) = 0; virtual bool updateSurface(TextureStorageInterfaceCube *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) = 0; virtual bool redefine(Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease) = 0; virtual bool isRenderableFormat() const = 0; virtual void loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLint unpackAlignment, const void *input) = 0; virtual void loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, const void *input) = 0; virtual void copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source) = 0; static void loadAlphaDataToBGRA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadAlphaDataToNative(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadAlphaDataToBGRASSE2(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadAlphaFloatDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadAlphaHalfFloatDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadLuminanceDataToNativeOrBGRA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output, bool native); static void loadLuminanceFloatDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadLuminanceFloatDataToRGB(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadLuminanceHalfFloatDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadLuminanceAlphaDataToNativeOrBGRA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output, bool native); static void loadLuminanceAlphaFloatDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadLuminanceAlphaHalfFloatDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBUByteDataToBGRX(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBUByteDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGB565DataToBGRA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGB565DataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBFloatDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBFloatDataToNative(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBHalfFloatDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBAUByteDataToBGRASSE2(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBAUByteDataToBGRA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBAUByteDataToNative(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBA4444DataToBGRA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBA4444DataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBA5551DataToBGRA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBA5551DataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBAFloatDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadRGBAHalfFloatDataToRGBA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); static void loadBGRADataToBGRA(GLsizei width, GLsizei height, int inputPitch, const void *input, size_t outputPitch, void *output); protected: GLsizei mWidth; GLsizei mHeight; GLint mInternalFormat; GLenum mActualFormat; bool mDirty; private: DISALLOW_COPY_AND_ASSIGN(Image); }; } #endif // LIBGLESV2_RENDERER_IMAGE_H_
C++
#include "precompiled.h" // // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // RenderStateCache.cpp: Defines rx::RenderStateCache, a cache of Direct3D render // state objects. #include "libGLESv2/renderer/RenderStateCache.h" #include "libGLESv2/renderer/renderer11_utils.h" #include "common/debug.h" #include "third_party/murmurhash/MurmurHash3.h" namespace rx { // MSDN's documentation of ID3D11Device::CreateBlendState, ID3D11Device::CreateRasterizerState, // ID3D11Device::CreateDepthStencilState and ID3D11Device::CreateSamplerState claims the maximum // number of unique states of each type an application can create is 4096 const unsigned int RenderStateCache::kMaxBlendStates = 4096; const unsigned int RenderStateCache::kMaxRasterizerStates = 4096; const unsigned int RenderStateCache::kMaxDepthStencilStates = 4096; const unsigned int RenderStateCache::kMaxSamplerStates = 4096; RenderStateCache::RenderStateCache() : mDevice(NULL), mCounter(0), mBlendStateCache(kMaxBlendStates, hashBlendState, compareBlendStates), mRasterizerStateCache(kMaxRasterizerStates, hashRasterizerState, compareRasterizerStates), mDepthStencilStateCache(kMaxDepthStencilStates, hashDepthStencilState, compareDepthStencilStates), mSamplerStateCache(kMaxSamplerStates, hashSamplerState, compareSamplerStates) { } RenderStateCache::~RenderStateCache() { clear(); } void RenderStateCache::initialize(ID3D11Device *device) { clear(); mDevice = device; } void RenderStateCache::clear() { for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++) { i->second.first->Release(); } mBlendStateCache.clear(); for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++) { i->second.first->Release(); } mRasterizerStateCache.clear(); for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++) { i->second.first->Release(); } mDepthStencilStateCache.clear(); for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++) { i->second.first->Release(); } mSamplerStateCache.clear(); } std::size_t RenderStateCache::hashBlendState(const gl::BlendState &blendState) { static const unsigned int seed = 0xABCDEF98; std::size_t hash = 0; MurmurHash3_x86_32(&blendState, sizeof(gl::BlendState), seed, &hash); return hash; } bool RenderStateCache::compareBlendStates(const gl::BlendState &a, const gl::BlendState &b) { return memcmp(&a, &b, sizeof(gl::BlendState)) == 0; } ID3D11BlendState *RenderStateCache::getBlendState(const gl::BlendState &blendState) { if (!mDevice) { ERR("RenderStateCache is not initialized."); return NULL; } BlendStateMap::iterator i = mBlendStateCache.find(blendState); if (i != mBlendStateCache.end()) { BlendStateCounterPair &state = i->second; state.second = mCounter++; return state.first; } else { if (mBlendStateCache.size() >= kMaxBlendStates) { TRACE("Overflowed the limit of %u blend states, removing the least recently used " "to make room.", kMaxBlendStates); BlendStateMap::iterator leastRecentlyUsed = mBlendStateCache.begin(); for (BlendStateMap::iterator i = mBlendStateCache.begin(); i != mBlendStateCache.end(); i++) { if (i->second.second < leastRecentlyUsed->second.second) { leastRecentlyUsed = i; } } leastRecentlyUsed->second.first->Release(); mBlendStateCache.erase(leastRecentlyUsed); } // Create a new blend state and insert it into the cache D3D11_BLEND_DESC blendDesc = { 0 }; blendDesc.AlphaToCoverageEnable = blendState.sampleAlphaToCoverage; blendDesc.IndependentBlendEnable = FALSE; for (unsigned int i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; i++) { D3D11_RENDER_TARGET_BLEND_DESC &rtBlend = blendDesc.RenderTarget[i]; rtBlend.BlendEnable = blendState.blend; if (blendState.blend) { rtBlend.SrcBlend = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendRGB, false); rtBlend.DestBlend = gl_d3d11::ConvertBlendFunc(blendState.destBlendRGB, false); rtBlend.BlendOp = gl_d3d11::ConvertBlendOp(blendState.blendEquationRGB); rtBlend.SrcBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.sourceBlendAlpha, true); rtBlend.DestBlendAlpha = gl_d3d11::ConvertBlendFunc(blendState.destBlendAlpha, true); rtBlend.BlendOpAlpha = gl_d3d11::ConvertBlendOp(blendState.blendEquationAlpha); } rtBlend.RenderTargetWriteMask = gl_d3d11::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, blendState.colorMaskBlue, blendState.colorMaskAlpha); } ID3D11BlendState *dx11BlendState = NULL; HRESULT result = mDevice->CreateBlendState(&blendDesc, &dx11BlendState); if (FAILED(result) || !dx11BlendState) { ERR("Unable to create a ID3D11BlendState, HRESULT: 0x%X.", result); return NULL; } mBlendStateCache.insert(std::make_pair(blendState, std::make_pair(dx11BlendState, mCounter++))); return dx11BlendState; } } std::size_t RenderStateCache::hashRasterizerState(const RasterizerStateKey &rasterState) { static const unsigned int seed = 0xABCDEF98; std::size_t hash = 0; MurmurHash3_x86_32(&rasterState, sizeof(RasterizerStateKey), seed, &hash); return hash; } bool RenderStateCache::compareRasterizerStates(const RasterizerStateKey &a, const RasterizerStateKey &b) { return memcmp(&a, &b, sizeof(RasterizerStateKey)) == 0; } ID3D11RasterizerState *RenderStateCache::getRasterizerState(const gl::RasterizerState &rasterState, bool scissorEnabled, unsigned int depthSize) { if (!mDevice) { ERR("RenderStateCache is not initialized."); return NULL; } RasterizerStateKey key; key.rasterizerState = rasterState; key.scissorEnabled = scissorEnabled; key.depthSize = depthSize; RasterizerStateMap::iterator i = mRasterizerStateCache.find(key); if (i != mRasterizerStateCache.end()) { RasterizerStateCounterPair &state = i->second; state.second = mCounter++; return state.first; } else { if (mRasterizerStateCache.size() >= kMaxRasterizerStates) { TRACE("Overflowed the limit of %u rasterizer states, removing the least recently used " "to make room.", kMaxRasterizerStates); RasterizerStateMap::iterator leastRecentlyUsed = mRasterizerStateCache.begin(); for (RasterizerStateMap::iterator i = mRasterizerStateCache.begin(); i != mRasterizerStateCache.end(); i++) { if (i->second.second < leastRecentlyUsed->second.second) { leastRecentlyUsed = i; } } leastRecentlyUsed->second.first->Release(); mRasterizerStateCache.erase(leastRecentlyUsed); } D3D11_CULL_MODE cullMode = gl_d3d11::ConvertCullMode(rasterState.cullFace, rasterState.cullMode); // Disable culling if drawing points if (rasterState.pointDrawMode) { cullMode = D3D11_CULL_NONE; } D3D11_RASTERIZER_DESC rasterDesc; rasterDesc.FillMode = D3D11_FILL_SOLID; rasterDesc.CullMode = cullMode; rasterDesc.FrontCounterClockwise = (rasterState.frontFace == GL_CCW) ? FALSE: TRUE; rasterDesc.DepthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(depthSize)); rasterDesc.DepthBiasClamp = 0.0f; // MSDN documentation of DepthBiasClamp implies a value of zero will preform no clamping, must be tested though. rasterDesc.SlopeScaledDepthBias = rasterState.polygonOffsetFactor; rasterDesc.DepthClipEnable = TRUE; rasterDesc.ScissorEnable = scissorEnabled ? TRUE : FALSE; rasterDesc.MultisampleEnable = rasterState.multiSample; rasterDesc.AntialiasedLineEnable = FALSE; ID3D11RasterizerState *dx11RasterizerState = NULL; HRESULT result = mDevice->CreateRasterizerState(&rasterDesc, &dx11RasterizerState); if (FAILED(result) || !dx11RasterizerState) { ERR("Unable to create a ID3D11RasterizerState, HRESULT: 0x%X.", result); return NULL; } mRasterizerStateCache.insert(std::make_pair(key, std::make_pair(dx11RasterizerState, mCounter++))); return dx11RasterizerState; } } std::size_t RenderStateCache::hashDepthStencilState(const gl::DepthStencilState &dsState) { static const unsigned int seed = 0xABCDEF98; std::size_t hash = 0; MurmurHash3_x86_32(&dsState, sizeof(gl::DepthStencilState), seed, &hash); return hash; } bool RenderStateCache::compareDepthStencilStates(const gl::DepthStencilState &a, const gl::DepthStencilState &b) { return memcmp(&a, &b, sizeof(gl::DepthStencilState)) == 0; } ID3D11DepthStencilState *RenderStateCache::getDepthStencilState(const gl::DepthStencilState &dsState) { if (!mDevice) { ERR("RenderStateCache is not initialized."); return NULL; } DepthStencilStateMap::iterator i = mDepthStencilStateCache.find(dsState); if (i != mDepthStencilStateCache.end()) { DepthStencilStateCounterPair &state = i->second; state.second = mCounter++; return state.first; } else { if (mDepthStencilStateCache.size() >= kMaxDepthStencilStates) { TRACE("Overflowed the limit of %u depth stencil states, removing the least recently used " "to make room.", kMaxDepthStencilStates); DepthStencilStateMap::iterator leastRecentlyUsed = mDepthStencilStateCache.begin(); for (DepthStencilStateMap::iterator i = mDepthStencilStateCache.begin(); i != mDepthStencilStateCache.end(); i++) { if (i->second.second < leastRecentlyUsed->second.second) { leastRecentlyUsed = i; } } leastRecentlyUsed->second.first->Release(); mDepthStencilStateCache.erase(leastRecentlyUsed); } D3D11_DEPTH_STENCIL_DESC dsDesc = { 0 }; dsDesc.DepthEnable = dsState.depthTest ? TRUE : FALSE; dsDesc.DepthWriteMask = gl_d3d11::ConvertDepthMask(dsState.depthMask); dsDesc.DepthFunc = gl_d3d11::ConvertComparison(dsState.depthFunc); dsDesc.StencilEnable = dsState.stencilTest ? TRUE : FALSE; dsDesc.StencilReadMask = gl_d3d11::ConvertStencilMask(dsState.stencilMask); dsDesc.StencilWriteMask = gl_d3d11::ConvertStencilMask(dsState.stencilWritemask); dsDesc.FrontFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilFail); dsDesc.FrontFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthFail); dsDesc.FrontFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilPassDepthPass); dsDesc.FrontFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilFunc); dsDesc.BackFace.StencilFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackFail); dsDesc.BackFace.StencilDepthFailOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthFail); dsDesc.BackFace.StencilPassOp = gl_d3d11::ConvertStencilOp(dsState.stencilBackPassDepthPass); dsDesc.BackFace.StencilFunc = gl_d3d11::ConvertComparison(dsState.stencilBackFunc); ID3D11DepthStencilState *dx11DepthStencilState = NULL; HRESULT result = mDevice->CreateDepthStencilState(&dsDesc, &dx11DepthStencilState); if (FAILED(result) || !dx11DepthStencilState) { ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result); return NULL; } mDepthStencilStateCache.insert(std::make_pair(dsState, std::make_pair(dx11DepthStencilState, mCounter++))); return dx11DepthStencilState; } } std::size_t RenderStateCache::hashSamplerState(const gl::SamplerState &samplerState) { static const unsigned int seed = 0xABCDEF98; std::size_t hash = 0; MurmurHash3_x86_32(&samplerState, sizeof(gl::SamplerState), seed, &hash); return hash; } bool RenderStateCache::compareSamplerStates(const gl::SamplerState &a, const gl::SamplerState &b) { return memcmp(&a, &b, sizeof(gl::SamplerState)) == 0; } ID3D11SamplerState *RenderStateCache::getSamplerState(const gl::SamplerState &samplerState) { if (!mDevice) { ERR("RenderStateCache is not initialized."); return NULL; } SamplerStateMap::iterator i = mSamplerStateCache.find(samplerState); if (i != mSamplerStateCache.end()) { SamplerStateCounterPair &state = i->second; state.second = mCounter++; return state.first; } else { if (mSamplerStateCache.size() >= kMaxSamplerStates) { TRACE("Overflowed the limit of %u sampler states, removing the least recently used " "to make room.", kMaxSamplerStates); SamplerStateMap::iterator leastRecentlyUsed = mSamplerStateCache.begin(); for (SamplerStateMap::iterator i = mSamplerStateCache.begin(); i != mSamplerStateCache.end(); i++) { if (i->second.second < leastRecentlyUsed->second.second) { leastRecentlyUsed = i; } } leastRecentlyUsed->second.first->Release(); mSamplerStateCache.erase(leastRecentlyUsed); } D3D11_SAMPLER_DESC samplerDesc; samplerDesc.Filter = gl_d3d11::ConvertFilter(samplerState.minFilter, samplerState.magFilter, samplerState.maxAnisotropy); samplerDesc.AddressU = gl_d3d11::ConvertTextureWrap(samplerState.wrapS); samplerDesc.AddressV = gl_d3d11::ConvertTextureWrap(samplerState.wrapT); samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.MipLODBias = static_cast<float>(samplerState.lodOffset); samplerDesc.MaxAnisotropy = samplerState.maxAnisotropy; samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; samplerDesc.BorderColor[0] = 0.0f; samplerDesc.BorderColor[1] = 0.0f; samplerDesc.BorderColor[2] = 0.0f; samplerDesc.BorderColor[3] = 0.0f; samplerDesc.MinLOD = gl_d3d11::ConvertMinLOD(samplerState.minFilter, samplerState.lodOffset); samplerDesc.MaxLOD = gl_d3d11::ConvertMaxLOD(samplerState.minFilter, samplerState.lodOffset); ID3D11SamplerState *dx11SamplerState = NULL; HRESULT result = mDevice->CreateSamplerState(&samplerDesc, &dx11SamplerState); if (FAILED(result) || !dx11SamplerState) { ERR("Unable to create a ID3D11DepthStencilState, HRESULT: 0x%X.", result); return NULL; } mSamplerStateCache.insert(std::make_pair(samplerState, std::make_pair(dx11SamplerState, mCounter++))); return dx11SamplerState; } } }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // VertexBuffer11.h: Defines the D3D11 VertexBuffer implementation. #ifndef LIBGLESV2_RENDERER_VERTEXBUFFER11_H_ #define LIBGLESV2_RENDERER_VERTEXBUFFER11_H_ #include "libGLESv2/renderer/VertexBuffer.h" namespace rx { class Renderer11; class VertexBuffer11 : public VertexBuffer { public: explicit VertexBuffer11(rx::Renderer11 *const renderer); virtual ~VertexBuffer11(); virtual bool initialize(unsigned int size, bool dynamicUsage); static VertexBuffer11 *makeVertexBuffer11(VertexBuffer *vetexBuffer); virtual bool storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances, unsigned int offset); virtual bool storeRawData(const void* data, unsigned int size, unsigned int offset); virtual unsigned int getSpaceRequired(const gl::VertexAttribute &attrib, GLsizei count, GLsizei instances) const; virtual bool requiresConversion(const gl::VertexAttribute &attrib) const; virtual unsigned int getBufferSize() const; virtual bool setBufferSize(unsigned int size); virtual bool discard(); unsigned int getVertexSize(const gl::VertexAttribute &attrib) const; DXGI_FORMAT getDXGIFormat(const gl::VertexAttribute &attrib) const; ID3D11Buffer *getBuffer() const; private: DISALLOW_COPY_AND_ASSIGN(VertexBuffer11); rx::Renderer11 *const mRenderer; ID3D11Buffer *mBuffer; unsigned int mBufferSize; bool mDynamicUsage; typedef void (*VertexConversionFunction)(const void *, unsigned int, unsigned int, void *); struct VertexConverter { VertexConversionFunction conversionFunc; bool identity; DXGI_FORMAT dxgiFormat; unsigned int outputElementSize; }; enum { NUM_GL_VERTEX_ATTRIB_TYPES = 6 }; // This table is used to generate mAttributeTypes. static const VertexConverter mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4]; // [GL types as enumerated by typeIndex()][normalized][size - 1] static const VertexConverter &getVertexConversion(const gl::VertexAttribute &attribute); }; } #endif // LIBGLESV2_RENDERER_VERTEXBUFFER11_H_
C++
#include "precompiled.h" // // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // VertexDataManager.h: Defines the VertexDataManager, a class that // runs the Buffer translation process. #include "libGLESv2/renderer/VertexDataManager.h" #include "libGLESv2/renderer/BufferStorage.h" #include "libGLESv2/Buffer.h" #include "libGLESv2/ProgramBinary.h" #include "libGLESv2/Context.h" #include "libGLESv2/renderer/VertexBuffer.h" namespace { enum { INITIAL_STREAM_BUFFER_SIZE = 1024*1024 }; // This has to be at least 4k or else it fails on ATI cards. enum { CONSTANT_VERTEX_BUFFER_SIZE = 4096 }; } namespace rx { static int elementsInBuffer(const gl::VertexAttribute &attribute, int size) { int stride = attribute.stride(); return (size - attribute.mOffset % stride + (stride - attribute.typeSize())) / stride; } VertexDataManager::VertexDataManager(Renderer *renderer) : mRenderer(renderer) { for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { mCurrentValue[i][0] = std::numeric_limits<float>::quiet_NaN(); mCurrentValue[i][1] = std::numeric_limits<float>::quiet_NaN(); mCurrentValue[i][2] = std::numeric_limits<float>::quiet_NaN(); mCurrentValue[i][3] = std::numeric_limits<float>::quiet_NaN(); mCurrentValueBuffer[i] = NULL; mCurrentValueOffsets[i] = 0; } mStreamingBuffer = new StreamingVertexBufferInterface(renderer, INITIAL_STREAM_BUFFER_SIZE); if (!mStreamingBuffer) { ERR("Failed to allocate the streaming vertex buffer."); } } VertexDataManager::~VertexDataManager() { delete mStreamingBuffer; for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { delete mCurrentValueBuffer[i]; } } static bool directStoragePossible(VertexBufferInterface* vb, const gl::VertexAttribute& attrib) { gl::Buffer *buffer = attrib.mBoundBuffer.get(); BufferStorage *storage = buffer ? buffer->getStorage() : NULL; const bool isAligned = (attrib.stride() % 4 == 0) && (attrib.mOffset % 4 == 0); return storage && storage->supportsDirectBinding() && !vb->getVertexBuffer()->requiresConversion(attrib) && isAligned; } GLenum VertexDataManager::prepareVertexData(const gl::VertexAttribute attribs[], gl::ProgramBinary *programBinary, GLint start, GLsizei count, TranslatedAttribute *translated, GLsizei instances) { if (!mStreamingBuffer) { return GL_OUT_OF_MEMORY; } for (int attributeIndex = 0; attributeIndex < gl::MAX_VERTEX_ATTRIBS; attributeIndex++) { translated[attributeIndex].active = (programBinary->getSemanticIndex(attributeIndex) != -1); } // Invalidate static buffers that don't contain matching attributes for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { if (translated[i].active && attribs[i].mArrayEnabled) { gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; if (staticBuffer && staticBuffer->getBufferSize() > 0 && staticBuffer->lookupAttribute(attribs[i]) == -1 && !directStoragePossible(staticBuffer, attribs[i])) { buffer->invalidateStaticData(); } } } // Reserve the required space in the buffers for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { if (translated[i].active && attribs[i].mArrayEnabled) { gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer); if (!directStoragePossible(vertexBuffer, attribs[i])) { if (staticBuffer) { if (staticBuffer->getBufferSize() == 0) { int totalCount = elementsInBuffer(attribs[i], buffer->size()); staticBuffer->reserveVertexSpace(attribs[i], totalCount, 0); } } else { mStreamingBuffer->reserveVertexSpace(attribs[i], count, instances); } } } } // Perform the vertex data translations for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { if (translated[i].active) { if (attribs[i].mArrayEnabled) { gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); if (!buffer && attribs[i].mPointer == NULL) { // This is an application error that would normally result in a crash, but we catch it and return an error ERR("An enabled vertex array has no buffer and no pointer."); return GL_INVALID_OPERATION; } StaticVertexBufferInterface *staticBuffer = buffer ? buffer->getStaticVertexBuffer() : NULL; VertexBufferInterface *vertexBuffer = staticBuffer ? staticBuffer : static_cast<VertexBufferInterface*>(mStreamingBuffer); BufferStorage *storage = buffer ? buffer->getStorage() : NULL; bool directStorage = directStoragePossible(vertexBuffer, attribs[i]); std::size_t streamOffset = -1; unsigned int outputElementSize = 0; if (directStorage) { outputElementSize = attribs[i].stride(); streamOffset = attribs[i].mOffset + outputElementSize * start; storage->markBufferUsage(); } else if (staticBuffer) { streamOffset = staticBuffer->lookupAttribute(attribs[i]); outputElementSize = staticBuffer->getVertexBuffer()->getSpaceRequired(attribs[i], 1, 0); if (streamOffset == -1) { // Convert the entire buffer int totalCount = elementsInBuffer(attribs[i], storage->getSize()); int startIndex = attribs[i].mOffset / attribs[i].stride(); streamOffset = staticBuffer->storeVertexAttributes(attribs[i], -startIndex, totalCount, 0); } if (streamOffset != -1) { streamOffset += (attribs[i].mOffset / attribs[i].stride()) * outputElementSize; if (instances == 0 || attribs[i].mDivisor == 0) { streamOffset += start * outputElementSize; } } } else { outputElementSize = mStreamingBuffer->getVertexBuffer()->getSpaceRequired(attribs[i], 1, 0); streamOffset = mStreamingBuffer->storeVertexAttributes(attribs[i], start, count, instances); } if (streamOffset == -1) { return GL_OUT_OF_MEMORY; } translated[i].storage = directStorage ? storage : NULL; translated[i].vertexBuffer = vertexBuffer->getVertexBuffer(); translated[i].serial = directStorage ? storage->getSerial() : vertexBuffer->getSerial(); translated[i].divisor = attribs[i].mDivisor; translated[i].attribute = &attribs[i]; translated[i].stride = outputElementSize; translated[i].offset = streamOffset; } else { if (!mCurrentValueBuffer[i]) { mCurrentValueBuffer[i] = new StreamingVertexBufferInterface(mRenderer, CONSTANT_VERTEX_BUFFER_SIZE); } StreamingVertexBufferInterface *buffer = mCurrentValueBuffer[i]; if (mCurrentValue[i][0] != attribs[i].mCurrentValue[0] || mCurrentValue[i][1] != attribs[i].mCurrentValue[1] || mCurrentValue[i][2] != attribs[i].mCurrentValue[2] || mCurrentValue[i][3] != attribs[i].mCurrentValue[3]) { unsigned int requiredSpace = sizeof(float) * 4; buffer->reserveRawDataSpace(requiredSpace); int streamOffset = buffer->storeRawData(attribs[i].mCurrentValue, requiredSpace); if (streamOffset == -1) { return GL_OUT_OF_MEMORY; } mCurrentValueOffsets[i] = streamOffset; } translated[i].storage = NULL; translated[i].vertexBuffer = mCurrentValueBuffer[i]->getVertexBuffer(); translated[i].serial = mCurrentValueBuffer[i]->getSerial(); translated[i].divisor = 0; translated[i].attribute = &attribs[i]; translated[i].stride = 0; translated[i].offset = mCurrentValueOffsets[i]; } } } for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { if (translated[i].active && attribs[i].mArrayEnabled) { gl::Buffer *buffer = attribs[i].mBoundBuffer.get(); if (buffer) { buffer->promoteStaticUsage(count * attribs[i].typeSize()); } } } return GL_NO_ERROR; } }
C++
#include "precompiled.h" // // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Fence11.cpp: Defines the rx::Fence11 class which implements rx::FenceImpl. #include "libGLESv2/renderer/Fence11.h" #include "libGLESv2/main.h" #include "libGLESv2/renderer/Renderer11.h" namespace rx { Fence11::Fence11(rx::Renderer11 *renderer) { mRenderer = renderer; mQuery = NULL; } Fence11::~Fence11() { if (mQuery) { mQuery->Release(); mQuery = NULL; } } GLboolean Fence11::isFence() { // GL_NV_fence spec: // A name returned by GenFencesNV, but not yet set via SetFenceNV, is not the name of an existing fence. return mQuery != NULL; } void Fence11::setFence(GLenum condition) { if (!mQuery) { D3D11_QUERY_DESC queryDesc; queryDesc.Query = D3D11_QUERY_EVENT; queryDesc.MiscFlags = 0; if (FAILED(mRenderer->getDevice()->CreateQuery(&queryDesc, &mQuery))) { return gl::error(GL_OUT_OF_MEMORY); } } mRenderer->getDeviceContext()->End(mQuery); setCondition(condition); setStatus(GL_FALSE); } GLboolean Fence11::testFence() { if (mQuery == NULL) { return gl::error(GL_INVALID_OPERATION, GL_TRUE); } HRESULT result = mRenderer->getDeviceContext()->GetData(mQuery, NULL, 0, 0); if (mRenderer->isDeviceLost()) { return gl::error(GL_OUT_OF_MEMORY, GL_TRUE); } ASSERT(result == S_OK || result == S_FALSE); setStatus(result == S_OK); return getStatus(); } void Fence11::finishFence() { if (mQuery == NULL) { return gl::error(GL_INVALID_OPERATION); } while (!testFence()) { Sleep(0); } } void Fence11::getFenceiv(GLenum pname, GLint *params) { if (mQuery == NULL) { return gl::error(GL_INVALID_OPERATION); } switch (pname) { case GL_FENCE_STATUS_NV: { // GL_NV_fence spec: // Once the status of a fence has been finished (via FinishFenceNV) or tested and the returned status is TRUE (via either TestFenceNV // or GetFenceivNV querying the FENCE_STATUS_NV), the status remains TRUE until the next SetFenceNV of the fence. if (getStatus()) { params[0] = GL_TRUE; return; } HRESULT result = mRenderer->getDeviceContext()->GetData(mQuery, NULL, 0, D3D11_ASYNC_GETDATA_DONOTFLUSH); if (mRenderer->isDeviceLost()) { params[0] = GL_TRUE; return gl::error(GL_OUT_OF_MEMORY); } ASSERT(result == S_OK || result == S_FALSE); setStatus(result == S_OK); params[0] = getStatus(); break; } case GL_FENCE_CONDITION_NV: params[0] = getCondition(); break; default: return gl::error(GL_INVALID_ENUM); break; } } }
C++
// // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // IndexDataManager.h: Defines the IndexDataManager, a class that // runs the Buffer translation process for index buffers. #ifndef LIBGLESV2_INDEXDATAMANAGER_H_ #define LIBGLESV2_INDEXDATAMANAGER_H_ #include "common/angleutils.h" namespace { enum { INITIAL_INDEX_BUFFER_SIZE = 4096 * sizeof(GLuint) }; } namespace gl { class Buffer; } namespace rx { class StaticIndexBufferInterface; class StreamingIndexBufferInterface; class IndexBuffer; class BufferStorage; class Renderer; struct TranslatedIndexData { unsigned int minIndex; unsigned int maxIndex; unsigned int startIndex; unsigned int startOffset; // In bytes IndexBuffer *indexBuffer; BufferStorage *storage; unsigned int serial; }; class IndexDataManager { public: explicit IndexDataManager(Renderer *renderer); virtual ~IndexDataManager(); GLenum prepareIndexData(GLenum type, GLsizei count, gl::Buffer *arrayElementBuffer, const GLvoid *indices, TranslatedIndexData *translated); StaticIndexBufferInterface *getCountingIndices(GLsizei count); private: DISALLOW_COPY_AND_ASSIGN(IndexDataManager); Renderer *const mRenderer; StreamingIndexBufferInterface *mStreamingBufferShort; StreamingIndexBufferInterface *mStreamingBufferInt; StaticIndexBufferInterface *mCountingBuffer; }; } #endif // LIBGLESV2_INDEXDATAMANAGER_H_
C++
// // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // BufferStorage.h Defines the abstract BufferStorage class. #ifndef LIBGLESV2_RENDERER_BUFFERSTORAGE_H_ #define LIBGLESV2_RENDERER_BUFFERSTORAGE_H_ #include "common/angleutils.h" namespace rx { class BufferStorage { public: BufferStorage(); virtual ~BufferStorage(); // The data returned is only guaranteed valid until next non-const method. virtual void *getData() = 0; virtual void setData(const void* data, unsigned int size, unsigned int offset) = 0; virtual void clear() = 0; virtual unsigned int getSize() const = 0; virtual bool supportsDirectBinding() const = 0; virtual void markBufferUsage(); unsigned int getSerial() const; protected: void updateSerial(); private: DISALLOW_COPY_AND_ASSIGN(BufferStorage); unsigned int mSerial; static unsigned int mNextSerial; }; } #endif // LIBGLESV2_RENDERER_BUFFERSTORAGE_H_
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // IndexBuffer11.h: Defines the D3D11 IndexBuffer implementation. #ifndef LIBGLESV2_RENDERER_INDEXBUFFER11_H_ #define LIBGLESV2_RENDERER_INDEXBUFFER11_H_ #include "libGLESv2/renderer/IndexBuffer.h" namespace rx { class Renderer11; class IndexBuffer11 : public IndexBuffer { public: explicit IndexBuffer11(Renderer11 *const renderer); virtual ~IndexBuffer11(); virtual bool initialize(unsigned int bufferSize, GLenum indexType, bool dynamic); static IndexBuffer11 *makeIndexBuffer11(IndexBuffer *indexBuffer); virtual bool mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory); virtual bool unmapBuffer(); virtual GLenum getIndexType() const; virtual unsigned int getBufferSize() const; virtual bool setSize(unsigned int bufferSize, GLenum indexType); virtual bool discard(); DXGI_FORMAT getIndexFormat() const; ID3D11Buffer *getBuffer() const; private: DISALLOW_COPY_AND_ASSIGN(IndexBuffer11); rx::Renderer11 *const mRenderer; ID3D11Buffer *mBuffer; unsigned int mBufferSize; GLenum mIndexType; bool mDynamicUsage; }; } #endif // LIBGLESV2_RENDERER_INDEXBUFFER11_H_
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // RenderTarget11.h: Defines a DX11-specific wrapper for ID3D11View pointers // retained by Renderbuffers. #ifndef LIBGLESV2_RENDERER_RENDERTARGET11_H_ #define LIBGLESV2_RENDERER_RENDERTARGET11_H_ #include "libGLESv2/renderer/RenderTarget.h" namespace rx { class Renderer; class Renderer11; class RenderTarget11 : public RenderTarget { public: RenderTarget11(Renderer *renderer, ID3D11RenderTargetView *rtv, ID3D11Texture2D *tex, ID3D11ShaderResourceView *srv, GLsizei width, GLsizei height); RenderTarget11(Renderer *renderer, ID3D11DepthStencilView *dsv, ID3D11Texture2D *tex, ID3D11ShaderResourceView *srv, GLsizei width, GLsizei height); RenderTarget11(Renderer *renderer, GLsizei width, GLsizei height, GLenum format, GLsizei samples, bool depth); virtual ~RenderTarget11(); static RenderTarget11 *makeRenderTarget11(RenderTarget *renderTarget); // Adds reference, caller must call Release ID3D11Texture2D *getTexture() const; // Adds reference, caller must call Release ID3D11RenderTargetView *getRenderTargetView() const; // Adds reference, caller must call Release ID3D11DepthStencilView *getDepthStencilView() const; // Adds reference, caller must call Release ID3D11ShaderResourceView *getShaderResourceView() const; unsigned int getSubresourceIndex() const; private: DISALLOW_COPY_AND_ASSIGN(RenderTarget11); unsigned int mSubresourceIndex; ID3D11Texture2D *mTexture; ID3D11RenderTargetView *mRenderTarget; ID3D11DepthStencilView *mDepthStencil; ID3D11ShaderResourceView *mShaderResource; Renderer11 *mRenderer; }; } #endif LIBGLESV2_RENDERER_RENDERTARGET11_H_
C++
// // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Fence11.h: Defines the rx::Fence11 class which implements rx::FenceImpl. #ifndef LIBGLESV2_RENDERER_Fence11_H_ #define LIBGLESV2_RENDERER_Fence11_H_ #include "libGLESv2/renderer/FenceImpl.h" namespace rx { class Renderer11; class Fence11 : public FenceImpl { public: explicit Fence11(rx::Renderer11 *renderer); virtual ~Fence11(); GLboolean isFence(); void setFence(GLenum condition); GLboolean testFence(); void finishFence(); void getFenceiv(GLenum pname, GLint *params); private: DISALLOW_COPY_AND_ASSIGN(Fence11); rx::Renderer11 *mRenderer; ID3D11Query *mQuery; }; } #endif // LIBGLESV2_RENDERER_FENCE11_H_
C++
#include "precompiled.h" // // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Query11.cpp: Defines the rx::Query11 class which implements rx::QueryImpl. #include "libGLESv2/renderer/Query11.h" #include "libGLESv2/renderer/Renderer11.h" #include "libGLESv2/main.h" namespace rx { Query11::Query11(rx::Renderer11 *renderer, GLenum type) : QueryImpl(type) { mRenderer = renderer; mQuery = NULL; } Query11::~Query11() { if (mQuery) { mQuery->Release(); mQuery = NULL; } } void Query11::begin() { if (mQuery == NULL) { D3D11_QUERY_DESC queryDesc; queryDesc.Query = D3D11_QUERY_OCCLUSION; queryDesc.MiscFlags = 0; if (FAILED(mRenderer->getDevice()->CreateQuery(&queryDesc, &mQuery))) { return gl::error(GL_OUT_OF_MEMORY); } } mRenderer->getDeviceContext()->Begin(mQuery); } void Query11::end() { if (mQuery == NULL) { return gl::error(GL_INVALID_OPERATION); } mRenderer->getDeviceContext()->End(mQuery); mStatus = GL_FALSE; mResult = GL_FALSE; } GLuint Query11::getResult() { if (mQuery != NULL) { while (!testQuery()) { Sleep(0); // explicitly check for device loss, some drivers seem to return S_FALSE // if the device is lost if (mRenderer->testDeviceLost(true)) { return gl::error(GL_OUT_OF_MEMORY, 0); } } } return mResult; } GLboolean Query11::isResultAvailable() { if (mQuery != NULL) { testQuery(); } return mStatus; } GLboolean Query11::testQuery() { if (mQuery != NULL && mStatus != GL_TRUE) { UINT64 numPixels = 0; HRESULT result = mRenderer->getDeviceContext()->GetData(mQuery, &numPixels, sizeof(UINT64), 0); if (result == S_OK) { mStatus = GL_TRUE; switch (getType()) { case GL_ANY_SAMPLES_PASSED_EXT: case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT: mResult = (numPixels > 0) ? GL_TRUE : GL_FALSE; break; default: UNREACHABLE(); } } else if (mRenderer->testDeviceLost(true)) { return gl::error(GL_OUT_OF_MEMORY, GL_TRUE); } return mStatus; } return GL_TRUE; // prevent blocking when query is null } }
C++
// // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // ShaderExecutable9.h: Defines a D3D9-specific class to contain shader // executable implementation details. #ifndef LIBGLESV2_RENDERER_SHADEREXECUTABLE9_H_ #define LIBGLESV2_RENDERER_SHADEREXECUTABLE9_H_ #include "libGLESv2/renderer/ShaderExecutable.h" namespace rx { class ShaderExecutable9 : public ShaderExecutable { public: ShaderExecutable9(const void *function, size_t length, IDirect3DPixelShader9 *executable); ShaderExecutable9(const void *function, size_t length, IDirect3DVertexShader9 *executable); virtual ~ShaderExecutable9(); static ShaderExecutable9 *makeShaderExecutable9(ShaderExecutable *executable); IDirect3DPixelShader9 *getPixelShader() const; IDirect3DVertexShader9 *getVertexShader() const; private: DISALLOW_COPY_AND_ASSIGN(ShaderExecutable9); IDirect3DPixelShader9 *mPixelExecutable; IDirect3DVertexShader9 *mVertexExecutable; }; } #endif // LIBGLESV2_RENDERER_SHADEREXECUTABLE9_H_
C++
#include "precompiled.h" // // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // VertexDeclarationCache.cpp: Implements a helper class to construct and cache vertex declarations. #include "libGLESv2/ProgramBinary.h" #include "libGLESv2/Context.h" #include "libGLESv2/renderer/VertexBuffer9.h" #include "libGLESv2/renderer/VertexDeclarationCache.h" namespace rx { VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0) { for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) { mVertexDeclCache[i].vertexDeclaration = NULL; mVertexDeclCache[i].lruCount = 0; } for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { mAppliedVBs[i].serial = 0; } mLastSetVDecl = NULL; mInstancingEnabled = true; } VertexDeclarationCache::~VertexDeclarationCache() { for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) { if (mVertexDeclCache[i].vertexDeclaration) { mVertexDeclCache[i].vertexDeclaration->Release(); } } } GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], gl::ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw) { *repeatDraw = 1; int indexedAttribute = gl::MAX_VERTEX_ATTRIBS; int instancedAttribute = gl::MAX_VERTEX_ATTRIBS; if (instances > 0) { // Find an indexed attribute to be mapped to D3D stream 0 for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { if (attributes[i].active) { if (indexedAttribute == gl::MAX_VERTEX_ATTRIBS && attributes[i].divisor == 0) { indexedAttribute = i; } else if (instancedAttribute == gl::MAX_VERTEX_ATTRIBS && attributes[i].divisor != 0) { instancedAttribute = i; } if (indexedAttribute != gl::MAX_VERTEX_ATTRIBS && instancedAttribute != gl::MAX_VERTEX_ATTRIBS) break; // Found both an indexed and instanced attribute } } if (indexedAttribute == gl::MAX_VERTEX_ATTRIBS) { return GL_INVALID_OPERATION; } } D3DVERTEXELEMENT9 elements[gl::MAX_VERTEX_ATTRIBS + 1]; D3DVERTEXELEMENT9 *element = &elements[0]; for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { if (attributes[i].active) { // Directly binding the storage buffer is not supported for d3d9 ASSERT(attributes[i].storage == NULL); int stream = i; if (instances > 0) { // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced. if (instancedAttribute == gl::MAX_VERTEX_ATTRIBS) { *repeatDraw = instances; } else { if (i == indexedAttribute) { stream = 0; } else if (i == 0) { stream = indexedAttribute; } UINT frequency = 1; if (attributes[i].divisor == 0) { frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances; } else { frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor; } device->SetStreamSourceFreq(stream, frequency); mInstancingEnabled = true; } } VertexBuffer9 *vertexBuffer = VertexBuffer9::makeVertexBuffer9(attributes[i].vertexBuffer); if (mAppliedVBs[stream].serial != attributes[i].serial || mAppliedVBs[stream].stride != attributes[i].stride || mAppliedVBs[stream].offset != attributes[i].offset) { device->SetStreamSource(stream, vertexBuffer->getBuffer(), attributes[i].offset, attributes[i].stride); mAppliedVBs[stream].serial = attributes[i].serial; mAppliedVBs[stream].stride = attributes[i].stride; mAppliedVBs[stream].offset = attributes[i].offset; } element->Stream = stream; element->Offset = 0; element->Type = attributes[i].attribute->mArrayEnabled ? vertexBuffer->getDeclType(*attributes[i].attribute) : D3DDECLTYPE_FLOAT4; element->Method = D3DDECLMETHOD_DEFAULT; element->Usage = D3DDECLUSAGE_TEXCOORD; element->UsageIndex = programBinary->getSemanticIndex(i); element++; } } if (instances == 0 || instancedAttribute == gl::MAX_VERTEX_ATTRIBS) { if (mInstancingEnabled) { for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { device->SetStreamSourceFreq(i, 1); } mInstancingEnabled = false; } } static const D3DVERTEXELEMENT9 end = D3DDECL_END(); *(element++) = end; for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) { VertexDeclCacheEntry *entry = &mVertexDeclCache[i]; if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration) { entry->lruCount = ++mMaxLru; if(entry->vertexDeclaration != mLastSetVDecl) { device->SetVertexDeclaration(entry->vertexDeclaration); mLastSetVDecl = entry->vertexDeclaration; } return GL_NO_ERROR; } } VertexDeclCacheEntry *lastCache = mVertexDeclCache; for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++) { if (mVertexDeclCache[i].lruCount < lastCache->lruCount) { lastCache = &mVertexDeclCache[i]; } } if (lastCache->vertexDeclaration != NULL) { lastCache->vertexDeclaration->Release(); lastCache->vertexDeclaration = NULL; // mLastSetVDecl is set to the replacement, so we don't have to worry // about it. } memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)); device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration); device->SetVertexDeclaration(lastCache->vertexDeclaration); mLastSetVDecl = lastCache->vertexDeclaration; lastCache->lruCount = ++mMaxLru; return GL_NO_ERROR; } void VertexDeclarationCache::markStateDirty() { for (int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { mAppliedVBs[i].serial = 0; } mLastSetVDecl = NULL; mInstancingEnabled = true; // Forces it to be disabled when not used } }
C++
#include "precompiled.h" // // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Image9.cpp: Implements the rx::Image9 class, which acts as the interface to // the actual underlying surfaces of a Texture. #include "libGLESv2/renderer/Image9.h" #include "libGLESv2/main.h" #include "libGLESv2/Framebuffer.h" #include "libGLESv2/Renderbuffer.h" #include "libGLESv2/renderer/Renderer9.h" #include "libGLESv2/renderer/RenderTarget9.h" #include "libGLESv2/renderer/TextureStorage9.h" #include "libGLESv2/renderer/renderer9_utils.h" #include "libGLESv2/renderer/generatemip.h" namespace rx { Image9::Image9() { mSurface = NULL; mRenderer = NULL; mD3DPool = D3DPOOL_SYSTEMMEM; mD3DFormat = D3DFMT_UNKNOWN; } Image9::~Image9() { if (mSurface) { mSurface->Release(); } } void Image9::generateMip(IDirect3DSurface9 *destSurface, IDirect3DSurface9 *sourceSurface) { D3DSURFACE_DESC destDesc; HRESULT result = destSurface->GetDesc(&destDesc); ASSERT(SUCCEEDED(result)); D3DSURFACE_DESC sourceDesc; result = sourceSurface->GetDesc(&sourceDesc); ASSERT(SUCCEEDED(result)); ASSERT(sourceDesc.Format == destDesc.Format); ASSERT(sourceDesc.Width == 1 || sourceDesc.Width / 2 == destDesc.Width); ASSERT(sourceDesc.Height == 1 || sourceDesc.Height / 2 == destDesc.Height); D3DLOCKED_RECT sourceLocked = {0}; result = sourceSurface->LockRect(&sourceLocked, NULL, D3DLOCK_READONLY); ASSERT(SUCCEEDED(result)); D3DLOCKED_RECT destLocked = {0}; result = destSurface->LockRect(&destLocked, NULL, 0); ASSERT(SUCCEEDED(result)); const unsigned char *sourceData = reinterpret_cast<const unsigned char*>(sourceLocked.pBits); unsigned char *destData = reinterpret_cast<unsigned char*>(destLocked.pBits); if (sourceData && destData) { switch (sourceDesc.Format) { case D3DFMT_L8: GenerateMip<L8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); break; case D3DFMT_A8L8: GenerateMip<A8L8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); break; case D3DFMT_A8R8G8B8: case D3DFMT_X8R8G8B8: GenerateMip<A8R8G8B8>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); break; case D3DFMT_A16B16G16R16F: GenerateMip<A16B16G16R16F>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); break; case D3DFMT_A32B32G32R32F: GenerateMip<A32B32G32R32F>(sourceDesc.Width, sourceDesc.Height, sourceData, sourceLocked.Pitch, destData, destLocked.Pitch); break; default: UNREACHABLE(); break; } destSurface->UnlockRect(); sourceSurface->UnlockRect(); } } Image9 *Image9::makeImage9(Image *img) { ASSERT(HAS_DYNAMIC_TYPE(rx::Image9*, img)); return static_cast<rx::Image9*>(img); } void Image9::generateMipmap(Image9 *dest, Image9 *source) { IDirect3DSurface9 *sourceSurface = source->getSurface(); if (sourceSurface == NULL) return gl::error(GL_OUT_OF_MEMORY); IDirect3DSurface9 *destSurface = dest->getSurface(); generateMip(destSurface, sourceSurface); dest->markDirty(); } void Image9::copyLockableSurfaces(IDirect3DSurface9 *dest, IDirect3DSurface9 *source) { D3DLOCKED_RECT sourceLock = {0}; D3DLOCKED_RECT destLock = {0}; source->LockRect(&sourceLock, NULL, 0); dest->LockRect(&destLock, NULL, 0); if (sourceLock.pBits && destLock.pBits) { D3DSURFACE_DESC desc; source->GetDesc(&desc); int rows = d3d9::IsCompressedFormat(desc.Format) ? desc.Height / 4 : desc.Height; int bytes = d3d9::ComputeRowSize(desc.Format, desc.Width); ASSERT(bytes <= sourceLock.Pitch && bytes <= destLock.Pitch); for(int i = 0; i < rows; i++) { memcpy((char*)destLock.pBits + destLock.Pitch * i, (char*)sourceLock.pBits + sourceLock.Pitch * i, bytes); } source->UnlockRect(); dest->UnlockRect(); } else UNREACHABLE(); } bool Image9::redefine(rx::Renderer *renderer, GLint internalformat, GLsizei width, GLsizei height, bool forceRelease) { if (mWidth != width || mHeight != height || mInternalFormat != internalformat || forceRelease) { mRenderer = Renderer9::makeRenderer9(renderer); mWidth = width; mHeight = height; mInternalFormat = internalformat; // compute the d3d format that will be used mD3DFormat = mRenderer->ConvertTextureInternalFormat(internalformat); mActualFormat = d3d9_gl::GetEquivalentFormat(mD3DFormat); if (mSurface) { mSurface->Release(); mSurface = NULL; } return true; } return false; } void Image9::createSurface() { if(mSurface) { return; } IDirect3DTexture9 *newTexture = NULL; IDirect3DSurface9 *newSurface = NULL; const D3DPOOL poolToUse = D3DPOOL_SYSTEMMEM; const D3DFORMAT d3dFormat = getD3DFormat(); ASSERT(d3dFormat != D3DFMT_INTZ); // We should never get here for depth textures if (mWidth != 0 && mHeight != 0) { int levelToFetch = 0; GLsizei requestWidth = mWidth; GLsizei requestHeight = mHeight; gl::MakeValidSize(true, gl::IsCompressed(mInternalFormat), &requestWidth, &requestHeight, &levelToFetch); IDirect3DDevice9 *device = mRenderer->getDevice(); HRESULT result = device->CreateTexture(requestWidth, requestHeight, levelToFetch + 1, 0, d3dFormat, poolToUse, &newTexture, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); ERR("Creating image surface failed."); return gl::error(GL_OUT_OF_MEMORY); } newTexture->GetSurfaceLevel(levelToFetch, &newSurface); newTexture->Release(); } mSurface = newSurface; mDirty = false; mD3DPool = poolToUse; } HRESULT Image9::lock(D3DLOCKED_RECT *lockedRect, const RECT *rect) { createSurface(); HRESULT result = D3DERR_INVALIDCALL; if (mSurface) { result = mSurface->LockRect(lockedRect, rect, 0); ASSERT(SUCCEEDED(result)); mDirty = true; } return result; } void Image9::unlock() { if (mSurface) { HRESULT result = mSurface->UnlockRect(); ASSERT(SUCCEEDED(result)); } } bool Image9::isRenderableFormat() const { return TextureStorage9::IsTextureFormatRenderable(getD3DFormat()); } D3DFORMAT Image9::getD3DFormat() const { // this should only happen if the image hasn't been redefined first // which would be a bug by the caller ASSERT(mD3DFormat != D3DFMT_UNKNOWN); return mD3DFormat; } IDirect3DSurface9 *Image9::getSurface() { createSurface(); return mSurface; } void Image9::setManagedSurface(TextureStorageInterface2D *storage, int level) { TextureStorage9_2D *storage9 = TextureStorage9_2D::makeTextureStorage9_2D(storage->getStorageInstance()); setManagedSurface(storage9->getSurfaceLevel(level, false)); } void Image9::setManagedSurface(TextureStorageInterfaceCube *storage, int face, int level) { TextureStorage9_Cube *storage9 = TextureStorage9_Cube::makeTextureStorage9_Cube(storage->getStorageInstance()); setManagedSurface(storage9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, false)); } void Image9::setManagedSurface(IDirect3DSurface9 *surface) { D3DSURFACE_DESC desc; surface->GetDesc(&desc); ASSERT(desc.Pool == D3DPOOL_MANAGED); if ((GLsizei)desc.Width == mWidth && (GLsizei)desc.Height == mHeight) { if (mSurface) { copyLockableSurfaces(surface, mSurface); mSurface->Release(); } mSurface = surface; mD3DPool = desc.Pool; } } bool Image9::updateSurface(TextureStorageInterface2D *storage, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) { ASSERT(getSurface() != NULL); TextureStorage9_2D *storage9 = TextureStorage9_2D::makeTextureStorage9_2D(storage->getStorageInstance()); return updateSurface(storage9->getSurfaceLevel(level, true), xoffset, yoffset, width, height); } bool Image9::updateSurface(TextureStorageInterfaceCube *storage, int face, int level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) { ASSERT(getSurface() != NULL); TextureStorage9_Cube *storage9 = TextureStorage9_Cube::makeTextureStorage9_Cube(storage->getStorageInstance()); return updateSurface(storage9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, level, true), xoffset, yoffset, width, height); } bool Image9::updateSurface(IDirect3DSurface9 *destSurface, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) { if (!destSurface) return false; IDirect3DSurface9 *sourceSurface = getSurface(); if (sourceSurface && sourceSurface != destSurface) { RECT rect; rect.left = xoffset; rect.top = yoffset; rect.right = xoffset + width; rect.bottom = yoffset + height; POINT point = {rect.left, rect.top}; IDirect3DDevice9 *device = mRenderer->getDevice(); if (mD3DPool == D3DPOOL_MANAGED) { D3DSURFACE_DESC desc; sourceSurface->GetDesc(&desc); IDirect3DSurface9 *surf = 0; HRESULT result = device->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL); if (SUCCEEDED(result)) { copyLockableSurfaces(surf, sourceSurface); result = device->UpdateSurface(surf, &rect, destSurface, &point); ASSERT(SUCCEEDED(result)); surf->Release(); } } else { // UpdateSurface: source must be SYSTEMMEM, dest must be DEFAULT pools HRESULT result = device->UpdateSurface(sourceSurface, &rect, destSurface, &point); ASSERT(SUCCEEDED(result)); } } destSurface->Release(); return true; } // Store the pixel rectangle designated by xoffset,yoffset,width,height with pixels stored as format/type at input // into the target pixel rectangle. void Image9::loadData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLint unpackAlignment, const void *input) { RECT lockRect = { xoffset, yoffset, xoffset + width, yoffset + height }; D3DLOCKED_RECT locked; HRESULT result = lock(&locked, &lockRect); if (FAILED(result)) { return; } GLsizei inputPitch = gl::ComputePitch(width, mInternalFormat, unpackAlignment); switch (mInternalFormat) { case GL_ALPHA8_EXT: if (gl::supportsSSE2()) { loadAlphaDataToBGRASSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits); } else { loadAlphaDataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); } break; case GL_LUMINANCE8_EXT: loadLuminanceDataToNativeOrBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_L8); break; case GL_ALPHA32F_EXT: loadAlphaFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_LUMINANCE32F_EXT: loadLuminanceFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_ALPHA16F_EXT: loadAlphaHalfFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_LUMINANCE16F_EXT: loadLuminanceHalfFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_LUMINANCE8_ALPHA8_EXT: loadLuminanceAlphaDataToNativeOrBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits, getD3DFormat() == D3DFMT_A8L8); break; case GL_LUMINANCE_ALPHA32F_EXT: loadLuminanceAlphaFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_LUMINANCE_ALPHA16F_EXT: loadLuminanceAlphaHalfFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_RGB8_OES: loadRGBUByteDataToBGRX(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_RGB565: loadRGB565DataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_RGBA8_OES: if (gl::supportsSSE2()) { loadRGBAUByteDataToBGRASSE2(width, height, inputPitch, input, locked.Pitch, locked.pBits); } else { loadRGBAUByteDataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); } break; case GL_RGBA4: loadRGBA4444DataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_RGB5_A1: loadRGBA5551DataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_BGRA8_EXT: loadBGRADataToBGRA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; // float textures are converted to RGBA, not BGRA, as they're stored that way in D3D case GL_RGB32F_EXT: loadRGBFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_RGB16F_EXT: loadRGBHalfFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_RGBA32F_EXT: loadRGBAFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; case GL_RGBA16F_EXT: loadRGBAHalfFloatDataToRGBA(width, height, inputPitch, input, locked.Pitch, locked.pBits); break; default: UNREACHABLE(); } unlock(); } void Image9::loadCompressedData(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, const void *input) { ASSERT(xoffset % 4 == 0); ASSERT(yoffset % 4 == 0); RECT lockRect = { xoffset, yoffset, xoffset + width, yoffset + height }; D3DLOCKED_RECT locked; HRESULT result = lock(&locked, &lockRect); if (FAILED(result)) { return; } GLsizei inputSize = gl::ComputeCompressedSize(width, height, mInternalFormat); GLsizei inputPitch = gl::ComputeCompressedPitch(width, mInternalFormat); int rows = inputSize / inputPitch; for (int i = 0; i < rows; ++i) { memcpy((void*)((BYTE*)locked.pBits + i * locked.Pitch), (void*)((BYTE*)input + i * inputPitch), inputPitch); } unlock(); } // This implements glCopyTex[Sub]Image2D for non-renderable internal texture formats and incomplete textures void Image9::copy(GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, gl::Framebuffer *source) { RenderTarget9 *renderTarget = NULL; IDirect3DSurface9 *surface = NULL; gl::Renderbuffer *colorbuffer = source->getColorbuffer(0); if (colorbuffer) { renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget()); } if (renderTarget) { surface = renderTarget->getSurface(); } if (!surface) { ERR("Failed to retrieve the render target."); return gl::error(GL_OUT_OF_MEMORY); } IDirect3DDevice9 *device = mRenderer->getDevice(); IDirect3DSurface9 *renderTargetData = NULL; D3DSURFACE_DESC description; surface->GetDesc(&description); HRESULT result = device->CreateOffscreenPlainSurface(description.Width, description.Height, description.Format, D3DPOOL_SYSTEMMEM, &renderTargetData, NULL); if (FAILED(result)) { ERR("Could not create matching destination surface."); surface->Release(); return gl::error(GL_OUT_OF_MEMORY); } result = device->GetRenderTargetData(surface, renderTargetData); if (FAILED(result)) { ERR("GetRenderTargetData unexpectedly failed."); renderTargetData->Release(); surface->Release(); return gl::error(GL_OUT_OF_MEMORY); } RECT sourceRect = {x, y, x + width, y + height}; RECT destRect = {xoffset, yoffset, xoffset + width, yoffset + height}; D3DLOCKED_RECT sourceLock = {0}; result = renderTargetData->LockRect(&sourceLock, &sourceRect, 0); if (FAILED(result)) { ERR("Failed to lock the source surface (rectangle might be invalid)."); renderTargetData->Release(); surface->Release(); return gl::error(GL_OUT_OF_MEMORY); } D3DLOCKED_RECT destLock = {0}; result = lock(&destLock, &destRect); if (FAILED(result)) { ERR("Failed to lock the destination surface (rectangle might be invalid)."); renderTargetData->UnlockRect(); renderTargetData->Release(); surface->Release(); return gl::error(GL_OUT_OF_MEMORY); } if (destLock.pBits && sourceLock.pBits) { unsigned char *source = (unsigned char*)sourceLock.pBits; unsigned char *dest = (unsigned char*)destLock.pBits; switch (description.Format) { case D3DFMT_X8R8G8B8: case D3DFMT_A8R8G8B8: switch(getD3DFormat()) { case D3DFMT_X8R8G8B8: case D3DFMT_A8R8G8B8: for(int y = 0; y < height; y++) { memcpy(dest, source, 4 * width); source += sourceLock.Pitch; dest += destLock.Pitch; } break; case D3DFMT_L8: for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { dest[x] = source[x * 4 + 2]; } source += sourceLock.Pitch; dest += destLock.Pitch; } break; case D3DFMT_A8L8: for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { dest[x * 2 + 0] = source[x * 4 + 2]; dest[x * 2 + 1] = source[x * 4 + 3]; } source += sourceLock.Pitch; dest += destLock.Pitch; } break; default: UNREACHABLE(); } break; case D3DFMT_R5G6B5: switch(getD3DFormat()) { case D3DFMT_X8R8G8B8: for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { unsigned short rgb = ((unsigned short*)source)[x]; unsigned char red = (rgb & 0xF800) >> 8; unsigned char green = (rgb & 0x07E0) >> 3; unsigned char blue = (rgb & 0x001F) << 3; dest[x + 0] = blue | (blue >> 5); dest[x + 1] = green | (green >> 6); dest[x + 2] = red | (red >> 5); dest[x + 3] = 0xFF; } source += sourceLock.Pitch; dest += destLock.Pitch; } break; case D3DFMT_L8: for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { unsigned char red = source[x * 2 + 1] & 0xF8; dest[x] = red | (red >> 5); } source += sourceLock.Pitch; dest += destLock.Pitch; } break; default: UNREACHABLE(); } break; case D3DFMT_A1R5G5B5: switch(getD3DFormat()) { case D3DFMT_X8R8G8B8: for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { unsigned short argb = ((unsigned short*)source)[x]; unsigned char red = (argb & 0x7C00) >> 7; unsigned char green = (argb & 0x03E0) >> 2; unsigned char blue = (argb & 0x001F) << 3; dest[x + 0] = blue | (blue >> 5); dest[x + 1] = green | (green >> 5); dest[x + 2] = red | (red >> 5); dest[x + 3] = 0xFF; } source += sourceLock.Pitch; dest += destLock.Pitch; } break; case D3DFMT_A8R8G8B8: for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { unsigned short argb = ((unsigned short*)source)[x]; unsigned char red = (argb & 0x7C00) >> 7; unsigned char green = (argb & 0x03E0) >> 2; unsigned char blue = (argb & 0x001F) << 3; unsigned char alpha = (signed short)argb >> 15; dest[x + 0] = blue | (blue >> 5); dest[x + 1] = green | (green >> 5); dest[x + 2] = red | (red >> 5); dest[x + 3] = alpha; } source += sourceLock.Pitch; dest += destLock.Pitch; } break; case D3DFMT_L8: for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { unsigned char red = source[x * 2 + 1] & 0x7C; dest[x] = (red << 1) | (red >> 4); } source += sourceLock.Pitch; dest += destLock.Pitch; } break; case D3DFMT_A8L8: for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { unsigned char red = source[x * 2 + 1] & 0x7C; dest[x * 2 + 0] = (red << 1) | (red >> 4); dest[x * 2 + 1] = (signed char)source[x * 2 + 1] >> 7; } source += sourceLock.Pitch; dest += destLock.Pitch; } break; default: UNREACHABLE(); } break; default: UNREACHABLE(); } } unlock(); renderTargetData->UnlockRect(); renderTargetData->Release(); surface->Release(); mDirty = true; } }
C++
#include "precompiled.h" // // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // BufferStorage9.cpp Defines the BufferStorage9 class. #include "libGLESv2/renderer/BufferStorage9.h" #include "common/debug.h" namespace rx { BufferStorage9::BufferStorage9() { mMemory = NULL; mAllocatedSize = 0; mSize = 0; } BufferStorage9::~BufferStorage9() { delete[] mMemory; } BufferStorage9 *BufferStorage9::makeBufferStorage9(BufferStorage *bufferStorage) { ASSERT(HAS_DYNAMIC_TYPE(BufferStorage9*, bufferStorage)); return static_cast<BufferStorage9*>(bufferStorage); } void *BufferStorage9::getData() { return mMemory; } void BufferStorage9::setData(const void* data, unsigned int size, unsigned int offset) { if (!mMemory || offset + size > mAllocatedSize) { unsigned int newAllocatedSize = offset + size; void *newMemory = new char[newAllocatedSize]; if (offset > 0 && mMemory && mAllocatedSize > 0) { memcpy(newMemory, mMemory, std::min(offset, mAllocatedSize)); } delete[] mMemory; mMemory = newMemory; mAllocatedSize = newAllocatedSize; } mSize = std::max(mSize, offset + size); memcpy(reinterpret_cast<char*>(mMemory) + offset, data, size); } void BufferStorage9::clear() { mSize = 0; } unsigned int BufferStorage9::getSize() const { return mSize; } bool BufferStorage9::supportsDirectBinding() const { return false; } }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // RenderTarget9.h: Defines a D3D9-specific wrapper for IDirect3DSurface9 pointers // retained by Renderbuffers. #ifndef LIBGLESV2_RENDERER_RENDERTARGET9_H_ #define LIBGLESV2_RENDERER_RENDERTARGET9_H_ #include "libGLESv2/renderer/RenderTarget.h" namespace rx { class Renderer; class Renderer9; class RenderTarget9 : public RenderTarget { public: RenderTarget9(Renderer *renderer, IDirect3DSurface9 *surface); RenderTarget9(Renderer *renderer, GLsizei width, GLsizei height, GLenum format, GLsizei samples); virtual ~RenderTarget9(); static RenderTarget9 *makeRenderTarget9(RenderTarget *renderTarget); IDirect3DSurface9 *getSurface(); private: DISALLOW_COPY_AND_ASSIGN(RenderTarget9); IDirect3DSurface9 *mRenderTarget; Renderer9 *mRenderer; }; } #endif // LIBGLESV2_RENDERER_RENDERTARGET9_H_
C++
#include "precompiled.h" // // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // IndexBuffer11.cpp: Defines the D3D11 IndexBuffer implementation. #include "libGLESv2/renderer/IndexBuffer11.h" #include "libGLESv2/renderer/Renderer11.h" namespace rx { IndexBuffer11::IndexBuffer11(Renderer11 *const renderer) : mRenderer(renderer) { mBuffer = NULL; mBufferSize = 0; mDynamicUsage = false; } IndexBuffer11::~IndexBuffer11() { if (mBuffer) { mBuffer->Release(); mBuffer = NULL; } } bool IndexBuffer11::initialize(unsigned int bufferSize, GLenum indexType, bool dynamic) { if (mBuffer) { mBuffer->Release(); mBuffer = NULL; } updateSerial(); if (bufferSize > 0) { ID3D11Device* dxDevice = mRenderer->getDevice(); D3D11_BUFFER_DESC bufferDesc; bufferDesc.ByteWidth = bufferSize; bufferDesc.Usage = D3D11_USAGE_DYNAMIC; bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; bufferDesc.MiscFlags = 0; bufferDesc.StructureByteStride = 0; HRESULT result = dxDevice->CreateBuffer(&bufferDesc, NULL, &mBuffer); if (FAILED(result)) { return false; } } mBufferSize = bufferSize; mIndexType = indexType; mDynamicUsage = dynamic; return true; } IndexBuffer11 *IndexBuffer11::makeIndexBuffer11(IndexBuffer *indexBuffer) { ASSERT(HAS_DYNAMIC_TYPE(IndexBuffer11*, indexBuffer)); return static_cast<IndexBuffer11*>(indexBuffer); } bool IndexBuffer11::mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory) { if (mBuffer) { if (offset + size > mBufferSize) { ERR("Index buffer map range is not inside the buffer."); return false; } ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_NO_OVERWRITE, 0, &mappedResource); if (FAILED(result)) { ERR("Index buffer map failed with error 0x%08x", result); return false; } *outMappedMemory = reinterpret_cast<char*>(mappedResource.pData) + offset; return true; } else { ERR("Index buffer not initialized."); return false; } } bool IndexBuffer11::unmapBuffer() { if (mBuffer) { ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); dxContext->Unmap(mBuffer, 0); return true; } else { ERR("Index buffer not initialized."); return false; } } GLenum IndexBuffer11::getIndexType() const { return mIndexType; } unsigned int IndexBuffer11::getBufferSize() const { return mBufferSize; } bool IndexBuffer11::setSize(unsigned int bufferSize, GLenum indexType) { if (bufferSize > mBufferSize || indexType != mIndexType) { return initialize(bufferSize, indexType, mDynamicUsage); } else { return true; } } bool IndexBuffer11::discard() { if (mBuffer) { ID3D11DeviceContext *dxContext = mRenderer->getDeviceContext(); D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = dxContext->Map(mBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { ERR("Index buffer map failed with error 0x%08x", result); return false; } dxContext->Unmap(mBuffer, 0); return true; } else { ERR("Index buffer not initialized."); return false; } } DXGI_FORMAT IndexBuffer11::getIndexFormat() const { switch (mIndexType) { case GL_UNSIGNED_BYTE: return DXGI_FORMAT_R16_UINT; case GL_UNSIGNED_SHORT: return DXGI_FORMAT_R16_UINT; case GL_UNSIGNED_INT: return DXGI_FORMAT_R32_UINT; default: UNREACHABLE(); return DXGI_FORMAT_UNKNOWN; } } ID3D11Buffer *IndexBuffer11::getBuffer() const { return mBuffer; } }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // VertexDeclarationCache.h: Defines a helper class to construct and cache vertex declarations. #ifndef LIBGLESV2_RENDERER_VERTEXDECLARATIONCACHE_H_ #define LIBGLESV2_RENDERER_VERTEXDECLARATIONCACHE_H_ #include "libGLESv2/renderer/VertexDataManager.h" namespace gl { class VertexDataManager; } namespace rx { class VertexDeclarationCache { public: VertexDeclarationCache(); ~VertexDeclarationCache(); GLenum applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], gl::ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw); void markStateDirty(); private: UINT mMaxLru; enum { NUM_VERTEX_DECL_CACHE_ENTRIES = 32 }; struct VBData { unsigned int serial; unsigned int stride; unsigned int offset; }; VBData mAppliedVBs[gl::MAX_VERTEX_ATTRIBS]; IDirect3DVertexDeclaration9 *mLastSetVDecl; bool mInstancingEnabled; struct VertexDeclCacheEntry { D3DVERTEXELEMENT9 cachedElements[gl::MAX_VERTEX_ATTRIBS + 1]; UINT lruCount; IDirect3DVertexDeclaration9 *vertexDeclaration; } mVertexDeclCache[NUM_VERTEX_DECL_CACHE_ENTRIES]; }; } #endif // LIBGLESV2_RENDERER_VERTEXDECLARATIONCACHE_H_
C++
#include "precompiled.h" // // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // SwapChain9.cpp: Implements a back-end specific class for the D3D9 swap chain. #include "libGLESv2/renderer/SwapChain9.h" #include "libGLESv2/renderer/renderer9_utils.h" #include "libGLESv2/renderer/Renderer9.h" namespace rx { SwapChain9::SwapChain9(Renderer9 *renderer, HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat) : mRenderer(renderer), SwapChain(window, shareHandle, backBufferFormat, depthBufferFormat) { mSwapChain = NULL; mBackBuffer = NULL; mDepthStencil = NULL; mRenderTarget = NULL; mOffscreenTexture = NULL; mWidth = -1; mHeight = -1; mSwapInterval = -1; } SwapChain9::~SwapChain9() { release(); } void SwapChain9::release() { if (mSwapChain) { mSwapChain->Release(); mSwapChain = NULL; } if (mBackBuffer) { mBackBuffer->Release(); mBackBuffer = NULL; } if (mDepthStencil) { mDepthStencil->Release(); mDepthStencil = NULL; } if (mRenderTarget) { mRenderTarget->Release(); mRenderTarget = NULL; } if (mOffscreenTexture) { mOffscreenTexture->Release(); mOffscreenTexture = NULL; } if (mWindow) mShareHandle = NULL; } static DWORD convertInterval(EGLint interval) { switch(interval) { case 0: return D3DPRESENT_INTERVAL_IMMEDIATE; case 1: return D3DPRESENT_INTERVAL_ONE; case 2: return D3DPRESENT_INTERVAL_TWO; case 3: return D3DPRESENT_INTERVAL_THREE; case 4: return D3DPRESENT_INTERVAL_FOUR; default: UNREACHABLE(); } return D3DPRESENT_INTERVAL_DEFAULT; } EGLint SwapChain9::resize(int backbufferWidth, int backbufferHeight) { // D3D9 does not support resizing swap chains without recreating them return reset(backbufferWidth, backbufferHeight, mSwapInterval); } EGLint SwapChain9::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval) { IDirect3DDevice9 *device = mRenderer->getDevice(); if (device == NULL) { return EGL_BAD_ACCESS; } // Evict all non-render target textures to system memory and release all resources // before reallocating them to free up as much video memory as possible. device->EvictManagedResources(); HRESULT result; // Release specific resources to free up memory for the new render target, while the // old render target still exists for the purpose of preserving its contents. if (mSwapChain) { mSwapChain->Release(); mSwapChain = NULL; } if (mBackBuffer) { mBackBuffer->Release(); mBackBuffer = NULL; } if (mOffscreenTexture) { mOffscreenTexture->Release(); mOffscreenTexture = NULL; } if (mDepthStencil) { mDepthStencil->Release(); mDepthStencil = NULL; } HANDLE *pShareHandle = NULL; if (!mWindow && mRenderer->getShareHandleSupport()) { pShareHandle = &mShareHandle; } result = device->CreateTexture(backbufferWidth, backbufferHeight, 1, D3DUSAGE_RENDERTARGET, gl_d3d9::ConvertRenderbufferFormat(mBackBufferFormat), D3DPOOL_DEFAULT, &mOffscreenTexture, pShareHandle); if (FAILED(result)) { ERR("Could not create offscreen texture: %08lX", result); release(); if (d3d9::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } else { return EGL_BAD_ALLOC; } } IDirect3DSurface9 *oldRenderTarget = mRenderTarget; result = mOffscreenTexture->GetSurfaceLevel(0, &mRenderTarget); ASSERT(SUCCEEDED(result)); if (oldRenderTarget) { RECT rect = { 0, 0, mWidth, mHeight }; if (rect.right > static_cast<LONG>(backbufferWidth)) { rect.right = backbufferWidth; } if (rect.bottom > static_cast<LONG>(backbufferHeight)) { rect.bottom = backbufferHeight; } mRenderer->endScene(); result = device->StretchRect(oldRenderTarget, &rect, mRenderTarget, &rect, D3DTEXF_NONE); ASSERT(SUCCEEDED(result)); oldRenderTarget->Release(); } if (mWindow) { D3DPRESENT_PARAMETERS presentParameters = {0}; presentParameters.AutoDepthStencilFormat = gl_d3d9::ConvertRenderbufferFormat(mDepthBufferFormat); presentParameters.BackBufferCount = 1; presentParameters.BackBufferFormat = gl_d3d9::ConvertRenderbufferFormat(mBackBufferFormat); presentParameters.EnableAutoDepthStencil = FALSE; presentParameters.Flags = 0; presentParameters.hDeviceWindow = mWindow; presentParameters.MultiSampleQuality = 0; // FIXME: Unimplemented presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; // FIXME: Unimplemented presentParameters.PresentationInterval = convertInterval(swapInterval); presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; presentParameters.Windowed = TRUE; presentParameters.BackBufferWidth = backbufferWidth; presentParameters.BackBufferHeight = backbufferHeight; // http://crbug.com/140239 // http://crbug.com/143434 // // Some AMD/Intel switchable systems / drivers appear to round swap chain surfaces to a multiple of 64 pixels in width // when using the integrated Intel. This rounds the width up rather than down. // // Some non-switchable AMD GPUs / drivers do not respect the source rectangle to Present. Therefore, when the vendor ID // is not Intel, the back buffer width must be exactly the same width as the window or horizontal scaling will occur. if (mRenderer->getAdapterVendor() == VENDOR_ID_INTEL) { presentParameters.BackBufferWidth = (presentParameters.BackBufferWidth + 63) / 64 * 64; } result = device->CreateAdditionalSwapChain(&presentParameters, &mSwapChain); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_INVALIDCALL || result == D3DERR_DEVICELOST); ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result); release(); if (d3d9::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } else { return EGL_BAD_ALLOC; } } result = mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &mBackBuffer); ASSERT(SUCCEEDED(result)); InvalidateRect(mWindow, NULL, FALSE); } if (mDepthBufferFormat != GL_NONE) { result = device->CreateDepthStencilSurface(backbufferWidth, backbufferHeight, gl_d3d9::ConvertRenderbufferFormat(mDepthBufferFormat), D3DMULTISAMPLE_NONE, 0, FALSE, &mDepthStencil, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_INVALIDCALL); ERR("Could not create depthstencil surface for new swap chain: 0x%08X", result); release(); if (d3d9::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } else { return EGL_BAD_ALLOC; } } } mWidth = backbufferWidth; mHeight = backbufferHeight; mSwapInterval = swapInterval; return EGL_SUCCESS; } // parameters should be validated/clamped by caller EGLint SwapChain9::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) { if (!mSwapChain) { return EGL_SUCCESS; } IDirect3DDevice9 *device = mRenderer->getDevice(); // Disable all pipeline operations device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); device->SetRenderState(D3DRS_STENCILENABLE, FALSE); device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED); device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); device->SetPixelShader(NULL); device->SetVertexShader(NULL); device->SetRenderTarget(0, mBackBuffer); device->SetDepthStencilSurface(NULL); device->SetTexture(0, mOffscreenTexture); device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); device->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1); D3DVIEWPORT9 viewport = {0, 0, mWidth, mHeight, 0.0f, 1.0f}; device->SetViewport(&viewport); float x1 = x - 0.5f; float y1 = (mHeight - y - height) - 0.5f; float x2 = (x + width) - 0.5f; float y2 = (mHeight - y) - 0.5f; float u1 = x / float(mWidth); float v1 = y / float(mHeight); float u2 = (x + width) / float(mWidth); float v2 = (y + height) / float(mHeight); float quad[4][6] = {{x1, y1, 0.0f, 1.0f, u1, v2}, {x2, y1, 0.0f, 1.0f, u2, v2}, {x2, y2, 0.0f, 1.0f, u2, v1}, {x1, y2, 0.0f, 1.0f, u1, v1}}; // x, y, z, rhw, u, v mRenderer->startScene(); device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, quad, 6 * sizeof(float)); mRenderer->endScene(); device->SetTexture(0, NULL); RECT rect = { x, mHeight - y - height, x + width, mHeight - y }; HRESULT result = mSwapChain->Present(&rect, &rect, NULL, NULL, 0); mRenderer->markAllStateDirty(); if (d3d9::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DRIVERINTERNALERROR) { return EGL_BAD_ALLOC; } ASSERT(SUCCEEDED(result)); return EGL_SUCCESS; } // Increments refcount on surface. // caller must Release() the returned surface IDirect3DSurface9 *SwapChain9::getRenderTarget() { if (mRenderTarget) { mRenderTarget->AddRef(); } return mRenderTarget; } // Increments refcount on surface. // caller must Release() the returned surface IDirect3DSurface9 *SwapChain9::getDepthStencil() { if (mDepthStencil) { mDepthStencil->AddRef(); } return mDepthStencil; } // Increments refcount on texture. // caller must Release() the returned texture IDirect3DTexture9 *SwapChain9::getOffscreenTexture() { if (mOffscreenTexture) { mOffscreenTexture->AddRef(); } return mOffscreenTexture; } SwapChain9 *SwapChain9::makeSwapChain9(SwapChain *swapChain) { ASSERT(HAS_DYNAMIC_TYPE(rx::SwapChain9*, swapChain)); return static_cast<rx::SwapChain9*>(swapChain); } void SwapChain9::recreate() { if (!mSwapChain) { return; } IDirect3DDevice9 *device = mRenderer->getDevice(); if (device == NULL) { return; } D3DPRESENT_PARAMETERS presentParameters; HRESULT result = mSwapChain->GetPresentParameters(&presentParameters); ASSERT(SUCCEEDED(result)); IDirect3DSwapChain9* newSwapChain = NULL; result = device->CreateAdditionalSwapChain(&presentParameters, &newSwapChain); if (FAILED(result)) { return; } mSwapChain->Release(); mSwapChain = newSwapChain; mBackBuffer->Release(); result = mSwapChain->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &mBackBuffer); ASSERT(SUCCEEDED(result)); } }
C++
// // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Renderer11.h: Defines a back-end specific class for the D3D11 renderer. #ifndef LIBGLESV2_RENDERER_RENDERER11_H_ #define LIBGLESV2_RENDERER_RENDERER11_H_ #include "common/angleutils.h" #include "libGLESv2/angletypes.h" #include "libGLESv2/mathutil.h" #include "libGLESv2/renderer/Renderer.h" #include "libGLESv2/renderer/RenderStateCache.h" #include "libGLESv2/renderer/InputLayoutCache.h" #include "libGLESv2/renderer/RenderTarget.h" namespace gl { class Renderbuffer; } namespace rx { class VertexDataManager; class IndexDataManager; class StreamingIndexBufferInterface; enum { MAX_VERTEX_UNIFORM_VECTORS_D3D11 = 1024, MAX_FRAGMENT_UNIFORM_VECTORS_D3D11 = 1024 }; class Renderer11 : public Renderer { public: Renderer11(egl::Display *display, HDC hDc); virtual ~Renderer11(); static Renderer11 *makeRenderer11(Renderer *renderer); virtual EGLint initialize(); virtual bool resetDevice(); virtual int generateConfigs(ConfigDesc **configDescList); virtual void deleteConfigs(ConfigDesc *configDescList); virtual void sync(bool block); virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat); virtual void setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &sampler); virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture); virtual void setRasterizerState(const gl::RasterizerState &rasterState); virtual void setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask); virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, int stencilBackRef, bool frontFaceCCW); virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled); virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace, bool ignoreViewport); virtual bool applyPrimitiveType(GLenum mode, GLsizei count); virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer); virtual void applyShaders(gl::ProgramBinary *programBinary); virtual void applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray); virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances); virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo); virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances); virtual void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances); virtual void clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer); virtual void markAllStateDirty(); // lost device void notifyDeviceLost(); virtual bool isDeviceLost(); virtual bool testDeviceLost(bool notify); virtual bool testDeviceResettable(); // Renderer capabilities virtual DWORD getAdapterVendor() const; virtual std::string getRendererDescription() const; virtual GUID getAdapterIdentifier() const; virtual bool getBGRATextureSupport() const; virtual bool getDXT1TextureSupport(); virtual bool getDXT3TextureSupport(); virtual bool getDXT5TextureSupport(); virtual bool getEventQuerySupport(); virtual bool getFloat32TextureSupport(bool *filtering, bool *renderable); virtual bool getFloat16TextureSupport(bool *filtering, bool *renderable); virtual bool getLuminanceTextureSupport(); virtual bool getLuminanceAlphaTextureSupport(); virtual unsigned int getMaxVertexTextureImageUnits() const; virtual unsigned int getMaxCombinedTextureImageUnits() const; virtual unsigned int getReservedVertexUniformVectors() const; virtual unsigned int getReservedFragmentUniformVectors() const; virtual unsigned int getMaxVertexUniformVectors() const; virtual unsigned int getMaxFragmentUniformVectors() const; virtual unsigned int getMaxVaryingVectors() const; virtual bool getNonPower2TextureSupport() const; virtual bool getDepthTextureSupport() const; virtual bool getOcclusionQuerySupport() const; virtual bool getInstancingSupport() const; virtual bool getTextureFilterAnisotropySupport() const; virtual float getTextureMaxAnisotropy() const; virtual bool getShareHandleSupport() const; virtual bool getDerivativeInstructionSupport() const; virtual bool getPostSubBufferSupport() const; virtual int getMajorShaderModel() const; virtual float getMaxPointSize() const; virtual int getMaxViewportDimension() const; virtual int getMaxTextureWidth() const; virtual int getMaxTextureHeight() const; virtual bool get32BitIndexSupport() const; virtual int getMinSwapInterval() const; virtual int getMaxSwapInterval() const; virtual GLsizei getMaxSupportedSamples() const; int getNearestSupportedSamples(DXGI_FORMAT format, unsigned int requested) const; virtual unsigned int getMaxRenderTargets() const; // Pixel operations virtual bool copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source); virtual bool copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source); virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level); virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level); bool copyTexture(ID3D11ShaderResourceView *source, const gl::Rectangle &sourceArea, unsigned int sourceWidth, unsigned int sourceHeight, ID3D11RenderTargetView *dest, const gl::Rectangle &destArea, unsigned int destWidth, unsigned int destHeight, GLenum destFormat); virtual bool blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect, bool blitRenderTarget, bool blitDepthStencil); virtual void readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels); // RenderTarget creation virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth); virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth); // Shader operations virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type); virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type); // Image operations virtual Image *createImage(); virtual void generateMipmap(Image *dest, Image *source); virtual TextureStorage *createTextureStorage2D(SwapChain *swapChain); virtual TextureStorage *createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height); virtual TextureStorage *createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size); // Buffer creation virtual VertexBuffer *createVertexBuffer(); virtual IndexBuffer *createIndexBuffer(); virtual BufferStorage *createBufferStorage(); // Query and Fence creation virtual QueryImpl *createQuery(GLenum type); virtual FenceImpl *createFence(); // D3D11-renderer specific methods ID3D11Device *getDevice() { return mDevice; } ID3D11DeviceContext *getDeviceContext() { return mDeviceContext; }; IDXGIFactory *getDxgiFactory() { return mDxgiFactory; }; bool getRenderTargetResource(gl::Renderbuffer *colorbuffer, unsigned int *subresourceIndex, ID3D11Texture2D **resource); void unapplyRenderTargets(); void setOneTimeRenderTarget(ID3D11RenderTargetView *renderTargetView); virtual bool getLUID(LUID *adapterLuid) const; private: DISALLOW_COPY_AND_ASSIGN(Renderer11); void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer); void drawTriangleFan(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer, int instances); void readTextureData(ID3D11Texture2D *texture, unsigned int subResource, const gl::Rectangle &area, GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void *pixels); void maskedClear(const gl::ClearParameters &clearParams, bool usingExtendedDrawBuffers); rx::Range getViewportBounds() const; bool blitRenderbufferRect(const gl::Rectangle &readRect, const gl::Rectangle &drawRect, RenderTarget *readRenderTarget, RenderTarget *drawRenderTarget, bool wholeBufferCopy); ID3D11Texture2D *resolveMultisampledTexture(ID3D11Texture2D *source, unsigned int subresource); HMODULE mD3d11Module; HMODULE mDxgiModule; HDC mDc; bool mDeviceLost; void initializeDevice(); void releaseDeviceResources(); int getMinorShaderModel() const; void release(); RenderStateCache mStateCache; // Support flags bool mFloat16TextureSupport; bool mFloat16FilterSupport; bool mFloat16RenderSupport; bool mFloat32TextureSupport; bool mFloat32FilterSupport; bool mFloat32RenderSupport; bool mDXT1TextureSupport; bool mDXT3TextureSupport; bool mDXT5TextureSupport; bool mDepthTextureSupport; // Multisample format support struct MultisampleSupportInfo { unsigned int qualityLevels[D3D11_MAX_MULTISAMPLE_SAMPLE_COUNT]; }; typedef std::unordered_map<DXGI_FORMAT, MultisampleSupportInfo> MultisampleSupportMap; MultisampleSupportMap mMultisampleSupportMap; unsigned int mMaxSupportedSamples; // current render target states unsigned int mAppliedRenderTargetSerials[gl::IMPLEMENTATION_MAX_DRAW_BUFFERS]; unsigned int mAppliedDepthbufferSerial; unsigned int mAppliedStencilbufferSerial; bool mDepthStencilInitialized; bool mRenderTargetDescInitialized; rx::RenderTarget::Desc mRenderTargetDesc; unsigned int mCurDepthSize; unsigned int mCurStencilSize; // Currently applied sampler states bool mForceSetVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS]; gl::SamplerState mCurVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS]; bool mForceSetPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS]; gl::SamplerState mCurPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS]; // Currently applied textures unsigned int mCurVertexTextureSerials[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS]; unsigned int mCurPixelTextureSerials[gl::MAX_TEXTURE_IMAGE_UNITS]; // Currently applied blend state bool mForceSetBlendState; gl::BlendState mCurBlendState; gl::Color mCurBlendColor; unsigned int mCurSampleMask; // Currently applied rasterizer state bool mForceSetRasterState; gl::RasterizerState mCurRasterState; // Currently applied depth stencil state bool mForceSetDepthStencilState; gl::DepthStencilState mCurDepthStencilState; int mCurStencilRef; int mCurStencilBackRef; // Currently applied scissor rectangle bool mForceSetScissor; bool mScissorEnabled; gl::Rectangle mCurScissor; // Currently applied viewport bool mForceSetViewport; gl::Rectangle mCurViewport; float mCurNear; float mCurFar; unsigned int mAppliedIBSerial; unsigned int mAppliedStorageIBSerial; unsigned int mAppliedIBOffset; unsigned int mAppliedProgramBinarySerial; bool mIsGeometryShaderActive; dx_VertexConstants mVertexConstants; dx_VertexConstants mAppliedVertexConstants; ID3D11Buffer *mDriverConstantBufferVS; dx_PixelConstants mPixelConstants; dx_PixelConstants mAppliedPixelConstants; ID3D11Buffer *mDriverConstantBufferPS; // Vertex, index and input layouts VertexDataManager *mVertexDataManager; IndexDataManager *mIndexDataManager; InputLayoutCache mInputLayoutCache; StreamingIndexBufferInterface *mLineLoopIB; StreamingIndexBufferInterface *mTriangleFanIB; // Texture copy resources bool mCopyResourcesInitialized; ID3D11Buffer *mCopyVB; ID3D11SamplerState *mCopySampler; ID3D11InputLayout *mCopyIL; ID3D11VertexShader *mCopyVS; ID3D11PixelShader *mCopyRGBAPS; ID3D11PixelShader *mCopyRGBPS; ID3D11PixelShader *mCopyLumPS; ID3D11PixelShader *mCopyLumAlphaPS; // Masked clear resources bool mClearResourcesInitialized; ID3D11Buffer *mClearVB; ID3D11InputLayout *mClearIL; ID3D11VertexShader *mClearVS; ID3D11PixelShader *mClearSinglePS; ID3D11PixelShader *mClearMultiplePS; ID3D11RasterizerState *mClearScissorRS; ID3D11RasterizerState *mClearNoScissorRS; // Sync query ID3D11Query *mSyncQuery; ID3D11Device *mDevice; D3D_FEATURE_LEVEL mFeatureLevel; ID3D11DeviceContext *mDeviceContext; IDXGIAdapter *mDxgiAdapter; DXGI_ADAPTER_DESC mAdapterDescription; char mDescription[128]; IDXGIFactory *mDxgiFactory; // Cached device caps bool mBGRATextureSupport; }; } #endif // LIBGLESV2_RENDERER_RENDERER11_H_
C++
// // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Blit.cpp: Surface copy utility class. #ifndef LIBGLESV2_BLIT_H_ #define LIBGLESV2_BLIT_H_ #include "common/angleutils.h" namespace gl { class Framebuffer; } namespace rx { class Renderer9; class TextureStorageInterface2D; class TextureStorageInterfaceCube; class Blit { public: explicit Blit(Renderer9 *renderer); ~Blit(); // Copy from source surface to dest surface. // sourceRect, xoffset, yoffset are in D3D coordinates (0,0 in upper-left) bool copy(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level); bool copy(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level); // Copy from source surface to dest surface. // sourceRect, xoffset, yoffset are in D3D coordinates (0,0 in upper-left) // source is interpreted as RGBA and destFormat specifies the desired result format. For example, if destFormat = GL_RGB, the alpha channel will be forced to 0. bool formatConvert(IDirect3DSurface9 *source, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, IDirect3DSurface9 *dest); // 2x2 box filter sample from source to dest. // Requires that source is RGB(A) and dest has the same format as source. bool boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest); private: rx::Renderer9 *mRenderer; IDirect3DVertexBuffer9 *mQuadVertexBuffer; IDirect3DVertexDeclaration9 *mQuadVertexDeclaration; void initGeometry(); bool setFormatConvertShaders(GLenum destFormat); bool copy(IDirect3DSurface9 *source, const RECT &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, IDirect3DSurface9 *dest); IDirect3DTexture9 *copySurfaceToTexture(IDirect3DSurface9 *surface, const RECT &sourceRect); void setViewport(const RECT &sourceRect, GLint xoffset, GLint yoffset); void setCommonBlitState(); RECT getSurfaceRect(IDirect3DSurface9 *surface) const; // This enum is used to index mCompiledShaders and mShaderSource. enum ShaderId { SHADER_VS_STANDARD, SHADER_VS_FLIPY, SHADER_PS_PASSTHROUGH, SHADER_PS_LUMINANCE, SHADER_PS_COMPONENTMASK, SHADER_COUNT }; // This actually contains IDirect3DVertexShader9 or IDirect3DPixelShader9 casted to IUnknown. IUnknown *mCompiledShaders[SHADER_COUNT]; template <class D3DShaderType> bool setShader(ShaderId source, const char *profile, D3DShaderType *(Renderer9::*createShader)(const DWORD *, size_t length), HRESULT (WINAPI IDirect3DDevice9::*setShader)(D3DShaderType*)); bool setVertexShader(ShaderId shader); bool setPixelShader(ShaderId shader); void render(); void saveState(); void restoreState(); IDirect3DStateBlock9 *mSavedStateBlock; IDirect3DSurface9 *mSavedRenderTarget; IDirect3DSurface9 *mSavedDepthStencil; DISALLOW_COPY_AND_ASSIGN(Blit); }; } #endif // LIBGLESV2_BLIT_H_
C++
#include "precompiled.h" // // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // ShaderExecutable9.cpp: Implements a D3D9-specific class to contain shader // executable implementation details. #include "libGLESv2/renderer/ShaderExecutable9.h" #include "common/debug.h" namespace rx { ShaderExecutable9::ShaderExecutable9(const void *function, size_t length, IDirect3DPixelShader9 *executable) : ShaderExecutable(function, length) { mPixelExecutable = executable; mVertexExecutable = NULL; } ShaderExecutable9::ShaderExecutable9(const void *function, size_t length, IDirect3DVertexShader9 *executable) : ShaderExecutable(function, length) { mVertexExecutable = executable; mPixelExecutable = NULL; } ShaderExecutable9::~ShaderExecutable9() { if (mVertexExecutable) { mVertexExecutable->Release(); } if (mPixelExecutable) { mPixelExecutable->Release(); } } ShaderExecutable9 *ShaderExecutable9::makeShaderExecutable9(ShaderExecutable *executable) { ASSERT(HAS_DYNAMIC_TYPE(ShaderExecutable9*, executable)); return static_cast<ShaderExecutable9*>(executable); } IDirect3DVertexShader9 *ShaderExecutable9::getVertexShader() const { return mVertexExecutable; } IDirect3DPixelShader9 *ShaderExecutable9::getPixelShader() const { return mPixelExecutable; } }
C++
// // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Fence9.h: Defines the rx::Fence9 class which implements rx::FenceImpl. #ifndef LIBGLESV2_RENDERER_FENCE9_H_ #define LIBGLESV2_RENDERER_FENCE9_H_ #include "libGLESv2/renderer/FenceImpl.h" namespace rx { class Renderer9; class Fence9 : public FenceImpl { public: explicit Fence9(rx::Renderer9 *renderer); virtual ~Fence9(); GLboolean isFence(); void setFence(GLenum condition); GLboolean testFence(); void finishFence(); void getFenceiv(GLenum pname, GLint *params); private: DISALLOW_COPY_AND_ASSIGN(Fence9); rx::Renderer9 *mRenderer; IDirect3DQuery9 *mQuery; }; } #endif // LIBGLESV2_RENDERER_FENCE9_H_
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // InputLayoutCache.h: Defines InputLayoutCache, a class that builds and caches // D3D11 input layouts. #ifndef LIBGLESV2_RENDERER_INPUTLAYOUTCACHE_H_ #define LIBGLESV2_RENDERER_INPUTLAYOUTCACHE_H_ #include "libGLESv2/Constants.h" #include "common/angleutils.h" namespace gl { class ProgramBinary; } namespace rx { struct TranslatedAttribute; class InputLayoutCache { public: InputLayoutCache(); virtual ~InputLayoutCache(); void initialize(ID3D11Device *device, ID3D11DeviceContext *context); void clear(); GLenum applyVertexBuffers(TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], gl::ProgramBinary *programBinary); private: DISALLOW_COPY_AND_ASSIGN(InputLayoutCache); struct InputLayoutKey { unsigned int elementCount; D3D11_INPUT_ELEMENT_DESC elements[gl::MAX_VERTEX_ATTRIBS]; GLenum glslElementType[gl::MAX_VERTEX_ATTRIBS]; }; struct InputLayoutCounterPair { ID3D11InputLayout *inputLayout; unsigned long long lastUsedTime; }; static std::size_t hashInputLayout(const InputLayoutKey &inputLayout); static bool compareInputLayouts(const InputLayoutKey &a, const InputLayoutKey &b); typedef std::size_t (*InputLayoutHashFunction)(const InputLayoutKey &); typedef bool (*InputLayoutEqualityFunction)(const InputLayoutKey &, const InputLayoutKey &); typedef std::unordered_map<InputLayoutKey, InputLayoutCounterPair, InputLayoutHashFunction, InputLayoutEqualityFunction> InputLayoutMap; InputLayoutMap mInputLayoutMap; static const unsigned int kMaxInputLayouts; unsigned long long mCounter; ID3D11Device *mDevice; ID3D11DeviceContext *mDeviceContext; }; } #endif // LIBGLESV2_RENDERER_INPUTLAYOUTCACHE_H_
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // RenderTarget.h: Defines an abstract wrapper class to manage IDirect3DSurface9 // and ID3D11View objects belonging to renderbuffers. #ifndef LIBGLESV2_RENDERER_RENDERTARGET_H_ #define LIBGLESV2_RENDERER_RENDERTARGET_H_ #include "common/angleutils.h" namespace rx { class RenderTarget { public: RenderTarget() { mWidth = 0; mHeight = 0; mInternalFormat = GL_NONE; mActualFormat = GL_NONE; mSamples = 0; } virtual ~RenderTarget() {}; GLsizei getWidth() { return mWidth; } GLsizei getHeight() { return mHeight; } GLenum getInternalFormat() { return mInternalFormat; } GLenum getActualFormat() { return mActualFormat; } GLsizei getSamples() { return mSamples; } struct Desc { GLsizei width; GLsizei height; GLenum format; }; protected: GLsizei mWidth; GLsizei mHeight; GLenum mInternalFormat; GLenum mActualFormat; GLsizei mSamples; private: DISALLOW_COPY_AND_ASSIGN(RenderTarget); }; } #endif // LIBGLESV2_RENDERTARGET_H_
C++
// // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // vertexconversion.h: A library of vertex conversion classes that can be used to build // the FormatConverter objects used by the buffer conversion system. #ifndef LIBGLESV2_VERTEXCONVERSION_H_ #define LIBGLESV2_VERTEXCONVERSION_H_ namespace rx { // Conversion types: // static const bool identity: true if this is an identity transform, false otherwise // static U convert(T): convert a single element from the input type to the output type // typedef ... OutputType: the type produced by this conversion template <class T> struct Identity { static const bool identity = true; typedef T OutputType; static T convert(T x) { return x; } }; template <class FromT, class ToT> struct Cast { static const bool identity = false; typedef ToT OutputType; static ToT convert(FromT x) { return static_cast<ToT>(x); } }; template <class T> struct Cast<T, T> { static const bool identity = true; typedef T OutputType; static T convert(T x) { return static_cast<T>(x); } }; template <class T> struct Normalize { static const bool identity = false; typedef float OutputType; static float convert(T x) { typedef std::numeric_limits<T> NL; float f = static_cast<float>(x); if (NL::is_signed) { // const float => VC2008 computes it at compile time // static const float => VC2008 computes it the first time we get here, stores it to memory with static guard and all that. const float divisor = 1.0f/(2*static_cast<float>(NL::max())+1); return (2*f+1)*divisor; } else { return f/NL::max(); } } }; template <class FromType, std::size_t ScaleBits> struct FixedToFloat { static const bool identity = false; typedef float OutputType; static float convert(FromType x) { const float divisor = 1.0f / static_cast<float>(static_cast<FromType>(1) << ScaleBits); return static_cast<float>(x) * divisor; } }; // Widen types: // static const unsigned int initialWidth: number of components before conversion // static const unsigned int finalWidth: number of components after conversion // Float is supported at any size. template <std::size_t N> struct NoWiden { static const std::size_t initialWidth = N; static const std::size_t finalWidth = N; }; // SHORT, norm-SHORT, norm-UNSIGNED_SHORT are supported but only with 2 or 4 components template <std::size_t N> struct WidenToEven { static const std::size_t initialWidth = N; static const std::size_t finalWidth = N+(N&1); }; template <std::size_t N> struct WidenToFour { static const std::size_t initialWidth = N; static const std::size_t finalWidth = 4; }; // Most types have 0 and 1 that are just that. template <class T> struct SimpleDefaultValues { static T zero() { return static_cast<T>(0); } static T one() { return static_cast<T>(1); } }; // But normalised types only store [0,1] or [-1,1] so 1.0 is represented by the max value. template <class T> struct NormalizedDefaultValues { static T zero() { return static_cast<T>(0); } static T one() { return std::numeric_limits<T>::max(); } }; // Converter: // static const bool identity: true if this is an identity transform (with no widening) // static const std::size_t finalSize: number of bytes per output vertex // static void convertArray(const void *in, std::size_t stride, std::size_t n, void *out): convert an array of vertices. Input may be strided, but output will be unstrided. template <class InT, class WidenRule, class Converter, class DefaultValueRule = SimpleDefaultValues<InT> > struct VertexDataConverter { typedef typename Converter::OutputType OutputType; typedef InT InputType; static const bool identity = (WidenRule::initialWidth == WidenRule::finalWidth) && Converter::identity; static const std::size_t finalSize = WidenRule::finalWidth * sizeof(OutputType); static void convertArray(const InputType *in, std::size_t stride, std::size_t n, OutputType *out) { for (std::size_t i = 0; i < n; i++) { const InputType *ein = pointerAddBytes(in, i * stride); copyComponent(out, ein, 0, static_cast<OutputType>(DefaultValueRule::zero())); copyComponent(out, ein, 1, static_cast<OutputType>(DefaultValueRule::zero())); copyComponent(out, ein, 2, static_cast<OutputType>(DefaultValueRule::zero())); copyComponent(out, ein, 3, static_cast<OutputType>(DefaultValueRule::one())); out += WidenRule::finalWidth; } } static void convertArray(const void *in, std::size_t stride, std::size_t n, void *out) { return convertArray(static_cast<const InputType*>(in), stride, n, static_cast<OutputType*>(out)); } private: // Advance the given pointer by a number of bytes (not pointed-to elements). template <class T> static T *pointerAddBytes(T *basePtr, std::size_t numBytes) { return reinterpret_cast<T *>(reinterpret_cast<uintptr_t>(basePtr) + numBytes); } static void copyComponent(OutputType *out, const InputType *in, std::size_t elementindex, OutputType defaultvalue) { if (WidenRule::finalWidth > elementindex) { if (WidenRule::initialWidth > elementindex) { out[elementindex] = Converter::convert(in[elementindex]); } else { out[elementindex] = defaultvalue; } } } }; } #endif // LIBGLESV2_VERTEXCONVERSION_H_
C++
// // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // VertexBuffer.h: Defines the abstract VertexBuffer class and VertexBufferInterface // class with derivations, classes that perform graphics API agnostic vertex buffer operations. #ifndef LIBGLESV2_RENDERER_VERTEXBUFFER_H_ #define LIBGLESV2_RENDERER_VERTEXBUFFER_H_ #include "common/angleutils.h" namespace gl { class VertexAttribute; } namespace rx { class Renderer; class VertexBuffer { public: VertexBuffer(); virtual ~VertexBuffer(); virtual bool initialize(unsigned int size, bool dynamicUsage) = 0; virtual bool storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances, unsigned int offset) = 0; virtual bool storeRawData(const void* data, unsigned int size, unsigned int offset) = 0; virtual unsigned int getSpaceRequired(const gl::VertexAttribute &attrib, GLsizei count, GLsizei instances) const = 0; virtual bool requiresConversion(const gl::VertexAttribute &attrib) const = 0; virtual unsigned int getBufferSize() const = 0; virtual bool setBufferSize(unsigned int size) = 0; virtual bool discard() = 0; unsigned int getSerial() const; protected: void updateSerial(); private: DISALLOW_COPY_AND_ASSIGN(VertexBuffer); unsigned int mSerial; static unsigned int mNextSerial; }; class VertexBufferInterface { public: VertexBufferInterface(rx::Renderer *renderer, bool dynamic); virtual ~VertexBufferInterface(); void reserveVertexSpace(const gl::VertexAttribute &attribute, GLsizei count, GLsizei instances); void reserveRawDataSpace(unsigned int size); unsigned int getBufferSize() const; unsigned int getSerial() const; virtual int storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances); virtual int storeRawData(const void* data, unsigned int size); VertexBuffer* getVertexBuffer() const; protected: virtual bool reserveSpace(unsigned int size) = 0; unsigned int getWritePosition() const; void setWritePosition(unsigned int writePosition); bool discard(); bool setBufferSize(unsigned int size); private: DISALLOW_COPY_AND_ASSIGN(VertexBufferInterface); rx::Renderer *const mRenderer; VertexBuffer* mVertexBuffer; unsigned int mWritePosition; unsigned int mReservedSpace; bool mDynamic; }; class StreamingVertexBufferInterface : public VertexBufferInterface { public: StreamingVertexBufferInterface(rx::Renderer *renderer, std::size_t initialSize); ~StreamingVertexBufferInterface(); protected: bool reserveSpace(unsigned int size); }; class StaticVertexBufferInterface : public VertexBufferInterface { public: explicit StaticVertexBufferInterface(rx::Renderer *renderer); ~StaticVertexBufferInterface(); int storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances); // Returns the offset into the vertex buffer, or -1 if not found int lookupAttribute(const gl::VertexAttribute &attribute); protected: bool reserveSpace(unsigned int size); private: struct VertexElement { GLenum type; GLint size; GLsizei stride; bool normalized; int attributeOffset; unsigned int streamOffset; }; std::vector<VertexElement> mCache; }; } #endif // LIBGLESV2_RENDERER_VERTEXBUFFER_H_
C++
// // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // IndexBuffer.h: Defines the abstract IndexBuffer class and IndexBufferInterface // class with derivations, classes that perform graphics API agnostic index buffer operations. #ifndef LIBGLESV2_RENDERER_INDEXBUFFER_H_ #define LIBGLESV2_RENDERER_INDEXBUFFER_H_ #include "common/angleutils.h" namespace rx { class Renderer; class IndexBuffer { public: IndexBuffer(); virtual ~IndexBuffer(); virtual bool initialize(unsigned int bufferSize, GLenum indexType, bool dynamic) = 0; virtual bool mapBuffer(unsigned int offset, unsigned int size, void** outMappedMemory) = 0; virtual bool unmapBuffer() = 0; virtual bool discard() = 0; virtual GLenum getIndexType() const = 0; virtual unsigned int getBufferSize() const = 0; virtual bool setSize(unsigned int bufferSize, GLenum indexType) = 0; unsigned int getSerial() const; protected: void updateSerial(); private: DISALLOW_COPY_AND_ASSIGN(IndexBuffer); unsigned int mSerial; static unsigned int mNextSerial; }; class IndexBufferInterface { public: IndexBufferInterface(Renderer *renderer, bool dynamic); virtual ~IndexBufferInterface(); virtual bool reserveBufferSpace(unsigned int size, GLenum indexType) = 0; GLenum getIndexType() const; unsigned int getBufferSize() const; unsigned int getSerial() const; int mapBuffer(unsigned int size, void** outMappedMemory); bool unmapBuffer(); IndexBuffer *getIndexBuffer() const; protected: unsigned int getWritePosition() const; void setWritePosition(unsigned int writePosition); bool discard(); bool setBufferSize(unsigned int bufferSize, GLenum indexType); private: DISALLOW_COPY_AND_ASSIGN(IndexBufferInterface); rx::Renderer *const mRenderer; IndexBuffer* mIndexBuffer; unsigned int mWritePosition; bool mDynamic; }; class StreamingIndexBufferInterface : public IndexBufferInterface { public: StreamingIndexBufferInterface(Renderer *renderer); ~StreamingIndexBufferInterface(); virtual bool reserveBufferSpace(unsigned int size, GLenum indexType); }; class StaticIndexBufferInterface : public IndexBufferInterface { public: explicit StaticIndexBufferInterface(Renderer *renderer); ~StaticIndexBufferInterface(); virtual bool reserveBufferSpace(unsigned int size, GLenum indexType); unsigned int lookupRange(intptr_t offset, GLsizei count, unsigned int *minIndex, unsigned int *maxIndex); // Returns the offset into the index buffer, or -1 if not found void addRange(intptr_t offset, GLsizei count, unsigned int minIndex, unsigned int maxIndex, unsigned int streamOffset); private: struct IndexRange { intptr_t offset; GLsizei count; bool operator<(const IndexRange& rhs) const { if (offset != rhs.offset) { return offset < rhs.offset; } if (count != rhs.count) { return count < rhs.count; } return false; } }; struct IndexResult { unsigned int minIndex; unsigned int maxIndex; unsigned int streamOffset; }; std::map<IndexRange, IndexResult> mCache; }; } #endif // LIBGLESV2_RENDERER_INDEXBUFFER_H_
C++
// // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // FenceImpl.h: Defines the rx::FenceImpl class. #ifndef LIBGLESV2_RENDERER_FENCEIMPL_H_ #define LIBGLESV2_RENDERER_FENCEIMPL_H_ #include "common/angleutils.h" namespace rx { class FenceImpl { public: FenceImpl() : mStatus(GL_FALSE), mCondition(GL_NONE) { }; virtual ~FenceImpl() { }; virtual GLboolean isFence() = 0; virtual void setFence(GLenum condition) = 0; virtual GLboolean testFence() = 0; virtual void finishFence() = 0; virtual void getFenceiv(GLenum pname, GLint *params) = 0; protected: void setStatus(GLboolean status) { mStatus = status; } GLboolean getStatus() const { return mStatus; } void setCondition(GLuint condition) { mCondition = condition; } GLuint getCondition() const { return mCondition; } private: DISALLOW_COPY_AND_ASSIGN(FenceImpl); GLboolean mStatus; GLenum mCondition; }; } #endif // LIBGLESV2_RENDERER_FENCEIMPL_H_
C++
#include "precompiled.h" // // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Renderer.cpp: Implements EGL dependencies for creating and destroying Renderer instances. #include <EGL/eglext.h> #include "libGLESv2/main.h" #include "libGLESv2/Program.h" #include "libGLESv2/renderer/Renderer.h" #include "libGLESv2/renderer/Renderer9.h" #include "libGLESv2/renderer/Renderer11.h" #include "libGLESv2/utilities.h" #if !defined(ANGLE_ENABLE_D3D11) // Enables use of the Direct3D 11 API for a default display, when available #define ANGLE_ENABLE_D3D11 0 #endif namespace rx { Renderer::Renderer(egl::Display *display) : mDisplay(display) { mD3dCompilerModule = NULL; mD3DCompileFunc = NULL; } Renderer::~Renderer() { if (mD3dCompilerModule) { FreeLibrary(mD3dCompilerModule); mD3dCompilerModule = NULL; } } bool Renderer::initializeCompiler() { #if defined(ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES) // Find a D3DCompiler module that had already been loaded based on a predefined list of versions. static TCHAR* d3dCompilerNames[] = ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES; for (size_t i = 0; i < ArraySize(d3dCompilerNames); ++i) { if (GetModuleHandleEx(0, d3dCompilerNames[i], &mD3dCompilerModule)) { break; } } #else // Load the version of the D3DCompiler DLL associated with the Direct3D version ANGLE was built with. mD3dCompilerModule = LoadLibrary(D3DCOMPILER_DLL); #endif // ANGLE_PRELOADED_D3DCOMPILER_MODULE_NAMES if (!mD3dCompilerModule) { ERR("No D3D compiler module found - aborting!\n"); return false; } mD3DCompileFunc = reinterpret_cast<pCompileFunc>(GetProcAddress(mD3dCompilerModule, "D3DCompile")); ASSERT(mD3DCompileFunc); return mD3DCompileFunc != NULL; } // Compiles HLSL code into executable binaries ShaderBlob *Renderer::compileToBinary(gl::InfoLog &infoLog, const char *hlsl, const char *profile, UINT optimizationFlags, bool alternateFlags) { if (!hlsl) { return NULL; } HRESULT result = S_OK; UINT flags = 0; std::string sourceText; if (gl::perfActive()) { flags |= D3DCOMPILE_DEBUG; #ifdef NDEBUG flags |= optimizationFlags; #else flags |= D3DCOMPILE_SKIP_OPTIMIZATION; #endif std::string sourcePath = getTempPath(); sourceText = std::string("#line 2 \"") + sourcePath + std::string("\"\n\n") + std::string(hlsl); writeFile(sourcePath.c_str(), sourceText.c_str(), sourceText.size()); } else { flags |= optimizationFlags; sourceText = hlsl; } // Sometimes D3DCompile will fail with the default compilation flags for complicated shaders when it would otherwise pass with alternative options. // Try the default flags first and if compilation fails, try some alternatives. const static UINT extraFlags[] = { 0, D3DCOMPILE_AVOID_FLOW_CONTROL, D3DCOMPILE_PREFER_FLOW_CONTROL }; const static char * const extraFlagNames[] = { "default", "avoid flow control", "prefer flow control" }; int attempts = alternateFlags ? ArraySize(extraFlags) : 1; pD3DCompile compileFunc = reinterpret_cast<pD3DCompile>(mD3DCompileFunc); for (int i = 0; i < attempts; ++i) { ID3DBlob *errorMessage = NULL; ID3DBlob *binary = NULL; result = compileFunc(hlsl, strlen(hlsl), gl::g_fakepath, NULL, NULL, "main", profile, flags | extraFlags[i], 0, &binary, &errorMessage); if (errorMessage) { const char *message = (const char*)errorMessage->GetBufferPointer(); infoLog.appendSanitized(message); TRACE("\n%s", hlsl); TRACE("\n%s", message); errorMessage->Release(); errorMessage = NULL; } if (SUCCEEDED(result)) { return (ShaderBlob*)binary; } else { if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) { return gl::error(GL_OUT_OF_MEMORY, (ShaderBlob*) NULL); } infoLog.append("Warning: D3D shader compilation failed with "); infoLog.append(extraFlagNames[i]); infoLog.append(" flags."); if (i + 1 < attempts) { infoLog.append(" Retrying with "); infoLog.append(extraFlagNames[i + 1]); infoLog.append(".\n"); } } } return NULL; } } extern "C" { rx::Renderer *glCreateRenderer(egl::Display *display, HDC hDc, EGLNativeDisplayType displayId) { rx::Renderer *renderer = NULL; EGLint status = EGL_BAD_ALLOC; if (ANGLE_ENABLE_D3D11 || displayId == EGL_D3D11_ELSE_D3D9_DISPLAY_ANGLE || displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE) { renderer = new rx::Renderer11(display, hDc); if (renderer) { status = renderer->initialize(); } if (status == EGL_SUCCESS) { return renderer; } else if (displayId == EGL_D3D11_ONLY_DISPLAY_ANGLE) { return NULL; } // Failed to create a D3D11 renderer, try creating a D3D9 renderer delete renderer; } bool softwareDevice = (displayId == EGL_SOFTWARE_DISPLAY_ANGLE); renderer = new rx::Renderer9(display, hDc, softwareDevice); if (renderer) { status = renderer->initialize(); } if (status == EGL_SUCCESS) { return renderer; } return NULL; } void glDestroyRenderer(rx::Renderer *renderer) { delete renderer; } }
C++
#include "precompiled.h" // // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // SwapChain11.cpp: Implements a back-end specific class for the D3D11 swap chain. #include "libGLESv2/renderer/SwapChain11.h" #include "libGLESv2/renderer/renderer11_utils.h" #include "libGLESv2/renderer/Renderer11.h" #include "libGLESv2/renderer/shaders/compiled/passthrough11vs.h" #include "libGLESv2/renderer/shaders/compiled/passthroughrgba11ps.h" namespace rx { SwapChain11::SwapChain11(Renderer11 *renderer, HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat) : mRenderer(renderer), SwapChain(window, shareHandle, backBufferFormat, depthBufferFormat) { mSwapChain = NULL; mBackBufferTexture = NULL; mBackBufferRTView = NULL; mOffscreenTexture = NULL; mOffscreenRTView = NULL; mOffscreenSRView = NULL; mDepthStencilTexture = NULL; mDepthStencilDSView = NULL; mQuadVB = NULL; mPassThroughSampler = NULL; mPassThroughIL = NULL; mPassThroughVS = NULL; mPassThroughPS = NULL; mWidth = -1; mHeight = -1; mSwapInterval = 0; mAppCreatedShareHandle = mShareHandle != NULL; mPassThroughResourcesInit = false; } SwapChain11::~SwapChain11() { release(); } void SwapChain11::release() { if (mSwapChain) { mSwapChain->Release(); mSwapChain = NULL; } if (mBackBufferTexture) { mBackBufferTexture->Release(); mBackBufferTexture = NULL; } if (mBackBufferRTView) { mBackBufferRTView->Release(); mBackBufferRTView = NULL; } if (mOffscreenTexture) { mOffscreenTexture->Release(); mOffscreenTexture = NULL; } if (mOffscreenRTView) { mOffscreenRTView->Release(); mOffscreenRTView = NULL; } if (mOffscreenSRView) { mOffscreenSRView->Release(); mOffscreenSRView = NULL; } if (mDepthStencilTexture) { mDepthStencilTexture->Release(); mDepthStencilTexture = NULL; } if (mDepthStencilDSView) { mDepthStencilDSView->Release(); mDepthStencilDSView = NULL; } if (mQuadVB) { mQuadVB->Release(); mQuadVB = NULL; } if (mPassThroughSampler) { mPassThroughSampler->Release(); mPassThroughSampler = NULL; } if (mPassThroughIL) { mPassThroughIL->Release(); mPassThroughIL = NULL; } if (mPassThroughVS) { mPassThroughVS->Release(); mPassThroughVS = NULL; } if (mPassThroughPS) { mPassThroughPS->Release(); mPassThroughPS = NULL; } if (!mAppCreatedShareHandle) { mShareHandle = NULL; } } void SwapChain11::releaseOffscreenTexture() { if (mOffscreenTexture) { mOffscreenTexture->Release(); mOffscreenTexture = NULL; } if (mOffscreenRTView) { mOffscreenRTView->Release(); mOffscreenRTView = NULL; } if (mOffscreenSRView) { mOffscreenSRView->Release(); mOffscreenSRView = NULL; } if (mDepthStencilTexture) { mDepthStencilTexture->Release(); mDepthStencilTexture = NULL; } if (mDepthStencilDSView) { mDepthStencilDSView->Release(); mDepthStencilDSView = NULL; } } EGLint SwapChain11::resetOffscreenTexture(int backbufferWidth, int backbufferHeight) { ID3D11Device *device = mRenderer->getDevice(); ASSERT(device != NULL); // D3D11 does not allow zero size textures ASSERT(backbufferWidth >= 1); ASSERT(backbufferHeight >= 1); // Preserve the render target content ID3D11Texture2D *previousOffscreenTexture = mOffscreenTexture; if (previousOffscreenTexture) { previousOffscreenTexture->AddRef(); } const int previousWidth = mWidth; const int previousHeight = mHeight; releaseOffscreenTexture(); // If the app passed in a share handle, open the resource // See EGL_ANGLE_d3d_share_handle_client_buffer if (mAppCreatedShareHandle) { ID3D11Resource *tempResource11; HRESULT result = device->OpenSharedResource(mShareHandle, __uuidof(ID3D11Resource), (void**)&tempResource11); if (FAILED(result)) { ERR("Failed to open the swap chain pbuffer share handle: %08lX", result); release(); return EGL_BAD_PARAMETER; } result = tempResource11->QueryInterface(__uuidof(ID3D11Texture2D), (void**)&mOffscreenTexture); tempResource11->Release(); if (FAILED(result)) { ERR("Failed to query texture2d interface in pbuffer share handle: %08lX", result); release(); return EGL_BAD_PARAMETER; } // Validate offscreen texture parameters D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0}; mOffscreenTexture->GetDesc(&offscreenTextureDesc); if (offscreenTextureDesc.Width != (UINT)backbufferWidth || offscreenTextureDesc.Height != (UINT)backbufferHeight || offscreenTextureDesc.Format != gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat) || offscreenTextureDesc.MipLevels != 1 || offscreenTextureDesc.ArraySize != 1) { ERR("Invalid texture parameters in the shared offscreen texture pbuffer"); release(); return EGL_BAD_PARAMETER; } } else { const bool useSharedResource = !mWindow && mRenderer->getShareHandleSupport(); D3D11_TEXTURE2D_DESC offscreenTextureDesc = {0}; offscreenTextureDesc.Width = backbufferWidth; offscreenTextureDesc.Height = backbufferHeight; offscreenTextureDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat); offscreenTextureDesc.MipLevels = 1; offscreenTextureDesc.ArraySize = 1; offscreenTextureDesc.SampleDesc.Count = 1; offscreenTextureDesc.SampleDesc.Quality = 0; offscreenTextureDesc.Usage = D3D11_USAGE_DEFAULT; offscreenTextureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; offscreenTextureDesc.CPUAccessFlags = 0; offscreenTextureDesc.MiscFlags = useSharedResource ? D3D11_RESOURCE_MISC_SHARED : 0; HRESULT result = device->CreateTexture2D(&offscreenTextureDesc, NULL, &mOffscreenTexture); if (FAILED(result)) { ERR("Could not create offscreen texture: %08lX", result); release(); if (d3d11::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } else { return EGL_BAD_ALLOC; } } d3d11::SetDebugName(mOffscreenTexture, "Offscreen texture"); // EGL_ANGLE_surface_d3d_texture_2d_share_handle requires that we store a share handle for the client if (useSharedResource) { IDXGIResource *offscreenTextureResource = NULL; result = mOffscreenTexture->QueryInterface(__uuidof(IDXGIResource), (void**)&offscreenTextureResource); // Fall back to no share handle on failure if (FAILED(result)) { ERR("Could not query offscreen texture resource: %08lX", result); } else { result = offscreenTextureResource->GetSharedHandle(&mShareHandle); if (FAILED(result)) { mShareHandle = NULL; ERR("Could not get offscreen texture shared handle: %08lX", result); } } } } HRESULT result = device->CreateRenderTargetView(mOffscreenTexture, NULL, &mOffscreenRTView); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mOffscreenRTView, "Offscreen render target"); result = device->CreateShaderResourceView(mOffscreenTexture, NULL, &mOffscreenSRView); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mOffscreenSRView, "Offscreen shader resource"); if (mDepthBufferFormat != GL_NONE) { D3D11_TEXTURE2D_DESC depthStencilDesc = {0}; depthStencilDesc.Width = backbufferWidth; depthStencilDesc.Height = backbufferHeight; depthStencilDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mDepthBufferFormat); depthStencilDesc.MipLevels = 1; depthStencilDesc.ArraySize = 1; depthStencilDesc.SampleDesc.Count = 1; depthStencilDesc.SampleDesc.Quality = 0; depthStencilDesc.Usage = D3D11_USAGE_DEFAULT; depthStencilDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; depthStencilDesc.CPUAccessFlags = 0; depthStencilDesc.MiscFlags = 0; result = device->CreateTexture2D(&depthStencilDesc, NULL, &mDepthStencilTexture); if (FAILED(result)) { ERR("Could not create depthstencil surface for new swap chain: 0x%08X", result); release(); if (d3d11::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } else { return EGL_BAD_ALLOC; } } d3d11::SetDebugName(mDepthStencilTexture, "Depth stencil texture"); result = device->CreateDepthStencilView(mDepthStencilTexture, NULL, &mDepthStencilDSView); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mDepthStencilDSView, "Depth stencil view"); } mWidth = backbufferWidth; mHeight = backbufferHeight; if (previousOffscreenTexture != NULL) { D3D11_BOX sourceBox = {0}; sourceBox.left = 0; sourceBox.right = std::min(previousWidth, mWidth); sourceBox.top = std::max(previousHeight - mHeight, 0); sourceBox.bottom = previousHeight; sourceBox.front = 0; sourceBox.back = 1; ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); const int yoffset = std::max(mHeight - previousHeight, 0); deviceContext->CopySubresourceRegion(mOffscreenTexture, 0, 0, yoffset, 0, previousOffscreenTexture, 0, &sourceBox); previousOffscreenTexture->Release(); if (mSwapChain) { swapRect(0, 0, mWidth, mHeight); } } return EGL_SUCCESS; } EGLint SwapChain11::resize(EGLint backbufferWidth, EGLint backbufferHeight) { ID3D11Device *device = mRenderer->getDevice(); if (device == NULL) { return EGL_BAD_ACCESS; } // Can only call resize if we have already created our swap buffer and resources ASSERT(mSwapChain && mBackBufferTexture && mBackBufferRTView); if (mBackBufferTexture) { mBackBufferTexture->Release(); mBackBufferTexture = NULL; } if (mBackBufferRTView) { mBackBufferRTView->Release(); mBackBufferRTView = NULL; } // Resize swap chain DXGI_FORMAT backbufferDXGIFormat = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat); HRESULT result = mSwapChain->ResizeBuffers(2, backbufferWidth, backbufferHeight, backbufferDXGIFormat, 0); if (FAILED(result)) { ERR("Error resizing swap chain buffers: 0x%08X", result); release(); if (d3d11::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } else { return EGL_BAD_ALLOC; } } result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture); ASSERT(SUCCEEDED(result)); if (SUCCEEDED(result)) { d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture"); } result = device->CreateRenderTargetView(mBackBufferTexture, NULL, &mBackBufferRTView); ASSERT(SUCCEEDED(result)); if (SUCCEEDED(result)) { d3d11::SetDebugName(mBackBufferRTView, "Back buffer render target"); } return resetOffscreenTexture(backbufferWidth, backbufferHeight); } EGLint SwapChain11::reset(int backbufferWidth, int backbufferHeight, EGLint swapInterval) { ID3D11Device *device = mRenderer->getDevice(); if (device == NULL) { return EGL_BAD_ACCESS; } // Release specific resources to free up memory for the new render target, while the // old render target still exists for the purpose of preserving its contents. if (mSwapChain) { mSwapChain->Release(); mSwapChain = NULL; } if (mBackBufferTexture) { mBackBufferTexture->Release(); mBackBufferTexture = NULL; } if (mBackBufferRTView) { mBackBufferRTView->Release(); mBackBufferRTView = NULL; } mSwapInterval = static_cast<unsigned int>(swapInterval); if (mSwapInterval > 4) { // IDXGISwapChain::Present documentation states that valid sync intervals are in the [0,4] range return EGL_BAD_PARAMETER; } // EGL allows creating a surface with 0x0 dimension, however, DXGI does not like 0x0 swapchains if (backbufferWidth < 1 || backbufferHeight < 1) { releaseOffscreenTexture(); return EGL_SUCCESS; } if (mWindow) { // We cannot create a swap chain for an HWND that is owned by a different process DWORD currentProcessId = GetCurrentProcessId(); DWORD wndProcessId; GetWindowThreadProcessId(mWindow, &wndProcessId); if (currentProcessId != wndProcessId) { ERR("Could not create swap chain, window owned by different process"); release(); return EGL_BAD_NATIVE_WINDOW; } IDXGIFactory *factory = mRenderer->getDxgiFactory(); DXGI_SWAP_CHAIN_DESC swapChainDesc = {0}; swapChainDesc.BufferCount = 2; swapChainDesc.BufferDesc.Format = gl_d3d11::ConvertRenderbufferFormat(mBackBufferFormat); swapChainDesc.BufferDesc.Width = backbufferWidth; swapChainDesc.BufferDesc.Height = backbufferHeight; swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; swapChainDesc.BufferDesc.RefreshRate.Numerator = 0; swapChainDesc.BufferDesc.RefreshRate.Denominator = 1; swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; swapChainDesc.Flags = 0; swapChainDesc.OutputWindow = mWindow; swapChainDesc.SampleDesc.Count = 1; swapChainDesc.SampleDesc.Quality = 0; swapChainDesc.Windowed = TRUE; HRESULT result = factory->CreateSwapChain(device, &swapChainDesc, &mSwapChain); if (FAILED(result)) { ERR("Could not create additional swap chains or offscreen surfaces: %08lX", result); release(); if (d3d11::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } else { return EGL_BAD_ALLOC; } } result = mSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&mBackBufferTexture); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mBackBufferTexture, "Back buffer texture"); result = device->CreateRenderTargetView(mBackBufferTexture, NULL, &mBackBufferRTView); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mBackBufferRTView, "Back buffer render target"); } // If we are resizing the swap chain, we don't wish to recreate all the static resources if (!mPassThroughResourcesInit) { mPassThroughResourcesInit = true; initPassThroughResources(); } return resetOffscreenTexture(backbufferWidth, backbufferHeight); } void SwapChain11::initPassThroughResources() { ID3D11Device *device = mRenderer->getDevice(); ASSERT(device != NULL); // Make sure our resources are all not allocated, when we create ASSERT(mQuadVB == NULL && mPassThroughSampler == NULL); ASSERT(mPassThroughIL == NULL && mPassThroughVS == NULL && mPassThroughPS == NULL); D3D11_BUFFER_DESC vbDesc; vbDesc.ByteWidth = sizeof(d3d11::PositionTexCoordVertex) * 4; vbDesc.Usage = D3D11_USAGE_DYNAMIC; vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; vbDesc.MiscFlags = 0; vbDesc.StructureByteStride = 0; HRESULT result = device->CreateBuffer(&vbDesc, NULL, &mQuadVB); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mQuadVB, "Swap chain quad vertex buffer"); D3D11_SAMPLER_DESC samplerDesc; samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; samplerDesc.MipLODBias = 0.0f; samplerDesc.MaxAnisotropy = 0; samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; samplerDesc.BorderColor[0] = 0.0f; samplerDesc.BorderColor[1] = 0.0f; samplerDesc.BorderColor[2] = 0.0f; samplerDesc.BorderColor[3] = 0.0f; samplerDesc.MinLOD = 0; samplerDesc.MaxLOD = D3D11_FLOAT32_MAX; result = device->CreateSamplerState(&samplerDesc, &mPassThroughSampler); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughSampler, "Swap chain pass through sampler"); D3D11_INPUT_ELEMENT_DESC quadLayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 }, }; result = device->CreateInputLayout(quadLayout, 2, g_VS_Passthrough, sizeof(g_VS_Passthrough), &mPassThroughIL); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughIL, "Swap chain pass through layout"); result = device->CreateVertexShader(g_VS_Passthrough, sizeof(g_VS_Passthrough), NULL, &mPassThroughVS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughVS, "Swap chain pass through vertex shader"); result = device->CreatePixelShader(g_PS_PassthroughRGBA, sizeof(g_PS_PassthroughRGBA), NULL, &mPassThroughPS); ASSERT(SUCCEEDED(result)); d3d11::SetDebugName(mPassThroughPS, "Swap chain pass through pixel shader"); } // parameters should be validated/clamped by caller EGLint SwapChain11::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) { if (!mSwapChain) { return EGL_SUCCESS; } ID3D11Device *device = mRenderer->getDevice(); ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); // Set vertices D3D11_MAPPED_SUBRESOURCE mappedResource; HRESULT result = deviceContext->Map(mQuadVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { return EGL_BAD_ACCESS; } d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(mappedResource.pData); // Create a quad in homogeneous coordinates float x1 = (x / float(mWidth)) * 2.0f - 1.0f; float y1 = (y / float(mHeight)) * 2.0f - 1.0f; float x2 = ((x + width) / float(mWidth)) * 2.0f - 1.0f; float y2 = ((y + height) / float(mHeight)) * 2.0f - 1.0f; float u1 = x / float(mWidth); float v1 = y / float(mHeight); float u2 = (x + width) / float(mWidth); float v2 = (y + height) / float(mHeight); d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v1); d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v2); d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v1); d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v2); deviceContext->Unmap(mQuadVB, 0); static UINT stride = sizeof(d3d11::PositionTexCoordVertex); static UINT startIdx = 0; deviceContext->IASetVertexBuffers(0, 1, &mQuadVB, &stride, &startIdx); // Apply state deviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF); static const float blendFactor[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; deviceContext->OMSetBlendState(NULL, blendFactor, 0xFFFFFFF); deviceContext->RSSetState(NULL); // Apply shaders deviceContext->IASetInputLayout(mPassThroughIL); deviceContext->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); deviceContext->VSSetShader(mPassThroughVS, NULL, 0); deviceContext->PSSetShader(mPassThroughPS, NULL, 0); deviceContext->GSSetShader(NULL, NULL, 0); // Apply render targets mRenderer->setOneTimeRenderTarget(mBackBufferRTView); // Set the viewport D3D11_VIEWPORT viewport; viewport.TopLeftX = 0; viewport.TopLeftY = 0; viewport.Width = mWidth; viewport.Height = mHeight; viewport.MinDepth = 0.0f; viewport.MaxDepth = 1.0f; deviceContext->RSSetViewports(1, &viewport); // Apply textures deviceContext->PSSetShaderResources(0, 1, &mOffscreenSRView); deviceContext->PSSetSamplers(0, 1, &mPassThroughSampler); // Draw deviceContext->Draw(4, 0); result = mSwapChain->Present(mSwapInterval, 0); if (result == DXGI_ERROR_DEVICE_REMOVED) { HRESULT removedReason = device->GetDeviceRemovedReason(); ERR("Present failed: the D3D11 device was removed: 0x%08X", removedReason); return EGL_CONTEXT_LOST; } else if (result == DXGI_ERROR_DEVICE_RESET) { ERR("Present failed: the D3D11 device was reset from a bad command."); return EGL_CONTEXT_LOST; } else if (FAILED(result)) { ERR("Present failed with error code 0x%08X", result); } // Unbind static ID3D11ShaderResourceView *const nullSRV = NULL; deviceContext->PSSetShaderResources(0, 1, &nullSRV); mRenderer->unapplyRenderTargets(); mRenderer->markAllStateDirty(); return EGL_SUCCESS; } // Increments refcount on texture. // caller must Release() the returned texture ID3D11Texture2D *SwapChain11::getOffscreenTexture() { if (mOffscreenTexture) { mOffscreenTexture->AddRef(); } return mOffscreenTexture; } // Increments refcount on view. // caller must Release() the returned view ID3D11RenderTargetView *SwapChain11::getRenderTarget() { if (mOffscreenRTView) { mOffscreenRTView->AddRef(); } return mOffscreenRTView; } // Increments refcount on view. // caller must Release() the returned view ID3D11ShaderResourceView *SwapChain11::getRenderTargetShaderResource() { if (mOffscreenSRView) { mOffscreenSRView->AddRef(); } return mOffscreenSRView; } // Increments refcount on view. // caller must Release() the returned view ID3D11DepthStencilView *SwapChain11::getDepthStencil() { if (mDepthStencilDSView) { mDepthStencilDSView->AddRef(); } return mDepthStencilDSView; } ID3D11Texture2D *SwapChain11::getDepthStencilTexture() { if (mDepthStencilTexture) { mDepthStencilTexture->AddRef(); } return mDepthStencilTexture; } SwapChain11 *SwapChain11::makeSwapChain11(SwapChain *swapChain) { ASSERT(HAS_DYNAMIC_TYPE(rx::SwapChain11*, swapChain)); return static_cast<rx::SwapChain11*>(swapChain); } void SwapChain11::recreate() { // possibly should use this method instead of reset } }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // TextureStorage11.h: Defines the abstract rx::TextureStorage11 class and its concrete derived // classes TextureStorage11_2D and TextureStorage11_Cube, which act as the interface to the D3D11 texture. #ifndef LIBGLESV2_RENDERER_TEXTURESTORAGE11_H_ #define LIBGLESV2_RENDERER_TEXTURESTORAGE11_H_ #include "libGLESv2/Texture.h" #include "libGLESv2/renderer/TextureStorage.h" namespace rx { class RenderTarget; class RenderTarget11; class Renderer; class Renderer11; class SwapChain11; class TextureStorage11 : public TextureStorage { public: TextureStorage11(Renderer *renderer, UINT bindFlags); virtual ~TextureStorage11(); static TextureStorage11 *makeTextureStorage11(TextureStorage *storage); static DWORD GetTextureBindFlags(DXGI_FORMAT d3dfmt, GLenum glusage, bool forceRenderable); static bool IsTextureFormatRenderable(DXGI_FORMAT format); UINT getBindFlags() const; virtual ID3D11Texture2D *getBaseTexture() const; virtual ID3D11ShaderResourceView *getSRV() = 0; virtual RenderTarget *getRenderTarget() { return getRenderTarget(0); } virtual RenderTarget *getRenderTarget(int level) { return NULL; } virtual RenderTarget *getRenderTarget(GLenum faceTarget) { return getRenderTarget(faceTarget, 0); } virtual RenderTarget *getRenderTarget(GLenum faceTarget, int level) { return NULL; } virtual void generateMipmap(int level) {}; virtual void generateMipmap(int face, int level) {}; virtual int getLodOffset() const; virtual bool isRenderTarget() const; virtual bool isManaged() const; virtual int levelCount(); UINT getSubresourceIndex(int level, int faceTarget); bool updateSubresourceLevel(ID3D11Texture2D *texture, unsigned int sourceSubresource, int level, int faceTarget, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height); protected: void generateMipmapLayer(RenderTarget11 *source, RenderTarget11 *dest); Renderer11 *mRenderer; int mLodOffset; unsigned int mMipLevels; ID3D11Texture2D *mTexture; DXGI_FORMAT mTextureFormat; DXGI_FORMAT mShaderResourceFormat; DXGI_FORMAT mRenderTargetFormat; DXGI_FORMAT mDepthStencilFormat; unsigned int mTextureWidth; unsigned int mTextureHeight; ID3D11ShaderResourceView *mSRV; private: DISALLOW_COPY_AND_ASSIGN(TextureStorage11); const UINT mBindFlags; }; class TextureStorage11_2D : public TextureStorage11 { public: TextureStorage11_2D(Renderer *renderer, SwapChain11 *swapchain); TextureStorage11_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height); virtual ~TextureStorage11_2D(); static TextureStorage11_2D *makeTextureStorage11_2D(TextureStorage *storage); virtual ID3D11ShaderResourceView *getSRV(); virtual RenderTarget *getRenderTarget(int level); virtual void generateMipmap(int level); private: DISALLOW_COPY_AND_ASSIGN(TextureStorage11_2D); RenderTarget11 *mRenderTarget[gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS]; }; class TextureStorage11_Cube : public TextureStorage11 { public: TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size); virtual ~TextureStorage11_Cube(); static TextureStorage11_Cube *makeTextureStorage11_Cube(TextureStorage *storage); virtual ID3D11ShaderResourceView *getSRV(); virtual RenderTarget *getRenderTarget(GLenum faceTarget, int level); virtual void generateMipmap(int face, int level); private: DISALLOW_COPY_AND_ASSIGN(TextureStorage11_Cube); RenderTarget11 *mRenderTarget[6][gl::IMPLEMENTATION_MAX_TEXTURE_LEVELS]; }; } #endif // LIBGLESV2_RENDERER_TEXTURESTORAGE11_H_
C++
#include "precompiled.h" // // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Renderer9.cpp: Implements a back-end specific class for the D3D9 renderer. #include "libGLESv2/main.h" #include "libGLESv2/Buffer.h" #include "libGLESv2/Texture.h" #include "libGLESv2/Framebuffer.h" #include "libGLESv2/Renderbuffer.h" #include "libGLESv2/ProgramBinary.h" #include "libGLESv2/renderer/IndexDataManager.h" #include "libGLESv2/renderer/Renderer9.h" #include "libGLESv2/renderer/renderer9_utils.h" #include "libGLESv2/renderer/ShaderExecutable9.h" #include "libGLESv2/renderer/SwapChain9.h" #include "libGLESv2/renderer/TextureStorage9.h" #include "libGLESv2/renderer/Image9.h" #include "libGLESv2/renderer/Blit.h" #include "libGLESv2/renderer/RenderTarget9.h" #include "libGLESv2/renderer/VertexBuffer9.h" #include "libGLESv2/renderer/IndexBuffer9.h" #include "libGLESv2/renderer/BufferStorage9.h" #include "libGLESv2/renderer/Query9.h" #include "libGLESv2/renderer/Fence9.h" #include "libEGL/Display.h" // Can also be enabled by defining FORCE_REF_RAST in the project's predefined macros #define REF_RAST 0 // The "Debug This Pixel..." feature in PIX often fails when using the // D3D9Ex interfaces. In order to get debug pixel to work on a Vista/Win 7 // machine, define "ANGLE_ENABLE_D3D9EX=0" in your project file. #if !defined(ANGLE_ENABLE_D3D9EX) // Enables use of the IDirect3D9Ex interface, when available #define ANGLE_ENABLE_D3D9EX 1 #endif // !defined(ANGLE_ENABLE_D3D9EX) namespace rx { static const D3DFORMAT RenderTargetFormats[] = { D3DFMT_A1R5G5B5, // D3DFMT_A2R10G10B10, // The color_ramp conformance test uses ReadPixels with UNSIGNED_BYTE causing it to think that rendering skipped a colour value. D3DFMT_A8R8G8B8, D3DFMT_R5G6B5, // D3DFMT_X1R5G5B5, // Has no compatible OpenGL ES renderbuffer format D3DFMT_X8R8G8B8 }; static const D3DFORMAT DepthStencilFormats[] = { D3DFMT_UNKNOWN, // D3DFMT_D16_LOCKABLE, D3DFMT_D32, // D3DFMT_D15S1, D3DFMT_D24S8, D3DFMT_D24X8, // D3DFMT_D24X4S4, D3DFMT_D16, // D3DFMT_D32F_LOCKABLE, // D3DFMT_D24FS8 }; enum { MAX_VERTEX_CONSTANT_VECTORS_D3D9 = 256, MAX_PIXEL_CONSTANT_VECTORS_SM2 = 32, MAX_PIXEL_CONSTANT_VECTORS_SM3 = 224, MAX_VARYING_VECTORS_SM2 = 8, MAX_VARYING_VECTORS_SM3 = 10, MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 = 4 }; Renderer9::Renderer9(egl::Display *display, HDC hDc, bool softwareDevice) : Renderer(display), mDc(hDc), mSoftwareDevice(softwareDevice) { mD3d9Module = NULL; mD3d9 = NULL; mD3d9Ex = NULL; mDevice = NULL; mDeviceEx = NULL; mDeviceWindow = NULL; mBlit = NULL; mAdapter = D3DADAPTER_DEFAULT; #if REF_RAST == 1 || defined(FORCE_REF_RAST) mDeviceType = D3DDEVTYPE_REF; #else mDeviceType = D3DDEVTYPE_HAL; #endif mDeviceLost = false; mMaxSupportedSamples = 0; mMaskedClearSavedState = NULL; mVertexDataManager = NULL; mIndexDataManager = NULL; mLineLoopIB = NULL; mMaxNullColorbufferLRU = 0; for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) { mNullColorbufferCache[i].lruCount = 0; mNullColorbufferCache[i].width = 0; mNullColorbufferCache[i].height = 0; mNullColorbufferCache[i].buffer = NULL; } } Renderer9::~Renderer9() { releaseDeviceResources(); if (mDevice) { // If the device is lost, reset it first to prevent leaving the driver in an unstable state if (testDeviceLost(false)) { resetDevice(); } mDevice->Release(); mDevice = NULL; } if (mDeviceEx) { mDeviceEx->Release(); mDeviceEx = NULL; } if (mD3d9) { mD3d9->Release(); mD3d9 = NULL; } if (mDeviceWindow) { DestroyWindow(mDeviceWindow); mDeviceWindow = NULL; } if (mD3d9Ex) { mD3d9Ex->Release(); mD3d9Ex = NULL; } if (mD3d9Module) { mD3d9Module = NULL; } while (!mMultiSampleSupport.empty()) { delete [] mMultiSampleSupport.begin()->second; mMultiSampleSupport.erase(mMultiSampleSupport.begin()); } } Renderer9 *Renderer9::makeRenderer9(Renderer *renderer) { ASSERT(HAS_DYNAMIC_TYPE(rx::Renderer9*, renderer)); return static_cast<rx::Renderer9*>(renderer); } EGLint Renderer9::initialize() { if (!initializeCompiler()) { return EGL_NOT_INITIALIZED; } if (mSoftwareDevice) { mD3d9Module = GetModuleHandle(TEXT("swiftshader_d3d9.dll")); } else { mD3d9Module = GetModuleHandle(TEXT("d3d9.dll")); } if (mD3d9Module == NULL) { ERR("No D3D9 module found - aborting!\n"); return EGL_NOT_INITIALIZED; } typedef HRESULT (WINAPI *Direct3DCreate9ExFunc)(UINT, IDirect3D9Ex**); Direct3DCreate9ExFunc Direct3DCreate9ExPtr = reinterpret_cast<Direct3DCreate9ExFunc>(GetProcAddress(mD3d9Module, "Direct3DCreate9Ex")); // Use Direct3D9Ex if available. Among other things, this version is less // inclined to report a lost context, for example when the user switches // desktop. Direct3D9Ex is available in Windows Vista and later if suitable drivers are available. if (ANGLE_ENABLE_D3D9EX && Direct3DCreate9ExPtr && SUCCEEDED(Direct3DCreate9ExPtr(D3D_SDK_VERSION, &mD3d9Ex))) { ASSERT(mD3d9Ex); mD3d9Ex->QueryInterface(IID_IDirect3D9, reinterpret_cast<void**>(&mD3d9)); ASSERT(mD3d9); } else { mD3d9 = Direct3DCreate9(D3D_SDK_VERSION); } if (!mD3d9) { ERR("Could not create D3D9 device - aborting!\n"); return EGL_NOT_INITIALIZED; } if (mDc != NULL) { // UNIMPLEMENTED(); // FIXME: Determine which adapter index the device context corresponds to } HRESULT result; // Give up on getting device caps after about one second. for (int i = 0; i < 10; ++i) { result = mD3d9->GetDeviceCaps(mAdapter, mDeviceType, &mDeviceCaps); if (SUCCEEDED(result)) { break; } else if (result == D3DERR_NOTAVAILABLE) { Sleep(100); // Give the driver some time to initialize/recover } else if (FAILED(result)) // D3DERR_OUTOFVIDEOMEMORY, E_OUTOFMEMORY, D3DERR_INVALIDDEVICE, or another error we can't recover from { ERR("failed to get device caps (0x%x)\n", result); return EGL_NOT_INITIALIZED; } } if (mDeviceCaps.PixelShaderVersion < D3DPS_VERSION(2, 0)) { ERR("Renderer does not support PS 2.0. aborting!\n"); return EGL_NOT_INITIALIZED; } // When DirectX9 is running with an older DirectX8 driver, a StretchRect from a regular texture to a render target texture is not supported. // This is required by Texture2D::convertToRenderTarget. if ((mDeviceCaps.DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES) == 0) { ERR("Renderer does not support stretctrect from textures!\n"); return EGL_NOT_INITIALIZED; } mD3d9->GetAdapterIdentifier(mAdapter, 0, &mAdapterIdentifier); // ATI cards on XP have problems with non-power-of-two textures. mSupportsNonPower2Textures = !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_POW2) && !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2) && !(mDeviceCaps.TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL) && !(getComparableOSVersion() < versionWindowsVista && mAdapterIdentifier.VendorId == VENDOR_ID_AMD); // Must support a minimum of 2:1 anisotropy for max anisotropy to be considered supported, per the spec mSupportsTextureFilterAnisotropy = ((mDeviceCaps.RasterCaps & D3DPRASTERCAPS_ANISOTROPY) && (mDeviceCaps.MaxAnisotropy >= 2)); mMinSwapInterval = 4; mMaxSwapInterval = 0; if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE) { mMinSwapInterval = std::min(mMinSwapInterval, 0); mMaxSwapInterval = std::max(mMaxSwapInterval, 0); } if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_ONE) { mMinSwapInterval = std::min(mMinSwapInterval, 1); mMaxSwapInterval = std::max(mMaxSwapInterval, 1); } if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_TWO) { mMinSwapInterval = std::min(mMinSwapInterval, 2); mMaxSwapInterval = std::max(mMaxSwapInterval, 2); } if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_THREE) { mMinSwapInterval = std::min(mMinSwapInterval, 3); mMaxSwapInterval = std::max(mMaxSwapInterval, 3); } if (mDeviceCaps.PresentationIntervals & D3DPRESENT_INTERVAL_FOUR) { mMinSwapInterval = std::min(mMinSwapInterval, 4); mMaxSwapInterval = std::max(mMaxSwapInterval, 4); } int max = 0; for (unsigned int i = 0; i < ArraySize(RenderTargetFormats); ++i) { bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; getMultiSampleSupport(RenderTargetFormats[i], multisampleArray); mMultiSampleSupport[RenderTargetFormats[i]] = multisampleArray; for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) { if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) { max = j; } } } for (unsigned int i = 0; i < ArraySize(DepthStencilFormats); ++i) { if (DepthStencilFormats[i] == D3DFMT_UNKNOWN) continue; bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1]; getMultiSampleSupport(DepthStencilFormats[i], multisampleArray); mMultiSampleSupport[DepthStencilFormats[i]] = multisampleArray; for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j) { if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max) { max = j; } } } mMaxSupportedSamples = max; static const TCHAR windowName[] = TEXT("AngleHiddenWindow"); static const TCHAR className[] = TEXT("STATIC"); mDeviceWindow = CreateWindowEx(WS_EX_NOACTIVATE, className, windowName, WS_DISABLED | WS_POPUP, 0, 0, 1, 1, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL); D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); DWORD behaviorFlags = D3DCREATE_FPU_PRESERVE | D3DCREATE_NOWINDOWCHANGES; result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &presentParameters, &mDevice); if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DEVICELOST) { return EGL_BAD_ALLOC; } if (FAILED(result)) { result = mD3d9->CreateDevice(mAdapter, mDeviceType, mDeviceWindow, behaviorFlags | D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presentParameters, &mDevice); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_NOTAVAILABLE || result == D3DERR_DEVICELOST); return EGL_BAD_ALLOC; } } if (mD3d9Ex) { result = mDevice->QueryInterface(IID_IDirect3DDevice9Ex, (void**) &mDeviceEx); ASSERT(SUCCEEDED(result)); } mVertexShaderCache.initialize(mDevice); mPixelShaderCache.initialize(mDevice); // Check occlusion query support IDirect3DQuery9 *occlusionQuery = NULL; if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &occlusionQuery)) && occlusionQuery) { occlusionQuery->Release(); mOcclusionQuerySupport = true; } else { mOcclusionQuerySupport = false; } // Check event query support IDirect3DQuery9 *eventQuery = NULL; if (SUCCEEDED(mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery)) && eventQuery) { eventQuery->Release(); mEventQuerySupport = true; } else { mEventQuerySupport = false; } D3DDISPLAYMODE currentDisplayMode; mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode); // Check vertex texture support // Only Direct3D 10 ready devices support all the necessary vertex texture formats. // We test this using D3D9 by checking support for the R16F format. mVertexTextureSupport = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_VERTEXTEXTURE, D3DRTYPE_TEXTURE, D3DFMT_R16F)); // Check depth texture support // we use INTZ for depth textures in Direct3D9 // we also want NULL texture support to ensure the we can make depth-only FBOs // see http://aras-p.info/texts/D3D9GPUHacks.html mDepthTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_INTZ)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, D3DFMT_NULL)); // Check 32 bit floating point texture support mFloat32FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); mFloat32RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); if (!mFloat32FilterSupport && !mFloat32RenderSupport) { mFloat32TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A32B32G32R32F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_CUBETEXTURE, D3DFMT_A32B32G32R32F)); } else { mFloat32TextureSupport = true; } // Check 16 bit floating point texture support mFloat16FilterSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_QUERY_FILTER, D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); mFloat16RenderSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); if (!mFloat16FilterSupport && !mFloat16RenderSupport) { mFloat16TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A16B16G16R16F)) && SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_CUBETEXTURE, D3DFMT_A16B16G16R16F)); } else { mFloat16TextureSupport = true; } // Check DXT texture support mDXT1TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT1)); mDXT3TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT3)); mDXT5TextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_DXT5)); // Check luminance[alpha] texture support mLuminanceTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_L8)); mLuminanceAlphaTextureSupport = SUCCEEDED(mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, 0, D3DRTYPE_TEXTURE, D3DFMT_A8L8)); initializeDevice(); return EGL_SUCCESS; } // do any one-time device initialization // NOTE: this is also needed after a device lost/reset // to reset the scene status and ensure the default states are reset. void Renderer9::initializeDevice() { // Permanent non-default states mDevice->SetRenderState(D3DRS_POINTSPRITEENABLE, TRUE); mDevice->SetRenderState(D3DRS_LASTPIXEL, FALSE); if (mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0)) { mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, (DWORD&)mDeviceCaps.MaxPointSize); } else { mDevice->SetRenderState(D3DRS_POINTSIZE_MAX, 0x3F800000); // 1.0f } markAllStateDirty(); mSceneStarted = false; ASSERT(!mBlit && !mVertexDataManager && !mIndexDataManager); mBlit = new Blit(this); mVertexDataManager = new rx::VertexDataManager(this); mIndexDataManager = new rx::IndexDataManager(this); } D3DPRESENT_PARAMETERS Renderer9::getDefaultPresentParameters() { D3DPRESENT_PARAMETERS presentParameters = {0}; // The default swap chain is never actually used. Surface will create a new swap chain with the proper parameters. presentParameters.AutoDepthStencilFormat = D3DFMT_UNKNOWN; presentParameters.BackBufferCount = 1; presentParameters.BackBufferFormat = D3DFMT_UNKNOWN; presentParameters.BackBufferWidth = 1; presentParameters.BackBufferHeight = 1; presentParameters.EnableAutoDepthStencil = FALSE; presentParameters.Flags = 0; presentParameters.hDeviceWindow = mDeviceWindow; presentParameters.MultiSampleQuality = 0; presentParameters.MultiSampleType = D3DMULTISAMPLE_NONE; presentParameters.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; presentParameters.SwapEffect = D3DSWAPEFFECT_DISCARD; presentParameters.Windowed = TRUE; return presentParameters; } int Renderer9::generateConfigs(ConfigDesc **configDescList) { D3DDISPLAYMODE currentDisplayMode; mD3d9->GetAdapterDisplayMode(mAdapter, &currentDisplayMode); unsigned int numRenderFormats = ArraySize(RenderTargetFormats); unsigned int numDepthFormats = ArraySize(DepthStencilFormats); (*configDescList) = new ConfigDesc[numRenderFormats * numDepthFormats]; int numConfigs = 0; for (unsigned int formatIndex = 0; formatIndex < numRenderFormats; formatIndex++) { D3DFORMAT renderTargetFormat = RenderTargetFormats[formatIndex]; HRESULT result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, renderTargetFormat); if (SUCCEEDED(result)) { for (unsigned int depthStencilIndex = 0; depthStencilIndex < numDepthFormats; depthStencilIndex++) { D3DFORMAT depthStencilFormat = DepthStencilFormats[depthStencilIndex]; HRESULT result = D3D_OK; if(depthStencilFormat != D3DFMT_UNKNOWN) { result = mD3d9->CheckDeviceFormat(mAdapter, mDeviceType, currentDisplayMode.Format, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_SURFACE, depthStencilFormat); } if (SUCCEEDED(result)) { if(depthStencilFormat != D3DFMT_UNKNOWN) { result = mD3d9->CheckDepthStencilMatch(mAdapter, mDeviceType, currentDisplayMode.Format, renderTargetFormat, depthStencilFormat); } if (SUCCEEDED(result)) { ConfigDesc newConfig; newConfig.renderTargetFormat = d3d9_gl::ConvertBackBufferFormat(renderTargetFormat); newConfig.depthStencilFormat = d3d9_gl::ConvertDepthStencilFormat(depthStencilFormat); newConfig.multiSample = 0; // FIXME: enumerate multi-sampling newConfig.fastConfig = (currentDisplayMode.Format == renderTargetFormat); (*configDescList)[numConfigs++] = newConfig; } } } } } return numConfigs; } void Renderer9::deleteConfigs(ConfigDesc *configDescList) { delete [] (configDescList); } void Renderer9::startScene() { if (!mSceneStarted) { long result = mDevice->BeginScene(); if (SUCCEEDED(result)) { // This is defensive checking against the device being // lost at unexpected times. mSceneStarted = true; } } } void Renderer9::endScene() { if (mSceneStarted) { // EndScene can fail if the device was lost, for example due // to a TDR during a draw call. mDevice->EndScene(); mSceneStarted = false; } } void Renderer9::sync(bool block) { HRESULT result; IDirect3DQuery9* query = allocateEventQuery(); if (!query) { return; } result = query->Issue(D3DISSUE_END); ASSERT(SUCCEEDED(result)); do { result = query->GetData(NULL, 0, D3DGETDATA_FLUSH); if(block && result == S_FALSE) { // Keep polling, but allow other threads to do something useful first Sleep(0); // explicitly check for device loss // some drivers seem to return S_FALSE even if the device is lost // instead of D3DERR_DEVICELOST like they should if (testDeviceLost(false)) { result = D3DERR_DEVICELOST; } } } while(block && result == S_FALSE); freeEventQuery(query); if (d3d9::isDeviceLostError(result)) { notifyDeviceLost(); } } SwapChain *Renderer9::createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat) { return new rx::SwapChain9(this, window, shareHandle, backBufferFormat, depthBufferFormat); } IDirect3DQuery9* Renderer9::allocateEventQuery() { IDirect3DQuery9 *query = NULL; if (mEventQueryPool.empty()) { HRESULT result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &query); ASSERT(SUCCEEDED(result)); } else { query = mEventQueryPool.back(); mEventQueryPool.pop_back(); } return query; } void Renderer9::freeEventQuery(IDirect3DQuery9* query) { if (mEventQueryPool.size() > 1000) { query->Release(); } else { mEventQueryPool.push_back(query); } } IDirect3DVertexShader9 *Renderer9::createVertexShader(const DWORD *function, size_t length) { return mVertexShaderCache.create(function, length); } IDirect3DPixelShader9 *Renderer9::createPixelShader(const DWORD *function, size_t length) { return mPixelShaderCache.create(function, length); } HRESULT Renderer9::createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer) { D3DPOOL Pool = getBufferPool(Usage); return mDevice->CreateVertexBuffer(Length, Usage, 0, Pool, ppVertexBuffer, NULL); } VertexBuffer *Renderer9::createVertexBuffer() { return new VertexBuffer9(this); } HRESULT Renderer9::createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer) { D3DPOOL Pool = getBufferPool(Usage); return mDevice->CreateIndexBuffer(Length, Usage, Format, Pool, ppIndexBuffer, NULL); } IndexBuffer *Renderer9::createIndexBuffer() { return new IndexBuffer9(this); } BufferStorage *Renderer9::createBufferStorage() { return new BufferStorage9(); } QueryImpl *Renderer9::createQuery(GLenum type) { return new Query9(this, type); } FenceImpl *Renderer9::createFence() { return new Fence9(this); } void Renderer9::setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &samplerState) { bool *forceSetSamplers = (type == gl::SAMPLER_PIXEL) ? mForceSetPixelSamplerStates : mForceSetVertexSamplerStates; gl::SamplerState *appliedSamplers = (type == gl::SAMPLER_PIXEL) ? mCurPixelSamplerStates: mCurVertexSamplerStates; if (forceSetSamplers[index] || memcmp(&samplerState, &appliedSamplers[index], sizeof(gl::SamplerState)) != 0) { int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; int d3dSampler = index + d3dSamplerOffset; mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, gl_d3d9::ConvertTextureWrap(samplerState.wrapS)); mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, gl_d3d9::ConvertTextureWrap(samplerState.wrapT)); mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, gl_d3d9::ConvertMagFilter(samplerState.magFilter, samplerState.maxAnisotropy)); D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter; gl_d3d9::ConvertMinFilter(samplerState.minFilter, &d3dMinFilter, &d3dMipFilter, samplerState.maxAnisotropy); mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter); mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter); mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, samplerState.lodOffset); if (mSupportsTextureFilterAnisotropy) { mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)samplerState.maxAnisotropy); } } forceSetSamplers[index] = false; appliedSamplers[index] = samplerState; } void Renderer9::setTexture(gl::SamplerType type, int index, gl::Texture *texture) { int d3dSamplerOffset = (type == gl::SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0; int d3dSampler = index + d3dSamplerOffset; IDirect3DBaseTexture9 *d3dTexture = NULL; unsigned int serial = 0; bool forceSetTexture = false; unsigned int *appliedSerials = (type == gl::SAMPLER_PIXEL) ? mCurPixelTextureSerials : mCurVertexTextureSerials; if (texture) { TextureStorageInterface *texStorage = texture->getNativeTexture(); if (texStorage) { TextureStorage9 *storage9 = TextureStorage9::makeTextureStorage9(texStorage->getStorageInstance()); d3dTexture = storage9->getBaseTexture(); } // If we get NULL back from getBaseTexture here, something went wrong // in the texture class and we're unexpectedly missing the d3d texture ASSERT(d3dTexture != NULL); serial = texture->getTextureSerial(); forceSetTexture = texture->hasDirtyImages(); } if (forceSetTexture || appliedSerials[index] != serial) { mDevice->SetTexture(d3dSampler, d3dTexture); } appliedSerials[index] = serial; } void Renderer9::setRasterizerState(const gl::RasterizerState &rasterState) { bool rasterStateChanged = mForceSetRasterState || memcmp(&rasterState, &mCurRasterState, sizeof(gl::RasterizerState)) != 0; if (rasterStateChanged) { // Set the cull mode if (rasterState.cullFace) { mDevice->SetRenderState(D3DRS_CULLMODE, gl_d3d9::ConvertCullMode(rasterState.cullMode, rasterState.frontFace)); } else { mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); } if (rasterState.polygonOffsetFill) { if (mCurDepthSize > 0) { mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *(DWORD*)&rasterState.polygonOffsetFactor); float depthBias = ldexp(rasterState.polygonOffsetUnits, -static_cast<int>(mCurDepthSize)); mDevice->SetRenderState(D3DRS_DEPTHBIAS, *(DWORD*)&depthBias); } } else { mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0); mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0); } mCurRasterState = rasterState; } mForceSetRasterState = false; } void Renderer9::setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor, unsigned int sampleMask) { bool blendStateChanged = mForceSetBlendState || memcmp(&blendState, &mCurBlendState, sizeof(gl::BlendState)) != 0; bool blendColorChanged = mForceSetBlendState || memcmp(&blendColor, &mCurBlendColor, sizeof(gl::Color)) != 0; bool sampleMaskChanged = mForceSetBlendState || sampleMask != mCurSampleMask; if (blendStateChanged || blendColorChanged) { if (blendState.blend) { mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); if (blendState.sourceBlendRGB != GL_CONSTANT_ALPHA && blendState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA && blendState.destBlendRGB != GL_CONSTANT_ALPHA && blendState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA) { mDevice->SetRenderState(D3DRS_BLENDFACTOR, gl_d3d9::ConvertColor(blendColor)); } else { mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(gl::unorm<8>(blendColor.alpha), gl::unorm<8>(blendColor.alpha), gl::unorm<8>(blendColor.alpha), gl::unorm<8>(blendColor.alpha))); } mDevice->SetRenderState(D3DRS_SRCBLEND, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendRGB)); mDevice->SetRenderState(D3DRS_DESTBLEND, gl_d3d9::ConvertBlendFunc(blendState.destBlendRGB)); mDevice->SetRenderState(D3DRS_BLENDOP, gl_d3d9::ConvertBlendOp(blendState.blendEquationRGB)); if (blendState.sourceBlendRGB != blendState.sourceBlendAlpha || blendState.destBlendRGB != blendState.destBlendAlpha || blendState.blendEquationRGB != blendState.blendEquationAlpha) { mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.sourceBlendAlpha)); mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, gl_d3d9::ConvertBlendFunc(blendState.destBlendAlpha)); mDevice->SetRenderState(D3DRS_BLENDOPALPHA, gl_d3d9::ConvertBlendOp(blendState.blendEquationAlpha)); } else { mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE); } } else { mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); } if (blendState.sampleAlphaToCoverage) { FIXME("Sample alpha to coverage is unimplemented."); } // Set the color mask bool zeroColorMaskAllowed = getAdapterVendor() != VENDOR_ID_AMD; // Apparently some ATI cards have a bug where a draw with a zero color // write mask can cause later draws to have incorrect results. Instead, // set a nonzero color write mask but modify the blend state so that no // drawing is done. // http://code.google.com/p/angleproject/issues/detail?id=169 DWORD colorMask = gl_d3d9::ConvertColorMask(blendState.colorMaskRed, blendState.colorMaskGreen, blendState.colorMaskBlue, blendState.colorMaskAlpha); if (colorMask == 0 && !zeroColorMaskAllowed) { // Enable green channel, but set blending so nothing will be drawn. mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN); mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO); mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE); mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD); } else { mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask); } mDevice->SetRenderState(D3DRS_DITHERENABLE, blendState.dither ? TRUE : FALSE); mCurBlendState = blendState; mCurBlendColor = blendColor; } if (sampleMaskChanged) { // Set the multisample mask mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE); mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, static_cast<DWORD>(sampleMask)); mCurSampleMask = sampleMask; } mForceSetBlendState = false; } void Renderer9::setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef, int stencilBackRef, bool frontFaceCCW) { bool depthStencilStateChanged = mForceSetDepthStencilState || memcmp(&depthStencilState, &mCurDepthStencilState, sizeof(gl::DepthStencilState)) != 0; bool stencilRefChanged = mForceSetDepthStencilState || stencilRef != mCurStencilRef || stencilBackRef != mCurStencilBackRef; bool frontFaceCCWChanged = mForceSetDepthStencilState || frontFaceCCW != mCurFrontFaceCCW; if (depthStencilStateChanged) { if (depthStencilState.depthTest) { mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); mDevice->SetRenderState(D3DRS_ZFUNC, gl_d3d9::ConvertComparison(depthStencilState.depthFunc)); } else { mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); } mCurDepthStencilState = depthStencilState; } if (depthStencilStateChanged || stencilRefChanged || frontFaceCCWChanged) { if (depthStencilState.stencilTest && mCurStencilSize > 0) { mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE); // FIXME: Unsupported by D3D9 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF; const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK; const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK; if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask || stencilRef != stencilBackRef || depthStencilState.stencilMask != depthStencilState.stencilBackMask) { ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL."); return gl::error(GL_INVALID_OPERATION); } // get the maximum size of the stencil ref unsigned int maxStencil = (1 << mCurStencilSize) - 1; mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, depthStencilState.stencilWritemask); mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, gl_d3d9::ConvertComparison(depthStencilState.stencilFunc)); mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (stencilRef < (int)maxStencil) ? stencilRef : maxStencil); mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, depthStencilState.stencilMask); mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, gl_d3d9::ConvertStencilOp(depthStencilState.stencilFail)); mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthFail)); mDevice->SetRenderState(frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, gl_d3d9::ConvertStencilOp(depthStencilState.stencilPassDepthPass)); mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, depthStencilState.stencilBackWritemask); mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC, gl_d3d9::ConvertComparison(depthStencilState.stencilBackFunc)); mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (stencilBackRef < (int)maxStencil) ? stencilBackRef : maxStencil); mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, depthStencilState.stencilBackMask); mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL, gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackFail)); mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL, gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthFail)); mDevice->SetRenderState(!frontFaceCCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS, gl_d3d9::ConvertStencilOp(depthStencilState.stencilBackPassDepthPass)); } else { mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); } mDevice->SetRenderState(D3DRS_ZWRITEENABLE, depthStencilState.depthMask ? TRUE : FALSE); mCurStencilRef = stencilRef; mCurStencilBackRef = stencilBackRef; mCurFrontFaceCCW = frontFaceCCW; } mForceSetDepthStencilState = false; } void Renderer9::setScissorRectangle(const gl::Rectangle &scissor, bool enabled) { bool scissorChanged = mForceSetScissor || memcmp(&scissor, &mCurScissor, sizeof(gl::Rectangle)) != 0 || enabled != mScissorEnabled; if (scissorChanged) { if (enabled) { RECT rect; rect.left = gl::clamp(scissor.x, 0, static_cast<int>(mRenderTargetDesc.width)); rect.top = gl::clamp(scissor.y, 0, static_cast<int>(mRenderTargetDesc.height)); rect.right = gl::clamp(scissor.x + scissor.width, 0, static_cast<int>(mRenderTargetDesc.width)); rect.bottom = gl::clamp(scissor.y + scissor.height, 0, static_cast<int>(mRenderTargetDesc.height)); mDevice->SetScissorRect(&rect); } mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, enabled ? TRUE : FALSE); mScissorEnabled = enabled; mCurScissor = scissor; } mForceSetScissor = false; } bool Renderer9::setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace, bool ignoreViewport) { gl::Rectangle actualViewport = viewport; float actualZNear = gl::clamp01(zNear); float actualZFar = gl::clamp01(zFar); if (ignoreViewport) { actualViewport.x = 0; actualViewport.y = 0; actualViewport.width = mRenderTargetDesc.width; actualViewport.height = mRenderTargetDesc.height; actualZNear = 0.0f; actualZFar = 1.0f; } D3DVIEWPORT9 dxViewport; dxViewport.X = gl::clamp(actualViewport.x, 0, static_cast<int>(mRenderTargetDesc.width)); dxViewport.Y = gl::clamp(actualViewport.y, 0, static_cast<int>(mRenderTargetDesc.height)); dxViewport.Width = gl::clamp(actualViewport.width, 0, static_cast<int>(mRenderTargetDesc.width) - static_cast<int>(dxViewport.X)); dxViewport.Height = gl::clamp(actualViewport.height, 0, static_cast<int>(mRenderTargetDesc.height) - static_cast<int>(dxViewport.Y)); dxViewport.MinZ = actualZNear; dxViewport.MaxZ = actualZFar; if (dxViewport.Width <= 0 || dxViewport.Height <= 0) { return false; // Nothing to render } bool viewportChanged = mForceSetViewport || memcmp(&actualViewport, &mCurViewport, sizeof(gl::Rectangle)) != 0 || actualZNear != mCurNear || actualZFar != mCurFar; if (viewportChanged) { mDevice->SetViewport(&dxViewport); mCurViewport = actualViewport; mCurNear = actualZNear; mCurFar = actualZFar; dx_VertexConstants vc = {0}; dx_PixelConstants pc = {0}; vc.viewAdjust[0] = (float)((actualViewport.width - (int)dxViewport.Width) + 2 * (actualViewport.x - (int)dxViewport.X) - 1) / dxViewport.Width; vc.viewAdjust[1] = (float)((actualViewport.height - (int)dxViewport.Height) + 2 * (actualViewport.y - (int)dxViewport.Y) - 1) / dxViewport.Height; vc.viewAdjust[2] = (float)actualViewport.width / dxViewport.Width; vc.viewAdjust[3] = (float)actualViewport.height / dxViewport.Height; pc.viewCoords[0] = actualViewport.width * 0.5f; pc.viewCoords[1] = actualViewport.height * 0.5f; pc.viewCoords[2] = actualViewport.x + (actualViewport.width * 0.5f); pc.viewCoords[3] = actualViewport.y + (actualViewport.height * 0.5f); pc.depthFront[0] = (actualZFar - actualZNear) * 0.5f; pc.depthFront[1] = (actualZNear + actualZFar) * 0.5f; pc.depthFront[2] = !gl::IsTriangleMode(drawMode) ? 0.0f : (frontFace == GL_CCW ? 1.0f : -1.0f);; vc.depthRange[0] = actualZNear; vc.depthRange[1] = actualZFar; vc.depthRange[2] = actualZFar - actualZNear; pc.depthRange[0] = actualZNear; pc.depthRange[1] = actualZFar; pc.depthRange[2] = actualZFar - actualZNear; if (memcmp(&vc, &mVertexConstants, sizeof(dx_VertexConstants)) != 0) { mVertexConstants = vc; mDxUniformsDirty = true; } if (memcmp(&pc, &mPixelConstants, sizeof(dx_PixelConstants)) != 0) { mPixelConstants = pc; mDxUniformsDirty = true; } } mForceSetViewport = false; return true; } bool Renderer9::applyPrimitiveType(GLenum mode, GLsizei count) { switch (mode) { case GL_POINTS: mPrimitiveType = D3DPT_POINTLIST; mPrimitiveCount = count; break; case GL_LINES: mPrimitiveType = D3DPT_LINELIST; mPrimitiveCount = count / 2; break; case GL_LINE_LOOP: mPrimitiveType = D3DPT_LINESTRIP; mPrimitiveCount = count - 1; // D3D doesn't support line loops, so we draw the last line separately break; case GL_LINE_STRIP: mPrimitiveType = D3DPT_LINESTRIP; mPrimitiveCount = count - 1; break; case GL_TRIANGLES: mPrimitiveType = D3DPT_TRIANGLELIST; mPrimitiveCount = count / 3; break; case GL_TRIANGLE_STRIP: mPrimitiveType = D3DPT_TRIANGLESTRIP; mPrimitiveCount = count - 2; break; case GL_TRIANGLE_FAN: mPrimitiveType = D3DPT_TRIANGLEFAN; mPrimitiveCount = count - 2; break; default: return gl::error(GL_INVALID_ENUM, false); } return mPrimitiveCount > 0; } gl::Renderbuffer *Renderer9::getNullColorbuffer(gl::Renderbuffer *depthbuffer) { if (!depthbuffer) { ERR("Unexpected null depthbuffer for depth-only FBO."); return NULL; } GLsizei width = depthbuffer->getWidth(); GLsizei height = depthbuffer->getHeight(); // search cached nullcolorbuffers for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) { if (mNullColorbufferCache[i].buffer != NULL && mNullColorbufferCache[i].width == width && mNullColorbufferCache[i].height == height) { mNullColorbufferCache[i].lruCount = ++mMaxNullColorbufferLRU; return mNullColorbufferCache[i].buffer; } } gl::Renderbuffer *nullbuffer = new gl::Renderbuffer(this, 0, new gl::Colorbuffer(this, width, height, GL_NONE, 0)); // add nullbuffer to the cache NullColorbufferCacheEntry *oldest = &mNullColorbufferCache[0]; for (int i = 1; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) { if (mNullColorbufferCache[i].lruCount < oldest->lruCount) { oldest = &mNullColorbufferCache[i]; } } delete oldest->buffer; oldest->buffer = nullbuffer; oldest->lruCount = ++mMaxNullColorbufferLRU; oldest->width = width; oldest->height = height; return nullbuffer; } bool Renderer9::applyRenderTarget(gl::Framebuffer *framebuffer) { // if there is no color attachment we must synthesize a NULL colorattachment // to keep the D3D runtime happy. This should only be possible if depth texturing. gl::Renderbuffer *renderbufferObject = NULL; if (framebuffer->getColorbufferType(0) != GL_NONE) { renderbufferObject = framebuffer->getColorbuffer(0); } else { renderbufferObject = getNullColorbuffer(framebuffer->getDepthbuffer()); } if (!renderbufferObject) { ERR("unable to locate renderbuffer for FBO."); return false; } bool renderTargetChanged = false; unsigned int renderTargetSerial = renderbufferObject->getSerial(); if (renderTargetSerial != mAppliedRenderTargetSerial) { // Apply the render target on the device IDirect3DSurface9 *renderTargetSurface = NULL; RenderTarget *renderTarget = renderbufferObject->getRenderTarget(); if (renderTarget) { renderTargetSurface = RenderTarget9::makeRenderTarget9(renderTarget)->getSurface(); } if (!renderTargetSurface) { ERR("render target pointer unexpectedly null."); return false; // Context must be lost } mDevice->SetRenderTarget(0, renderTargetSurface); renderTargetSurface->Release(); mAppliedRenderTargetSerial = renderTargetSerial; renderTargetChanged = true; } gl::Renderbuffer *depthStencil = NULL; unsigned int depthbufferSerial = 0; unsigned int stencilbufferSerial = 0; if (framebuffer->getDepthbufferType() != GL_NONE) { depthStencil = framebuffer->getDepthbuffer(); if (!depthStencil) { ERR("Depth stencil pointer unexpectedly null."); return false; } depthbufferSerial = depthStencil->getSerial(); } else if (framebuffer->getStencilbufferType() != GL_NONE) { depthStencil = framebuffer->getStencilbuffer(); if (!depthStencil) { ERR("Depth stencil pointer unexpectedly null."); return false; } stencilbufferSerial = depthStencil->getSerial(); } if (depthbufferSerial != mAppliedDepthbufferSerial || stencilbufferSerial != mAppliedStencilbufferSerial || !mDepthStencilInitialized) { unsigned int depthSize = 0; unsigned int stencilSize = 0; // Apply the depth stencil on the device if (depthStencil) { IDirect3DSurface9 *depthStencilSurface = NULL; RenderTarget *depthStencilRenderTarget = depthStencil->getDepthStencil(); if (depthStencilRenderTarget) { depthStencilSurface = RenderTarget9::makeRenderTarget9(depthStencilRenderTarget)->getSurface(); } if (!depthStencilSurface) { ERR("depth stencil pointer unexpectedly null."); return false; // Context must be lost } mDevice->SetDepthStencilSurface(depthStencilSurface); depthStencilSurface->Release(); depthSize = depthStencil->getDepthSize(); stencilSize = depthStencil->getStencilSize(); } else { mDevice->SetDepthStencilSurface(NULL); } if (!mDepthStencilInitialized || depthSize != mCurDepthSize) { mCurDepthSize = depthSize; mForceSetRasterState = true; } if (!mDepthStencilInitialized || stencilSize != mCurStencilSize) { mCurStencilSize = stencilSize; mForceSetDepthStencilState = true; } mAppliedDepthbufferSerial = depthbufferSerial; mAppliedStencilbufferSerial = stencilbufferSerial; mDepthStencilInitialized = true; } if (renderTargetChanged || !mRenderTargetDescInitialized) { mForceSetScissor = true; mForceSetViewport = true; mRenderTargetDesc.width = renderbufferObject->getWidth(); mRenderTargetDesc.height = renderbufferObject->getHeight(); mRenderTargetDesc.format = renderbufferObject->getActualFormat(); mRenderTargetDescInitialized = true; } return true; } GLenum Renderer9::applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances) { TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS]; GLenum err = mVertexDataManager->prepareVertexData(vertexAttributes, programBinary, first, count, attributes, instances); if (err != GL_NO_ERROR) { return err; } return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, &mRepeatDraw); } // Applies the indices and element array bindings to the Direct3D 9 device GLenum Renderer9::applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo) { GLenum err = mIndexDataManager->prepareIndexData(type, count, elementArrayBuffer, indices, indexInfo); if (err == GL_NO_ERROR) { // Directly binding the storage buffer is not supported for d3d9 ASSERT(indexInfo->storage == NULL); if (indexInfo->serial != mAppliedIBSerial) { IndexBuffer9* indexBuffer = IndexBuffer9::makeIndexBuffer9(indexInfo->indexBuffer); mDevice->SetIndices(indexBuffer->getBuffer()); mAppliedIBSerial = indexInfo->serial; } } return err; } void Renderer9::drawArrays(GLenum mode, GLsizei count, GLsizei instances) { startScene(); if (mode == GL_LINE_LOOP) { drawLineLoop(count, GL_NONE, NULL, 0, NULL); } else if (instances > 0) { StaticIndexBufferInterface *countingIB = mIndexDataManager->getCountingIndices(count); if (countingIB) { if (mAppliedIBSerial != countingIB->getSerial()) { IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(countingIB->getIndexBuffer()); mDevice->SetIndices(indexBuffer->getBuffer()); mAppliedIBSerial = countingIB->getSerial(); } for (int i = 0; i < mRepeatDraw; i++) { mDevice->DrawIndexedPrimitive(mPrimitiveType, 0, 0, count, 0, mPrimitiveCount); } } else { ERR("Could not create a counting index buffer for glDrawArraysInstanced."); return gl::error(GL_OUT_OF_MEMORY); } } else // Regular case { mDevice->DrawPrimitive(mPrimitiveType, 0, mPrimitiveCount); } } void Renderer9::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei /*instances*/) { startScene(); if (mode == GL_POINTS) { drawIndexedPoints(count, type, indices, elementArrayBuffer); } else if (mode == GL_LINE_LOOP) { drawLineLoop(count, type, indices, indexInfo.minIndex, elementArrayBuffer); } else { for (int i = 0; i < mRepeatDraw; i++) { GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1; mDevice->DrawIndexedPrimitive(mPrimitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, mPrimitiveCount); } } } void Renderer9::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer) { // Get the raw indices for an indexed draw if (type != GL_NONE && elementArrayBuffer) { gl::Buffer *indexBuffer = elementArrayBuffer; BufferStorage *storage = indexBuffer->getStorage(); intptr_t offset = reinterpret_cast<intptr_t>(indices); indices = static_cast<const GLubyte*>(storage->getData()) + offset; } UINT startIndex = 0; if (get32BitIndexSupport()) { if (!mLineLoopIB) { mLineLoopIB = new StreamingIndexBufferInterface(this); if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_INT)) { delete mLineLoopIB; mLineLoopIB = NULL; ERR("Could not create a 32-bit looping index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } } const int spaceNeeded = (count + 1) * sizeof(unsigned int); if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_INT)) { ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } void* mappedMemory = NULL; int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory); if (offset == -1 || mappedMemory == NULL) { ERR("Could not map index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } startIndex = static_cast<UINT>(offset) / 4; unsigned int *data = reinterpret_cast<unsigned int*>(mappedMemory); switch (type) { case GL_NONE: // Non-indexed draw for (int i = 0; i < count; i++) { data[i] = i; } data[count] = 0; break; case GL_UNSIGNED_BYTE: for (int i = 0; i < count; i++) { data[i] = static_cast<const GLubyte*>(indices)[i]; } data[count] = static_cast<const GLubyte*>(indices)[0]; break; case GL_UNSIGNED_SHORT: for (int i = 0; i < count; i++) { data[i] = static_cast<const GLushort*>(indices)[i]; } data[count] = static_cast<const GLushort*>(indices)[0]; break; case GL_UNSIGNED_INT: for (int i = 0; i < count; i++) { data[i] = static_cast<const GLuint*>(indices)[i]; } data[count] = static_cast<const GLuint*>(indices)[0]; break; default: UNREACHABLE(); } if (!mLineLoopIB->unmapBuffer()) { ERR("Could not unmap index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } } else { if (!mLineLoopIB) { mLineLoopIB = new StreamingIndexBufferInterface(this); if (!mLineLoopIB->reserveBufferSpace(INITIAL_INDEX_BUFFER_SIZE, GL_UNSIGNED_SHORT)) { delete mLineLoopIB; mLineLoopIB = NULL; ERR("Could not create a 16-bit looping index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } } const int spaceNeeded = (count + 1) * sizeof(unsigned short); if (!mLineLoopIB->reserveBufferSpace(spaceNeeded, GL_UNSIGNED_SHORT)) { ERR("Could not reserve enough space in looping index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } void* mappedMemory = NULL; int offset = mLineLoopIB->mapBuffer(spaceNeeded, &mappedMemory); if (offset == -1 || mappedMemory == NULL) { ERR("Could not map index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } startIndex = static_cast<UINT>(offset) / 2; unsigned short *data = reinterpret_cast<unsigned short*>(mappedMemory); switch (type) { case GL_NONE: // Non-indexed draw for (int i = 0; i < count; i++) { data[i] = i; } data[count] = 0; break; case GL_UNSIGNED_BYTE: for (int i = 0; i < count; i++) { data[i] = static_cast<const GLubyte*>(indices)[i]; } data[count] = static_cast<const GLubyte*>(indices)[0]; break; case GL_UNSIGNED_SHORT: for (int i = 0; i < count; i++) { data[i] = static_cast<const GLushort*>(indices)[i]; } data[count] = static_cast<const GLushort*>(indices)[0]; break; case GL_UNSIGNED_INT: for (int i = 0; i < count; i++) { data[i] = static_cast<const GLuint*>(indices)[i]; } data[count] = static_cast<const GLuint*>(indices)[0]; break; default: UNREACHABLE(); } if (!mLineLoopIB->unmapBuffer()) { ERR("Could not unmap index buffer for GL_LINE_LOOP."); return gl::error(GL_OUT_OF_MEMORY); } } if (mAppliedIBSerial != mLineLoopIB->getSerial()) { IndexBuffer9 *indexBuffer = IndexBuffer9::makeIndexBuffer9(mLineLoopIB->getIndexBuffer()); mDevice->SetIndices(indexBuffer->getBuffer()); mAppliedIBSerial = mLineLoopIB->getSerial(); } mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count); } template <typename T> static void drawPoints(IDirect3DDevice9* device, GLsizei count, const GLvoid *indices) { for (int i = 0; i < count; i++) { unsigned int indexValue = static_cast<unsigned int>(static_cast<const T*>(indices)[i]); device->DrawPrimitive(D3DPT_POINTLIST, indexValue, 1); } } void Renderer9::drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer) { // Drawing index point lists is unsupported in d3d9, fall back to a regular DrawPrimitive call // for each individual point. This call is not expected to happen often. if (elementArrayBuffer) { BufferStorage *storage = elementArrayBuffer->getStorage(); intptr_t offset = reinterpret_cast<intptr_t>(indices); indices = static_cast<const GLubyte*>(storage->getData()) + offset; } switch (type) { case GL_UNSIGNED_BYTE: drawPoints<GLubyte>(mDevice, count, indices); break; case GL_UNSIGNED_SHORT: drawPoints<GLushort>(mDevice, count, indices); break; case GL_UNSIGNED_INT: drawPoints<GLuint>(mDevice, count, indices); break; default: UNREACHABLE(); } } void Renderer9::applyShaders(gl::ProgramBinary *programBinary) { unsigned int programBinarySerial = programBinary->getSerial(); if (programBinarySerial != mAppliedProgramBinarySerial) { ShaderExecutable *vertexExe = programBinary->getVertexExecutable(); ShaderExecutable *pixelExe = programBinary->getPixelExecutable(); IDirect3DVertexShader9 *vertexShader = NULL; if (vertexExe) vertexShader = ShaderExecutable9::makeShaderExecutable9(vertexExe)->getVertexShader(); IDirect3DPixelShader9 *pixelShader = NULL; if (pixelExe) pixelShader = ShaderExecutable9::makeShaderExecutable9(pixelExe)->getPixelShader(); mDevice->SetPixelShader(pixelShader); mDevice->SetVertexShader(vertexShader); programBinary->dirtyAllUniforms(); mDxUniformsDirty = true; mAppliedProgramBinarySerial = programBinarySerial; } } void Renderer9::applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray) { for (std::vector<gl::Uniform*>::const_iterator ub = uniformArray->begin(), ue = uniformArray->end(); ub != ue; ++ub) { gl::Uniform *targetUniform = *ub; if (targetUniform->dirty) { GLfloat *f = (GLfloat*)targetUniform->data; GLint *i = (GLint*)targetUniform->data; switch (targetUniform->type) { case GL_SAMPLER_2D: case GL_SAMPLER_CUBE: break; case GL_BOOL: case GL_BOOL_VEC2: case GL_BOOL_VEC3: case GL_BOOL_VEC4: applyUniformnbv(targetUniform, i); break; case GL_FLOAT: case GL_FLOAT_VEC2: case GL_FLOAT_VEC3: case GL_FLOAT_VEC4: case GL_FLOAT_MAT2: case GL_FLOAT_MAT3: case GL_FLOAT_MAT4: applyUniformnfv(targetUniform, f); break; case GL_INT: case GL_INT_VEC2: case GL_INT_VEC3: case GL_INT_VEC4: applyUniformniv(targetUniform, i); break; default: UNREACHABLE(); } targetUniform->dirty = false; } } // Driver uniforms if (mDxUniformsDirty) { mDevice->SetVertexShaderConstantF(0, (float*)&mVertexConstants, sizeof(dx_VertexConstants) / sizeof(float[4])); mDevice->SetPixelShaderConstantF(0, (float*)&mPixelConstants, sizeof(dx_PixelConstants) / sizeof(float[4])); mDxUniformsDirty = false; } } void Renderer9::applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v) { if (targetUniform->psRegisterIndex >= 0) { mDevice->SetPixelShaderConstantF(targetUniform->psRegisterIndex, v, targetUniform->registerCount); } if (targetUniform->vsRegisterIndex >= 0) { mDevice->SetVertexShaderConstantF(targetUniform->vsRegisterIndex, v, targetUniform->registerCount); } } void Renderer9::applyUniformniv(gl::Uniform *targetUniform, const GLint *v) { ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9); GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4]; for (unsigned int i = 0; i < targetUniform->registerCount; i++) { vector[i][0] = (GLfloat)v[4 * i + 0]; vector[i][1] = (GLfloat)v[4 * i + 1]; vector[i][2] = (GLfloat)v[4 * i + 2]; vector[i][3] = (GLfloat)v[4 * i + 3]; } applyUniformnfv(targetUniform, (GLfloat*)vector); } void Renderer9::applyUniformnbv(gl::Uniform *targetUniform, const GLint *v) { ASSERT(targetUniform->registerCount <= MAX_VERTEX_CONSTANT_VECTORS_D3D9); GLfloat vector[MAX_VERTEX_CONSTANT_VECTORS_D3D9][4]; for (unsigned int i = 0; i < targetUniform->registerCount; i++) { vector[i][0] = (v[4 * i + 0] == GL_FALSE) ? 0.0f : 1.0f; vector[i][1] = (v[4 * i + 1] == GL_FALSE) ? 0.0f : 1.0f; vector[i][2] = (v[4 * i + 2] == GL_FALSE) ? 0.0f : 1.0f; vector[i][3] = (v[4 * i + 3] == GL_FALSE) ? 0.0f : 1.0f; } applyUniformnfv(targetUniform, (GLfloat*)vector); } void Renderer9::clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer) { D3DCOLOR color = D3DCOLOR_ARGB(gl::unorm<8>(clearParams.colorClearValue.alpha), gl::unorm<8>(clearParams.colorClearValue.red), gl::unorm<8>(clearParams.colorClearValue.green), gl::unorm<8>(clearParams.colorClearValue.blue)); float depth = gl::clamp01(clearParams.depthClearValue); int stencil = clearParams.stencilClearValue & 0x000000FF; unsigned int stencilUnmasked = 0x0; if ((clearParams.mask & GL_STENCIL_BUFFER_BIT) && frameBuffer->hasStencil()) { unsigned int stencilSize = gl::GetStencilSize(frameBuffer->getStencilbuffer()->getActualFormat()); stencilUnmasked = (0x1 << stencilSize) - 1; } bool alphaUnmasked = (gl::GetAlphaSize(mRenderTargetDesc.format) == 0) || clearParams.colorMaskAlpha; const bool needMaskedStencilClear = (clearParams.mask & GL_STENCIL_BUFFER_BIT) && (clearParams.stencilWriteMask & stencilUnmasked) != stencilUnmasked; const bool needMaskedColorClear = (clearParams.mask & GL_COLOR_BUFFER_BIT) && !(clearParams.colorMaskRed && clearParams.colorMaskGreen && clearParams.colorMaskBlue && alphaUnmasked); if (needMaskedColorClear || needMaskedStencilClear) { // State which is altered in all paths from this point to the clear call is saved. // State which is altered in only some paths will be flagged dirty in the case that // that path is taken. HRESULT hr; if (mMaskedClearSavedState == NULL) { hr = mDevice->BeginStateBlock(); ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); mDevice->SetRenderState(D3DRS_ZENABLE, FALSE); mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0); mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); mDevice->SetPixelShader(NULL); mDevice->SetVertexShader(NULL); mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE); mDevice->SetStreamSource(0, NULL, 0, 0); mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color); mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { mDevice->SetStreamSourceFreq(i, 1); } hr = mDevice->EndStateBlock(&mMaskedClearSavedState); ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY); } ASSERT(mMaskedClearSavedState != NULL); if (mMaskedClearSavedState != NULL) { hr = mMaskedClearSavedState->Capture(); ASSERT(SUCCEEDED(hr)); } mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); mDevice->SetRenderState(D3DRS_ZENABLE, FALSE); mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); if (clearParams.mask & GL_COLOR_BUFFER_BIT) { mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, gl_d3d9::ConvertColorMask(clearParams.colorMaskRed, clearParams.colorMaskGreen, clearParams.colorMaskBlue, clearParams.colorMaskAlpha)); } else { mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0); } if (stencilUnmasked != 0x0 && (clearParams.mask & GL_STENCIL_BUFFER_BIT)) { mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE); mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE); mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS); mDevice->SetRenderState(D3DRS_STENCILREF, stencil); mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, clearParams.stencilWriteMask); mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE); mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE); mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE); } else { mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE); } mDevice->SetPixelShader(NULL); mDevice->SetVertexShader(NULL); mDevice->SetFVF(D3DFVF_XYZRHW); mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE); mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR); mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR); mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color); mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF); for(int i = 0; i < gl::MAX_VERTEX_ATTRIBS; i++) { mDevice->SetStreamSourceFreq(i, 1); } float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges quad[0][0] = -0.5f; quad[0][1] = mRenderTargetDesc.height - 0.5f; quad[0][2] = 0.0f; quad[0][3] = 1.0f; quad[1][0] = mRenderTargetDesc.width - 0.5f; quad[1][1] = mRenderTargetDesc.height - 0.5f; quad[1][2] = 0.0f; quad[1][3] = 1.0f; quad[2][0] = -0.5f; quad[2][1] = -0.5f; quad[2][2] = 0.0f; quad[2][3] = 1.0f; quad[3][0] = mRenderTargetDesc.width - 0.5f; quad[3][1] = -0.5f; quad[3][2] = 0.0f; quad[3][3] = 1.0f; startScene(); mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4])); if (clearParams.mask & GL_DEPTH_BUFFER_BIT) { mDevice->SetRenderState(D3DRS_ZENABLE, TRUE); mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE); mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil); } if (mMaskedClearSavedState != NULL) { mMaskedClearSavedState->Apply(); } } else if (clearParams.mask) { DWORD dxClearFlags = 0; if (clearParams.mask & GL_COLOR_BUFFER_BIT) { dxClearFlags |= D3DCLEAR_TARGET; } if (clearParams.mask & GL_DEPTH_BUFFER_BIT) { dxClearFlags |= D3DCLEAR_ZBUFFER; } if (clearParams.mask & GL_STENCIL_BUFFER_BIT) { dxClearFlags |= D3DCLEAR_STENCIL; } mDevice->Clear(0, NULL, dxClearFlags, color, depth, stencil); } } void Renderer9::markAllStateDirty() { mAppliedRenderTargetSerial = 0; mAppliedDepthbufferSerial = 0; mAppliedStencilbufferSerial = 0; mDepthStencilInitialized = false; mRenderTargetDescInitialized = false; mForceSetDepthStencilState = true; mForceSetRasterState = true; mForceSetScissor = true; mForceSetViewport = true; mForceSetBlendState = true; for (unsigned int i = 0; i < gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS; i++) { mForceSetVertexSamplerStates[i] = true; mCurVertexTextureSerials[i] = 0; } for (unsigned int i = 0; i < gl::MAX_TEXTURE_IMAGE_UNITS; i++) { mForceSetPixelSamplerStates[i] = true; mCurPixelTextureSerials[i] = 0; } mAppliedIBSerial = 0; mAppliedProgramBinarySerial = 0; mDxUniformsDirty = true; mVertexDeclarationCache.markStateDirty(); } void Renderer9::releaseDeviceResources() { while (!mEventQueryPool.empty()) { mEventQueryPool.back()->Release(); mEventQueryPool.pop_back(); } if (mMaskedClearSavedState) { mMaskedClearSavedState->Release(); mMaskedClearSavedState = NULL; } mVertexShaderCache.clear(); mPixelShaderCache.clear(); delete mBlit; mBlit = NULL; delete mVertexDataManager; mVertexDataManager = NULL; delete mIndexDataManager; mIndexDataManager = NULL; delete mLineLoopIB; mLineLoopIB = NULL; for (int i = 0; i < NUM_NULL_COLORBUFFER_CACHE_ENTRIES; i++) { delete mNullColorbufferCache[i].buffer; mNullColorbufferCache[i].buffer = NULL; } } void Renderer9::notifyDeviceLost() { mDeviceLost = true; mDisplay->notifyDeviceLost(); } bool Renderer9::isDeviceLost() { return mDeviceLost; } // set notify to true to broadcast a message to all contexts of the device loss bool Renderer9::testDeviceLost(bool notify) { HRESULT status = S_OK; if (mDeviceEx) { status = mDeviceEx->CheckDeviceState(NULL); } else if (mDevice) { status = mDevice->TestCooperativeLevel(); } else { // No device yet, so no reset required } bool isLost = FAILED(status) || d3d9::isDeviceLostError(status); if (isLost) { // ensure we note the device loss -- // we'll probably get this done again by notifyDeviceLost // but best to remember it! // Note that we don't want to clear the device loss status here // -- this needs to be done by resetDevice mDeviceLost = true; if (notify) { notifyDeviceLost(); } } return isLost; } bool Renderer9::testDeviceResettable() { HRESULT status = D3D_OK; if (mDeviceEx) { status = mDeviceEx->CheckDeviceState(NULL); } else if (mDevice) { status = mDevice->TestCooperativeLevel(); } // On D3D9Ex, DEVICELOST represents a hung device that needs to be restarted // DEVICEREMOVED indicates the device has been stopped and must be recreated switch (status) { case D3DERR_DEVICENOTRESET: case D3DERR_DEVICEHUNG: return true; case D3DERR_DEVICELOST: return (mDeviceEx != NULL); case D3DERR_DEVICEREMOVED: UNIMPLEMENTED(); return false; default: return false; } } bool Renderer9::resetDevice() { releaseDeviceResources(); D3DPRESENT_PARAMETERS presentParameters = getDefaultPresentParameters(); HRESULT result = D3D_OK; bool lost = testDeviceLost(false); int attempts = 3; while (lost && attempts > 0) { if (mDeviceEx) { Sleep(500); // Give the graphics driver some CPU time result = mDeviceEx->ResetEx(&presentParameters, NULL); } else { result = mDevice->TestCooperativeLevel(); while (result == D3DERR_DEVICELOST) { Sleep(100); // Give the graphics driver some CPU time result = mDevice->TestCooperativeLevel(); } if (result == D3DERR_DEVICENOTRESET) { result = mDevice->Reset(&presentParameters); } } lost = testDeviceLost(false); attempts --; } if (FAILED(result)) { ERR("Reset/ResetEx failed multiple times: 0x%08X", result); return false; } // reset device defaults initializeDevice(); mDeviceLost = false; return true; } DWORD Renderer9::getAdapterVendor() const { return mAdapterIdentifier.VendorId; } std::string Renderer9::getRendererDescription() const { std::ostringstream rendererString; rendererString << mAdapterIdentifier.Description; if (getShareHandleSupport()) { rendererString << " Direct3D9Ex"; } else { rendererString << " Direct3D9"; } rendererString << " vs_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.VertexShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.VertexShaderVersion); rendererString << " ps_" << D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion) << "_" << D3DSHADER_VERSION_MINOR(mDeviceCaps.PixelShaderVersion); return rendererString.str(); } GUID Renderer9::getAdapterIdentifier() const { return mAdapterIdentifier.DeviceIdentifier; } void Renderer9::getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray) { for (int multiSampleIndex = 0; multiSampleIndex <= D3DMULTISAMPLE_16_SAMPLES; multiSampleIndex++) { HRESULT result = mD3d9->CheckDeviceMultiSampleType(mAdapter, mDeviceType, format, TRUE, (D3DMULTISAMPLE_TYPE)multiSampleIndex, NULL); multiSampleArray[multiSampleIndex] = SUCCEEDED(result); } } bool Renderer9::getBGRATextureSupport() const { // DirectX 9 always supports BGRA return true; } bool Renderer9::getDXT1TextureSupport() { return mDXT1TextureSupport; } bool Renderer9::getDXT3TextureSupport() { return mDXT3TextureSupport; } bool Renderer9::getDXT5TextureSupport() { return mDXT5TextureSupport; } bool Renderer9::getDepthTextureSupport() const { return mDepthTextureSupport; } bool Renderer9::getFloat32TextureSupport(bool *filtering, bool *renderable) { *filtering = mFloat32FilterSupport; *renderable = mFloat32RenderSupport; return mFloat32TextureSupport; } bool Renderer9::getFloat16TextureSupport(bool *filtering, bool *renderable) { *filtering = mFloat16FilterSupport; *renderable = mFloat16RenderSupport; return mFloat16TextureSupport; } bool Renderer9::getLuminanceTextureSupport() { return mLuminanceTextureSupport; } bool Renderer9::getLuminanceAlphaTextureSupport() { return mLuminanceAlphaTextureSupport; } bool Renderer9::getTextureFilterAnisotropySupport() const { return mSupportsTextureFilterAnisotropy; } float Renderer9::getTextureMaxAnisotropy() const { if (mSupportsTextureFilterAnisotropy) { return static_cast<float>(mDeviceCaps.MaxAnisotropy); } return 1.0f; } bool Renderer9::getEventQuerySupport() { return mEventQuerySupport; } unsigned int Renderer9::getMaxVertexTextureImageUnits() const { META_ASSERT(MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 <= gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS); return mVertexTextureSupport ? MAX_TEXTURE_IMAGE_UNITS_VTF_SM3 : 0; } unsigned int Renderer9::getMaxCombinedTextureImageUnits() const { return gl::MAX_TEXTURE_IMAGE_UNITS + getMaxVertexTextureImageUnits(); } unsigned int Renderer9::getReservedVertexUniformVectors() const { return 2; // dx_ViewAdjust and dx_DepthRange. } unsigned int Renderer9::getReservedFragmentUniformVectors() const { return 3; // dx_ViewCoords, dx_DepthFront and dx_DepthRange. } unsigned int Renderer9::getMaxVertexUniformVectors() const { return MAX_VERTEX_CONSTANT_VECTORS_D3D9 - getReservedVertexUniformVectors(); } unsigned int Renderer9::getMaxFragmentUniformVectors() const { const int maxPixelConstantVectors = (getMajorShaderModel() >= 3) ? MAX_PIXEL_CONSTANT_VECTORS_SM3 : MAX_PIXEL_CONSTANT_VECTORS_SM2; return maxPixelConstantVectors - getReservedFragmentUniformVectors(); } unsigned int Renderer9::getMaxVaryingVectors() const { return (getMajorShaderModel() >= 3) ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2; } bool Renderer9::getNonPower2TextureSupport() const { return mSupportsNonPower2Textures; } bool Renderer9::getOcclusionQuerySupport() const { return mOcclusionQuerySupport; } bool Renderer9::getInstancingSupport() const { return mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0); } bool Renderer9::getShareHandleSupport() const { // PIX doesn't seem to support using share handles, so disable them. return (mD3d9Ex != NULL) && !gl::perfActive(); } bool Renderer9::getDerivativeInstructionSupport() const { return (mDeviceCaps.PS20Caps.Caps & D3DPS20CAPS_GRADIENTINSTRUCTIONS) != 0; } bool Renderer9::getPostSubBufferSupport() const { return true; } int Renderer9::getMajorShaderModel() const { return D3DSHADER_VERSION_MAJOR(mDeviceCaps.PixelShaderVersion); } float Renderer9::getMaxPointSize() const { // Point size clamped at 1.0f for SM2 return getMajorShaderModel() == 3 ? mDeviceCaps.MaxPointSize : 1.0f; } int Renderer9::getMaxViewportDimension() const { int maxTextureDimension = std::min(std::min(getMaxTextureWidth(), getMaxTextureHeight()), (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE); return maxTextureDimension; } int Renderer9::getMaxTextureWidth() const { return (int)mDeviceCaps.MaxTextureWidth; } int Renderer9::getMaxTextureHeight() const { return (int)mDeviceCaps.MaxTextureHeight; } bool Renderer9::get32BitIndexSupport() const { return mDeviceCaps.MaxVertexIndex >= (1 << 16); } DWORD Renderer9::getCapsDeclTypes() const { return mDeviceCaps.DeclTypes; } int Renderer9::getMinSwapInterval() const { return mMinSwapInterval; } int Renderer9::getMaxSwapInterval() const { return mMaxSwapInterval; } int Renderer9::getMaxSupportedSamples() const { return mMaxSupportedSamples; } int Renderer9::getNearestSupportedSamples(D3DFORMAT format, int requested) const { if (requested == 0) { return requested; } std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format); if (itr == mMultiSampleSupport.end()) { if (format == D3DFMT_UNKNOWN) return 0; return -1; } for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i) { if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE) { return i; } } return -1; } unsigned int Renderer9::getMaxRenderTargets() const { // we do not support MRT in d3d9 return 1; } D3DFORMAT Renderer9::ConvertTextureInternalFormat(GLint internalformat) { switch (internalformat) { case GL_DEPTH_COMPONENT16: case GL_DEPTH_COMPONENT32_OES: case GL_DEPTH24_STENCIL8_OES: return D3DFMT_INTZ; case GL_COMPRESSED_RGB_S3TC_DXT1_EXT: case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT: return D3DFMT_DXT1; case GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE: return D3DFMT_DXT3; case GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE: return D3DFMT_DXT5; case GL_RGBA32F_EXT: case GL_RGB32F_EXT: case GL_ALPHA32F_EXT: case GL_LUMINANCE32F_EXT: case GL_LUMINANCE_ALPHA32F_EXT: return D3DFMT_A32B32G32R32F; case GL_RGBA16F_EXT: case GL_RGB16F_EXT: case GL_ALPHA16F_EXT: case GL_LUMINANCE16F_EXT: case GL_LUMINANCE_ALPHA16F_EXT: return D3DFMT_A16B16G16R16F; case GL_LUMINANCE8_EXT: if (getLuminanceTextureSupport()) { return D3DFMT_L8; } break; case GL_LUMINANCE8_ALPHA8_EXT: if (getLuminanceAlphaTextureSupport()) { return D3DFMT_A8L8; } break; case GL_RGB8_OES: case GL_RGB565: return D3DFMT_X8R8G8B8; } return D3DFMT_A8R8G8B8; } bool Renderer9::copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source) { bool result = false; if (source && dest) { TextureStorage9_2D *source9 = TextureStorage9_2D::makeTextureStorage9_2D(source->getStorageInstance()); TextureStorage9_2D *dest9 = TextureStorage9_2D::makeTextureStorage9_2D(dest->getStorageInstance()); int levels = source9->levelCount(); for (int i = 0; i < levels; ++i) { IDirect3DSurface9 *srcSurf = source9->getSurfaceLevel(i, false); IDirect3DSurface9 *dstSurf = dest9->getSurfaceLevel(i, false); result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged()); if (srcSurf) srcSurf->Release(); if (dstSurf) dstSurf->Release(); if (!result) return false; } } return result; } bool Renderer9::copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source) { bool result = false; if (source && dest) { TextureStorage9_Cube *source9 = TextureStorage9_Cube::makeTextureStorage9_Cube(source->getStorageInstance()); TextureStorage9_Cube *dest9 = TextureStorage9_Cube::makeTextureStorage9_Cube(dest->getStorageInstance()); int levels = source9->levelCount(); for (int f = 0; f < 6; f++) { for (int i = 0; i < levels; i++) { IDirect3DSurface9 *srcSurf = source9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, false); IDirect3DSurface9 *dstSurf = dest9->getCubeMapSurface(GL_TEXTURE_CUBE_MAP_POSITIVE_X + f, i, true); result = copyToRenderTarget(dstSurf, srcSurf, source9->isManaged()); if (srcSurf) srcSurf->Release(); if (dstSurf) dstSurf->Release(); if (!result) return false; } } } return result; } D3DPOOL Renderer9::getBufferPool(DWORD usage) const { if (mD3d9Ex != NULL) { return D3DPOOL_DEFAULT; } else { if (!(usage & D3DUSAGE_DYNAMIC)) { return D3DPOOL_MANAGED; } } return D3DPOOL_DEFAULT; } bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level) { RECT rect; rect.left = sourceRect.x; rect.top = sourceRect.y; rect.right = sourceRect.x + sourceRect.width; rect.bottom = sourceRect.y + sourceRect.height; return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, level); } bool Renderer9::copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat, GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level) { RECT rect; rect.left = sourceRect.x; rect.top = sourceRect.y; rect.right = sourceRect.x + sourceRect.width; rect.bottom = sourceRect.y + sourceRect.height; return mBlit->copy(framebuffer, rect, destFormat, xoffset, yoffset, storage, target, level); } bool Renderer9::blitRect(gl::Framebuffer *readFramebuffer, const gl::Rectangle &readRect, gl::Framebuffer *drawFramebuffer, const gl::Rectangle &drawRect, bool blitRenderTarget, bool blitDepthStencil) { endScene(); if (blitRenderTarget) { gl::Renderbuffer *readBuffer = readFramebuffer->getColorbuffer(0); gl::Renderbuffer *drawBuffer = drawFramebuffer->getColorbuffer(0); RenderTarget9 *readRenderTarget = NULL; RenderTarget9 *drawRenderTarget = NULL; IDirect3DSurface9* readSurface = NULL; IDirect3DSurface9* drawSurface = NULL; if (readBuffer) { readRenderTarget = RenderTarget9::makeRenderTarget9(readBuffer->getRenderTarget()); } if (drawBuffer) { drawRenderTarget = RenderTarget9::makeRenderTarget9(drawBuffer->getRenderTarget()); } if (readRenderTarget) { readSurface = readRenderTarget->getSurface(); } if (drawRenderTarget) { drawSurface = drawRenderTarget->getSurface(); } if (!readSurface || !drawSurface) { ERR("Failed to retrieve the render target."); return gl::error(GL_OUT_OF_MEMORY, false); } RECT srcRect; srcRect.left = readRect.x; srcRect.right = readRect.x + readRect.width; srcRect.top = readRect.y; srcRect.bottom = readRect.y + readRect.height; RECT dstRect; dstRect.left = drawRect.x; dstRect.right = drawRect.x + drawRect.width; dstRect.top = drawRect.y; dstRect.bottom = drawRect.y + drawRect.height; HRESULT result = mDevice->StretchRect(readSurface, &srcRect, drawSurface, &dstRect, D3DTEXF_NONE); readSurface->Release(); drawSurface->Release(); if (FAILED(result)) { ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); return false; } } if (blitDepthStencil) { gl::Renderbuffer *readBuffer = readFramebuffer->getDepthOrStencilbuffer(); gl::Renderbuffer *drawBuffer = drawFramebuffer->getDepthOrStencilbuffer(); RenderTarget9 *readDepthStencil = NULL; RenderTarget9 *drawDepthStencil = NULL; IDirect3DSurface9* readSurface = NULL; IDirect3DSurface9* drawSurface = NULL; if (readBuffer) { readDepthStencil = RenderTarget9::makeRenderTarget9(readBuffer->getDepthStencil()); } if (drawBuffer) { drawDepthStencil = RenderTarget9::makeRenderTarget9(drawBuffer->getDepthStencil()); } if (readDepthStencil) { readSurface = readDepthStencil->getSurface(); } if (drawDepthStencil) { drawSurface = drawDepthStencil->getSurface(); } if (!readSurface || !drawSurface) { ERR("Failed to retrieve the render target."); return gl::error(GL_OUT_OF_MEMORY, false); } HRESULT result = mDevice->StretchRect(readSurface, NULL, drawSurface, NULL, D3DTEXF_NONE); readSurface->Release(); drawSurface->Release(); if (FAILED(result)) { ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result); return false; } } return true; } void Renderer9::readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels) { RenderTarget9 *renderTarget = NULL; IDirect3DSurface9 *surface = NULL; gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer(0); if (colorbuffer) { renderTarget = RenderTarget9::makeRenderTarget9(colorbuffer->getRenderTarget()); } if (renderTarget) { surface = renderTarget->getSurface(); } if (!surface) { // context must be lost return; } D3DSURFACE_DESC desc; surface->GetDesc(&desc); if (desc.MultiSampleType != D3DMULTISAMPLE_NONE) { UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target surface->Release(); return gl::error(GL_OUT_OF_MEMORY); } HRESULT result; IDirect3DSurface9 *systemSurface = NULL; bool directToPixels = !packReverseRowOrder && packAlignment <= 4 && getShareHandleSupport() && x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height && desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE; if (directToPixels) { // Use the pixels ptr as a shared handle to write directly into client's memory result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, &pixels); if (FAILED(result)) { // Try again without the shared handle directToPixels = false; } } if (!directToPixels) { result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL); if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); surface->Release(); return gl::error(GL_OUT_OF_MEMORY); } } result = mDevice->GetRenderTargetData(surface, systemSurface); surface->Release(); surface = NULL; if (FAILED(result)) { systemSurface->Release(); // It turns out that D3D will sometimes produce more error // codes than those documented. if (d3d9::isDeviceLostError(result)) { notifyDeviceLost(); return gl::error(GL_OUT_OF_MEMORY); } else { UNREACHABLE(); return; } } if (directToPixels) { systemSurface->Release(); return; } RECT rect; rect.left = gl::clamp(x, 0L, static_cast<LONG>(desc.Width)); rect.top = gl::clamp(y, 0L, static_cast<LONG>(desc.Height)); rect.right = gl::clamp(x + width, 0L, static_cast<LONG>(desc.Width)); rect.bottom = gl::clamp(y + height, 0L, static_cast<LONG>(desc.Height)); D3DLOCKED_RECT lock; result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY); if (FAILED(result)) { UNREACHABLE(); systemSurface->Release(); return; // No sensible error to generate } unsigned char *dest = (unsigned char*)pixels; unsigned short *dest16 = (unsigned short*)pixels; unsigned char *source; int inputPitch; if (packReverseRowOrder) { source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1); inputPitch = -lock.Pitch; } else { source = (unsigned char*)lock.pBits; inputPitch = lock.Pitch; } unsigned int fastPixelSize = 0; if (desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE) { fastPixelSize = 4; } else if ((desc.Format == D3DFMT_A4R4G4B4 && format == GL_BGRA_EXT && type == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT) || (desc.Format == D3DFMT_A1R5G5B5 && format == GL_BGRA_EXT && type == GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT)) { fastPixelSize = 2; } else if (desc.Format == D3DFMT_A16B16G16R16F && format == GL_RGBA && type == GL_HALF_FLOAT_OES) { fastPixelSize = 8; } else if (desc.Format == D3DFMT_A32B32G32R32F && format == GL_RGBA && type == GL_FLOAT) { fastPixelSize = 16; } for (int j = 0; j < rect.bottom - rect.top; j++) { if (fastPixelSize != 0) { // Fast path for formats which require no translation: // D3DFMT_A8R8G8B8 to BGRA/UNSIGNED_BYTE // D3DFMT_A4R4G4B4 to BGRA/UNSIGNED_SHORT_4_4_4_4_REV_EXT // D3DFMT_A1R5G5B5 to BGRA/UNSIGNED_SHORT_1_5_5_5_REV_EXT // D3DFMT_A16B16G16R16F to RGBA/HALF_FLOAT_OES // D3DFMT_A32B32G32R32F to RGBA/FLOAT // // Note that buffers with no alpha go through the slow path below. memcpy(dest + j * outputPitch, source + j * inputPitch, (rect.right - rect.left) * fastPixelSize); continue; } else if (desc.Format == D3DFMT_A8R8G8B8 && format == GL_RGBA && type == GL_UNSIGNED_BYTE) { // Fast path for swapping red with blue for (int i = 0; i < rect.right - rect.left; i++) { unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); *(unsigned int*)(dest + 4 * i + j * outputPitch) = (argb & 0xFF00FF00) | // Keep alpha and green (argb & 0x00FF0000) >> 16 | // Move red to blue (argb & 0x000000FF) << 16; // Move blue to red } continue; } for (int i = 0; i < rect.right - rect.left; i++) { float r; float g; float b; float a; switch (desc.Format) { case D3DFMT_R5G6B5: { unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch); a = 1.0f; b = (rgb & 0x001F) * (1.0f / 0x001F); g = (rgb & 0x07E0) * (1.0f / 0x07E0); r = (rgb & 0xF800) * (1.0f / 0xF800); } break; case D3DFMT_A1R5G5B5: { unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch); a = (argb & 0x8000) ? 1.0f : 0.0f; b = (argb & 0x001F) * (1.0f / 0x001F); g = (argb & 0x03E0) * (1.0f / 0x03E0); r = (argb & 0x7C00) * (1.0f / 0x7C00); } break; case D3DFMT_A8R8G8B8: { unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); a = (argb & 0xFF000000) * (1.0f / 0xFF000000); b = (argb & 0x000000FF) * (1.0f / 0x000000FF); g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00); r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000); } break; case D3DFMT_X8R8G8B8: { unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch); a = 1.0f; b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF); g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00); r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000); } break; case D3DFMT_A2R10G10B10: { unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch); a = (argb & 0xC0000000) * (1.0f / 0xC0000000); b = (argb & 0x000003FF) * (1.0f / 0x000003FF); g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00); r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000); } break; case D3DFMT_A32B32G32R32F: { // float formats in D3D are stored rgba, rather than the other way round r = *((float*)(source + 16 * i + j * inputPitch) + 0); g = *((float*)(source + 16 * i + j * inputPitch) + 1); b = *((float*)(source + 16 * i + j * inputPitch) + 2); a = *((float*)(source + 16 * i + j * inputPitch) + 3); } break; case D3DFMT_A16B16G16R16F: { // float formats in D3D are stored rgba, rather than the other way round r = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 0)); g = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 1)); b = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 2)); a = gl::float16ToFloat32(*((unsigned short*)(source + 8 * i + j * inputPitch) + 3)); } break; default: UNIMPLEMENTED(); // FIXME UNREACHABLE(); return; } switch (format) { case GL_RGBA: switch (type) { case GL_UNSIGNED_BYTE: dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); break; default: UNREACHABLE(); } break; case GL_BGRA_EXT: switch (type) { case GL_UNSIGNED_BYTE: dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f); dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f); dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f); break; case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT: // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section // this type is packed as follows: // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 // -------------------------------------------------------------------------------- // | 4th | 3rd | 2nd | 1st component | // -------------------------------------------------------------------------------- // in the case of BGRA_EXT, B is the first component, G the second, and so forth. dest16[i + j * outputPitch / sizeof(unsigned short)] = ((unsigned short)(15 * a + 0.5f) << 12)| ((unsigned short)(15 * r + 0.5f) << 8) | ((unsigned short)(15 * g + 0.5f) << 4) | ((unsigned short)(15 * b + 0.5f) << 0); break; case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT: // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section // this type is packed as follows: // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 // -------------------------------------------------------------------------------- // | 4th | 3rd | 2nd | 1st component | // -------------------------------------------------------------------------------- // in the case of BGRA_EXT, B is the first component, G the second, and so forth. dest16[i + j * outputPitch / sizeof(unsigned short)] = ((unsigned short)( a + 0.5f) << 15) | ((unsigned short)(31 * r + 0.5f) << 10) | ((unsigned short)(31 * g + 0.5f) << 5) | ((unsigned short)(31 * b + 0.5f) << 0); break; default: UNREACHABLE(); } break; case GL_RGB: switch (type) { case GL_UNSIGNED_SHORT_5_6_5: dest16[i + j * outputPitch / sizeof(unsigned short)] = ((unsigned short)(31 * b + 0.5f) << 0) | ((unsigned short)(63 * g + 0.5f) << 5) | ((unsigned short)(31 * r + 0.5f) << 11); break; case GL_UNSIGNED_BYTE: dest[3 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f); dest[3 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f); dest[3 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f); break; default: UNREACHABLE(); } break; default: UNREACHABLE(); } } } systemSurface->UnlockRect(); systemSurface->Release(); } RenderTarget *Renderer9::createRenderTarget(SwapChain *swapChain, bool depth) { SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain); IDirect3DSurface9 *surface = NULL; if (depth) { surface = swapChain9->getDepthStencil(); } else { surface = swapChain9->getRenderTarget(); } RenderTarget9 *renderTarget = new RenderTarget9(this, surface); return renderTarget; } RenderTarget *Renderer9::createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth) { RenderTarget9 *renderTarget = new RenderTarget9(this, width, height, format, samples); return renderTarget; } ShaderExecutable *Renderer9::loadExecutable(const void *function, size_t length, rx::ShaderType type) { ShaderExecutable9 *executable = NULL; switch (type) { case rx::SHADER_VERTEX: { IDirect3DVertexShader9 *vshader = createVertexShader((DWORD*)function, length); if (vshader) { executable = new ShaderExecutable9(function, length, vshader); } } break; case rx::SHADER_PIXEL: { IDirect3DPixelShader9 *pshader = createPixelShader((DWORD*)function, length); if (pshader) { executable = new ShaderExecutable9(function, length, pshader); } } break; default: UNREACHABLE(); break; } return executable; } ShaderExecutable *Renderer9::compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type) { const char *profile = NULL; switch (type) { case rx::SHADER_VERTEX: profile = getMajorShaderModel() >= 3 ? "vs_3_0" : "vs_2_0"; break; case rx::SHADER_PIXEL: profile = getMajorShaderModel() >= 3 ? "ps_3_0" : "ps_2_0"; break; default: UNREACHABLE(); return NULL; } ID3DBlob *binary = (ID3DBlob*)compileToBinary(infoLog, shaderHLSL, profile, ANGLE_COMPILE_OPTIMIZATION_LEVEL, true); if (!binary) return NULL; ShaderExecutable *executable = loadExecutable(binary->GetBufferPointer(), binary->GetBufferSize(), type); binary->Release(); return executable; } bool Renderer9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) { return mBlit->boxFilter(source, dest); } D3DPOOL Renderer9::getTexturePool(DWORD usage) const { if (mD3d9Ex != NULL) { return D3DPOOL_DEFAULT; } else { if (!(usage & (D3DUSAGE_DEPTHSTENCIL | D3DUSAGE_RENDERTARGET))) { return D3DPOOL_MANAGED; } } return D3DPOOL_DEFAULT; } bool Renderer9::copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged) { if (source && dest) { HRESULT result = D3DERR_OUTOFVIDEOMEMORY; if (fromManaged) { D3DSURFACE_DESC desc; source->GetDesc(&desc); IDirect3DSurface9 *surf = 0; result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surf, NULL); if (SUCCEEDED(result)) { Image9::copyLockableSurfaces(surf, source); result = mDevice->UpdateSurface(surf, NULL, dest, NULL); surf->Release(); } } else { endScene(); result = mDevice->StretchRect(source, NULL, dest, NULL, D3DTEXF_NONE); } if (FAILED(result)) { ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY); return false; } } return true; } Image *Renderer9::createImage() { return new Image9(); } void Renderer9::generateMipmap(Image *dest, Image *src) { Image9 *src9 = Image9::makeImage9(src); Image9 *dst9 = Image9::makeImage9(dest); Image9::generateMipmap(dst9, src9); } TextureStorage *Renderer9::createTextureStorage2D(SwapChain *swapChain) { SwapChain9 *swapChain9 = SwapChain9::makeSwapChain9(swapChain); return new TextureStorage9_2D(this, swapChain9); } TextureStorage *Renderer9::createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height) { return new TextureStorage9_2D(this, levels, internalformat, usage, forceRenderable, width, height); } TextureStorage *Renderer9::createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size) { return new TextureStorage9_Cube(this, levels, internalformat, usage, forceRenderable, size); } bool Renderer9::getLUID(LUID *adapterLuid) const { adapterLuid->HighPart = 0; adapterLuid->LowPart = 0; if (mD3d9Ex) { mD3d9Ex->GetAdapterLUID(mAdapter, adapterLuid); return true; } return false; } }
C++
#include "precompiled.h" // // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // BufferStorage11.cpp Defines the BufferStorage11 class. #include "libGLESv2/renderer/BufferStorage11.h" #include "libGLESv2/main.h" #include "libGLESv2/renderer/Renderer11.h" namespace rx { BufferStorage11::BufferStorage11(Renderer11 *renderer) { mRenderer = renderer; mStagingBuffer = NULL; mStagingBufferSize = 0; mBuffer = NULL; mBufferSize = 0; mSize = 0; mResolvedData = NULL; mResolvedDataSize = 0; mResolvedDataValid = false; mReadUsageCount = 0; mWriteUsageCount = 0; } BufferStorage11::~BufferStorage11() { if (mStagingBuffer) { mStagingBuffer->Release(); mStagingBuffer = NULL; } if (mBuffer) { mBuffer->Release(); mBuffer = NULL; } if (mResolvedData) { free(mResolvedData); mResolvedData = NULL; } } BufferStorage11 *BufferStorage11::makeBufferStorage11(BufferStorage *bufferStorage) { ASSERT(HAS_DYNAMIC_TYPE(BufferStorage11*, bufferStorage)); return static_cast<BufferStorage11*>(bufferStorage); } void *BufferStorage11::getData() { if (!mResolvedDataValid) { ID3D11Device *device = mRenderer->getDevice(); ID3D11DeviceContext *context = mRenderer->getDeviceContext(); HRESULT result; if (!mStagingBuffer || mStagingBufferSize < mBufferSize) { if (mStagingBuffer) { mStagingBuffer->Release(); mStagingBuffer = NULL; mStagingBufferSize = 0; } D3D11_BUFFER_DESC bufferDesc; bufferDesc.ByteWidth = mSize; bufferDesc.Usage = D3D11_USAGE_STAGING; bufferDesc.BindFlags = 0; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; bufferDesc.MiscFlags = 0; bufferDesc.StructureByteStride = 0; result = device->CreateBuffer(&bufferDesc, NULL, &mStagingBuffer); if (FAILED(result)) { return gl::error(GL_OUT_OF_MEMORY, (void*)NULL); } mStagingBufferSize = bufferDesc.ByteWidth; } if (!mResolvedData || mResolvedDataSize < mBufferSize) { free(mResolvedData); mResolvedData = malloc(mSize); mResolvedDataSize = mSize; } D3D11_BOX srcBox; srcBox.left = 0; srcBox.right = mSize; srcBox.top = 0; srcBox.bottom = 1; srcBox.front = 0; srcBox.back = 1; context->CopySubresourceRegion(mStagingBuffer, 0, 0, 0, 0, mBuffer, 0, &srcBox); D3D11_MAPPED_SUBRESOURCE mappedResource; result = context->Map(mStagingBuffer, 0, D3D11_MAP_READ, 0, &mappedResource); if (FAILED(result)) { return gl::error(GL_OUT_OF_MEMORY, (void*)NULL); } memcpy(mResolvedData, mappedResource.pData, mSize); context->Unmap(mStagingBuffer, 0); mResolvedDataValid = true; } mReadUsageCount = 0; return mResolvedData; } void BufferStorage11::setData(const void* data, unsigned int size, unsigned int offset) { ID3D11Device *device = mRenderer->getDevice(); ID3D11DeviceContext *context = mRenderer->getDeviceContext(); HRESULT result; unsigned int requiredBufferSize = size + offset; unsigned int requiredStagingSize = size; bool directInitialization = offset == 0 && (!mBuffer || mBufferSize < size + offset); if (!directInitialization) { if (!mStagingBuffer || mStagingBufferSize < requiredStagingSize) { if (mStagingBuffer) { mStagingBuffer->Release(); mStagingBuffer = NULL; mStagingBufferSize = 0; } D3D11_BUFFER_DESC bufferDesc; bufferDesc.ByteWidth = size; bufferDesc.Usage = D3D11_USAGE_STAGING; bufferDesc.BindFlags = 0; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE; bufferDesc.MiscFlags = 0; bufferDesc.StructureByteStride = 0; D3D11_SUBRESOURCE_DATA initialData; initialData.pSysMem = data; initialData.SysMemPitch = size; initialData.SysMemSlicePitch = 0; result = device->CreateBuffer(&bufferDesc, &initialData, &mStagingBuffer); if (FAILED(result)) { return gl::error(GL_OUT_OF_MEMORY); } mStagingBufferSize = size; } else { D3D11_MAPPED_SUBRESOURCE mappedResource; result = context->Map(mStagingBuffer, 0, D3D11_MAP_WRITE, 0, &mappedResource); if (FAILED(result)) { return gl::error(GL_OUT_OF_MEMORY); } memcpy(mappedResource.pData, data, size); context->Unmap(mStagingBuffer, 0); } } if (!mBuffer || mBufferSize < size + offset) { D3D11_BUFFER_DESC bufferDesc; bufferDesc.ByteWidth = requiredBufferSize; bufferDesc.Usage = D3D11_USAGE_DEFAULT; bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER | D3D11_BIND_INDEX_BUFFER; bufferDesc.CPUAccessFlags = 0; bufferDesc.MiscFlags = 0; bufferDesc.StructureByteStride = 0; if (directInitialization) { // Since the data will fill the entire buffer (being larger than the initial size and having // no offset), the buffer can be initialized with the data so no staging buffer is required // No longer need the old buffer if (mBuffer) { mBuffer->Release(); mBuffer = NULL; mBufferSize = 0; } D3D11_SUBRESOURCE_DATA initialData; initialData.pSysMem = data; initialData.SysMemPitch = size; initialData.SysMemSlicePitch = 0; result = device->CreateBuffer(&bufferDesc, &initialData, &mBuffer); if (FAILED(result)) { return gl::error(GL_OUT_OF_MEMORY); } } else if (mBuffer && offset > 0) { // If offset is greater than zero and the buffer is non-null, need to preserve the data from // the old buffer up to offset ID3D11Buffer *newBuffer = NULL; result = device->CreateBuffer(&bufferDesc, NULL, &newBuffer); if (FAILED(result)) { return gl::error(GL_OUT_OF_MEMORY); } D3D11_BOX srcBox; srcBox.left = 0; srcBox.right = std::min(offset, mBufferSize); srcBox.top = 0; srcBox.bottom = 1; srcBox.front = 0; srcBox.back = 1; context->CopySubresourceRegion(newBuffer, 0, 0, 0, 0, mBuffer, 0, &srcBox); mBuffer->Release(); mBuffer = newBuffer; } else { // Simple case, nothing needs to be copied from the old buffer to the new one, just create // a new buffer // No longer need the old buffer if (mBuffer) { mBuffer->Release(); mBuffer = NULL; mBufferSize = 0; } // Create a new buffer for data storage result = device->CreateBuffer(&bufferDesc, NULL, &mBuffer); if (FAILED(result)) { return gl::error(GL_OUT_OF_MEMORY); } } updateSerial(); mBufferSize = bufferDesc.ByteWidth; } if (!directInitialization) { ASSERT(mStagingBuffer && mStagingBufferSize >= requiredStagingSize); // Data is already put into the staging buffer, copy it over to the data buffer D3D11_BOX srcBox; srcBox.left = 0; srcBox.right = size; srcBox.top = 0; srcBox.bottom = 1; srcBox.front = 0; srcBox.back = 1; context->CopySubresourceRegion(mBuffer, 0, offset, 0, 0, mStagingBuffer, 0, &srcBox); } mSize = std::max(mSize, offset + size); mWriteUsageCount = 0; mResolvedDataValid = false; } void BufferStorage11::clear() { mResolvedDataValid = false; mSize = 0; } unsigned int BufferStorage11::getSize() const { return mSize; } bool BufferStorage11::supportsDirectBinding() const { return true; } void BufferStorage11::markBufferUsage() { mReadUsageCount++; mWriteUsageCount++; static const unsigned int usageLimit = 5; if (mReadUsageCount > usageLimit && mResolvedData) { free(mResolvedData); mResolvedData = NULL; mResolvedDataSize = 0; mResolvedDataValid = false; } if (mReadUsageCount > usageLimit && mWriteUsageCount > usageLimit && mStagingBuffer) { mStagingBuffer->Release(); mStagingBuffer = NULL; mStagingBufferSize = 0; } } ID3D11Buffer *BufferStorage11::getBuffer() const { return mBuffer; } }
C++
// // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // BufferStorage9.h Defines the BufferStorage9 class. #ifndef LIBGLESV2_RENDERER_BUFFERSTORAGE9_H_ #define LIBGLESV2_RENDERER_BUFFERSTORAGE9_H_ #include "libGLESv2/renderer/BufferStorage.h" namespace rx { class BufferStorage9 : public BufferStorage { public: BufferStorage9(); virtual ~BufferStorage9(); static BufferStorage9 *makeBufferStorage9(BufferStorage *bufferStorage); virtual void *getData(); virtual void setData(const void* data, unsigned int size, unsigned int offset); virtual void clear(); virtual unsigned int getSize() const; virtual bool supportsDirectBinding() const; private: DISALLOW_COPY_AND_ASSIGN(BufferStorage9); void *mMemory; unsigned int mAllocatedSize; unsigned int mSize; }; } #endif // LIBGLESV2_RENDERER_BUFFERSTORAGE9_H_
C++
#include "precompiled.h" // // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // VertexBuffer9.cpp: Defines the D3D9 VertexBuffer implementation. #include "libGLESv2/renderer/VertexBuffer9.h" #include "libGLESv2/renderer/vertexconversion.h" #include "libGLESv2/renderer/BufferStorage.h" #include "libGLESv2/Context.h" #include "libGLESv2/renderer/Renderer9.h" #include "libGLESv2/Buffer.h" namespace rx { bool VertexBuffer9::mTranslationsInitialized = false; VertexBuffer9::FormatConverter VertexBuffer9::mFormatConverters[NUM_GL_VERTEX_ATTRIB_TYPES][2][4]; VertexBuffer9::VertexBuffer9(rx::Renderer9 *const renderer) : mRenderer(renderer) { mVertexBuffer = NULL; mBufferSize = 0; mDynamicUsage = false; if (!mTranslationsInitialized) { initializeTranslations(renderer->getCapsDeclTypes()); mTranslationsInitialized = true; } } VertexBuffer9::~VertexBuffer9() { if (mVertexBuffer) { mVertexBuffer->Release(); mVertexBuffer = NULL; } } bool VertexBuffer9::initialize(unsigned int size, bool dynamicUsage) { if (mVertexBuffer) { mVertexBuffer->Release(); mVertexBuffer = NULL; } updateSerial(); if (size > 0) { DWORD flags = D3DUSAGE_WRITEONLY; if (dynamicUsage) { flags |= D3DUSAGE_DYNAMIC; } HRESULT result = mRenderer->createVertexBuffer(size, flags, &mVertexBuffer); if (FAILED(result)) { ERR("Out of memory allocating a vertex buffer of size %lu.", size); return false; } } mBufferSize = size; mDynamicUsage = dynamicUsage; return true; } VertexBuffer9 *VertexBuffer9::makeVertexBuffer9(VertexBuffer *vertexBuffer) { ASSERT(HAS_DYNAMIC_TYPE(VertexBuffer9*, vertexBuffer)); return static_cast<VertexBuffer9*>(vertexBuffer); } bool VertexBuffer9::storeVertexAttributes(const gl::VertexAttribute &attrib, GLint start, GLsizei count, GLsizei instances, unsigned int offset) { if (mVertexBuffer) { gl::Buffer *buffer = attrib.mBoundBuffer.get(); int inputStride = attrib.stride(); int elementSize = attrib.typeSize(); const FormatConverter &converter = formatConverter(attrib); DWORD lockFlags = mDynamicUsage ? D3DLOCK_NOOVERWRITE : 0; void *mapPtr = NULL; HRESULT result = mVertexBuffer->Lock(offset, spaceRequired(attrib, count, instances), &mapPtr, lockFlags); if (FAILED(result)) { ERR("Lock failed with error 0x%08x", result); return false; } const char *input = NULL; if (buffer) { BufferStorage *storage = buffer->getStorage(); input = static_cast<const char*>(storage->getData()) + static_cast<int>(attrib.mOffset); } else { input = static_cast<const char*>(attrib.mPointer); } if (instances == 0 || attrib.mDivisor == 0) { input += inputStride * start; } if (converter.identity && inputStride == elementSize) { memcpy(mapPtr, input, count * inputStride); } else { converter.convertArray(input, inputStride, count, mapPtr); } mVertexBuffer->Unlock(); return true; } else { ERR("Vertex buffer not initialized."); return false; } } bool VertexBuffer9::storeRawData(const void* data, unsigned int size, unsigned int offset) { if (mVertexBuffer) { DWORD lockFlags = mDynamicUsage ? D3DLOCK_NOOVERWRITE : 0; void *mapPtr = NULL; HRESULT result = mVertexBuffer->Lock(offset, size, &mapPtr, lockFlags); if (FAILED(result)) { ERR("Lock failed with error 0x%08x", result); return false; } memcpy(mapPtr, data, size); mVertexBuffer->Unlock(); return true; } else { ERR("Vertex buffer not initialized."); return false; } } unsigned int VertexBuffer9::getSpaceRequired(const gl::VertexAttribute &attrib, GLsizei count, GLsizei instances) const { return spaceRequired(attrib, count, instances); } bool VertexBuffer9::requiresConversion(const gl::VertexAttribute &attrib) const { return formatConverter(attrib).identity; } unsigned int VertexBuffer9::getVertexSize(const gl::VertexAttribute &attrib) const { return spaceRequired(attrib, 1, 0); } D3DDECLTYPE VertexBuffer9::getDeclType(const gl::VertexAttribute &attrib) const { return formatConverter(attrib).d3dDeclType; } unsigned int VertexBuffer9::getBufferSize() const { return mBufferSize; } bool VertexBuffer9::setBufferSize(unsigned int size) { if (size > mBufferSize) { return initialize(size, mDynamicUsage); } else { return true; } } bool VertexBuffer9::discard() { if (mVertexBuffer) { void *dummy; HRESULT result; result = mVertexBuffer->Lock(0, 1, &dummy, D3DLOCK_DISCARD); if (FAILED(result)) { ERR("Discard lock failed with error 0x%08x", result); return false; } result = mVertexBuffer->Unlock(); if (FAILED(result)) { ERR("Discard unlock failed with error 0x%08x", result); return false; } return true; } else { ERR("Vertex buffer not initialized."); return false; } } IDirect3DVertexBuffer9 * VertexBuffer9::getBuffer() const { return mVertexBuffer; } // Mapping from OpenGL-ES vertex attrib type to D3D decl type: // // BYTE SHORT (Cast) // BYTE-norm FLOAT (Normalize) (can't be exactly represented as SHORT-norm) // UNSIGNED_BYTE UBYTE4 (Identity) or SHORT (Cast) // UNSIGNED_BYTE-norm UBYTE4N (Identity) or FLOAT (Normalize) // SHORT SHORT (Identity) // SHORT-norm SHORT-norm (Identity) or FLOAT (Normalize) // UNSIGNED_SHORT FLOAT (Cast) // UNSIGNED_SHORT-norm USHORT-norm (Identity) or FLOAT (Normalize) // FIXED (not in WebGL) FLOAT (FixedToFloat) // FLOAT FLOAT (Identity) // GLToCType maps from GL type (as GLenum) to the C typedef. template <GLenum GLType> struct GLToCType { }; template <> struct GLToCType<GL_BYTE> { typedef GLbyte type; }; template <> struct GLToCType<GL_UNSIGNED_BYTE> { typedef GLubyte type; }; template <> struct GLToCType<GL_SHORT> { typedef GLshort type; }; template <> struct GLToCType<GL_UNSIGNED_SHORT> { typedef GLushort type; }; template <> struct GLToCType<GL_FIXED> { typedef GLuint type; }; template <> struct GLToCType<GL_FLOAT> { typedef GLfloat type; }; // This differs from D3DDECLTYPE in that it is unsized. (Size expansion is applied last.) enum D3DVertexType { D3DVT_FLOAT, D3DVT_SHORT, D3DVT_SHORT_NORM, D3DVT_UBYTE, D3DVT_UBYTE_NORM, D3DVT_USHORT_NORM }; // D3DToCType maps from D3D vertex type (as enum D3DVertexType) to the corresponding C type. template <unsigned int D3DType> struct D3DToCType { }; template <> struct D3DToCType<D3DVT_FLOAT> { typedef float type; }; template <> struct D3DToCType<D3DVT_SHORT> { typedef short type; }; template <> struct D3DToCType<D3DVT_SHORT_NORM> { typedef short type; }; template <> struct D3DToCType<D3DVT_UBYTE> { typedef unsigned char type; }; template <> struct D3DToCType<D3DVT_UBYTE_NORM> { typedef unsigned char type; }; template <> struct D3DToCType<D3DVT_USHORT_NORM> { typedef unsigned short type; }; // Encode the type/size combinations that D3D permits. For each type/size it expands to a widener that will provide the appropriate final size. template <unsigned int type, int size> struct WidenRule { }; template <int size> struct WidenRule<D3DVT_FLOAT, size> : NoWiden<size> { }; template <int size> struct WidenRule<D3DVT_SHORT, size> : WidenToEven<size> { }; template <int size> struct WidenRule<D3DVT_SHORT_NORM, size> : WidenToEven<size> { }; template <int size> struct WidenRule<D3DVT_UBYTE, size> : WidenToFour<size> { }; template <int size> struct WidenRule<D3DVT_UBYTE_NORM, size> : WidenToFour<size> { }; template <int size> struct WidenRule<D3DVT_USHORT_NORM, size> : WidenToEven<size> { }; // VertexTypeFlags encodes the D3DCAPS9::DeclType flag and vertex declaration flag for each D3D vertex type & size combination. template <unsigned int d3dtype, int size> struct VertexTypeFlags { }; template <unsigned int _capflag, unsigned int _declflag> struct VertexTypeFlagsHelper { enum { capflag = _capflag }; enum { declflag = _declflag }; }; template <> struct VertexTypeFlags<D3DVT_FLOAT, 1> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT1> { }; template <> struct VertexTypeFlags<D3DVT_FLOAT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT2> { }; template <> struct VertexTypeFlags<D3DVT_FLOAT, 3> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT3> { }; template <> struct VertexTypeFlags<D3DVT_FLOAT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_FLOAT4> { }; template <> struct VertexTypeFlags<D3DVT_SHORT, 2> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT2> { }; template <> struct VertexTypeFlags<D3DVT_SHORT, 4> : VertexTypeFlagsHelper<0, D3DDECLTYPE_SHORT4> { }; template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT2N, D3DDECLTYPE_SHORT2N> { }; template <> struct VertexTypeFlags<D3DVT_SHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_SHORT4N, D3DDECLTYPE_SHORT4N> { }; template <> struct VertexTypeFlags<D3DVT_UBYTE, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4, D3DDECLTYPE_UBYTE4> { }; template <> struct VertexTypeFlags<D3DVT_UBYTE_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_UBYTE4N, D3DDECLTYPE_UBYTE4N> { }; template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 2> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT2N, D3DDECLTYPE_USHORT2N> { }; template <> struct VertexTypeFlags<D3DVT_USHORT_NORM, 4> : VertexTypeFlagsHelper<D3DDTCAPS_USHORT4N, D3DDECLTYPE_USHORT4N> { }; // VertexTypeMapping maps GL type & normalized flag to preferred and fallback D3D vertex types (as D3DVertexType enums). template <GLenum GLtype, bool normalized> struct VertexTypeMapping { }; template <D3DVertexType Preferred, D3DVertexType Fallback = Preferred> struct VertexTypeMappingBase { enum { preferred = Preferred }; enum { fallback = Fallback }; }; template <> struct VertexTypeMapping<GL_BYTE, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Cast template <> struct VertexTypeMapping<GL_BYTE, true> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Normalize template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, false> : VertexTypeMappingBase<D3DVT_UBYTE, D3DVT_FLOAT> { }; // Identity, Cast template <> struct VertexTypeMapping<GL_UNSIGNED_BYTE, true> : VertexTypeMappingBase<D3DVT_UBYTE_NORM, D3DVT_FLOAT> { }; // Identity, Normalize template <> struct VertexTypeMapping<GL_SHORT, false> : VertexTypeMappingBase<D3DVT_SHORT> { }; // Identity template <> struct VertexTypeMapping<GL_SHORT, true> : VertexTypeMappingBase<D3DVT_SHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, false> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Cast template <> struct VertexTypeMapping<GL_UNSIGNED_SHORT, true> : VertexTypeMappingBase<D3DVT_USHORT_NORM, D3DVT_FLOAT> { }; // Cast, Normalize template <bool normalized> struct VertexTypeMapping<GL_FIXED, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // FixedToFloat template <bool normalized> struct VertexTypeMapping<GL_FLOAT, normalized> : VertexTypeMappingBase<D3DVT_FLOAT> { }; // Identity // Given a GL type & norm flag and a D3D type, ConversionRule provides the type conversion rule (Cast, Normalize, Identity, FixedToFloat). // The conversion rules themselves are defined in vertexconversion.h. // Almost all cases are covered by Cast (including those that are actually Identity since Cast<T,T> knows it's an identity mapping). template <GLenum fromType, bool normalized, unsigned int toType> struct ConversionRule : Cast<typename GLToCType<fromType>::type, typename D3DToCType<toType>::type> { }; // All conversions from normalized types to float use the Normalize operator. template <GLenum fromType> struct ConversionRule<fromType, true, D3DVT_FLOAT> : Normalize<typename GLToCType<fromType>::type> { }; // Use a full specialization for this so that it preferentially matches ahead of the generic normalize-to-float rules. template <> struct ConversionRule<GL_FIXED, true, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { }; template <> struct ConversionRule<GL_FIXED, false, D3DVT_FLOAT> : FixedToFloat<GLint, 16> { }; // A 2-stage construction is used for DefaultVertexValues because float must use SimpleDefaultValues (i.e. 0/1) // whether it is normalized or not. template <class T, bool normalized> struct DefaultVertexValuesStage2 { }; template <class T> struct DefaultVertexValuesStage2<T, true> : NormalizedDefaultValues<T> { }; template <class T> struct DefaultVertexValuesStage2<T, false> : SimpleDefaultValues<T> { }; // Work out the default value rule for a D3D type (expressed as the C type) and template <class T, bool normalized> struct DefaultVertexValues : DefaultVertexValuesStage2<T, normalized> { }; template <bool normalized> struct DefaultVertexValues<float, normalized> : SimpleDefaultValues<float> { }; // Policy rules for use with Converter, to choose whether to use the preferred or fallback conversion. // The fallback conversion produces an output that all D3D9 devices must support. template <class T> struct UsePreferred { enum { type = T::preferred }; }; template <class T> struct UseFallback { enum { type = T::fallback }; }; // Converter ties it all together. Given an OpenGL type/norm/size and choice of preferred/fallback conversion, // it provides all the members of the appropriate VertexDataConverter, the D3DCAPS9::DeclTypes flag in cap flag // and the D3DDECLTYPE member needed for the vertex declaration in declflag. template <GLenum fromType, bool normalized, int size, template <class T> class PreferenceRule> struct Converter : VertexDataConverter<typename GLToCType<fromType>::type, WidenRule<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type, size>, ConversionRule<fromType, normalized, PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>, DefaultVertexValues<typename D3DToCType<PreferenceRule< VertexTypeMapping<fromType, normalized> >::type>::type, normalized > > { private: enum { d3dtype = PreferenceRule< VertexTypeMapping<fromType, normalized> >::type }; enum { d3dsize = WidenRule<d3dtype, size>::finalWidth }; public: enum { capflag = VertexTypeFlags<d3dtype, d3dsize>::capflag }; enum { declflag = VertexTypeFlags<d3dtype, d3dsize>::declflag }; }; // Initialize a TranslationInfo #define TRANSLATION(type, norm, size, preferred) \ { \ Converter<type, norm, size, preferred>::identity, \ Converter<type, norm, size, preferred>::finalSize, \ Converter<type, norm, size, preferred>::convertArray, \ static_cast<D3DDECLTYPE>(Converter<type, norm, size, preferred>::declflag) \ } #define TRANSLATION_FOR_TYPE_NORM_SIZE(type, norm, size) \ { \ Converter<type, norm, size, UsePreferred>::capflag, \ TRANSLATION(type, norm, size, UsePreferred), \ TRANSLATION(type, norm, size, UseFallback) \ } #define TRANSLATIONS_FOR_TYPE(type) \ { \ { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \ { TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, true, 4) }, \ } #define TRANSLATIONS_FOR_TYPE_NO_NORM(type) \ { \ { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \ { TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 1), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 2), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 3), TRANSLATION_FOR_TYPE_NORM_SIZE(type, false, 4) }, \ } const VertexBuffer9::TranslationDescription VertexBuffer9::mPossibleTranslations[NUM_GL_VERTEX_ATTRIB_TYPES][2][4] = // [GL types as enumerated by typeIndex()][normalized][size-1] { TRANSLATIONS_FOR_TYPE(GL_BYTE), TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_BYTE), TRANSLATIONS_FOR_TYPE(GL_SHORT), TRANSLATIONS_FOR_TYPE(GL_UNSIGNED_SHORT), TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FIXED), TRANSLATIONS_FOR_TYPE_NO_NORM(GL_FLOAT) }; void VertexBuffer9::initializeTranslations(DWORD declTypes) { for (unsigned int i = 0; i < NUM_GL_VERTEX_ATTRIB_TYPES; i++) { for (unsigned int j = 0; j < 2; j++) { for (unsigned int k = 0; k < 4; k++) { if (mPossibleTranslations[i][j][k].capsFlag == 0 || (declTypes & mPossibleTranslations[i][j][k].capsFlag) != 0) { mFormatConverters[i][j][k] = mPossibleTranslations[i][j][k].preferredConversion; } else { mFormatConverters[i][j][k] = mPossibleTranslations[i][j][k].fallbackConversion; } } } } } unsigned int VertexBuffer9::typeIndex(GLenum type) { switch (type) { case GL_BYTE: return 0; case GL_UNSIGNED_BYTE: return 1; case GL_SHORT: return 2; case GL_UNSIGNED_SHORT: return 3; case GL_FIXED: return 4; case GL_FLOAT: return 5; default: UNREACHABLE(); return 5; } } const VertexBuffer9::FormatConverter &VertexBuffer9::formatConverter(const gl::VertexAttribute &attribute) { return mFormatConverters[typeIndex(attribute.mType)][attribute.mNormalized][attribute.mSize - 1]; } unsigned int VertexBuffer9::spaceRequired(const gl::VertexAttribute &attrib, std::size_t count, GLsizei instances) { unsigned int elementSize = formatConverter(attrib).outputElementSize; if (instances == 0 || attrib.mDivisor == 0) { return elementSize * count; } else { return elementSize * ((instances + attrib.mDivisor - 1) / attrib.mDivisor); } } }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // SwapChain11.h: Defines a back-end specific class for the D3D11 swap chain. #ifndef LIBGLESV2_RENDERER_SWAPCHAIN11_H_ #define LIBGLESV2_RENDERER_SWAPCHAIN11_H_ #include "common/angleutils.h" #include "libGLESv2/renderer/SwapChain.h" namespace rx { class Renderer11; class SwapChain11 : public SwapChain { public: SwapChain11(Renderer11 *renderer, HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat); virtual ~SwapChain11(); EGLint resize(EGLint backbufferWidth, EGLint backbufferHeight); virtual EGLint reset(EGLint backbufferWidth, EGLint backbufferHeight, EGLint swapInterval); virtual EGLint swapRect(EGLint x, EGLint y, EGLint width, EGLint height); virtual void recreate(); virtual ID3D11Texture2D *getOffscreenTexture(); virtual ID3D11RenderTargetView *getRenderTarget(); virtual ID3D11ShaderResourceView *getRenderTargetShaderResource(); virtual ID3D11Texture2D *getDepthStencilTexture(); virtual ID3D11DepthStencilView *getDepthStencil(); EGLint getWidth() const { return mWidth; } EGLint getHeight() const { return mHeight; } static SwapChain11 *makeSwapChain11(SwapChain *swapChain); private: DISALLOW_COPY_AND_ASSIGN(SwapChain11); void release(); void initPassThroughResources(); void releaseOffscreenTexture(); EGLint resetOffscreenTexture(int backbufferWidth, int backbufferHeight); Renderer11 *mRenderer; EGLint mHeight; EGLint mWidth; bool mAppCreatedShareHandle; unsigned int mSwapInterval; bool mPassThroughResourcesInit; IDXGISwapChain *mSwapChain; ID3D11Texture2D *mBackBufferTexture; ID3D11RenderTargetView *mBackBufferRTView; ID3D11Texture2D *mOffscreenTexture; ID3D11RenderTargetView *mOffscreenRTView; ID3D11ShaderResourceView *mOffscreenSRView; ID3D11Texture2D *mDepthStencilTexture; ID3D11DepthStencilView *mDepthStencilDSView; ID3D11Buffer *mQuadVB; ID3D11SamplerState *mPassThroughSampler; ID3D11InputLayout *mPassThroughIL; ID3D11VertexShader *mPassThroughVS; ID3D11PixelShader *mPassThroughPS; }; } #endif // LIBGLESV2_RENDERER_SWAPCHAIN11_H_
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // ShaderExecutable.h: Defines a renderer-agnostic class to contain shader // executable implementation details. #ifndef LIBGLESV2_RENDERER_SHADEREXECUTABLE_H_ #define LIBGLESV2_RENDERER_SHADEREXECUTABLE_H_ #include "common/angleutils.h" namespace rx { class ShaderExecutable { public: ShaderExecutable(const void *function, size_t length) : mLength(length) { mFunction = new char[length]; memcpy(mFunction, function, length); } virtual ~ShaderExecutable() { delete mFunction; } void *getFunction() const { return mFunction; } size_t getLength() const { return mLength; } private: DISALLOW_COPY_AND_ASSIGN(ShaderExecutable); void *mFunction; const size_t mLength; }; } #endif // LIBGLESV2_RENDERER_SHADEREXECUTABLE9_H_
C++
// // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Fence.h: Defines the gl::Fence class, which supports the GL_NV_fence extension. #ifndef LIBGLESV2_FENCE_H_ #define LIBGLESV2_FENCE_H_ #include "common/angleutils.h" namespace rx { class Renderer; class FenceImpl; } namespace gl { class Fence { public: explicit Fence(rx::Renderer *renderer); virtual ~Fence(); GLboolean isFence(); void setFence(GLenum condition); GLboolean testFence(); void finishFence(); void getFenceiv(GLenum pname, GLint *params); private: DISALLOW_COPY_AND_ASSIGN(Fence); rx::FenceImpl *mFence; }; } #endif // LIBGLESV2_FENCE_H_
C++
// // Copyright (c) 2010-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #ifndef LIBGLESV2_UNIFORM_H_ #define LIBGLESV2_UNIFORM_H_ #include <string> #include <vector> #define GL_APICALL #include <GLES2/gl2.h> #include "common/debug.h" namespace gl { // Helper struct representing a single shader uniform struct Uniform { Uniform(GLenum type, GLenum precision, const std::string &name, unsigned int arraySize); ~Uniform(); bool isArray() const; unsigned int elementCount() const; const GLenum type; const GLenum precision; const std::string name; const unsigned int arraySize; unsigned char *data; bool dirty; int psRegisterIndex; int vsRegisterIndex; unsigned int registerCount; }; typedef std::vector<Uniform*> UniformArray; } #endif // LIBGLESV2_UNIFORM_H_
C++
#include "precompiled.h" // // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Fence.cpp: Implements the gl::Fence class, which supports the GL_NV_fence extension. #include "libGLESv2/Fence.h" #include "libGLESv2/renderer/FenceImpl.h" #include "libGLESv2/renderer/Renderer.h" namespace gl { Fence::Fence(rx::Renderer *renderer) { mFence = renderer->createFence(); } Fence::~Fence() { delete mFence; } GLboolean Fence::isFence() { return mFence->isFence(); } void Fence::setFence(GLenum condition) { mFence->setFence(condition); } GLboolean Fence::testFence() { return mFence->testFence(); } void Fence::finishFence() { mFence->finishFence(); } void Fence::getFenceiv(GLenum pname, GLint *params) { mFence->getFenceiv(pname, params); } }
C++
// // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Renderbuffer.h: Defines the wrapper class gl::Renderbuffer, as well as the // class hierarchy used to store its contents: RenderbufferStorage, Colorbuffer, // DepthStencilbuffer, Depthbuffer and Stencilbuffer. Implements GL renderbuffer // objects and related functionality. [OpenGL ES 2.0.24] section 4.4.3 page 108. #ifndef LIBGLESV2_RENDERBUFFER_H_ #define LIBGLESV2_RENDERBUFFER_H_ #define GL_APICALL #include <GLES2/gl2.h> #include "common/angleutils.h" #include "common/RefCountObject.h" namespace rx { class Renderer; class SwapChain; class RenderTarget; } namespace gl { class Texture2D; class TextureCubeMap; class Renderbuffer; class Colorbuffer; class DepthStencilbuffer; class RenderbufferInterface { public: RenderbufferInterface(); virtual ~RenderbufferInterface() {}; virtual void addProxyRef(const Renderbuffer *proxy); virtual void releaseProxy(const Renderbuffer *proxy); virtual rx::RenderTarget *getRenderTarget() = 0; virtual rx::RenderTarget *getDepthStencil() = 0; virtual GLsizei getWidth() const = 0; virtual GLsizei getHeight() const = 0; virtual GLenum getInternalFormat() const = 0; virtual GLenum getActualFormat() const = 0; virtual GLsizei getSamples() const = 0; GLuint getRedSize() const; GLuint getGreenSize() const; GLuint getBlueSize() const; GLuint getAlphaSize() const; GLuint getDepthSize() const; GLuint getStencilSize() const; virtual unsigned int getSerial() const = 0; private: DISALLOW_COPY_AND_ASSIGN(RenderbufferInterface); }; class RenderbufferTexture2D : public RenderbufferInterface { public: RenderbufferTexture2D(Texture2D *texture, GLenum target); virtual ~RenderbufferTexture2D(); void addProxyRef(const Renderbuffer *proxy); void releaseProxy(const Renderbuffer *proxy); rx::RenderTarget *getRenderTarget(); rx::RenderTarget *getDepthStencil(); virtual GLsizei getWidth() const; virtual GLsizei getHeight() const; virtual GLenum getInternalFormat() const; virtual GLenum getActualFormat() const; virtual GLsizei getSamples() const; virtual unsigned int getSerial() const; private: DISALLOW_COPY_AND_ASSIGN(RenderbufferTexture2D); BindingPointer <Texture2D> mTexture2D; GLenum mTarget; }; class RenderbufferTextureCubeMap : public RenderbufferInterface { public: RenderbufferTextureCubeMap(TextureCubeMap *texture, GLenum target); virtual ~RenderbufferTextureCubeMap(); void addProxyRef(const Renderbuffer *proxy); void releaseProxy(const Renderbuffer *proxy); rx::RenderTarget *getRenderTarget(); rx::RenderTarget *getDepthStencil(); virtual GLsizei getWidth() const; virtual GLsizei getHeight() const; virtual GLenum getInternalFormat() const; virtual GLenum getActualFormat() const; virtual GLsizei getSamples() const; virtual unsigned int getSerial() const; private: DISALLOW_COPY_AND_ASSIGN(RenderbufferTextureCubeMap); BindingPointer <TextureCubeMap> mTextureCubeMap; GLenum mTarget; }; // A class derived from RenderbufferStorage is created whenever glRenderbufferStorage // is called. The specific concrete type depends on whether the internal format is // colour depth, stencil or packed depth/stencil. class RenderbufferStorage : public RenderbufferInterface { public: RenderbufferStorage(); virtual ~RenderbufferStorage() = 0; virtual rx::RenderTarget *getRenderTarget(); virtual rx::RenderTarget *getDepthStencil(); virtual GLsizei getWidth() const; virtual GLsizei getHeight() const; virtual GLenum getInternalFormat() const; virtual GLenum getActualFormat() const; virtual GLsizei getSamples() const; virtual unsigned int getSerial() const; static unsigned int issueSerial(); static unsigned int issueCubeSerials(); protected: GLsizei mWidth; GLsizei mHeight; GLenum mInternalFormat; GLenum mActualFormat; GLsizei mSamples; private: DISALLOW_COPY_AND_ASSIGN(RenderbufferStorage); const unsigned int mSerial; static unsigned int mCurrentSerial; }; // Renderbuffer implements the GL renderbuffer object. // It's only a proxy for a RenderbufferInterface instance; the internal object // can change whenever glRenderbufferStorage is called. class Renderbuffer : public RefCountObject { public: Renderbuffer(rx::Renderer *renderer, GLuint id, RenderbufferInterface *storage); virtual ~Renderbuffer(); // These functions from RefCountObject are overloaded here because // Textures need to maintain their own count of references to them via // Renderbuffers/RenderbufferTextures. These functions invoke those // reference counting functions on the RenderbufferInterface. void addRef() const; void release() const; rx::RenderTarget *getRenderTarget(); rx::RenderTarget *getDepthStencil(); GLsizei getWidth() const; GLsizei getHeight() const; GLenum getInternalFormat() const; GLenum getActualFormat() const; GLuint getRedSize() const; GLuint getGreenSize() const; GLuint getBlueSize() const; GLuint getAlphaSize() const; GLuint getDepthSize() const; GLuint getStencilSize() const; GLsizei getSamples() const; unsigned int getSerial() const; void setStorage(RenderbufferStorage *newStorage); private: DISALLOW_COPY_AND_ASSIGN(Renderbuffer); RenderbufferInterface *mInstance; }; class Colorbuffer : public RenderbufferStorage { public: Colorbuffer(rx::Renderer *renderer, rx::SwapChain *swapChain); Colorbuffer(rx::Renderer *renderer, GLsizei width, GLsizei height, GLenum format, GLsizei samples); virtual ~Colorbuffer(); virtual rx::RenderTarget *getRenderTarget(); private: DISALLOW_COPY_AND_ASSIGN(Colorbuffer); rx::RenderTarget *mRenderTarget; }; class DepthStencilbuffer : public RenderbufferStorage { public: DepthStencilbuffer(rx::Renderer *renderer, rx::SwapChain *swapChain); DepthStencilbuffer(rx::Renderer *renderer, GLsizei width, GLsizei height, GLsizei samples); ~DepthStencilbuffer(); virtual rx::RenderTarget *getDepthStencil(); protected: rx::RenderTarget *mDepthStencil; private: DISALLOW_COPY_AND_ASSIGN(DepthStencilbuffer); }; class Depthbuffer : public DepthStencilbuffer { public: Depthbuffer(rx::Renderer *renderer, GLsizei width, GLsizei height, GLsizei samples); virtual ~Depthbuffer(); private: DISALLOW_COPY_AND_ASSIGN(Depthbuffer); }; class Stencilbuffer : public DepthStencilbuffer { public: Stencilbuffer(rx::Renderer *renderer, GLsizei width, GLsizei height, GLsizei samples); virtual ~Stencilbuffer(); private: DISALLOW_COPY_AND_ASSIGN(Stencilbuffer); }; } #endif // LIBGLESV2_RENDERBUFFER_H_
C++
#include "precompiled.h" // // Copyright (c) 2010-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "libGLESv2/Uniform.h" #include "libGLESv2/utilities.h" namespace gl { Uniform::Uniform(GLenum type, GLenum precision, const std::string &name, unsigned int arraySize) : type(type), precision(precision), name(name), arraySize(arraySize) { int bytes = gl::UniformInternalSize(type) * elementCount(); data = new unsigned char[bytes]; memset(data, 0, bytes); dirty = true; psRegisterIndex = -1; vsRegisterIndex = -1; registerCount = VariableRowCount(type) * elementCount(); } Uniform::~Uniform() { delete[] data; } bool Uniform::isArray() const { return arraySize > 0; } unsigned int Uniform::elementCount() const { return arraySize > 0 ? arraySize : 1; } }
C++
// // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // mathutil.h: Math and bit manipulation functions. #ifndef LIBGLESV2_MATHUTIL_H_ #define LIBGLESV2_MATHUTIL_H_ #include <intrin.h> #include "common/system.h" #include "common/debug.h" namespace gl { struct Vector4 { Vector4() {} Vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {} float x; float y; float z; float w; }; inline bool isPow2(int x) { return (x & (x - 1)) == 0 && (x != 0); } inline int log2(int x) { int r = 0; while ((x >> r) > 1) r++; return r; } inline unsigned int ceilPow2(unsigned int x) { if (x != 0) x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; return x; } template<typename T, typename MIN, typename MAX> inline T clamp(T x, MIN min, MAX max) { // Since NaNs fail all comparison tests, a NaN value will default to min return x > min ? (x > max ? max : x) : min; } inline float clamp01(float x) { return clamp(x, 0.0f, 1.0f); } template<const int n> inline unsigned int unorm(float x) { const unsigned int max = 0xFFFFFFFF >> (32 - n); if (x > 1) { return max; } else if (x < 0) { return 0; } else { return (unsigned int)(max * x + 0.5f); } } inline bool supportsSSE2() { static bool checked = false; static bool supports = false; if (checked) { return supports; } int info[4]; __cpuid(info, 0); if (info[0] >= 1) { __cpuid(info, 1); supports = (info[3] >> 26) & 1; } checked = true; return supports; } inline unsigned short float32ToFloat16(float fp32) { unsigned int fp32i = (unsigned int&)fp32; unsigned int sign = (fp32i & 0x80000000) >> 16; unsigned int abs = fp32i & 0x7FFFFFFF; if(abs > 0x47FFEFFF) // Infinity { return sign | 0x7FFF; } else if(abs < 0x38800000) // Denormal { unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000; int e = 113 - (abs >> 23); if(e < 24) { abs = mantissa >> e; } else { abs = 0; } return sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13; } else { return sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13; } } float float16ToFloat32(unsigned short h); } namespace rx { struct Range { Range() {} Range(int lo, int hi) : start(lo), end(hi) { ASSERT(lo <= hi); } int start; int end; }; } #endif // LIBGLESV2_MATHUTIL_H_
C++
// // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Program.h: Defines the gl::Program class. Implements GL program objects // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28. #ifndef LIBGLESV2_PROGRAM_BINARY_H_ #define LIBGLESV2_PROGRAM_BINARY_H_ #define GL_APICALL #include <GLES2/gl2.h> #include <GLES2/gl2ext.h> #include <string> #include <vector> #include "common/RefCountObject.h" #include "angletypes.h" #include "libGLESv2/mathutil.h" #include "libGLESv2/Uniform.h" #include "libGLESv2/Shader.h" #include "libGLESv2/Constants.h" namespace rx { class ShaderExecutable; class Renderer; struct TranslatedAttribute; } namespace gl { class FragmentShader; class VertexShader; class InfoLog; class AttributeBindings; struct Varying; // Struct used for correlating uniforms/elements of uniform arrays to handles struct UniformLocation { UniformLocation() { } UniformLocation(const std::string &name, unsigned int element, unsigned int index); std::string name; unsigned int element; unsigned int index; }; // This is the result of linking a program. It is the state that would be passed to ProgramBinary. class ProgramBinary : public RefCountObject { public: explicit ProgramBinary(rx::Renderer *renderer); ~ProgramBinary(); rx::ShaderExecutable *getPixelExecutable(); rx::ShaderExecutable *getVertexExecutable(); rx::ShaderExecutable *getGeometryExecutable(); GLuint getAttributeLocation(const char *name); int getSemanticIndex(int attributeIndex); GLint getSamplerMapping(SamplerType type, unsigned int samplerIndex); TextureType getSamplerTextureType(SamplerType type, unsigned int samplerIndex); GLint getUsedSamplerRange(SamplerType type); bool usesPointSize() const; bool usesPointSpriteEmulation() const; bool usesGeometryShader() const; GLint getUniformLocation(std::string name); bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v); bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v); bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v); bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v); bool setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value); bool setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value); bool setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value); bool setUniform1iv(GLint location, GLsizei count, const GLint *v); bool setUniform2iv(GLint location, GLsizei count, const GLint *v); bool setUniform3iv(GLint location, GLsizei count, const GLint *v); bool setUniform4iv(GLint location, GLsizei count, const GLint *v); bool getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params); bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params); void dirtyAllUniforms(); void applyUniforms(); bool load(InfoLog &infoLog, const void *binary, GLsizei length); bool save(void* binary, GLsizei bufSize, GLsizei *length); GLint getLength(); bool link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader); void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders); void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const; GLint getActiveAttributeCount() const; GLint getActiveAttributeMaxLength() const; void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const; GLint getActiveUniformCount() const; GLint getActiveUniformMaxLength() const; void validate(InfoLog &infoLog); bool validateSamplers(InfoLog *infoLog); bool isValidated() const; unsigned int getSerial() const; void sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const; static std::string decorateAttribute(const std::string &name); // Prepend an underscore private: DISALLOW_COPY_AND_ASSIGN(ProgramBinary); int packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader); bool linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4], std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader); bool linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader); bool linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms); bool defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog); std::string generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const; std::string generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const; rx::Renderer *const mRenderer; rx::ShaderExecutable *mPixelExecutable; rx::ShaderExecutable *mVertexExecutable; rx::ShaderExecutable *mGeometryExecutable; Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS]; int mSemanticIndex[MAX_VERTEX_ATTRIBS]; struct Sampler { Sampler(); bool active; GLint logicalTextureUnit; TextureType textureType; }; Sampler mSamplersPS[MAX_TEXTURE_IMAGE_UNITS]; Sampler mSamplersVS[IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS]; GLuint mUsedVertexSamplerRange; GLuint mUsedPixelSamplerRange; bool mUsesPointSize; UniformArray mUniforms; typedef std::vector<UniformLocation> UniformIndex; UniformIndex mUniformIndex; bool mValidated; const unsigned int mSerial; static unsigned int issueSerial(); static unsigned int mCurrentSerial; }; } #endif // LIBGLESV2_PROGRAM_BINARY_H_
C++
// // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Buffer.h: Defines the gl::Buffer class, representing storage of vertex and/or // index data. Implements GL buffer objects and related functionality. // [OpenGL ES 2.0.24] section 2.9 page 21. #ifndef LIBGLESV2_BUFFER_H_ #define LIBGLESV2_BUFFER_H_ #include "common/angleutils.h" #include "common/RefCountObject.h" namespace rx { class Renderer; class BufferStorage; class StaticIndexBufferInterface; class StaticVertexBufferInterface; }; namespace gl { class Buffer : public RefCountObject { public: Buffer(rx::Renderer *renderer, GLuint id); virtual ~Buffer(); void bufferData(const void *data, GLsizeiptr size, GLenum usage); void bufferSubData(const void *data, GLsizeiptr size, GLintptr offset); GLenum usage() const; rx::BufferStorage *getStorage() const; unsigned int size(); rx::StaticVertexBufferInterface *getStaticVertexBuffer(); rx::StaticIndexBufferInterface *getStaticIndexBuffer(); void invalidateStaticData(); void promoteStaticUsage(int dataSize); private: DISALLOW_COPY_AND_ASSIGN(Buffer); rx::Renderer *mRenderer; GLenum mUsage; rx::BufferStorage *mBufferStorage; rx::StaticVertexBufferInterface *mStaticVertexBuffer; rx::StaticIndexBufferInterface *mStaticIndexBuffer; unsigned int mUnmodifiedDataUse; }; } #endif // LIBGLESV2_BUFFER_H_
C++
// // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Program.h: Defines the gl::Program class. Implements GL program objects // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28. #ifndef LIBGLESV2_PROGRAM_H_ #define LIBGLESV2_PROGRAM_H_ #include <string> #include <set> #include "common/angleutils.h" #include "common/RefCountObject.h" #include "libGLESv2/Constants.h" namespace rx { class Renderer; } namespace gl { class ResourceManager; class FragmentShader; class VertexShader; class ProgramBinary; class Shader; extern const char * const g_fakepath; class AttributeBindings { public: AttributeBindings(); ~AttributeBindings(); void bindAttributeLocation(GLuint index, const char *name); int getAttributeBinding(const std::string &name) const; private: std::set<std::string> mAttributeBinding[MAX_VERTEX_ATTRIBS]; }; class InfoLog { public: InfoLog(); ~InfoLog(); int getLength() const; void getLog(GLsizei bufSize, GLsizei *length, char *infoLog); void appendSanitized(const char *message); void append(const char *info, ...); void reset(); private: DISALLOW_COPY_AND_ASSIGN(InfoLog); char *mInfoLog; }; class Program { public: Program(rx::Renderer *renderer, ResourceManager *manager, GLuint handle); ~Program(); bool attachShader(Shader *shader); bool detachShader(Shader *shader); int getAttachedShadersCount() const; void bindAttributeLocation(GLuint index, const char *name); bool link(); bool isLinked(); bool setProgramBinary(const void *binary, GLsizei length); ProgramBinary *getProgramBinary(); int getInfoLogLength() const; void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog); void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders); void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name); GLint getActiveAttributeCount(); GLint getActiveAttributeMaxLength(); void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name); GLint getActiveUniformCount(); GLint getActiveUniformMaxLength(); void addRef(); void release(); unsigned int getRefCount() const; void flagForDeletion(); bool isFlaggedForDeletion() const; void validate(); bool isValidated() const; GLint getProgramBinaryLength() const; private: DISALLOW_COPY_AND_ASSIGN(Program); void unlink(bool destroy = false); FragmentShader *mFragmentShader; VertexShader *mVertexShader; AttributeBindings mAttributeBindings; BindingPointer<ProgramBinary> mProgramBinary; bool mLinked; bool mDeleteStatus; // Flag to indicate that the program can be deleted when no longer in use unsigned int mRefCount; ResourceManager *mResourceManager; rx::Renderer *mRenderer; const GLuint mHandle; InfoLog mInfoLog; }; } #endif // LIBGLESV2_PROGRAM_H_
C++
#include "precompiled.h" // // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // Texture.cpp: Implements the gl::Texture class and its derived classes // Texture2D and TextureCubeMap. Implements GL texture objects and related // functionality. [OpenGL ES 2.0.24] section 3.7 page 63. #include "libGLESv2/Texture.h" #include "libGLESv2/main.h" #include "libGLESv2/mathutil.h" #include "libGLESv2/utilities.h" #include "libGLESv2/renderer/Blit.h" #include "libGLESv2/Renderbuffer.h" #include "libGLESv2/renderer/Image.h" #include "libGLESv2/renderer/Renderer.h" #include "libGLESv2/renderer/TextureStorage.h" #include "libEGL/Surface.h" namespace gl { Texture::Texture(rx::Renderer *renderer, GLuint id) : RefCountObject(id) { mRenderer = renderer; mSamplerState.minFilter = GL_NEAREST_MIPMAP_LINEAR; mSamplerState.magFilter = GL_LINEAR; mSamplerState.wrapS = GL_REPEAT; mSamplerState.wrapT = GL_REPEAT; mSamplerState.maxAnisotropy = 1.0f; mSamplerState.lodOffset = 0; mUsage = GL_NONE; mDirtyImages = true; mImmutable = false; } Texture::~Texture() { } // Returns true on successful filter state update (valid enum parameter) bool Texture::setMinFilter(GLenum filter) { switch (filter) { case GL_NEAREST: case GL_LINEAR: case GL_NEAREST_MIPMAP_NEAREST: case GL_LINEAR_MIPMAP_NEAREST: case GL_NEAREST_MIPMAP_LINEAR: case GL_LINEAR_MIPMAP_LINEAR: mSamplerState.minFilter = filter; return true; default: return false; } } // Returns true on successful filter state update (valid enum parameter) bool Texture::setMagFilter(GLenum filter) { switch (filter) { case GL_NEAREST: case GL_LINEAR: mSamplerState.magFilter = filter; return true; default: return false; } } // Returns true on successful wrap state update (valid enum parameter) bool Texture::setWrapS(GLenum wrap) { switch (wrap) { case GL_REPEAT: case GL_CLAMP_TO_EDGE: case GL_MIRRORED_REPEAT: mSamplerState.wrapS = wrap; return true; default: return false; } } // Returns true on successful wrap state update (valid enum parameter) bool Texture::setWrapT(GLenum wrap) { switch (wrap) { case GL_REPEAT: case GL_CLAMP_TO_EDGE: case GL_MIRRORED_REPEAT: mSamplerState.wrapT = wrap; return true; default: return false; } } // Returns true on successful max anisotropy update (valid anisotropy value) bool Texture::setMaxAnisotropy(float textureMaxAnisotropy, float contextMaxAnisotropy) { textureMaxAnisotropy = std::min(textureMaxAnisotropy, contextMaxAnisotropy); if (textureMaxAnisotropy < 1.0f) { return false; } mSamplerState.maxAnisotropy = textureMaxAnisotropy; return true; } // Returns true on successful usage state update (valid enum parameter) bool Texture::setUsage(GLenum usage) { switch (usage) { case GL_NONE: case GL_FRAMEBUFFER_ATTACHMENT_ANGLE: mUsage = usage; return true; default: return false; } } GLenum Texture::getMinFilter() const { return mSamplerState.minFilter; } GLenum Texture::getMagFilter() const { return mSamplerState.magFilter; } GLenum Texture::getWrapS() const { return mSamplerState.wrapS; } GLenum Texture::getWrapT() const { return mSamplerState.wrapT; } float Texture::getMaxAnisotropy() const { return mSamplerState.maxAnisotropy; } int Texture::getLodOffset() { rx::TextureStorageInterface *texture = getStorage(false); return texture ? texture->getLodOffset() : 0; } void Texture::getSamplerState(SamplerState *sampler) { *sampler = mSamplerState; sampler->lodOffset = getLodOffset(); } GLenum Texture::getUsage() const { return mUsage; } bool Texture::isMipmapFiltered() const { switch (mSamplerState.minFilter) { case GL_NEAREST: case GL_LINEAR: return false; case GL_NEAREST_MIPMAP_NEAREST: case GL_LINEAR_MIPMAP_NEAREST: case GL_NEAREST_MIPMAP_LINEAR: case GL_LINEAR_MIPMAP_LINEAR: return true; default: UNREACHABLE(); return false; } } void Texture::setImage(GLint unpackAlignment, const void *pixels, rx::Image *image) { if (pixels != NULL) { image->loadData(0, 0, image->getWidth(), image->getHeight(), unpackAlignment, pixels); mDirtyImages = true; } } void Texture::setCompressedImage(GLsizei imageSize, const void *pixels, rx::Image *image) { if (pixels != NULL) { image->loadCompressedData(0, 0, image->getWidth(), image->getHeight(), pixels); mDirtyImages = true; } } bool Texture::subImage(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels, rx::Image *image) { if (pixels != NULL) { image->loadData(xoffset, yoffset, width, height, unpackAlignment, pixels); mDirtyImages = true; } return true; } bool Texture::subImageCompressed(GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels, rx::Image *image) { if (pixels != NULL) { image->loadCompressedData(xoffset, yoffset, width, height, pixels); mDirtyImages = true; } return true; } rx::TextureStorageInterface *Texture::getNativeTexture() { // ensure the underlying texture is created rx::TextureStorageInterface *storage = getStorage(false); if (storage) { updateTexture(); } return storage; } bool Texture::hasDirtyImages() const { return mDirtyImages; } void Texture::resetDirty() { mDirtyImages = false; } unsigned int Texture::getTextureSerial() { rx::TextureStorageInterface *texture = getStorage(false); return texture ? texture->getTextureSerial() : 0; } unsigned int Texture::getRenderTargetSerial(GLenum target) { rx::TextureStorageInterface *texture = getStorage(true); return texture ? texture->getRenderTargetSerial(target) : 0; } bool Texture::isImmutable() const { return mImmutable; } GLint Texture::creationLevels(GLsizei width, GLsizei height) const { if ((isPow2(width) && isPow2(height)) || mRenderer->getNonPower2TextureSupport()) { return 0; // Maximum number of levels } else { // OpenGL ES 2.0 without GL_OES_texture_npot does not permit NPOT mipmaps. return 1; } } GLint Texture::creationLevels(GLsizei size) const { return creationLevels(size, size); } Texture2D::Texture2D(rx::Renderer *renderer, GLuint id) : Texture(renderer, id) { mTexStorage = NULL; mSurface = NULL; mColorbufferProxy = NULL; mProxyRefs = 0; for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++i) { mImageArray[i] = renderer->createImage(); } } Texture2D::~Texture2D() { mColorbufferProxy = NULL; delete mTexStorage; mTexStorage = NULL; if (mSurface) { mSurface->setBoundTexture(NULL); mSurface = NULL; } for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++i) { delete mImageArray[i]; } } // We need to maintain a count of references to renderbuffers acting as // proxies for this texture, so that we do not attempt to use a pointer // to a renderbuffer proxy which has been deleted. void Texture2D::addProxyRef(const Renderbuffer *proxy) { mProxyRefs++; } void Texture2D::releaseProxy(const Renderbuffer *proxy) { if (mProxyRefs > 0) mProxyRefs--; if (mProxyRefs == 0) mColorbufferProxy = NULL; } GLenum Texture2D::getTarget() const { return GL_TEXTURE_2D; } GLsizei Texture2D::getWidth(GLint level) const { if (level < IMPLEMENTATION_MAX_TEXTURE_LEVELS) return mImageArray[level]->getWidth(); else return 0; } GLsizei Texture2D::getHeight(GLint level) const { if (level < IMPLEMENTATION_MAX_TEXTURE_LEVELS) return mImageArray[level]->getHeight(); else return 0; } GLenum Texture2D::getInternalFormat(GLint level) const { if (level < IMPLEMENTATION_MAX_TEXTURE_LEVELS) return mImageArray[level]->getInternalFormat(); else return GL_NONE; } GLenum Texture2D::getActualFormat(GLint level) const { if (level < IMPLEMENTATION_MAX_TEXTURE_LEVELS) return mImageArray[level]->getActualFormat(); else return D3DFMT_UNKNOWN; } void Texture2D::redefineImage(GLint level, GLint internalformat, GLsizei width, GLsizei height) { releaseTexImage(); // If there currently is a corresponding storage texture image, it has these parameters const int storageWidth = std::max(1, mImageArray[0]->getWidth() >> level); const int storageHeight = std::max(1, mImageArray[0]->getHeight() >> level); const int storageFormat = mImageArray[0]->getInternalFormat(); mImageArray[level]->redefine(mRenderer, internalformat, width, height, false); if (mTexStorage) { const int storageLevels = mTexStorage->levelCount(); if ((level >= storageLevels && storageLevels != 0) || width != storageWidth || height != storageHeight || internalformat != storageFormat) // Discard mismatched storage { for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) { mImageArray[i]->markDirty(); } delete mTexStorage; mTexStorage = NULL; mDirtyImages = true; } } } void Texture2D::setImage(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) { GLint internalformat = ConvertSizedInternalFormat(format, type); redefineImage(level, internalformat, width, height); Texture::setImage(unpackAlignment, pixels, mImageArray[level]); } void Texture2D::bindTexImage(egl::Surface *surface) { releaseTexImage(); GLint internalformat = surface->getFormat(); mImageArray[0]->redefine(mRenderer, internalformat, surface->getWidth(), surface->getHeight(), true); delete mTexStorage; mTexStorage = new rx::TextureStorageInterface2D(mRenderer, surface->getSwapChain()); mDirtyImages = true; mSurface = surface; mSurface->setBoundTexture(this); } void Texture2D::releaseTexImage() { if (mSurface) { mSurface->setBoundTexture(NULL); mSurface = NULL; if (mTexStorage) { delete mTexStorage; mTexStorage = NULL; } for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) { mImageArray[i]->redefine(mRenderer, GL_NONE, 0, 0, true); } } } void Texture2D::setCompressedImage(GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels) { // compressed formats don't have separate sized internal formats-- we can just use the compressed format directly redefineImage(level, format, width, height); Texture::setCompressedImage(imageSize, pixels, mImageArray[level]); } void Texture2D::commitRect(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) { if (level < levelCount()) { rx::Image *image = mImageArray[level]; if (image->updateSurface(mTexStorage, level, xoffset, yoffset, width, height)) { image->markClean(); } } } void Texture2D::subImage(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) { if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, mImageArray[level])) { commitRect(level, xoffset, yoffset, width, height); } } void Texture2D::subImageCompressed(GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels) { if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, mImageArray[level])) { commitRect(level, xoffset, yoffset, width, height); } } void Texture2D::copyImage(GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) { GLint internalformat = ConvertSizedInternalFormat(format, GL_UNSIGNED_BYTE); redefineImage(level, internalformat, width, height); if (!mImageArray[level]->isRenderableFormat()) { mImageArray[level]->copy(0, 0, x, y, width, height, source); mDirtyImages = true; } else { if (!mTexStorage || !mTexStorage->isRenderTarget()) { convertToRenderTarget(); } mImageArray[level]->markClean(); if (width != 0 && height != 0 && level < levelCount()) { gl::Rectangle sourceRect; sourceRect.x = x; sourceRect.width = width; sourceRect.y = y; sourceRect.height = height; mRenderer->copyImage(source, sourceRect, format, 0, 0, mTexStorage, level); } } } void Texture2D::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) { if (xoffset + width > mImageArray[level]->getWidth() || yoffset + height > mImageArray[level]->getHeight()) { return gl::error(GL_INVALID_VALUE); } if (!mImageArray[level]->isRenderableFormat() || (!mTexStorage && !isSamplerComplete())) { mImageArray[level]->copy(xoffset, yoffset, x, y, width, height, source); mDirtyImages = true; } else { if (!mTexStorage || !mTexStorage->isRenderTarget()) { convertToRenderTarget(); } updateTexture(); if (level < levelCount()) { gl::Rectangle sourceRect; sourceRect.x = x; sourceRect.width = width; sourceRect.y = y; sourceRect.height = height; mRenderer->copyImage(source, sourceRect, gl::ExtractFormat(mImageArray[0]->getInternalFormat()), xoffset, yoffset, mTexStorage, level); } } } void Texture2D::storage(GLsizei levels, GLenum internalformat, GLsizei width, GLsizei height) { delete mTexStorage; mTexStorage = new rx::TextureStorageInterface2D(mRenderer, levels, internalformat, mUsage, false, width, height); mImmutable = true; for (int level = 0; level < levels; level++) { mImageArray[level]->redefine(mRenderer, internalformat, width, height, true); width = std::max(1, width >> 1); height = std::max(1, height >> 1); } for (int level = levels; level < IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++) { mImageArray[level]->redefine(mRenderer, GL_NONE, 0, 0, true); } if (mTexStorage->isManaged()) { int levels = levelCount(); for (int level = 0; level < levels; level++) { mImageArray[level]->setManagedSurface(mTexStorage, level); } } } // Tests for 2D texture sampling completeness. [OpenGL ES 2.0.24] section 3.8.2 page 85. bool Texture2D::isSamplerComplete() const { GLsizei width = mImageArray[0]->getWidth(); GLsizei height = mImageArray[0]->getHeight(); if (width <= 0 || height <= 0) { return false; } bool mipmapping = isMipmapFiltered(); bool filtering, renderable; if ((IsFloat32Format(getInternalFormat(0)) && !mRenderer->getFloat32TextureSupport(&filtering, &renderable)) || (IsFloat16Format(getInternalFormat(0)) && !mRenderer->getFloat16TextureSupport(&filtering, &renderable))) { if (mSamplerState.magFilter != GL_NEAREST || (mSamplerState.minFilter != GL_NEAREST && mSamplerState.minFilter != GL_NEAREST_MIPMAP_NEAREST)) { return false; } } bool npotSupport = mRenderer->getNonPower2TextureSupport(); if (!npotSupport) { if ((mSamplerState.wrapS != GL_CLAMP_TO_EDGE && !isPow2(width)) || (mSamplerState.wrapT != GL_CLAMP_TO_EDGE && !isPow2(height))) { return false; } } if (mipmapping) { if (!npotSupport) { if (!isPow2(width) || !isPow2(height)) { return false; } } if (!isMipmapComplete()) { return false; } } return true; } // Tests for 2D texture (mipmap) completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81. bool Texture2D::isMipmapComplete() const { if (isImmutable()) { return true; } GLsizei width = mImageArray[0]->getWidth(); GLsizei height = mImageArray[0]->getHeight(); if (width <= 0 || height <= 0) { return false; } int q = log2(std::max(width, height)); for (int level = 1; level <= q; level++) { if (mImageArray[level]->getInternalFormat() != mImageArray[0]->getInternalFormat()) { return false; } if (mImageArray[level]->getWidth() != std::max(1, width >> level)) { return false; } if (mImageArray[level]->getHeight() != std::max(1, height >> level)) { return false; } } return true; } bool Texture2D::isCompressed(GLint level) const { return IsCompressed(getInternalFormat(level)); } bool Texture2D::isDepth(GLint level) const { return IsDepthTexture(getInternalFormat(level)); } // Constructs a native texture resource from the texture images void Texture2D::createTexture() { GLsizei width = mImageArray[0]->getWidth(); GLsizei height = mImageArray[0]->getHeight(); if (!(width > 0 && height > 0)) return; // do not attempt to create native textures for nonexistant data GLint levels = creationLevels(width, height); GLenum internalformat = mImageArray[0]->getInternalFormat(); delete mTexStorage; mTexStorage = new rx::TextureStorageInterface2D(mRenderer, levels, internalformat, mUsage, false, width, height); if (mTexStorage->isManaged()) { int levels = levelCount(); for (int level = 0; level < levels; level++) { mImageArray[level]->setManagedSurface(mTexStorage, level); } } mDirtyImages = true; } void Texture2D::updateTexture() { bool mipmapping = (isMipmapFiltered() && isMipmapComplete()); int levels = (mipmapping ? levelCount() : 1); for (int level = 0; level < levels; level++) { rx::Image *image = mImageArray[level]; if (image->isDirty()) { commitRect(level, 0, 0, mImageArray[level]->getWidth(), mImageArray[level]->getHeight()); } } } void Texture2D::convertToRenderTarget() { rx::TextureStorageInterface2D *newTexStorage = NULL; if (mImageArray[0]->getWidth() != 0 && mImageArray[0]->getHeight() != 0) { GLsizei width = mImageArray[0]->getWidth(); GLsizei height = mImageArray[0]->getHeight(); GLint levels = mTexStorage != NULL ? mTexStorage->levelCount() : creationLevels(width, height); GLenum internalformat = mImageArray[0]->getInternalFormat(); newTexStorage = new rx::TextureStorageInterface2D(mRenderer, levels, internalformat, GL_FRAMEBUFFER_ATTACHMENT_ANGLE, true, width, height); if (mTexStorage != NULL) { if (!mRenderer->copyToRenderTarget(newTexStorage, mTexStorage)) { delete newTexStorage; return gl::error(GL_OUT_OF_MEMORY); } } } delete mTexStorage; mTexStorage = newTexStorage; mDirtyImages = true; } void Texture2D::generateMipmaps() { if (!mRenderer->getNonPower2TextureSupport()) { if (!isPow2(mImageArray[0]->getWidth()) || !isPow2(mImageArray[0]->getHeight())) { return gl::error(GL_INVALID_OPERATION); } } // Purge array levels 1 through q and reset them to represent the generated mipmap levels. unsigned int q = log2(std::max(mImageArray[0]->getWidth(), mImageArray[0]->getHeight())); for (unsigned int i = 1; i <= q; i++) { redefineImage(i, mImageArray[0]->getInternalFormat(), std::max(mImageArray[0]->getWidth() >> i, 1), std::max(mImageArray[0]->getHeight() >> i, 1)); } if (mTexStorage && mTexStorage->isRenderTarget()) { for (unsigned int i = 1; i <= q; i++) { mTexStorage->generateMipmap(i); mImageArray[i]->markClean(); } } else { for (unsigned int i = 1; i <= q; i++) { mRenderer->generateMipmap(mImageArray[i], mImageArray[i - 1]); } } } Renderbuffer *Texture2D::getRenderbuffer(GLenum target) { if (target != GL_TEXTURE_2D) { return gl::error(GL_INVALID_OPERATION, (Renderbuffer *)NULL); } if (mColorbufferProxy == NULL) { mColorbufferProxy = new Renderbuffer(mRenderer, id(), new RenderbufferTexture2D(this, target)); } return mColorbufferProxy; } rx::RenderTarget *Texture2D::getRenderTarget(GLenum target) { ASSERT(target == GL_TEXTURE_2D); // ensure the underlying texture is created if (getStorage(true) == NULL) { return NULL; } updateTexture(); // ensure this is NOT a depth texture if (isDepth(0)) { return NULL; } return mTexStorage->getRenderTarget(); } rx::RenderTarget *Texture2D::getDepthStencil(GLenum target) { ASSERT(target == GL_TEXTURE_2D); // ensure the underlying texture is created if (getStorage(true) == NULL) { return NULL; } updateTexture(); // ensure this is actually a depth texture if (!isDepth(0)) { return NULL; } return mTexStorage->getRenderTarget(); } int Texture2D::levelCount() { return mTexStorage ? mTexStorage->levelCount() : 0; } rx::TextureStorageInterface *Texture2D::getStorage(bool renderTarget) { if (!mTexStorage || (renderTarget && !mTexStorage->isRenderTarget())) { if (renderTarget) { convertToRenderTarget(); } else { createTexture(); } } return mTexStorage; } TextureCubeMap::TextureCubeMap(rx::Renderer *renderer, GLuint id) : Texture(renderer, id) { mTexStorage = NULL; for (int i = 0; i < 6; i++) { mFaceProxies[i] = NULL; mFaceProxyRefs[i] = 0; for (int j = 0; j < IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++j) { mImageArray[i][j] = renderer->createImage(); } } } TextureCubeMap::~TextureCubeMap() { for (int i = 0; i < 6; i++) { mFaceProxies[i] = NULL; for (int j = 0; j < IMPLEMENTATION_MAX_TEXTURE_LEVELS; ++j) { delete mImageArray[i][j]; } } delete mTexStorage; mTexStorage = NULL; } // We need to maintain a count of references to renderbuffers acting as // proxies for this texture, so that the texture is not deleted while // proxy references still exist. If the reference count drops to zero, // we set our proxy pointer NULL, so that a new attempt at referencing // will cause recreation. void TextureCubeMap::addProxyRef(const Renderbuffer *proxy) { for (int i = 0; i < 6; i++) { if (mFaceProxies[i] == proxy) mFaceProxyRefs[i]++; } } void TextureCubeMap::releaseProxy(const Renderbuffer *proxy) { for (int i = 0; i < 6; i++) { if (mFaceProxies[i] == proxy) { if (mFaceProxyRefs[i] > 0) mFaceProxyRefs[i]--; if (mFaceProxyRefs[i] == 0) mFaceProxies[i] = NULL; } } } GLenum TextureCubeMap::getTarget() const { return GL_TEXTURE_CUBE_MAP; } GLsizei TextureCubeMap::getWidth(GLenum target, GLint level) const { if (level < IMPLEMENTATION_MAX_TEXTURE_LEVELS) return mImageArray[faceIndex(target)][level]->getWidth(); else return 0; } GLsizei TextureCubeMap::getHeight(GLenum target, GLint level) const { if (level < IMPLEMENTATION_MAX_TEXTURE_LEVELS) return mImageArray[faceIndex(target)][level]->getHeight(); else return 0; } GLenum TextureCubeMap::getInternalFormat(GLenum target, GLint level) const { if (level < IMPLEMENTATION_MAX_TEXTURE_LEVELS) return mImageArray[faceIndex(target)][level]->getInternalFormat(); else return GL_NONE; } GLenum TextureCubeMap::getActualFormat(GLenum target, GLint level) const { if (level < IMPLEMENTATION_MAX_TEXTURE_LEVELS) return mImageArray[faceIndex(target)][level]->getActualFormat(); else return D3DFMT_UNKNOWN; } void TextureCubeMap::setImagePosX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) { setImage(0, level, width, height, format, type, unpackAlignment, pixels); } void TextureCubeMap::setImageNegX(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) { setImage(1, level, width, height, format, type, unpackAlignment, pixels); } void TextureCubeMap::setImagePosY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) { setImage(2, level, width, height, format, type, unpackAlignment, pixels); } void TextureCubeMap::setImageNegY(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) { setImage(3, level, width, height, format, type, unpackAlignment, pixels); } void TextureCubeMap::setImagePosZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) { setImage(4, level, width, height, format, type, unpackAlignment, pixels); } void TextureCubeMap::setImageNegZ(GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) { setImage(5, level, width, height, format, type, unpackAlignment, pixels); } void TextureCubeMap::setCompressedImage(GLenum face, GLint level, GLenum format, GLsizei width, GLsizei height, GLsizei imageSize, const void *pixels) { // compressed formats don't have separate sized internal formats-- we can just use the compressed format directly redefineImage(faceIndex(face), level, format, width, height); Texture::setCompressedImage(imageSize, pixels, mImageArray[faceIndex(face)][level]); } void TextureCubeMap::commitRect(int face, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height) { if (level < levelCount()) { rx::Image *image = mImageArray[face][level]; if (image->updateSurface(mTexStorage, face, level, xoffset, yoffset, width, height)) image->markClean(); } } void TextureCubeMap::subImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) { if (Texture::subImage(xoffset, yoffset, width, height, format, type, unpackAlignment, pixels, mImageArray[faceIndex(target)][level])) { commitRect(faceIndex(target), level, xoffset, yoffset, width, height); } } void TextureCubeMap::subImageCompressed(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLsizei imageSize, const void *pixels) { if (Texture::subImageCompressed(xoffset, yoffset, width, height, format, imageSize, pixels, mImageArray[faceIndex(target)][level])) { commitRect(faceIndex(target), level, xoffset, yoffset, width, height); } } // Tests for cube map sampling completeness. [OpenGL ES 2.0.24] section 3.8.2 page 86. bool TextureCubeMap::isSamplerComplete() const { int size = mImageArray[0][0]->getWidth(); bool mipmapping = isMipmapFiltered(); bool filtering, renderable; if ((gl::ExtractType(getInternalFormat(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0)) == GL_FLOAT && !mRenderer->getFloat32TextureSupport(&filtering, &renderable)) || (gl::ExtractType(getInternalFormat(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0) == GL_HALF_FLOAT_OES) && !mRenderer->getFloat16TextureSupport(&filtering, &renderable))) { if (mSamplerState.magFilter != GL_NEAREST || (mSamplerState.minFilter != GL_NEAREST && mSamplerState.minFilter != GL_NEAREST_MIPMAP_NEAREST)) { return false; } } if (!isPow2(size) && !mRenderer->getNonPower2TextureSupport()) { if (mSamplerState.wrapS != GL_CLAMP_TO_EDGE || mSamplerState.wrapT != GL_CLAMP_TO_EDGE || mipmapping) { return false; } } if (!mipmapping) { if (!isCubeComplete()) { return false; } } else { if (!isMipmapCubeComplete()) // Also tests for isCubeComplete() { return false; } } return true; } // Tests for cube texture completeness. [OpenGL ES 2.0.24] section 3.7.10 page 81. bool TextureCubeMap::isCubeComplete() const { if (mImageArray[0][0]->getWidth() <= 0 || mImageArray[0][0]->getHeight() != mImageArray[0][0]->getWidth()) { return false; } for (unsigned int face = 1; face < 6; face++) { if (mImageArray[face][0]->getWidth() != mImageArray[0][0]->getWidth() || mImageArray[face][0]->getWidth() != mImageArray[0][0]->getHeight() || mImageArray[face][0]->getInternalFormat() != mImageArray[0][0]->getInternalFormat()) { return false; } } return true; } bool TextureCubeMap::isMipmapCubeComplete() const { if (isImmutable()) { return true; } if (!isCubeComplete()) { return false; } GLsizei size = mImageArray[0][0]->getWidth(); int q = log2(size); for (int face = 0; face < 6; face++) { for (int level = 1; level <= q; level++) { if (mImageArray[face][level]->getInternalFormat() != mImageArray[0][0]->getInternalFormat()) { return false; } if (mImageArray[face][level]->getWidth() != std::max(1, size >> level)) { return false; } } } return true; } bool TextureCubeMap::isCompressed(GLenum target, GLint level) const { return IsCompressed(getInternalFormat(target, level)); } // Constructs a native texture resource from the texture images, or returns an existing one void TextureCubeMap::createTexture() { GLsizei size = mImageArray[0][0]->getWidth(); if (!(size > 0)) return; // do not attempt to create native textures for nonexistant data GLint levels = creationLevels(size); GLenum internalformat = mImageArray[0][0]->getInternalFormat(); delete mTexStorage; mTexStorage = new rx::TextureStorageInterfaceCube(mRenderer, levels, internalformat, mUsage, false, size); if (mTexStorage->isManaged()) { int levels = levelCount(); for (int face = 0; face < 6; face++) { for (int level = 0; level < levels; level++) { mImageArray[face][level]->setManagedSurface(mTexStorage, face, level); } } } mDirtyImages = true; } void TextureCubeMap::updateTexture() { bool mipmapping = isMipmapFiltered() && isMipmapCubeComplete(); for (int face = 0; face < 6; face++) { int levels = (mipmapping ? levelCount() : 1); for (int level = 0; level < levels; level++) { rx::Image *image = mImageArray[face][level]; if (image->isDirty()) { commitRect(face, level, 0, 0, image->getWidth(), image->getHeight()); } } } } void TextureCubeMap::convertToRenderTarget() { rx::TextureStorageInterfaceCube *newTexStorage = NULL; if (mImageArray[0][0]->getWidth() != 0) { GLsizei size = mImageArray[0][0]->getWidth(); GLint levels = mTexStorage != NULL ? mTexStorage->levelCount() : creationLevels(size); GLenum internalformat = mImageArray[0][0]->getInternalFormat(); newTexStorage = new rx::TextureStorageInterfaceCube(mRenderer, levels, internalformat, GL_FRAMEBUFFER_ATTACHMENT_ANGLE, true, size); if (mTexStorage != NULL) { if (!mRenderer->copyToRenderTarget(newTexStorage, mTexStorage)) { delete newTexStorage; return gl::error(GL_OUT_OF_MEMORY); } } } delete mTexStorage; mTexStorage = newTexStorage; mDirtyImages = true; } void TextureCubeMap::setImage(int faceIndex, GLint level, GLsizei width, GLsizei height, GLenum format, GLenum type, GLint unpackAlignment, const void *pixels) { GLint internalformat = ConvertSizedInternalFormat(format, type); redefineImage(faceIndex, level, internalformat, width, height); Texture::setImage(unpackAlignment, pixels, mImageArray[faceIndex][level]); } unsigned int TextureCubeMap::faceIndex(GLenum face) { META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_X - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 1); META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 2); META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 3); META_ASSERT(GL_TEXTURE_CUBE_MAP_POSITIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 4); META_ASSERT(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z - GL_TEXTURE_CUBE_MAP_POSITIVE_X == 5); return face - GL_TEXTURE_CUBE_MAP_POSITIVE_X; } void TextureCubeMap::redefineImage(int face, GLint level, GLint internalformat, GLsizei width, GLsizei height) { // If there currently is a corresponding storage texture image, it has these parameters const int storageWidth = std::max(1, mImageArray[0][0]->getWidth() >> level); const int storageHeight = std::max(1, mImageArray[0][0]->getHeight() >> level); const int storageFormat = mImageArray[0][0]->getInternalFormat(); mImageArray[face][level]->redefine(mRenderer, internalformat, width, height, false); if (mTexStorage) { const int storageLevels = mTexStorage->levelCount(); if ((level >= storageLevels && storageLevels != 0) || width != storageWidth || height != storageHeight || internalformat != storageFormat) // Discard mismatched storage { for (int i = 0; i < IMPLEMENTATION_MAX_TEXTURE_LEVELS; i++) { for (int f = 0; f < 6; f++) { mImageArray[f][i]->markDirty(); } } delete mTexStorage; mTexStorage = NULL; mDirtyImages = true; } } } void TextureCubeMap::copyImage(GLenum target, GLint level, GLenum format, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) { unsigned int faceindex = faceIndex(target); GLint internalformat = gl::ConvertSizedInternalFormat(format, GL_UNSIGNED_BYTE); redefineImage(faceindex, level, internalformat, width, height); if (!mImageArray[faceindex][level]->isRenderableFormat()) { mImageArray[faceindex][level]->copy(0, 0, x, y, width, height, source); mDirtyImages = true; } else { if (!mTexStorage || !mTexStorage->isRenderTarget()) { convertToRenderTarget(); } mImageArray[faceindex][level]->markClean(); ASSERT(width == height); if (width > 0 && level < levelCount()) { gl::Rectangle sourceRect; sourceRect.x = x; sourceRect.width = width; sourceRect.y = y; sourceRect.height = height; mRenderer->copyImage(source, sourceRect, format, 0, 0, mTexStorage, target, level); } } } void TextureCubeMap::copySubImage(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height, Framebuffer *source) { GLsizei size = mImageArray[faceIndex(target)][level]->getWidth(); if (xoffset + width > size || yoffset + height > size) { return gl::error(GL_INVALID_VALUE); } unsigned int faceindex = faceIndex(target); if (!mImageArray[faceindex][level]->isRenderableFormat() || (!mTexStorage && !isSamplerComplete())) { mImageArray[faceindex][level]->copy(0, 0, x, y, width, height, source); mDirtyImages = true; } else { if (!mTexStorage || !mTexStorage->isRenderTarget()) { convertToRenderTarget(); } updateTexture(); if (level < levelCount()) { gl::Rectangle sourceRect; sourceRect.x = x; sourceRect.width = width; sourceRect.y = y; sourceRect.height = height; mRenderer->copyImage(source, sourceRect, gl::ExtractFormat(mImageArray[0][0]->getInternalFormat()), xoffset, yoffset, mTexStorage, target, level); } } } void TextureCubeMap::storage(GLsizei levels, GLenum internalformat, GLsizei size) { delete mTexStorage; mTexStorage = new rx::TextureStorageInterfaceCube(mRenderer, levels, internalformat, mUsage, false, size); mImmutable = true; for (int level = 0; level < levels; level++) { for (int face = 0; face < 6; face++) { mImageArray[face][level]->redefine(mRenderer, internalformat, size, size, true); size = std::max(1, size >> 1); } } for (int level = levels; level < IMPLEMENTATION_MAX_TEXTURE_LEVELS; level++) { for (int face = 0; face < 6; face++) { mImageArray[face][level]->redefine(mRenderer, GL_NONE, 0, 0, true); } } if (mTexStorage->isManaged()) { int levels = levelCount(); for (int face = 0; face < 6; face++) { for (int level = 0; level < levels; level++) { mImageArray[face][level]->setManagedSurface(mTexStorage, face, level); } } } } void TextureCubeMap::generateMipmaps() { if (!isCubeComplete()) { return gl::error(GL_INVALID_OPERATION); } if (!mRenderer->getNonPower2TextureSupport()) { if (!isPow2(mImageArray[0][0]->getWidth())) { return gl::error(GL_INVALID_OPERATION); } } // Purge array levels 1 through q and reset them to represent the generated mipmap levels. unsigned int q = log2(mImageArray[0][0]->getWidth()); for (unsigned int f = 0; f < 6; f++) { for (unsigned int i = 1; i <= q; i++) { redefineImage(f, i, mImageArray[f][0]->getInternalFormat(), std::max(mImageArray[f][0]->getWidth() >> i, 1), std::max(mImageArray[f][0]->getWidth() >> i, 1)); } } if (mTexStorage && mTexStorage->isRenderTarget()) { for (unsigned int f = 0; f < 6; f++) { for (unsigned int i = 1; i <= q; i++) { mTexStorage->generateMipmap(f, i); mImageArray[f][i]->markClean(); } } } else { for (unsigned int f = 0; f < 6; f++) { for (unsigned int i = 1; i <= q; i++) { mRenderer->generateMipmap(mImageArray[f][i], mImageArray[f][i - 1]); } } } } Renderbuffer *TextureCubeMap::getRenderbuffer(GLenum target) { if (!IsCubemapTextureTarget(target)) { return gl::error(GL_INVALID_OPERATION, (Renderbuffer *)NULL); } unsigned int face = faceIndex(target); if (mFaceProxies[face] == NULL) { mFaceProxies[face] = new Renderbuffer(mRenderer, id(), new RenderbufferTextureCubeMap(this, target)); } return mFaceProxies[face]; } rx::RenderTarget *TextureCubeMap::getRenderTarget(GLenum target) { ASSERT(IsCubemapTextureTarget(target)); // ensure the underlying texture is created if (getStorage(true) == NULL) { return NULL; } updateTexture(); return mTexStorage->getRenderTarget(target); } int TextureCubeMap::levelCount() { return mTexStorage ? mTexStorage->levelCount() - getLodOffset() : 0; } rx::TextureStorageInterface *TextureCubeMap::getStorage(bool renderTarget) { if (!mTexStorage || (renderTarget && !mTexStorage->isRenderTarget())) { if (renderTarget) { convertToRenderTarget(); } else { createTexture(); } } return mTexStorage; } }
C++
// // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // // precompiled.h: Precompiled header file for libGLESv2. #define GL_APICALL #include <GLES2/gl2.h> #include <GLES2/gl2ext.h> #define EGLAPI #include <EGL/egl.h> #include <assert.h> #include <cstddef> #include <float.h> #include <intrin.h> #include <math.h> #include <stdarg.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <algorithm> // for std::min and std::max #include <limits> #include <map> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <vector> #include <d3d9.h> #include <D3D11.h> #include <dxgi.h> #include <D3Dcompiler.h> #ifdef _MSC_VER #include <hash_map> #endif
C++
// // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "GLSLANG/ShaderLang.h" #include <assert.h> #include <math.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <vector> // // Return codes from main. // enum TFailCode { ESuccess = 0, EFailUsage, EFailCompile, EFailCompilerCreate, }; static void usage(); static ShShaderType FindShaderType(const char* fileName); static bool CompileFile(char* fileName, ShHandle compiler, int compileOptions); static void LogMsg(const char* msg, const char* name, const int num, const char* logName); static void PrintActiveVariables(ShHandle compiler, ShShaderInfo varType, bool mapLongVariableNames); // If NUM_SOURCE_STRINGS is set to a value > 1, the input file data is // broken into that many chunks. const unsigned int NUM_SOURCE_STRINGS = 2; typedef std::vector<char*> ShaderSource; static bool ReadShaderSource(const char* fileName, ShaderSource& source); static void FreeShaderSource(ShaderSource& source); // // Set up the per compile resources // void GenerateResources(ShBuiltInResources* resources) { ShInitBuiltInResources(resources); resources->MaxVertexAttribs = 8; resources->MaxVertexUniformVectors = 128; resources->MaxVaryingVectors = 8; resources->MaxVertexTextureImageUnits = 0; resources->MaxCombinedTextureImageUnits = 8; resources->MaxTextureImageUnits = 8; resources->MaxFragmentUniformVectors = 16; resources->MaxDrawBuffers = 1; resources->OES_standard_derivatives = 0; resources->OES_EGL_image_external = 0; } int main(int argc, char* argv[]) { TFailCode failCode = ESuccess; int compileOptions = 0; int numCompiles = 0; ShHandle vertexCompiler = 0; ShHandle fragmentCompiler = 0; char* buffer = 0; size_t bufferLen = 0; int numAttribs = 0, numUniforms = 0; ShShaderSpec spec = SH_GLES2_SPEC; ShShaderOutput output = SH_ESSL_OUTPUT; ShInitialize(); ShBuiltInResources resources; GenerateResources(&resources); argc--; argv++; for (; (argc >= 1) && (failCode == ESuccess); argc--, argv++) { if (argv[0][0] == '-') { switch (argv[0][1]) { case 'i': compileOptions |= SH_INTERMEDIATE_TREE; break; case 'm': compileOptions |= SH_MAP_LONG_VARIABLE_NAMES; break; case 'o': compileOptions |= SH_OBJECT_CODE; break; case 'u': compileOptions |= SH_ATTRIBUTES_UNIFORMS; break; case 'l': compileOptions |= SH_UNROLL_FOR_LOOP_WITH_INTEGER_INDEX; break; case 'e': compileOptions |= SH_EMULATE_BUILT_IN_FUNCTIONS; break; case 'd': compileOptions |= SH_DEPENDENCY_GRAPH; break; case 't': compileOptions |= SH_TIMING_RESTRICTIONS; break; case 's': if (argv[0][2] == '=') { switch (argv[0][3]) { case 'e': spec = SH_GLES2_SPEC; break; case 'w': spec = SH_WEBGL_SPEC; break; case 'c': spec = SH_CSS_SHADERS_SPEC; break; default: failCode = EFailUsage; } } else { failCode = EFailUsage; } break; case 'b': if (argv[0][2] == '=') { switch (argv[0][3]) { case 'e': output = SH_ESSL_OUTPUT; break; case 'g': output = SH_GLSL_OUTPUT; break; case 'h': if (argv[0][4] == '1' && argv[0][5] == '1') { output = SH_HLSL11_OUTPUT; } else { output = SH_HLSL9_OUTPUT; } break; default: failCode = EFailUsage; } } else { failCode = EFailUsage; } break; case 'x': if (argv[0][2] == '=') { switch (argv[0][3]) { case 'i': resources.OES_EGL_image_external = 1; break; case 'd': resources.OES_standard_derivatives = 1; break; case 'r': resources.ARB_texture_rectangle = 1; break; default: failCode = EFailUsage; } } else { failCode = EFailUsage; } break; default: failCode = EFailUsage; } } else { ShHandle compiler = 0; switch (FindShaderType(argv[0])) { case SH_VERTEX_SHADER: if (vertexCompiler == 0) vertexCompiler = ShConstructCompiler( SH_VERTEX_SHADER, spec, output, &resources); compiler = vertexCompiler; break; case SH_FRAGMENT_SHADER: if (fragmentCompiler == 0) fragmentCompiler = ShConstructCompiler( SH_FRAGMENT_SHADER, spec, output, &resources); compiler = fragmentCompiler; break; default: break; } if (compiler) { bool compiled = CompileFile(argv[0], compiler, compileOptions); LogMsg("BEGIN", "COMPILER", numCompiles, "INFO LOG"); ShGetInfo(compiler, SH_INFO_LOG_LENGTH, &bufferLen); buffer = (char*) realloc(buffer, bufferLen * sizeof(char)); ShGetInfoLog(compiler, buffer); puts(buffer); LogMsg("END", "COMPILER", numCompiles, "INFO LOG"); printf("\n\n"); if (compiled && (compileOptions & SH_OBJECT_CODE)) { LogMsg("BEGIN", "COMPILER", numCompiles, "OBJ CODE"); ShGetInfo(compiler, SH_OBJECT_CODE_LENGTH, &bufferLen); buffer = (char*) realloc(buffer, bufferLen * sizeof(char)); ShGetObjectCode(compiler, buffer); puts(buffer); LogMsg("END", "COMPILER", numCompiles, "OBJ CODE"); printf("\n\n"); } if (compiled && (compileOptions & SH_ATTRIBUTES_UNIFORMS)) { LogMsg("BEGIN", "COMPILER", numCompiles, "ACTIVE ATTRIBS"); PrintActiveVariables(compiler, SH_ACTIVE_ATTRIBUTES, (compileOptions & SH_MAP_LONG_VARIABLE_NAMES) != 0); LogMsg("END", "COMPILER", numCompiles, "ACTIVE ATTRIBS"); printf("\n\n"); LogMsg("BEGIN", "COMPILER", numCompiles, "ACTIVE UNIFORMS"); PrintActiveVariables(compiler, SH_ACTIVE_UNIFORMS, (compileOptions & SH_MAP_LONG_VARIABLE_NAMES) != 0); LogMsg("END", "COMPILER", numCompiles, "ACTIVE UNIFORMS"); printf("\n\n"); } if (!compiled) failCode = EFailCompile; ++numCompiles; } else { failCode = EFailCompilerCreate; } } } if ((vertexCompiler == 0) && (fragmentCompiler == 0)) failCode = EFailUsage; if (failCode == EFailUsage) usage(); if (vertexCompiler) ShDestruct(vertexCompiler); if (fragmentCompiler) ShDestruct(fragmentCompiler); if (buffer) free(buffer); ShFinalize(); return failCode; } // // print usage to stdout // void usage() { printf("Usage: translate [-i -m -o -u -l -e -b=e -b=g -b=h -x=i -x=d] file1 file2 ...\n" "Where: filename : filename ending in .frag or .vert\n" " -i : print intermediate tree\n" " -m : map long variable names\n" " -o : print translated code\n" " -u : print active attribs and uniforms\n" " -l : unroll for-loops with integer indices\n" " -e : emulate certain built-in functions (workaround for driver bugs)\n" " -t : enforce experimental timing restrictions\n" " -d : print dependency graph used to enforce timing restrictions\n" " -s=e : use GLES2 spec (this is by default)\n" " -s=w : use WebGL spec\n" " -s=c : use CSS Shaders spec\n" " -b=e : output GLSL ES code (this is by default)\n" " -b=g : output GLSL code\n" " -b=h9 : output HLSL9 code\n" " -b=h11 : output HLSL11 code\n" " -x=i : enable GL_OES_EGL_image_external\n" " -x=d : enable GL_OES_EGL_standard_derivatives\n" " -x=r : enable ARB_texture_rectangle\n"); } // // Deduce the shader type from the filename. Files must end in one of the // following extensions: // // .frag* = fragment shader // .vert* = vertex shader // ShShaderType FindShaderType(const char* fileName) { assert(fileName); const char* ext = strrchr(fileName, '.'); if (ext && strcmp(ext, ".sl") == 0) for (; ext > fileName && ext[0] != '.'; ext--); ext = strrchr(fileName, '.'); if (ext) { if (strncmp(ext, ".frag", 4) == 0) return SH_FRAGMENT_SHADER; if (strncmp(ext, ".vert", 4) == 0) return SH_VERTEX_SHADER; } return SH_FRAGMENT_SHADER; } // // Read a file's data into a string, and compile it using ShCompile // bool CompileFile(char* fileName, ShHandle compiler, int compileOptions) { ShaderSource source; if (!ReadShaderSource(fileName, source)) return false; int ret = ShCompile(compiler, &source[0], source.size(), compileOptions); FreeShaderSource(source); return ret ? true : false; } void LogMsg(const char* msg, const char* name, const int num, const char* logName) { printf("#### %s %s %d %s ####\n", msg, name, num, logName); } void PrintActiveVariables(ShHandle compiler, ShShaderInfo varType, bool mapLongVariableNames) { size_t nameSize = 0; switch (varType) { case SH_ACTIVE_ATTRIBUTES: ShGetInfo(compiler, SH_ACTIVE_ATTRIBUTE_MAX_LENGTH, &nameSize); break; case SH_ACTIVE_UNIFORMS: ShGetInfo(compiler, SH_ACTIVE_UNIFORM_MAX_LENGTH, &nameSize); break; default: assert(0); } if (nameSize <= 1) return; char* name = new char[nameSize]; char* mappedName = NULL; if (mapLongVariableNames) { size_t mappedNameSize = 0; ShGetInfo(compiler, SH_MAPPED_NAME_MAX_LENGTH, &mappedNameSize); mappedName = new char[mappedNameSize]; } size_t activeVars = 0; int size = 0; ShDataType type = SH_NONE; const char* typeName = NULL; ShGetInfo(compiler, varType, &activeVars); for (size_t i = 0; i < activeVars; ++i) { switch (varType) { case SH_ACTIVE_ATTRIBUTES: ShGetActiveAttrib(compiler, static_cast<int>(i), NULL, &size, &type, name, mappedName); break; case SH_ACTIVE_UNIFORMS: ShGetActiveUniform(compiler, static_cast<int>(i), NULL, &size, &type, name, mappedName); break; default: assert(0); } switch (type) { case SH_FLOAT: typeName = "GL_FLOAT"; break; case SH_FLOAT_VEC2: typeName = "GL_FLOAT_VEC2"; break; case SH_FLOAT_VEC3: typeName = "GL_FLOAT_VEC3"; break; case SH_FLOAT_VEC4: typeName = "GL_FLOAT_VEC4"; break; case SH_INT: typeName = "GL_INT"; break; case SH_INT_VEC2: typeName = "GL_INT_VEC2"; break; case SH_INT_VEC3: typeName = "GL_INT_VEC3"; break; case SH_INT_VEC4: typeName = "GL_INT_VEC4"; break; case SH_BOOL: typeName = "GL_BOOL"; break; case SH_BOOL_VEC2: typeName = "GL_BOOL_VEC2"; break; case SH_BOOL_VEC3: typeName = "GL_BOOL_VEC3"; break; case SH_BOOL_VEC4: typeName = "GL_BOOL_VEC4"; break; case SH_FLOAT_MAT2: typeName = "GL_FLOAT_MAT2"; break; case SH_FLOAT_MAT3: typeName = "GL_FLOAT_MAT3"; break; case SH_FLOAT_MAT4: typeName = "GL_FLOAT_MAT4"; break; case SH_SAMPLER_2D: typeName = "GL_SAMPLER_2D"; break; case SH_SAMPLER_CUBE: typeName = "GL_SAMPLER_CUBE"; break; case SH_SAMPLER_EXTERNAL_OES: typeName = "GL_SAMPLER_EXTERNAL_OES"; break; default: assert(0); } printf("%u: name:%s type:%s size:%d", i, name, typeName, size); if (mapLongVariableNames) printf(" mapped name:%s", mappedName); printf("\n"); } delete [] name; if (mappedName) delete [] mappedName; } static bool ReadShaderSource(const char* fileName, ShaderSource& source) { FILE* in = fopen(fileName, "rb"); if (!in) { printf("Error: unable to open input file: %s\n", fileName); return false; } // Obtain file size. fseek(in, 0, SEEK_END); int count = ftell(in); rewind(in); int len = (int)ceil((float)count / (float)NUM_SOURCE_STRINGS); source.reserve(NUM_SOURCE_STRINGS); // Notice the usage of do-while instead of a while loop here. // It is there to handle empty files in which case a single empty // string is added to vector. do { char* data = new char[len + 1]; int nread = fread(data, 1, len, in); data[nread] = '\0'; source.push_back(data); count -= nread; } while (count > 0); fclose(in); return true; } static void FreeShaderSource(ShaderSource& source) { for (ShaderSource::size_type i = 0; i < source.size(); ++i) { delete [] source[i]; } source.clear(); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" class LocationTest : public PreprocessorTest { protected: void expectLocation(int count, const char* const string[], const int length[], const pp::SourceLocation& location) { ASSERT_TRUE(mPreprocessor.init(count, string, length)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::IDENTIFIER, token.type); EXPECT_EQ("foo", token.text); EXPECT_EQ(location.file, token.location.file); EXPECT_EQ(location.line, token.location.line); } }; TEST_F(LocationTest, String0_Line1) { const char* str = "foo"; pp::SourceLocation loc(0, 1); SCOPED_TRACE("String0_Line1"); expectLocation(1, &str, NULL, loc); } TEST_F(LocationTest, String0_Line2) { const char* str = "\nfoo"; pp::SourceLocation loc(0, 2); SCOPED_TRACE("String0_Line2"); expectLocation(1, &str, NULL, loc); } TEST_F(LocationTest, String1_Line1) { const char* const str[] = {"\n\n", "foo"}; pp::SourceLocation loc(1, 1); SCOPED_TRACE("String1_Line1"); expectLocation(2, str, NULL, loc); } TEST_F(LocationTest, String1_Line2) { const char* const str[] = {"\n\n", "\nfoo"}; pp::SourceLocation loc(1, 2); SCOPED_TRACE("String1_Line2"); expectLocation(2, str, NULL, loc); } TEST_F(LocationTest, NewlineInsideCommentCounted) { const char* str = "/*\n\n*/foo"; pp::SourceLocation loc(0, 3); SCOPED_TRACE("NewlineInsideCommentCounted"); expectLocation(1, &str, NULL, loc); } TEST_F(LocationTest, ErrorLocationAfterComment) { const char* str = "/*\n\n*/@"; ASSERT_TRUE(mPreprocessor.init(1, &str, NULL)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::INVALID_CHARACTER, pp::SourceLocation(0, 3), "@")); pp::Token token; mPreprocessor.lex(&token); } // The location of a token straddling two or more strings is that of the // first character of the token. TEST_F(LocationTest, TokenStraddlingTwoStrings) { const char* const str[] = {"f", "oo"}; pp::SourceLocation loc(0, 1); SCOPED_TRACE("TokenStraddlingTwoStrings"); expectLocation(2, str, NULL, loc); } TEST_F(LocationTest, TokenStraddlingThreeStrings) { const char* const str[] = {"f", "o", "o"}; pp::SourceLocation loc(0, 1); SCOPED_TRACE("TokenStraddlingThreeStrings"); expectLocation(3, str, NULL, loc); } TEST_F(LocationTest, EndOfFileWithoutNewline) { const char* const str[] = {"foo"}; ASSERT_TRUE(mPreprocessor.init(1, str, NULL)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::IDENTIFIER, token.type); EXPECT_EQ("foo", token.text); EXPECT_EQ(0, token.location.file); EXPECT_EQ(1, token.location.line); mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::LAST, token.type); EXPECT_EQ(0, token.location.file); EXPECT_EQ(1, token.location.line); } TEST_F(LocationTest, EndOfFileAfterNewline) { const char* const str[] = {"foo\n"}; ASSERT_TRUE(mPreprocessor.init(1, str, NULL)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::IDENTIFIER, token.type); EXPECT_EQ("foo", token.text); EXPECT_EQ(0, token.location.file); EXPECT_EQ(1, token.location.line); mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::LAST, token.type); EXPECT_EQ(0, token.location.file); EXPECT_EQ(2, token.location.line); } TEST_F(LocationTest, EndOfFileAfterEmptyString) { const char* const str[] = {"foo\n", "\n", ""}; ASSERT_TRUE(mPreprocessor.init(3, str, NULL)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::IDENTIFIER, token.type); EXPECT_EQ("foo", token.text); EXPECT_EQ(0, token.location.file); EXPECT_EQ(1, token.location.line); mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::LAST, token.type); EXPECT_EQ(2, token.location.file); EXPECT_EQ(1, token.location.line); } TEST_F(LocationTest, ValidLineDirective1) { const char* str = "#line 10\n" "foo"; pp::SourceLocation loc(0, 10); SCOPED_TRACE("ValidLineDirective1"); expectLocation(1, &str, NULL, loc); } TEST_F(LocationTest, ValidLineDirective2) { const char* str = "#line 10 20\n" "foo"; pp::SourceLocation loc(20, 10); SCOPED_TRACE("ValidLineDirective2"); expectLocation(1, &str, NULL, loc); } TEST_F(LocationTest, LineDirectiveCommentsIgnored) { const char* str = "/* bar */" "#" "/* bar */" "line" "/* bar */" "10" "/* bar */" "20" "/* bar */" "// bar " "\n" "foo"; pp::SourceLocation loc(20, 10); SCOPED_TRACE("LineDirectiveCommentsIgnored"); expectLocation(1, &str, NULL, loc); } TEST_F(LocationTest, LineDirectiveWithMacro1) { const char* str = "#define L 10\n" "#define F(x) x\n" "#line L F(20)\n" "foo"; pp::SourceLocation loc(20, 10); SCOPED_TRACE("LineDirectiveWithMacro1"); expectLocation(1, &str, NULL, loc); } TEST_F(LocationTest, LineDirectiveWithMacro2) { const char* str = "#define LOC 10 20\n" "#line LOC\n" "foo"; pp::SourceLocation loc(20, 10); SCOPED_TRACE("LineDirectiveWithMacro2"); expectLocation(1, &str, NULL, loc); } TEST_F(LocationTest, LineDirectiveWithPredefinedMacro) { const char* str = "#line __LINE__ __FILE__\n" "foo"; pp::SourceLocation loc(0, 1); SCOPED_TRACE("LineDirectiveWithMacro"); expectLocation(1, &str, NULL, loc); } TEST_F(LocationTest, LineDirectiveNewlineBeforeStringBreak) { const char* const str[] = {"#line 10 20\n", "foo"}; // String number is incremented after it is set by the line directive. // Also notice that line number is reset after the string break. pp::SourceLocation loc(21, 1); SCOPED_TRACE("LineDirectiveNewlineBeforeStringBreak"); expectLocation(2, str, NULL, loc); } TEST_F(LocationTest, LineDirectiveNewlineAfterStringBreak) { const char* const str[] = {"#line 10 20", "\nfoo"}; // String number is incremented before it is set by the line directive. pp::SourceLocation loc(20, 10); SCOPED_TRACE("LineDirectiveNewlineAfterStringBreak"); expectLocation(2, str, NULL, loc); } TEST_F(LocationTest, LineDirectiveMissingNewline) { const char* str = "#line 10"; ASSERT_TRUE(mPreprocessor.init(1, &str, NULL)); using testing::_; // Error reported about EOF. EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::EOF_IN_DIRECTIVE, _, _)); pp::Token token; mPreprocessor.lex(&token); } struct LineTestParam { const char* str; pp::Diagnostics::ID id; }; class InvalidLineTest : public LocationTest, public testing::WithParamInterface<LineTestParam> { }; TEST_P(InvalidLineTest, Identified) { LineTestParam param = GetParam(); ASSERT_TRUE(mPreprocessor.init(1, &param.str, NULL)); using testing::_; // Invalid line directive call. EXPECT_CALL(mDiagnostics, print(param.id, pp::SourceLocation(0, 1), _)); pp::Token token; mPreprocessor.lex(&token); } static const LineTestParam kParams[] = { {"#line\n", pp::Diagnostics::INVALID_LINE_DIRECTIVE}, {"#line foo\n", pp::Diagnostics::INVALID_LINE_NUMBER}, {"#line 10 foo\n", pp::Diagnostics::INVALID_FILE_NUMBER}, {"#line 10 20 foo\n", pp::Diagnostics::UNEXPECTED_TOKEN}, {"#line 0xffffffff\n", pp::Diagnostics::INTEGER_OVERFLOW}, {"#line 10 0xffffffff\n", pp::Diagnostics::INTEGER_OVERFLOW} }; INSTANTIATE_TEST_CASE_P(All, InvalidLineTest, testing::ValuesIn(kParams));
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #ifndef PREPROCESSOR_TESTS_MOCK_DIRECTIVE_HANDLER_H_ #define PREPROCESSOR_TESTS_MOCK_DIRECTIVE_HANDLER_H_ #include "gmock/gmock.h" #include "DirectiveHandlerBase.h" class MockDirectiveHandler : public pp::DirectiveHandler { public: MOCK_METHOD2(handleError, void(const pp::SourceLocation& loc, const std::string& msg)); MOCK_METHOD3(handlePragma, void(const pp::SourceLocation& loc, const std::string& name, const std::string& value)); MOCK_METHOD3(handleExtension, void(const pp::SourceLocation& loc, const std::string& name, const std::string& behavior)); MOCK_METHOD2(handleVersion, void(const pp::SourceLocation& loc, int version)); }; #endif // PREPROCESSOR_TESTS_MOCK_DIRECTIVE_HANDLER_H_
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" class DefineTest : public PreprocessorTest { }; TEST_F(DefineTest, NonIdentifier) { const char* input = "#define 2 foo\n" "2\n"; const char* expected = "\n" "2\n"; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::UNEXPECTED_TOKEN, pp::SourceLocation(0, 1), "2")); preprocess(input, expected); }; TEST_F(DefineTest, RedefinePredefined) { const char* input = "#define __LINE__ 10\n" "__LINE__\n" "#define __FILE__ 20\n" "__FILE__\n" "#define __VERSION__ 200\n" "__VERSION__\n" "#define GL_ES 0\n" "GL_ES\n"; const char* expected = "\n" "2\n" "\n" "0\n" "\n" "100\n" "\n" "1\n"; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_PREDEFINED_REDEFINED, pp::SourceLocation(0, 1), "__LINE__")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_PREDEFINED_REDEFINED, pp::SourceLocation(0, 3), "__FILE__")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_PREDEFINED_REDEFINED, pp::SourceLocation(0, 5), "__VERSION__")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_PREDEFINED_REDEFINED, pp::SourceLocation(0, 7), "GL_ES")); preprocess(input, expected); } TEST_F(DefineTest, ReservedUnderScore1) { const char* input = "#define __foo bar\n" "__foo\n"; const char* expected = "\n" "__foo\n"; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_NAME_RESERVED, pp::SourceLocation(0, 1), "__foo")); preprocess(input, expected); } TEST_F(DefineTest, ReservedUnderScore2) { const char* input = "#define foo__bar baz\n" "foo__bar\n"; const char* expected = "\n" "foo__bar\n"; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_NAME_RESERVED, pp::SourceLocation(0, 1), "foo__bar")); preprocess(input, expected); } TEST_F(DefineTest, ReservedGL) { const char* input = "#define GL_foo bar\n" "GL_foo\n"; const char* expected = "\n" "GL_foo\n"; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_NAME_RESERVED, pp::SourceLocation(0, 1), "GL_foo")); preprocess(input, expected); } TEST_F(DefineTest, ObjRedefineValid) { const char* input = "#define foo (1-1)\n" "#define foo /* whitespace */ (1-1) /* other */ \n" "foo\n"; const char* expected = "\n" "\n" "(1-1)\n"; // No error or warning. using testing::_; EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(input, expected); } TEST_F(DefineTest, ObjRedefineInvalid) { const char* input = "#define foo (0)\n" "#define foo (1-1)\n" "foo\n"; const char* expected = "\n" "\n" "(0)\n"; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_REDEFINED, pp::SourceLocation(0, 2), "foo")); preprocess(input, expected); } TEST_F(DefineTest, FuncRedefineValid) { const char* input = "#define foo(a) ( a )\n" "#define foo( a )( /* whitespace */ a /* other */ )\n" "foo(b)\n"; const char* expected = "\n" "\n" "( b )\n"; // No error or warning. using testing::_; EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(input, expected); } TEST_F(DefineTest, FuncRedefineInvalid) { const char* input = "#define foo(b) ( a )\n" "#define foo(b) ( b )\n" "foo(1)\n"; const char* expected = "\n" "\n" "( a )\n"; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_REDEFINED, pp::SourceLocation(0, 2), "foo")); preprocess(input, expected); } TEST_F(DefineTest, ObjBasic) { const char* input = "#define foo 1\n" "foo\n"; const char* expected = "\n" "1\n"; preprocess(input, expected); } TEST_F(DefineTest, ObjEmpty) { const char* input = "#define foo\n" "foo\n"; const char* expected = "\n" "\n"; preprocess(input, expected); } TEST_F(DefineTest, ObjChain) { const char* input = "#define foo 1\n" "#define bar foo\n" "bar\n"; const char* expected = "\n" "\n" "1\n"; preprocess(input, expected); } TEST_F(DefineTest, ObjChainReverse) { const char* input = "#define bar foo\n" "#define foo 1\n" "bar\n"; const char* expected = "\n" "\n" "1\n"; preprocess(input, expected); } TEST_F(DefineTest, ObjRecursive) { const char* input = "#define foo bar\n" "#define bar baz\n" "#define baz foo\n" "foo\n" "bar\n" "baz\n"; const char* expected = "\n" "\n" "\n" "foo\n" "bar\n" "baz\n"; preprocess(input, expected); } TEST_F(DefineTest, ObjCompositeChain) { const char* input = "#define foo 1\n" "#define bar a foo\n" "bar\n"; const char* expected = "\n" "\n" "a 1\n"; preprocess(input, expected); } TEST_F(DefineTest, ObjCompositeChainReverse) { const char* input = "#define bar a foo\n" "#define foo 1\n" "bar\n"; const char* expected = "\n" "\n" "a 1\n"; preprocess(input, expected); } TEST_F(DefineTest, ObjCompositeRecursive) { const char* input = "#define foo a bar\n" "#define bar b baz\n" "#define baz c foo\n" "foo\n" "bar\n" "baz\n"; const char* expected = "\n" "\n" "\n" "a b c foo\n" "b c a bar\n" "c a b baz\n"; preprocess(input, expected); } TEST_F(DefineTest, ObjChainSelfRecursive) { const char* input = "#define foo foo\n" "#define bar foo\n" "bar\n"; const char* expected = "\n" "\n" "foo\n"; preprocess(input, expected); } TEST_F(DefineTest, ObjectLikeWithParens) { const char* input = "#define foo ()1\n" "foo()\n" "#define bar ()2\n" "bar()\n"; const char* expected = "\n" "()1()\n" "\n" "()2()\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncEmpty) { const char* input = "#define foo()\n" "foo()\n"; const char* expected = "\n" "\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncNoArgs) { const char* input = "#define foo() bar\n" "foo()\n"; const char* expected = "\n" "bar\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncOneArgUnused) { const char* input = "#define foo(x) 1\n" "foo(bar)\n"; const char* expected = "\n" "1\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncTwoArgsUnused) { const char* input = "#define foo(x,y) 1\n" "foo(bar,baz)\n"; const char* expected = "\n" "1\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncOneArg) { const char* input = "#define foo(x) ((x)+1)\n" "foo(bar)\n"; const char* expected = "\n" "((bar)+1)\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncTwoArgs) { const char* input = "#define foo(x,y) ((x)*(y))\n" "foo(bar,baz)\n"; const char* expected = "\n" "((bar)*(baz))\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncEmptyArgs) { const char* input = "#define zero() pass\n" "#define one(x) pass\n" "#define two(x,y) pass\n" "zero()\n" "one()\n" "two(,)\n"; const char* expected = "\n" "\n" "\n" "pass\n" "pass\n" "pass\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncMacroAsParam) { const char* input = "#define x 0\n" "#define foo(x) x\n" "foo(1)\n"; const char* expected = "\n" "\n" "1\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncOneArgMulti) { const char* input = "#define foo(x) (x)\n" "foo(this is a multi-word argument)\n"; const char* expected = "\n" "(this is a multi-word argument)\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncTwoArgsMulti) { const char* input = "#define foo(x,y) x,two fish,red fish,y\n" "foo(one fish, blue fish)\n"; const char* expected = "\n" "one fish,two fish,red fish,blue fish\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncCompose) { const char* input = "#define bar(x) (1+(x))\n" "#define foo(y) (2*(y))\n" "foo(bar(3))\n"; const char* expected = "\n" "\n" "(2*((1+(3))))\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncArgWithParens) { const char* input = "#define foo(x) (x)\n" "foo(argument(with parens) FTW)\n"; const char* expected = "\n" "(argument(with parens) FTW)\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncMacroAsNonMacro) { const char* input = "#define foo(bar) bar\n" "foo bar\n"; const char* expected = "\n" "foo bar\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncExtraNewlines) { const char* input = "#define foo(a) (a)\n" "foo\n" "(\n" "1\n" ")\n"; const char* expected = "\n" "(1)\n" "\n" "\n" "\n"; preprocess(input, expected); } TEST_F(DefineTest, ChainObjToFunc) { const char* input = "#define foo() pass\n" "#define bar foo()\n" "bar\n"; const char* expected = "\n" "\n" "pass\n"; preprocess(input, expected); } TEST_F(DefineTest, ChainObjToNonFunc) { const char* input = "#define pass() fail\n" "#define bar pass\n" "bar\n"; const char* expected = "\n" "\n" "pass\n"; preprocess(input, expected); } TEST_F(DefineTest, ChainObjToFuncWithArgs) { const char* input = "#define foo(fail) fail\n" "#define bar foo(pass)\n" "bar\n"; const char* expected = "\n" "\n" "pass\n"; preprocess(input, expected); } TEST_F(DefineTest, ChainObjToFuncCompose) { const char* input = "#define baz(fail) fail\n" "#define bar(fail) fail\n" "#define foo bar(baz(pass))\n" "foo\n"; const char* expected = "\n" "\n" "\n" "pass\n"; preprocess(input, expected); } TEST_F(DefineTest, ChainObjToFuncParensInText1) { const char* input = "#define fail() pass\n" "#define foo fail\n" "foo()\n"; const char* expected = "\n" "\n" "pass\n"; preprocess(input, expected); } TEST_F(DefineTest, ChainObjToFuncParensInText2) { const char* input = "#define bar with,embedded,commas\n" "#define func(x) pass\n" "#define foo func\n" "foo(bar)\n"; const char* expected = "\n" "\n" "\n" "pass\n"; preprocess(input, expected); } TEST_F(DefineTest, ChainObjToFuncMultiLevel) { const char* input = "#define foo(x) pass\n" "#define bar foo\n" "#define baz bar\n" "#define joe baz\n" "joe (fail)\n"; const char* expected = "\n" "\n" "\n" "\n" "pass\n"; preprocess(input, expected); } TEST_F(DefineTest, ObjToFuncRecursive) { const char* input = "#define A(a,b) B(a,b)\n" "#define C A(0,C)\n" "C\n"; const char* expected = "\n" "\n" "B(0,C)\n"; preprocess(input, expected); } TEST_F(DefineTest, ChainFuncToFuncCompose) { const char* input = "#define baz(fail) fail\n" "#define bar(fail) fail\n" "#define foo() bar(baz(pass))\n" "foo()\n"; const char* expected = "\n" "\n" "\n" "pass\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncSelfRecursive) { const char* input = "#define foo(a) foo(2*(a))\n" "foo(3)\n"; const char* expected = "\n" "foo(2*(3))\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncSelfCompose) { const char* input = "#define foo(a) foo(2*(a))\n" "foo(foo(3))\n"; const char* expected = "\n" "foo(2*(foo(2*(3))))\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncSelfComposeNonFunc) { const char* input = "#define foo(bar) bar\n" "foo(foo)\n"; const char* expected = "\n" "foo\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncSelfComposeNonFuncMultiTokenArg) { const char* input = "#define foo(bar) bar\n" "foo(1+foo)\n"; const char* expected = "\n" "1+foo\n"; preprocess(input, expected); } TEST_F(DefineTest, FinalizeUnexpandedMacro) { const char* input = "#define expand(x) expand(x once)\n" "#define foo(x) x\n" "foo(expand(just))\n"; const char* expected = "\n" "\n" "expand(just once)\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncArgWithCommas) { const char* input = "#define foo(x) pass\n" "foo(argument (with,embedded, commas) -- baz)\n"; const char* expected = "\n" "pass\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncArgObjMaroWithComma) { const char* input = "#define foo(a) (a)\n" "#define bar two,words\n" "foo(bar)\n"; const char* expected = "\n" "\n" "(two,words)\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncLeftParenInMacroRightParenInText) { const char* input = "#define bar(a) a*2\n" "#define foo bar(\n" "foo b)\n"; const char* expected = "\n" "\n" "b*2\n"; preprocess(input, expected); } TEST_F(DefineTest, RepeatedArg) { const char* input = "#define double(x) x x\n" "double(1)\n"; const char* expected = "\n" "1 1\n"; preprocess(input, expected); } TEST_F(DefineTest, FuncMissingRightParen) { const char* input = "#define foo(x) (2*(x))\n" "foo(3\n"; const char* expected = "\n" "\n"; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_UNTERMINATED_INVOCATION, pp::SourceLocation(0, 2), "foo")); preprocess(input, expected); } TEST_F(DefineTest, FuncIncorrectArgCount) { const char* input = "#define foo(x,y) ((x)+(y))\n" "foo()\n" "foo(1)\n" "foo(1,2,3)\n"; const char* expected = "\n" "\n" "\n" "\n"; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_TOO_FEW_ARGS, pp::SourceLocation(0, 2), "foo")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_TOO_FEW_ARGS, pp::SourceLocation(0, 3), "foo")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_TOO_MANY_ARGS, pp::SourceLocation(0, 4), "foo")); preprocess(input, expected); } TEST_F(DefineTest, Undef) { const char* input = "#define foo 1\n" "foo\n" "#undef foo\n" "foo\n"; const char* expected = "\n" "1\n" "\n" "foo\n"; preprocess(input, expected); } TEST_F(DefineTest, UndefPredefined) { const char* input = "#undef __LINE__\n" "__LINE__\n" "#undef __FILE__\n" "__FILE__\n" "#undef __VERSION__\n" "__VERSION__\n" "#undef GL_ES\n" "GL_ES\n"; const char* expected = "\n" "2\n" "\n" "0\n" "\n" "100\n" "\n" "1\n"; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_PREDEFINED_UNDEFINED, pp::SourceLocation(0, 1), "__LINE__")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_PREDEFINED_UNDEFINED, pp::SourceLocation(0, 3), "__FILE__")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_PREDEFINED_UNDEFINED, pp::SourceLocation(0, 5), "__VERSION__")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::MACRO_PREDEFINED_UNDEFINED, pp::SourceLocation(0, 7), "GL_ES")); preprocess(input, expected); } TEST_F(DefineTest, UndefRedefine) { const char* input = "#define foo 1\n" "foo\n" "#undef foo\n" "foo\n" "#define foo 2\n" "foo\n"; const char* expected = "\n" "1\n" "\n" "foo\n" "\n" "2\n"; preprocess(input, expected); } TEST_F(DefineTest, C99Example) { const char* input = "#define x 3 \n" "#define f(a) f(x * (a)) \n" "#undef x \n" "#define x 2 \n" "#define g f \n" "#define z z[0] \n" "#define h g(~ \n" "#define m(a) a(w) \n" "#define w 0,1 \n" "#define t(a) a \n" "#define p() int \n" "#define q(x) x \n" " \n" "f(y+1) + f(f(z)) % t(t(g)(0) + t)(1);\n" "g(x+(3,4)-w) | h 5) & m\n" " (f)^m(m);\n" "p() i[q()] = { q(1), 23, 4, 5, };\n"; const char* expected = "\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "f(2 * (y+1)) + f(2 * (f(2 * (z[0])))) % f(2 * (0)) + t(1);\n" "f(2 * (2+(3,4)-0,1)) | f(2 * (~ 5)) & f(2 * (0,1))\n" "^m(0,1);\n" "int i[] = { 1, 23, 4, 5, };\n"; preprocess(input, expected); } TEST_F(DefineTest, Predefined_GL_ES) { const char* input = "GL_ES\n"; const char* expected = "1\n"; preprocess(input, expected); } TEST_F(DefineTest, Predefined_VERSION) { const char* input = "__VERSION__\n"; const char* expected = "100\n"; preprocess(input, expected); } TEST_F(DefineTest, Predefined_LINE1) { const char* str = "\n\n__LINE__"; ASSERT_TRUE(mPreprocessor.init(1, &str, NULL)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::CONST_INT, token.type); EXPECT_EQ("3", token.text); } TEST_F(DefineTest, Predefined_LINE2) { const char* str = "#line 10\n" "__LINE__\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, NULL)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::CONST_INT, token.type); EXPECT_EQ("10", token.text); } TEST_F(DefineTest, Predefined_FILE1) { const char* const str[] = {"", "", "__FILE__"}; ASSERT_TRUE(mPreprocessor.init(3, str, NULL)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::CONST_INT, token.type); EXPECT_EQ("2", token.text); } TEST_F(DefineTest, Predefined_FILE2) { const char* const str[] = {"#line 10 20\n", "__FILE__"}; ASSERT_TRUE(mPreprocessor.init(2, str, NULL)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::CONST_INT, token.type); EXPECT_EQ("21", token.text); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #ifndef PREPROCESSOR_TESTS_MOCK_DIAGNOSTICS_H_ #define PREPROCESSOR_TESTS_MOCK_DIAGNOSTICS_H_ #include "gmock/gmock.h" #include "DiagnosticsBase.h" class MockDiagnostics : public pp::Diagnostics { public: MOCK_METHOD3(print, void(ID id, const pp::SourceLocation& loc, const std::string& text)); }; #endif // PREPROCESSOR_TESTS_MOCK_DIAGNOSTICS_H_
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" void PreprocessorTest::preprocess(const char* input, const char* expected) { ASSERT_TRUE(mPreprocessor.init(1, &input, NULL)); int line = 1; pp::Token token; std::stringstream stream; do { mPreprocessor.lex(&token); for (; line < token.location.line; ++line) { stream << "\n"; } stream << token; } while (token.type != pp::Token::LAST); std::string actual = stream.str(); EXPECT_STREQ(expected, actual.c_str()); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" #define CLOSED_RANGE(x, y) testing::Range(x, static_cast<char>((y) + 1)) class InvalidNumberTest : public PreprocessorTest, public testing::WithParamInterface<const char*> { }; TEST_P(InvalidNumberTest, InvalidNumberIdentified) { const char* str = GetParam(); ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); using testing::_; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::INVALID_NUMBER, _, str)); pp::Token token; mPreprocessor.lex(&token); } INSTANTIATE_TEST_CASE_P(InvalidIntegers, InvalidNumberTest, testing::Values("1a", "08", "0xG")); INSTANTIATE_TEST_CASE_P(InvalidFloats, InvalidNumberTest, testing::Values("1eg", "0.a", "0.1.2", ".0a", ".0.1")); typedef std::tr1::tuple<const char*, char> IntegerParams; class IntegerTest : public PreprocessorTest, public testing::WithParamInterface<IntegerParams> { }; TEST_P(IntegerTest, Identified) { std::string str(std::tr1::get<0>(GetParam())); // prefix. str.push_back(std::tr1::get<1>(GetParam())); // digit. const char* cstr = str.c_str(); ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::CONST_INT, token.type); EXPECT_EQ(str, token.text); } INSTANTIATE_TEST_CASE_P(DecimalInteger, IntegerTest, testing::Combine(testing::Values(""), CLOSED_RANGE('0', '9'))); INSTANTIATE_TEST_CASE_P(OctalInteger, IntegerTest, testing::Combine(testing::Values("0"), CLOSED_RANGE('0', '7'))); INSTANTIATE_TEST_CASE_P(HexadecimalInteger_0_9, IntegerTest, testing::Combine(testing::Values("0x", "0X"), CLOSED_RANGE('0', '9'))); INSTANTIATE_TEST_CASE_P(HexadecimalInteger_a_f, IntegerTest, testing::Combine(testing::Values("0x", "0X"), CLOSED_RANGE('a', 'f'))); INSTANTIATE_TEST_CASE_P(HexadecimalInteger_A_F, IntegerTest, testing::Combine(testing::Values("0x", "0X"), CLOSED_RANGE('A', 'F'))); class FloatTest : public PreprocessorTest { protected: void expectFloat(const std::string& str) { const char* cstr = str.c_str(); ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::CONST_FLOAT, token.type); EXPECT_EQ(str, token.text); } }; typedef std::tr1::tuple<char, char, const char*, char> FloatScientificParams; class FloatScientificTest : public FloatTest, public testing::WithParamInterface<FloatScientificParams> { }; // This test covers floating point numbers of form [0-9][eE][+-]?[0-9]. TEST_P(FloatScientificTest, FloatIdentified) { std::string str; str.push_back(std::tr1::get<0>(GetParam())); // significand [0-9]. str.push_back(std::tr1::get<1>(GetParam())); // separator [eE]. str.append(std::tr1::get<2>(GetParam())); // sign [" " "+" "-"]. str.push_back(std::tr1::get<3>(GetParam())); // exponent [0-9]. SCOPED_TRACE("FloatScientificTest"); expectFloat(str); } INSTANTIATE_TEST_CASE_P(FloatScientific, FloatScientificTest, testing::Combine(CLOSED_RANGE('0', '9'), testing::Values('e', 'E'), testing::Values("", "+", "-"), CLOSED_RANGE('0', '9'))); typedef std::tr1::tuple<char, char> FloatFractionParams; class FloatFractionTest : public FloatTest, public testing::WithParamInterface<FloatFractionParams> { }; // This test covers floating point numbers of form [0-9]"." and [0-9]?"."[0-9]. TEST_P(FloatFractionTest, FloatIdentified) { std::string str; char significand = std::tr1::get<0>(GetParam()); if (significand != '\0') str.push_back(significand); str.push_back('.'); char fraction = std::tr1::get<1>(GetParam()); if (fraction != '\0') str.push_back(fraction); SCOPED_TRACE("FloatFractionTest"); expectFloat(str); } INSTANTIATE_TEST_CASE_P(FloatFraction_X_X, FloatFractionTest, testing::Combine(CLOSED_RANGE('0', '9'), CLOSED_RANGE('0', '9'))); INSTANTIATE_TEST_CASE_P(FloatFraction_0_X, FloatFractionTest, testing::Combine(testing::Values('\0'), CLOSED_RANGE('0', '9'))); INSTANTIATE_TEST_CASE_P(FloatFraction_X_0, FloatFractionTest, testing::Combine(CLOSED_RANGE('0', '9'), testing::Values('\0'))); // In the tests above we have tested individual parts of a float separately. // This test has all parts of a float. TEST_F(FloatTest, FractionScientific) { SCOPED_TRACE("FractionScientific"); expectFloat("0.1e+2"); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" class CommentTest : public PreprocessorTest, public testing::WithParamInterface<const char*> { }; TEST_P(CommentTest, CommentIgnored) { const char* str = GetParam(); ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::LAST, token.type); } INSTANTIATE_TEST_CASE_P(LineComment, CommentTest, testing::Values("//foo\n", // With newline. "//foo", // Without newline. "//**/", // Nested block comment. "////", // Nested line comment. "//\"")); // Invalid character. INSTANTIATE_TEST_CASE_P(BlockComment, CommentTest, testing::Values("/*foo*/", "/*foo\n*/", // With newline. "/*//*/", // Nested line comment. "/*/**/", // Nested block comment. "/***/", // With lone '*'. "/*\"*/")); // Invalid character. class BlockCommentTest : public PreprocessorTest { }; TEST_F(BlockCommentTest, CommentReplacedWithSpace) { const char* str = "/*foo*/bar"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::IDENTIFIER, token.type); EXPECT_EQ("bar", token.text); EXPECT_TRUE(token.hasLeadingSpace()); } TEST_F(BlockCommentTest, UnterminatedComment) { const char* str = "/*foo"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); using testing::_; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::EOF_IN_COMMENT, _, _)); pp::Token token; mPreprocessor.lex(&token); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "gtest/gtest.h" #include "Token.h" TEST(TokenTest, DefaultConstructor) { pp::Token token; EXPECT_EQ(0, token.type); EXPECT_EQ(0, token.flags); EXPECT_EQ(0, token.location.line); EXPECT_EQ(0, token.location.file); EXPECT_EQ("", token.text); } TEST(TokenTest, Assignment) { pp::Token token; token.type = 1; token.flags = 1; token.location.line = 1; token.location.file = 1; token.text.assign("foo"); token = pp::Token(); EXPECT_EQ(0, token.type); EXPECT_EQ(0, token.flags); EXPECT_EQ(0, token.location.line); EXPECT_EQ(0, token.location.file); EXPECT_EQ("", token.text); } TEST(TokenTest, Equals) { pp::Token token; EXPECT_TRUE(token.equals(pp::Token())); token.type = 1; EXPECT_FALSE(token.equals(pp::Token())); token.type = 0; token.flags = 1; EXPECT_FALSE(token.equals(pp::Token())); token.flags = 0; token.location.line = 1; EXPECT_FALSE(token.equals(pp::Token())); token.location.line = 0; token.location.file = 1; EXPECT_FALSE(token.equals(pp::Token())); token.location.file = 0; token.text.assign("foo"); EXPECT_FALSE(token.equals(pp::Token())); token.text.clear(); EXPECT_TRUE(token.equals(pp::Token())); } TEST(TokenTest, HasLeadingSpace) { pp::Token token; EXPECT_FALSE(token.hasLeadingSpace()); token.setHasLeadingSpace(true); EXPECT_TRUE(token.hasLeadingSpace()); token.setHasLeadingSpace(false); EXPECT_FALSE(token.hasLeadingSpace()); } TEST(TokenTest, Write) { pp::Token token; token.text.assign("foo"); std::stringstream out1; out1 << token; EXPECT_TRUE(out1.good()); EXPECT_EQ("foo", out1.str()); token.setHasLeadingSpace(true); std::stringstream out2; out2 << token; EXPECT_TRUE(out2.good()); EXPECT_EQ(" foo", out2.str()); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" class ExtensionTest : public PreprocessorTest { }; TEST_F(ExtensionTest, Valid) { const char* str = "#extension foo : bar\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handleExtension(pp::SourceLocation(0, 1), "foo", "bar")); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(ExtensionTest, Comments) { const char* str = "/*foo*/" "#" "/*foo*/" "extension" "/*foo*/" "foo" "/*foo*/" ":" "/*foo*/" "bar" "/*foo*/" "//foo" "\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handleExtension(pp::SourceLocation(0, 1), "foo", "bar")); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(ExtensionTest, MissingNewline) { const char* str = "#extension foo : bar"; const char* expected = ""; using testing::_; // Directive successfully parsed. EXPECT_CALL(mDirectiveHandler, handleExtension(pp::SourceLocation(0, 1), "foo", "bar")); // Error reported about EOF. EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::EOF_IN_DIRECTIVE, _, _)); preprocess(str, expected); } struct ExtensionTestParam { const char* str; pp::Diagnostics::ID id; }; using testing::WithParamInterface; class InvalidExtensionTest : public ExtensionTest, public WithParamInterface<ExtensionTestParam> { }; TEST_P(InvalidExtensionTest, Identified) { ExtensionTestParam param = GetParam(); const char* expected = "\n"; using testing::_; // No handleExtension call. EXPECT_CALL(mDirectiveHandler, handleExtension(_, _, _)).Times(0); // Invalid extension directive call. EXPECT_CALL(mDiagnostics, print(param.id, pp::SourceLocation(0, 1), _)); preprocess(param.str, expected); } static const ExtensionTestParam kParams[] = { {"#extension\n", pp::Diagnostics::INVALID_EXTENSION_DIRECTIVE}, {"#extension 1\n", pp::Diagnostics::INVALID_EXTENSION_NAME}, {"#extension foo bar\n", pp::Diagnostics::UNEXPECTED_TOKEN}, {"#extension foo : \n", pp::Diagnostics::INVALID_EXTENSION_DIRECTIVE}, {"#extension foo : 1\n", pp::Diagnostics::INVALID_EXTENSION_BEHAVIOR}, {"#extension foo : bar baz\n", pp::Diagnostics::UNEXPECTED_TOKEN} }; INSTANTIATE_TEST_CASE_P(All, InvalidExtensionTest, testing::ValuesIn(kParams));
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include <algorithm> #include <climits> #include "PreprocessorTest.h" #include "Token.h" class CharTest : public PreprocessorTest, public testing::WithParamInterface<int> { }; static const char kPunctuators[] = { '.', '+', '-', '/', '*', '%', '<', '>', '[', ']', '(', ')', '{', '}', '^', '|', '&', '~', '=', '!', ':', ';', ',', '?'}; static const int kNumPunctuators = sizeof(kPunctuators) / sizeof(kPunctuators[0]); bool isPunctuator(char c) { static const char* kPunctuatorBeg = kPunctuators; static const char* kPunctuatorEnd = kPunctuators + kNumPunctuators; return std::find(kPunctuatorBeg, kPunctuatorEnd, c) != kPunctuatorEnd; } static const char kWhitespaces[] = {' ', '\t', '\v', '\f', '\n', '\r'}; static const int kNumWhitespaces = sizeof(kWhitespaces) / sizeof(kWhitespaces[0]); bool isWhitespace(char c) { static const char* kWhitespaceBeg = kWhitespaces; static const char* kWhitespaceEnd = kWhitespaces + kNumWhitespaces; return std::find(kWhitespaceBeg, kWhitespaceEnd, c) != kWhitespaceEnd; } TEST_P(CharTest, Identified) { std::string str(1, GetParam()); const char* cstr = str.c_str(); int length = 1; // Note that we pass the length param as well because the invalid // string may contain the null character. ASSERT_TRUE(mPreprocessor.init(1, &cstr, &length)); int expectedType = pp::Token::LAST; std::string expectedValue; if (str[0] == '#') { // Lone '#' is ignored. } else if ((str[0] == '_') || ((str[0] >= 'a') && (str[0] <= 'z')) || ((str[0] >= 'A') && (str[0] <= 'Z'))) { expectedType = pp::Token::IDENTIFIER; expectedValue = str; } else if (str[0] >= '0' && str[0] <= '9') { expectedType = pp::Token::CONST_INT; expectedValue = str; } else if (isPunctuator(str[0])) { expectedType = str[0]; expectedValue = str; } else if (isWhitespace(str[0])) { // Whitespace is ignored. } else { // Everything else is invalid. using testing::_; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::INVALID_CHARACTER, _, str)); } pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(expectedType, token.type); EXPECT_EQ(expectedValue, token.text); }; // Note +1 for the max-value in range. It is there because the max-value // not included in the range. INSTANTIATE_TEST_CASE_P(All, CharTest, testing::Range(CHAR_MIN, CHAR_MAX + 1));
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" struct OperatorTestParam { const char* str; int op; }; class OperatorTest : public PreprocessorTest, public testing::WithParamInterface<OperatorTestParam> { }; TEST_P(OperatorTest, Identified) { OperatorTestParam param = GetParam(); ASSERT_TRUE(mPreprocessor.init(1, &param.str, 0)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(param.op, token.type); EXPECT_EQ(param.str, token.text); } static const OperatorTestParam kOperators[] = { {"(", '('}, {")", ')'}, {"[", '['}, {"]", ']'}, {".", '.'}, {"+", '+'}, {"-", '-'}, {"~", '~'}, {"!", '!'}, {"*", '*'}, {"/", '/'}, {"%", '%'}, {"<", '<'}, {">", '>'}, {"&", '&'}, {"^", '^'}, {"|", '|'}, {"?", '?'}, {":", ':'}, {"=", '='}, {",", ','}, {"++", pp::Token::OP_INC}, {"--", pp::Token::OP_DEC}, {"<<", pp::Token::OP_LEFT}, {">>", pp::Token::OP_RIGHT}, {"<=", pp::Token::OP_LE}, {">=", pp::Token::OP_GE}, {"==", pp::Token::OP_EQ}, {"!=", pp::Token::OP_NE}, {"&&", pp::Token::OP_AND}, {"^^", pp::Token::OP_XOR}, {"||", pp::Token::OP_OR}, {"+=", pp::Token::OP_ADD_ASSIGN}, {"-=", pp::Token::OP_SUB_ASSIGN}, {"*=", pp::Token::OP_MUL_ASSIGN}, {"/=", pp::Token::OP_DIV_ASSIGN}, {"%=", pp::Token::OP_MOD_ASSIGN}, {"<<=", pp::Token::OP_LEFT_ASSIGN}, {">>=", pp::Token::OP_RIGHT_ASSIGN}, {"&=", pp::Token::OP_AND_ASSIGN}, {"^=", pp::Token::OP_XOR_ASSIGN}, {"|=", pp::Token::OP_OR_ASSIGN} }; INSTANTIATE_TEST_CASE_P(All, OperatorTest, testing::ValuesIn(kOperators));
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" #define CLOSED_RANGE(x, y) testing::Range(x, static_cast<char>((y) + 1)) class IdentifierTest : public PreprocessorTest { protected: void expectIdentifier(const std::string& str) { const char* cstr = str.c_str(); ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::IDENTIFIER, token.type); EXPECT_EQ(str, token.text); } }; class SingleLetterIdentifierTest : public IdentifierTest, public testing::WithParamInterface<char> { }; // This test covers identifier names of form [_a-zA-Z]. TEST_P(SingleLetterIdentifierTest, Identified) { std::string str(1, GetParam()); expectIdentifier(str); } // Test string: '_' INSTANTIATE_TEST_CASE_P(Underscore, SingleLetterIdentifierTest, testing::Values('_')); // Test string: [a-z] INSTANTIATE_TEST_CASE_P(a_z, SingleLetterIdentifierTest, CLOSED_RANGE('a', 'z')); // Test string: [A-Z] INSTANTIATE_TEST_CASE_P(A_Z, SingleLetterIdentifierTest, CLOSED_RANGE('A', 'Z')); typedef std::tr1::tuple<char, char> IdentifierParams; class DoubleLetterIdentifierTest : public IdentifierTest, public testing::WithParamInterface<IdentifierParams> { }; // This test covers identifier names of form [_a-zA-Z][_a-zA-Z0-9]. TEST_P(DoubleLetterIdentifierTest, Identified) { std::string str; str.push_back(std::tr1::get<0>(GetParam())); str.push_back(std::tr1::get<1>(GetParam())); expectIdentifier(str); } // Test string: "__" INSTANTIATE_TEST_CASE_P(Underscore_Underscore, DoubleLetterIdentifierTest, testing::Combine(testing::Values('_'), testing::Values('_'))); // Test string: "_"[a-z] INSTANTIATE_TEST_CASE_P(Underscore_a_z, DoubleLetterIdentifierTest, testing::Combine(testing::Values('_'), CLOSED_RANGE('a', 'z'))); // Test string: "_"[A-Z] INSTANTIATE_TEST_CASE_P(Underscore_A_Z, DoubleLetterIdentifierTest, testing::Combine(testing::Values('_'), CLOSED_RANGE('A', 'Z'))); // Test string: "_"[0-9] INSTANTIATE_TEST_CASE_P(Underscore_0_9, DoubleLetterIdentifierTest, testing::Combine(testing::Values('_'), CLOSED_RANGE('0', '9'))); // Test string: [a-z]"_" INSTANTIATE_TEST_CASE_P(a_z_Underscore, DoubleLetterIdentifierTest, testing::Combine(CLOSED_RANGE('a', 'z'), testing::Values('_'))); // Test string: [a-z][a-z] INSTANTIATE_TEST_CASE_P(a_z_a_z, DoubleLetterIdentifierTest, testing::Combine(CLOSED_RANGE('a', 'z'), CLOSED_RANGE('a', 'z'))); // Test string: [a-z][A-Z] INSTANTIATE_TEST_CASE_P(a_z_A_Z, DoubleLetterIdentifierTest, testing::Combine(CLOSED_RANGE('a', 'z'), CLOSED_RANGE('A', 'Z'))); // Test string: [a-z][0-9] INSTANTIATE_TEST_CASE_P(a_z_0_9, DoubleLetterIdentifierTest, testing::Combine(CLOSED_RANGE('a', 'z'), CLOSED_RANGE('0', '9'))); // Test string: [A-Z]"_" INSTANTIATE_TEST_CASE_P(A_Z_Underscore, DoubleLetterIdentifierTest, testing::Combine(CLOSED_RANGE('A', 'Z'), testing::Values('_'))); // Test string: [A-Z][a-z] INSTANTIATE_TEST_CASE_P(A_Z_a_z, DoubleLetterIdentifierTest, testing::Combine(CLOSED_RANGE('A', 'Z'), CLOSED_RANGE('a', 'z'))); // Test string: [A-Z][A-Z] INSTANTIATE_TEST_CASE_P(A_Z_A_Z, DoubleLetterIdentifierTest, testing::Combine(CLOSED_RANGE('A', 'Z'), CLOSED_RANGE('A', 'Z'))); // Test string: [A-Z][0-9] INSTANTIATE_TEST_CASE_P(A_Z_0_9, DoubleLetterIdentifierTest, testing::Combine(CLOSED_RANGE('A', 'Z'), CLOSED_RANGE('0', '9'))); // The tests above cover one-letter and various combinations of two-letter // identifier names. This test covers all characters in a single string. TEST_F(IdentifierTest, AllLetters) { std::string str; for (int c = 'a'; c <= 'z'; ++c) str.push_back(c); str.push_back('_'); for (int c = 'A'; c <= 'Z'; ++c) str.push_back(c); str.push_back('_'); for (int c = '0'; c <= '9'; ++c) str.push_back(c); expectIdentifier(str); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" class SpaceTest : public PreprocessorTest { protected: void expectSpace(const std::string& str) { const char* cstr = str.c_str(); ASSERT_TRUE(mPreprocessor.init(1, &cstr, 0)); pp::Token token; // "foo" is returned after ignoring the whitespace characters. mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::IDENTIFIER, token.type); EXPECT_EQ("foo", token.text); // The whitespace character is however recorded with the next token. EXPECT_TRUE(token.hasLeadingSpace()); } }; // Whitespace characters allowed in GLSL. // Note that newline characters (\n) will be tested separately. static const char kSpaceChars[] = {' ', '\t', '\v', '\f'}; // This test fixture tests the processing of a single whitespace character. // All tests in this fixture are ran with all possible whitespace character // allowed in GLSL. class SpaceCharTest : public SpaceTest, public testing::WithParamInterface<char> { }; TEST_P(SpaceCharTest, SpaceIgnored) { // Construct test string with the whitespace char before "foo". std::string str(1, GetParam()); str.append("foo"); expectSpace(str); } INSTANTIATE_TEST_CASE_P(SingleSpaceChar, SpaceCharTest, testing::ValuesIn(kSpaceChars)); // This test fixture tests the processing of a string containing consecutive // whitespace characters. All tests in this fixture are ran with all possible // combinations of whitespace characters allowed in GLSL. typedef std::tr1::tuple<char, char, char> SpaceStringParams; class SpaceStringTest : public SpaceTest, public testing::WithParamInterface<SpaceStringParams> { }; TEST_P(SpaceStringTest, SpaceIgnored) { // Construct test string with the whitespace char before "foo". std::string str; str.push_back(std::tr1::get<0>(GetParam())); str.push_back(std::tr1::get<1>(GetParam())); str.push_back(std::tr1::get<2>(GetParam())); str.append("foo"); expectSpace(str); } INSTANTIATE_TEST_CASE_P(SpaceCharCombination, SpaceStringTest, testing::Combine(testing::ValuesIn(kSpaceChars), testing::ValuesIn(kSpaceChars), testing::ValuesIn(kSpaceChars))); // The tests above make sure that the space char is recorded in the // next token. This test makes sure that a token is not incorrectly marked // to have leading space. TEST_F(SpaceTest, LeadingSpace) { const char* str = " foo+ -bar"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::IDENTIFIER, token.type); EXPECT_EQ("foo", token.text); EXPECT_TRUE(token.hasLeadingSpace()); mPreprocessor.lex(&token); EXPECT_EQ('+', token.type); EXPECT_FALSE(token.hasLeadingSpace()); mPreprocessor.lex(&token); EXPECT_EQ('-', token.type); EXPECT_TRUE(token.hasLeadingSpace()); mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::IDENTIFIER, token.type); EXPECT_EQ("bar", token.text); EXPECT_FALSE(token.hasLeadingSpace()); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "gtest/gtest.h" #include "MockDiagnostics.h" #include "MockDirectiveHandler.h" #include "Preprocessor.h" #ifndef PREPROCESSOR_TESTS_PREPROCESSOR_TEST_H_ #define PREPROCESSOR_TESTS_PREPROCESSOR_TEST_H_ class PreprocessorTest : public testing::Test { protected: PreprocessorTest() : mPreprocessor(&mDiagnostics, &mDirectiveHandler) { } // Preprocesses the input string and verifies that it matches // expected output. void preprocess(const char* input, const char* expected); MockDiagnostics mDiagnostics; MockDirectiveHandler mDirectiveHandler; pp::Preprocessor mPreprocessor; }; #endif // PREPROCESSOR_TESTS_PREPROCESSOR_TEST_H_
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" class IfTest : public PreprocessorTest { }; TEST_F(IfTest, If_0) { const char* str = "pass_1\n" "#if 0\n" "fail\n" "#endif\n" "pass_2\n"; const char* expected = "pass_1\n" "\n" "\n" "\n" "pass_2\n"; preprocess(str, expected); } TEST_F(IfTest, If_1) { const char* str = "pass_1\n" "#if 1\n" "pass_2\n" "#endif\n" "pass_3\n"; const char* expected = "pass_1\n" "\n" "pass_2\n" "\n" "pass_3\n"; preprocess(str, expected); } TEST_F(IfTest, If_0_Else) { const char* str = "pass_1\n" "#if 0\n" "fail\n" "#else\n" "pass_2\n" "#endif\n" "pass_3\n"; const char* expected = "pass_1\n" "\n" "\n" "\n" "pass_2\n" "\n" "pass_3\n"; preprocess(str, expected); } TEST_F(IfTest, If_1_Else) { const char* str = "pass_1\n" "#if 1\n" "pass_2\n" "#else\n" "fail\n" "#endif\n" "pass_3\n"; const char* expected = "pass_1\n" "\n" "pass_2\n" "\n" "\n" "\n" "pass_3\n"; preprocess(str, expected); } TEST_F(IfTest, If_0_Elif) { const char* str = "pass_1\n" "#if 0\n" "fail_1\n" "#elif 0\n" "fail_2\n" "#elif 1\n" "pass_2\n" "#elif 1\n" "fail_3\n" "#else\n" "fail_4\n" "#endif\n" "pass_3\n"; const char* expected = "pass_1\n" "\n" "\n" "\n" "\n" "\n" "pass_2\n" "\n" "\n" "\n" "\n" "\n" "pass_3\n"; preprocess(str, expected); } TEST_F(IfTest, If_1_Elif) { const char* str = "pass_1\n" "#if 1\n" "pass_2\n" "#elif 0\n" "fail_1\n" "#elif 1\n" "fail_2\n" "#else\n" "fail_4\n" "#endif\n" "pass_3\n"; const char* expected = "pass_1\n" "\n" "pass_2\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "pass_3\n"; preprocess(str, expected); } TEST_F(IfTest, If_Elif_Else) { const char* str = "pass_1\n" "#if 0\n" "fail_1\n" "#elif 0\n" "fail_2\n" "#elif 0\n" "fail_3\n" "#else\n" "pass_2\n" "#endif\n" "pass_3\n"; const char* expected = "pass_1\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "pass_2\n" "\n" "pass_3\n"; preprocess(str, expected); } TEST_F(IfTest, If_0_Nested) { const char* str = "pass_1\n" "#if 0\n" "fail_1\n" "#if 1\n" "fail_2\n" "#else\n" "fail_3\n" "#endif\n" "#else\n" "pass_2\n" "#endif\n" "pass_3\n"; const char* expected = "pass_1\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "pass_2\n" "\n" "pass_3\n"; preprocess(str, expected); } TEST_F(IfTest, If_1_Nested) { const char* str = "pass_1\n" "#if 1\n" "pass_2\n" "#if 1\n" "pass_3\n" "#else\n" "fail_1\n" "#endif\n" "#else\n" "fail_2\n" "#endif\n" "pass_4\n"; const char* expected = "pass_1\n" "\n" "pass_2\n" "\n" "pass_3\n" "\n" "\n" "\n" "\n" "\n" "\n" "pass_4\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorPrecedence) { const char* str = "#if 1 + 2 * 3 + - (26 % 17 - + 4 / 2)\n" "fail_1\n" "#else\n" "pass_1\n" "#endif\n"; const char* expected = "\n" "\n" "\n" "pass_1\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorDefined) { const char* str = "#if defined foo\n" "fail_1\n" "#else\n" "pass_1\n" "#endif\n" "#define foo\n" "#if defined(foo)\n" "pass_2\n" "#else\n" "fail_2\n" "#endif\n" "#undef foo\n" "#if defined ( foo ) \n" "fail_3\n" "#else\n" "pass_3\n" "#endif\n"; const char* expected = "\n" "\n" "\n" "pass_1\n" "\n" "\n" "\n" "pass_2\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "pass_3\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorEQ) { const char* str = "#if 4 - 1 == 2 + 1\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorNE) { const char* str = "#if 1 != 2\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorLess) { const char* str = "#if 1 < 2\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorGreater) { const char* str = "#if 2 > 1\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorLE) { const char* str = "#if 1 <= 2\n" "pass_1\n" "#else\n" "fail_1\n" "#endif\n" "#if 2 <= 2\n" "pass_2\n" "#else\n" "fail_2\n" "#endif\n"; const char* expected = "\n" "pass_1\n" "\n" "\n" "\n" "\n" "pass_2\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorGE) { const char* str = "#if 2 >= 1\n" "pass_1\n" "#else\n" "fail_1\n" "#endif\n" "#if 2 >= 2\n" "pass_2\n" "#else\n" "fail_2\n" "#endif\n"; const char* expected = "\n" "pass_1\n" "\n" "\n" "\n" "\n" "pass_2\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorBitwiseOR) { const char* str = "#if (0xaaaaaaaa | 0x55555555) == 0xffffffff\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorBitwiseAND) { const char* str = "#if (0xaaaaaaa & 0x5555555) == 0\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorBitwiseXOR) { const char* str = "#if (0xaaaaaaa ^ 0x5555555) == 0xfffffff\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorBitwiseComplement) { const char* str = "#if (~ 0xdeadbeef) == -3735928560\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorLeft) { const char* str = "#if (1 << 12) == 4096\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, OperatorRight) { const char* str = "#if (31762 >> 8) == 124\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, ExpressionWithMacros) { const char* str = "#define one 1\n" "#define two 2\n" "#define three 3\n" "#if one + two == three\n" "pass\n" "#else\n" "fail\n" "#endif\n"; const char* expected = "\n" "\n" "\n" "\n" "pass\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, JunkInsideExcludedBlockIgnored) { const char* str = "#if 0\n" "foo !@#$%^&* .1bar\n" "#foo\n" "#if bar\n" "fail\n" "#endif\n" "#else\n" "pass\n" "#endif\n"; const char* expected = "\n" "\n" "\n" "\n" "\n" "\n" "\n" "pass\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, Ifdef) { const char* str = "#define foo\n" "#ifdef foo\n" "pass_1\n" "#else\n" "fail_1\n" "#endif\n" "#undef foo\n" "#ifdef foo\n" "fail_2\n" "#else\n" "pass_2\n" "#endif\n"; const char* expected = "\n" "\n" "pass_1\n" "\n" "\n" "\n" "\n" "\n" "\n" "\n" "pass_2\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, Ifndef) { const char* str = "#define foo\n" "#ifndef foo\n" "fail_1\n" "#else\n" "pass_1\n" "#endif\n" "#undef foo\n" "#ifndef foo\n" "pass_2\n" "#else\n" "fail_2\n" "#endif\n"; const char* expected = "\n" "\n" "\n" "\n" "pass_1\n" "\n" "\n" "\n" "pass_2\n" "\n" "\n" "\n"; preprocess(str, expected); } TEST_F(IfTest, MissingExpression) { const char* str = "#if\n" "#endif\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::INVALID_EXPRESSION, pp::SourceLocation(0, 1), "syntax error")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, DivisionByZero) { const char* str = "#if 1 / (3 - (1 + 2))\n" "#endif\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::DIVISION_BY_ZERO, pp::SourceLocation(0, 1), "1 / 0")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, ModuloByZero) { const char* str = "#if 1 % (3 - (1 + 2))\n" "#endif\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::DIVISION_BY_ZERO, pp::SourceLocation(0, 1), "1 % 0")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, DecIntegerOverflow) { const char* str = "#if 4294967296\n" "#endif\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::INTEGER_OVERFLOW, pp::SourceLocation(0, 1), "4294967296")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, OctIntegerOverflow) { const char* str = "#if 077777777777\n" "#endif\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::INTEGER_OVERFLOW, pp::SourceLocation(0, 1), "077777777777")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, HexIntegerOverflow) { const char* str = "#if 0xfffffffff\n" "#endif\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::INTEGER_OVERFLOW, pp::SourceLocation(0, 1), "0xfffffffff")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, UndefinedMacro) { const char* str = "#if UNDEFINED\n" "#endif\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::INVALID_EXPRESSION, pp::SourceLocation(0, 1), "syntax error")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::CONDITIONAL_UNEXPECTED_TOKEN, pp::SourceLocation(0, 1), "UNDEFINED")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, InvalidExpressionIgnoredForExcludedElif) { const char* str = "#if 1\n" "pass\n" "#elif UNDEFINED\n" "fail\n" "#endif\n"; const char* expected = "\n" "pass\n" "\n" "\n" "\n"; // No error or warning. using testing::_; EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(IfTest, ElseWithoutIf) { const char* str = "#else\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::CONDITIONAL_ELSE_WITHOUT_IF, pp::SourceLocation(0, 1), "else")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, ElifWithoutIf) { const char* str = "#elif 1\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::CONDITIONAL_ELIF_WITHOUT_IF, pp::SourceLocation(0, 1), "elif")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, EndifWithoutIf) { const char* str = "#endif\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::CONDITIONAL_ENDIF_WITHOUT_IF, pp::SourceLocation(0, 1), "endif")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, ElseAfterElse) { const char* str = "#if 1\n" "#else\n" "#else\n" "#endif\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::CONDITIONAL_ELSE_AFTER_ELSE, pp::SourceLocation(0, 3), "else")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, ElifAfterElse) { const char* str = "#if 1\n" "#else\n" "#elif 0\n" "#endif\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::CONDITIONAL_ELIF_AFTER_ELSE, pp::SourceLocation(0, 3), "elif")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, UnterminatedIf) { const char* str = "#if 1\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::CONDITIONAL_UNTERMINATED, pp::SourceLocation(0, 1), "if")); pp::Token token; mPreprocessor.lex(&token); } TEST_F(IfTest, UnterminatedIfdef) { const char* str = "#ifdef foo\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, 0)); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::CONDITIONAL_UNTERMINATED, pp::SourceLocation(0, 1), "ifdef")); pp::Token token; mPreprocessor.lex(&token); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" class ErrorTest : public PreprocessorTest { }; TEST_F(ErrorTest, Empty) { const char* str = "#error\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handleError(pp::SourceLocation(0, 1), "")); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(ErrorTest, OneTokenMessage) { const char* str = "#error foo\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handleError(pp::SourceLocation(0, 1), " foo")); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(ErrorTest, TwoTokenMessage) { const char* str = "#error foo bar\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handleError(pp::SourceLocation(0, 1), " foo bar")); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(ErrorTest, Comments) { const char* str = "/*foo*/" "#" "/*foo*/" "error" "/*foo*/" "foo" "/*foo*/" "bar" "/*foo*/" "//foo" "\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handleError(pp::SourceLocation(0, 1), " foo bar")); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(ErrorTest, MissingNewline) { const char* str = "#error foo"; const char* expected = ""; using testing::_; // Directive successfully parsed. EXPECT_CALL(mDirectiveHandler, handleError(pp::SourceLocation(0, 1), " foo")); // Error reported about EOF. EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::EOF_IN_DIRECTIVE, _, _)); preprocess(str, expected); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" class VersionTest : public PreprocessorTest { }; TEST_F(VersionTest, Valid) { const char* str = "#version 200\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 200)); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(VersionTest, CommentsIgnored) { const char* str = "/*foo*/" "#" "/*foo*/" "version" "/*foo*/" "200" "/*foo*/" "//foo" "\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 200)); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(VersionTest, MissingNewline) { const char* str = "#version 200"; const char* expected = ""; using testing::_; // Directive successfully parsed. EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 1), 200)); // Error reported about EOF. EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::EOF_IN_DIRECTIVE, _, _)); preprocess(str, expected); } TEST_F(VersionTest, AfterComments) { const char* str = "/* block comment acceptable */\n" "// line comment acceptable\n" "#version 200\n"; const char* expected = "\n\n\n"; using testing::_; // Directive successfully parsed. EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 3), 200)); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(VersionTest, AfterWhitespace) { const char* str = "\n" "\n" "#version 200\n"; const char* expected = "\n\n\n"; using testing::_; // Directive successfully parsed. EXPECT_CALL(mDirectiveHandler, handleVersion(pp::SourceLocation(0, 3), 200)); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(VersionTest, AfterValidToken) { const char* str = "foo\n" "#version 200\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, NULL)); using testing::_; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::VERSION_NOT_FIRST_STATEMENT, pp::SourceLocation(0, 2), _)); pp::Token token; do { mPreprocessor.lex(&token); } while (token.type != pp::Token::LAST); } TEST_F(VersionTest, AfterInvalidToken) { const char* str = "$\n" "#version 200\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, NULL)); using testing::_; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::INVALID_CHARACTER, pp::SourceLocation(0, 1), "$")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::VERSION_NOT_FIRST_STATEMENT, pp::SourceLocation(0, 2), _)); pp::Token token; do { mPreprocessor.lex(&token); } while (token.type != pp::Token::LAST); } TEST_F(VersionTest, AfterValidDirective) { const char* str = "#\n" "#version 200\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, NULL)); using testing::_; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::VERSION_NOT_FIRST_STATEMENT, pp::SourceLocation(0, 2), _)); pp::Token token; do { mPreprocessor.lex(&token); } while (token.type != pp::Token::LAST); } TEST_F(VersionTest, AfterInvalidDirective) { const char* str = "#foo\n" "#version 200\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, NULL)); using testing::_; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::DIRECTIVE_INVALID_NAME, pp::SourceLocation(0, 1), "foo")); EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::VERSION_NOT_FIRST_STATEMENT, pp::SourceLocation(0, 2), _)); pp::Token token; do { mPreprocessor.lex(&token); } while (token.type != pp::Token::LAST); } TEST_F(VersionTest, AfterExcludedBlock) { const char* str = "#if 0\n" "foo\n" "#endif\n" "#version 200\n"; ASSERT_TRUE(mPreprocessor.init(1, &str, NULL)); using testing::_; EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::VERSION_NOT_FIRST_STATEMENT, pp::SourceLocation(0, 4), _)); pp::Token token; do { mPreprocessor.lex(&token); } while (token.type != pp::Token::LAST); } struct VersionTestParam { const char* str; pp::Diagnostics::ID id; }; class InvalidVersionTest : public VersionTest, public testing::WithParamInterface<VersionTestParam> { }; TEST_P(InvalidVersionTest, Identified) { VersionTestParam param = GetParam(); const char* expected = "\n"; using testing::_; // No handleVersion call. EXPECT_CALL(mDirectiveHandler, handleVersion(_, _)).Times(0); // Invalid version directive call. EXPECT_CALL(mDiagnostics, print(param.id, pp::SourceLocation(0, 1), _)); preprocess(param.str, expected); } static const VersionTestParam kParams[] = { {"#version\n", pp::Diagnostics::INVALID_VERSION_DIRECTIVE}, {"#version foo\n", pp::Diagnostics::INVALID_VERSION_NUMBER}, {"#version 100 foo\n", pp::Diagnostics::UNEXPECTED_TOKEN}, {"#version 0xffffffff\n", pp::Diagnostics::INTEGER_OVERFLOW} }; INSTANTIATE_TEST_CASE_P(All, InvalidVersionTest, testing::ValuesIn(kParams));
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Input.h" #include "Token.h" class InitTest : public PreprocessorTest { }; TEST_F(InitTest, NegativeCount) { EXPECT_FALSE(mPreprocessor.init(-1, NULL, NULL)); } TEST_F(InitTest, ZeroCount) { EXPECT_TRUE(mPreprocessor.init(0, NULL, NULL)); pp::Token token; mPreprocessor.lex(&token); EXPECT_EQ(pp::Token::LAST, token.type); } TEST_F(InitTest, NullString) { EXPECT_FALSE(mPreprocessor.init(1, NULL, NULL)); } TEST(InputTest, DefaultConstructor) { pp::Input input; EXPECT_EQ(0, input.count()); EXPECT_EQ(0, input.read(NULL, 1)); } TEST(InputTest, NullLength) { const char* str[] = {"foo"}; pp::Input input(1, str, NULL); EXPECT_EQ(3, input.length(0)); } TEST(InputTest, NegativeLength) { const char* str[] = {"foo"}; int length[] = {-1}; pp::Input input(1, str, length); EXPECT_EQ(3, input.length(0)); } TEST(InputTest, ActualLength) { const char* str[] = {"foobar"}; int length[] = {3}; pp::Input input(1, str, length); // Note that strlen(str[0]) != length[0]. // Even then Input should just accept any non-negative number. EXPECT_EQ(length[0], input.length(0)); } TEST(InputTest, String) { const char* str[] = {"foo"}; pp::Input input(1, str, NULL); EXPECT_STREQ(str[0], input.string(0)); } TEST(InputTest, ReadSingleString) { int count = 1; const char* str[] = {"foo"}; char buf[4] = {'\0', '\0', '\0', '\0'}; int maxSize = 1; pp::Input input1(count, str, NULL); EXPECT_EQ(1, input1.read(buf, maxSize)); EXPECT_EQ('f', buf[0]); EXPECT_EQ(1, input1.read(buf, maxSize)); EXPECT_EQ('o', buf[0]); EXPECT_EQ(1, input1.read(buf, maxSize)); EXPECT_EQ('o', buf[0]); EXPECT_EQ(0, input1.read(buf, maxSize)); maxSize = 2; pp::Input input2(count, str, NULL); EXPECT_EQ(2, input2.read(buf, maxSize)); EXPECT_STREQ("fo", buf); EXPECT_EQ(1, input2.read(buf, maxSize)); EXPECT_EQ('o', buf[0]); EXPECT_EQ(0, input2.read(buf, maxSize)); maxSize = 3; pp::Input input3(count, str, NULL); EXPECT_EQ(3, input3.read(buf, maxSize)); EXPECT_STREQ("foo", buf); EXPECT_EQ(0, input3.read(buf, maxSize)); maxSize = 4; pp::Input input4(count, str, NULL); EXPECT_EQ(3, input4.read(buf, maxSize)); EXPECT_STREQ("foo", buf); EXPECT_EQ(0, input4.read(buf, maxSize)); } TEST(InputTest, ReadMultipleStrings) { int count = 3; const char* str[] = {"f", "o", "o"}; char buf[4] = {'\0', '\0', '\0', '\0'}; int maxSize = 1; pp::Input input1(count, str, NULL); EXPECT_EQ(1, input1.read(buf, maxSize)); EXPECT_EQ('f', buf[0]); EXPECT_EQ(1, input1.read(buf, maxSize)); EXPECT_EQ('o', buf[0]); EXPECT_EQ(1, input1.read(buf, maxSize)); EXPECT_EQ('o', buf[0]); EXPECT_EQ(0, input1.read(buf, maxSize)); maxSize = 2; pp::Input input2(count, str, NULL); EXPECT_EQ(2, input2.read(buf, maxSize)); EXPECT_STREQ("fo", buf); EXPECT_EQ(1, input2.read(buf, maxSize)); EXPECT_EQ('o', buf[0]); EXPECT_EQ(0, input2.read(buf, maxSize)); maxSize = 3; pp::Input input3(count, str, NULL); EXPECT_EQ(3, input3.read(buf, maxSize)); EXPECT_STREQ("foo", buf); EXPECT_EQ(0, input3.read(buf, maxSize)); maxSize = 4; pp::Input input4(count, str, NULL); EXPECT_EQ(3, input4.read(buf, maxSize)); EXPECT_STREQ("foo", buf); EXPECT_EQ(0, input4.read(buf, maxSize)); } TEST(InputTest, ReadStringsWithLength) { int count = 2; const char* str[] = {"foo", "bar"}; // Note that the length for the first string is 2 which is less than // strlen(str[0]. We want to make sure that the last character is ignored. int length[] = {2, 3}; char buf[6] = {'\0', '\0', '\0', '\0', '\0', '\0'}; int maxSize = 5; pp::Input input(count, str, length); EXPECT_EQ(maxSize, input.read(buf, maxSize)); EXPECT_STREQ("fobar", buf); }
C++
// // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "PreprocessorTest.h" #include "Token.h" class PragmaTest : public PreprocessorTest { }; TEST_F(PragmaTest, EmptyName) { const char* str = "#pragma\n"; const char* expected = "\n"; using testing::_; // No handlePragma calls. EXPECT_CALL(mDirectiveHandler, handlePragma(_, _, _)).Times(0); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(PragmaTest, EmptyValue) { const char* str = "#pragma foo\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handlePragma(pp::SourceLocation(0, 1), "foo", "")); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(PragmaTest, NameValue) { const char* str = "#pragma foo(bar)\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handlePragma(pp::SourceLocation(0, 1), "foo", "bar")); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(PragmaTest, Comments) { const char* str = "/*foo*/" "#" "/*foo*/" "pragma" "/*foo*/" "foo" "/*foo*/" "(" "/*foo*/" "bar" "/*foo*/" ")" "/*foo*/" "//foo" "\n"; const char* expected = "\n"; using testing::_; EXPECT_CALL(mDirectiveHandler, handlePragma(pp::SourceLocation(0, 1), "foo", "bar")); // No error or warning. EXPECT_CALL(mDiagnostics, print(_, _, _)).Times(0); preprocess(str, expected); } TEST_F(PragmaTest, MissingNewline) { const char* str = "#pragma foo(bar)"; const char* expected = ""; using testing::_; // Pragma successfully parsed. EXPECT_CALL(mDirectiveHandler, handlePragma(pp::SourceLocation(0, 1), "foo", "bar")); // Error reported about EOF. EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::EOF_IN_DIRECTIVE, _, _)); preprocess(str, expected); } class InvalidPragmaTest : public PragmaTest, public testing::WithParamInterface<const char*> { }; TEST_P(InvalidPragmaTest, Identified) { const char* str = GetParam(); const char* expected = "\n"; using testing::_; // No handlePragma calls. EXPECT_CALL(mDirectiveHandler, handlePragma(_, _, _)).Times(0); // Unrecognized pragma warning. EXPECT_CALL(mDiagnostics, print(pp::Diagnostics::UNRECOGNIZED_PRAGMA, pp::SourceLocation(0, 1), _)); preprocess(str, expected); } INSTANTIATE_TEST_CASE_P(All, InvalidPragmaTest, testing::Values( "#pragma 1\n", // Invalid name. "#pragma foo()\n", // Missing value. "#pragma foo bar)\n", // Missing left paren, "#pragma foo(bar\n", // Missing right paren. "#pragma foo bar\n", // Missing parens. "#pragma foo(bar) baz\n")); // Extra tokens.
C++
// // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include <sstream> #include <string> #include <vector> #include "GLSLANG/ShaderLang.h" #include "gtest/gtest.h" #define SHADER(Src) #Src class ExpressionLimitTest : public testing::Test { protected: static const int kMaxExpressionComplexity = 16; static const int kMaxCallStackDepth = 16; static const char* kExpressionTooComplex; static const char* kCallStackTooDeep; static const char* kHasRecursion; virtual void SetUp() { memset(&resources, 0, sizeof(resources)); ASSERT_TRUE(ShInitialize() != 0) << "Could not ShInitialize"; GenerateResources(&resources); } virtual void TearDown() { ASSERT_TRUE(ShFinalize() != 0); } // Set up the per compile resources void GenerateResources(ShBuiltInResources* resources) { ShInitBuiltInResources(resources); resources->MaxVertexAttribs = 8; resources->MaxVertexUniformVectors = 128; resources->MaxVaryingVectors = 8; resources->MaxVertexTextureImageUnits = 0; resources->MaxCombinedTextureImageUnits = 8; resources->MaxTextureImageUnits = 8; resources->MaxFragmentUniformVectors = 16; resources->MaxDrawBuffers = 1; resources->OES_standard_derivatives = 0; resources->OES_EGL_image_external = 0; resources->MaxExpressionComplexity = kMaxExpressionComplexity; resources->MaxCallStackDepth = kMaxCallStackDepth; } void GenerateLongExpression(int length, std::stringstream* ss) { for (int ii = 0; ii < length; ++ii) { *ss << "+ vec4(" << ii << ")"; } } std::string GenerateShaderWithLongExpression(int length) { static const char* shaderStart = SHADER( precision mediump float; uniform vec4 u_color; void main() { gl_FragColor = u_color ); std::stringstream ss; ss << shaderStart; GenerateLongExpression(length, &ss); ss << "; }"; return ss.str(); } std::string GenerateShaderWithUnusedLongExpression(int length) { static const char* shaderStart = SHADER( precision mediump float; uniform vec4 u_color; void main() { gl_FragColor = u_color; } vec4 someFunction() { return u_color ); std::stringstream ss; ss << shaderStart; GenerateLongExpression(length, &ss); ss << "; }"; return ss.str(); } void GenerateDeepFunctionStack(int length, std::stringstream* ss) { static const char* shaderStart = SHADER( precision mediump float; uniform vec4 u_color; vec4 function0() { return u_color; } ); *ss << shaderStart; for (int ii = 0; ii < length; ++ii) { *ss << "vec4 function" << (ii + 1) << "() {\n" << " return function" << ii << "();\n" << "}\n"; } } std::string GenerateShaderWithDeepFunctionStack(int length) { std::stringstream ss; GenerateDeepFunctionStack(length, &ss); ss << "void main() {\n" << " gl_FragColor = function" << length << "();\n" << "}"; return ss.str(); } std::string GenerateShaderWithUnusedDeepFunctionStack(int length) { std::stringstream ss; GenerateDeepFunctionStack(length, &ss); ss << "void main() {\n" << " gl_FragColor = vec4(0,0,0,0);\n" << "}"; return ss.str(); } // Compiles a shader and if there's an error checks for a specific // substring in the error log. This way we know the error is specific // to the issue we are testing. bool CheckShaderCompilation(ShHandle compiler, const char* source, int compileOptions, const char* expected_error) { bool success = ShCompile(compiler, &source, 1, compileOptions); if (success) { success = !expected_error; } else { size_t bufferLen = 0; ShGetInfo(compiler, SH_INFO_LOG_LENGTH, &bufferLen); char* buffer(new char [bufferLen]); ShGetInfoLog(compiler, buffer); std::string log(buffer, buffer + bufferLen); delete [] buffer; if (expected_error) success = log.find(expected_error) != std::string::npos; EXPECT_TRUE(success) << log << "\n----shader----\n" << source; } return success; } ShBuiltInResources resources; }; const char* ExpressionLimitTest::kExpressionTooComplex = "Expression too complex"; const char* ExpressionLimitTest::kCallStackTooDeep = "call stack too deep"; const char* ExpressionLimitTest::kHasRecursion = "Function recursion detected"; TEST_F(ExpressionLimitTest, ExpressionComplexity) { ShShaderSpec spec = SH_WEBGL_SPEC; ShShaderOutput output = SH_ESSL_OUTPUT; ShHandle vertexCompiler = ShConstructCompiler( SH_FRAGMENT_SHADER, spec, output, &resources); int compileOptions = SH_LIMIT_EXPRESSION_COMPLEXITY; // Test expression under the limit passes. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithLongExpression( kMaxExpressionComplexity - 10).c_str(), compileOptions, NULL)); // Test expression over the limit fails. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithLongExpression( kMaxExpressionComplexity + 10).c_str(), compileOptions, kExpressionTooComplex)); // Test expression over the limit without a limit does not fail. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithLongExpression( kMaxExpressionComplexity + 10).c_str(), compileOptions & ~SH_LIMIT_EXPRESSION_COMPLEXITY, NULL)); } TEST_F(ExpressionLimitTest, UnusedExpressionComplexity) { ShShaderSpec spec = SH_WEBGL_SPEC; ShShaderOutput output = SH_ESSL_OUTPUT; ShHandle vertexCompiler = ShConstructCompiler( SH_FRAGMENT_SHADER, spec, output, &resources); int compileOptions = SH_LIMIT_EXPRESSION_COMPLEXITY; // Test expression under the limit passes. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithUnusedLongExpression( kMaxExpressionComplexity - 10).c_str(), compileOptions, NULL)); // Test expression over the limit fails. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithUnusedLongExpression( kMaxExpressionComplexity + 10).c_str(), compileOptions, kExpressionTooComplex)); // Test expression over the limit without a limit does not fail. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithUnusedLongExpression( kMaxExpressionComplexity + 10).c_str(), compileOptions & ~SH_LIMIT_EXPRESSION_COMPLEXITY, NULL)); } TEST_F(ExpressionLimitTest, CallStackDepth) { ShShaderSpec spec = SH_WEBGL_SPEC; ShShaderOutput output = SH_ESSL_OUTPUT; ShHandle vertexCompiler = ShConstructCompiler( SH_FRAGMENT_SHADER, spec, output, &resources); int compileOptions = SH_LIMIT_CALL_STACK_DEPTH; // Test call stack under the limit passes. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithDeepFunctionStack( kMaxCallStackDepth - 10).c_str(), compileOptions, NULL)); // Test call stack over the limit fails. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithDeepFunctionStack( kMaxCallStackDepth + 10).c_str(), compileOptions, kCallStackTooDeep)); // Test call stack over the limit without limit does not fail. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithDeepFunctionStack( kMaxCallStackDepth + 10).c_str(), compileOptions & ~SH_LIMIT_CALL_STACK_DEPTH, NULL)); } TEST_F(ExpressionLimitTest, UnusedCallStackDepth) { ShShaderSpec spec = SH_WEBGL_SPEC; ShShaderOutput output = SH_ESSL_OUTPUT; ShHandle vertexCompiler = ShConstructCompiler( SH_FRAGMENT_SHADER, spec, output, &resources); int compileOptions = SH_LIMIT_CALL_STACK_DEPTH; // Test call stack under the limit passes. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithUnusedDeepFunctionStack( kMaxCallStackDepth - 10).c_str(), compileOptions, NULL)); // Test call stack over the limit fails. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithUnusedDeepFunctionStack( kMaxCallStackDepth + 10).c_str(), compileOptions, kCallStackTooDeep)); // Test call stack over the limit without limit does not fail. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, GenerateShaderWithUnusedDeepFunctionStack( kMaxCallStackDepth + 10).c_str(), compileOptions & ~SH_LIMIT_CALL_STACK_DEPTH, NULL)); } TEST_F(ExpressionLimitTest, Recursion) { ShShaderSpec spec = SH_WEBGL_SPEC; ShShaderOutput output = SH_ESSL_OUTPUT; ShHandle vertexCompiler = ShConstructCompiler( SH_FRAGMENT_SHADER, spec, output, &resources); int compileOptions = 0; static const char* shaderWithRecursion0 = SHADER( precision mediump float; uniform vec4 u_color; vec4 someFunc() { return someFunc(); } void main() { gl_FragColor = u_color * someFunc(); } ); static const char* shaderWithRecursion1 = SHADER( precision mediump float; uniform vec4 u_color; vec4 someFunc(); vec4 someFunc1() { return someFunc(); } vec4 someFunc() { return someFunc1(); } void main() { gl_FragColor = u_color * someFunc(); } ); static const char* shaderWithRecursion2 = SHADER( precision mediump float; uniform vec4 u_color; vec4 someFunc() { if (u_color.x > 0.5) { return someFunc(); } else { return vec4(1); } } void main() { gl_FragColor = someFunc(); } ); static const char* shaderWithRecursion3 = SHADER( precision mediump float; uniform vec4 u_color; vec4 someFunc() { if (u_color.x > 0.5) { return vec4(1); } else { return someFunc(); } } void main() { gl_FragColor = someFunc(); } ); static const char* shaderWithRecursion4 = SHADER( precision mediump float; uniform vec4 u_color; vec4 someFunc() { return (u_color.x > 0.5) ? vec4(1) : someFunc(); } void main() { gl_FragColor = someFunc(); } ); static const char* shaderWithRecursion5 = SHADER( precision mediump float; uniform vec4 u_color; vec4 someFunc() { return (u_color.x > 0.5) ? someFunc() : vec4(1); } void main() { gl_FragColor = someFunc(); } ); static const char* shaderWithRecursion6 = SHADER( precision mediump float; uniform vec4 u_color; vec4 someFunc() { return someFunc(); } void main() { gl_FragColor = u_color; } ); static const char* shaderWithNoRecursion = SHADER( precision mediump float; uniform vec4 u_color; vec3 rgb(int r, int g, int b) { return vec3(float(r) / 255.0, float(g) / 255.0, float(b) / 255.0); } // these external calls used to incorrectly trigger // recursion detection. vec3 hairColor0 = rgb(151, 200, 234); vec3 faceColor2 = rgb(183, 148, 133); void main() { gl_FragColor = u_color + vec4(hairColor0 + faceColor2, 0); } ); static const char* shaderWithRecursion7 = SHADER( precision mediump float; uniform vec4 u_color; vec4 function2() { return u_color; } vec4 function1() { vec4 a = function2(); vec4 b = function1(); return a + b; } void main() { gl_FragColor = function1(); } ); static const char* shaderWithRecursion8 = SHADER( precision mediump float; uniform vec4 u_color; vec4 function1(); vec4 function3() { return function1(); } vec4 function2() { return function3(); } vec4 function1() { return function2(); } void main() { gl_FragColor = function1(); } ); // Check simple recursions fails. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithRecursion0, compileOptions, kHasRecursion)); // Check simple recursions fails. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithRecursion1, compileOptions, kHasRecursion)); // Check if recursions fails. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithRecursion2, compileOptions, kHasRecursion)); // Check if recursions fails. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithRecursion3, compileOptions, kHasRecursion)); // Check ternary recursions fails. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithRecursion4, compileOptions, kHasRecursion)); // Check ternary recursions fails. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithRecursion5, compileOptions, kHasRecursion)); // Check unused recursions passes. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithRecursion6, compileOptions, NULL)); EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithRecursion7, compileOptions, kHasRecursion)); EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithRecursion8, compileOptions, kHasRecursion)); // Check unused recursions fails if limiting call stack // since we check all paths. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithRecursion6, compileOptions | SH_LIMIT_CALL_STACK_DEPTH, kHasRecursion)); // Check unused recursions passes. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithNoRecursion, compileOptions, NULL)); // Check unused recursions passes if limiting call stack. EXPECT_TRUE(CheckShaderCompilation( vertexCompiler, shaderWithNoRecursion, compileOptions | SH_LIMIT_CALL_STACK_DEPTH, NULL)); }
C++
// // Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // #include "compiler/VariablePacker.h" #include "gtest/gtest.h" TEST(VariablePacking, Pack) { VariablePacker packer; TVariableInfoList vars; const int kMaxRows = 16; // test no vars. EXPECT_TRUE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars)); ShDataType types[] = { SH_FLOAT_MAT4, // 0 SH_FLOAT_MAT2, // 1 SH_FLOAT_VEC4, // 2 SH_INT_VEC4, // 3 SH_BOOL_VEC4, // 4 SH_FLOAT_MAT3, // 5 SH_FLOAT_VEC3, // 6 SH_INT_VEC3, // 7 SH_BOOL_VEC3, // 8 SH_FLOAT_VEC2, // 9 SH_INT_VEC2, // 10 SH_BOOL_VEC2, // 11 SH_FLOAT, // 12 SH_INT, // 13 SH_BOOL, // 14 SH_SAMPLER_2D, // 15 SH_SAMPLER_CUBE, // 16 SH_SAMPLER_EXTERNAL_OES, // 17 SH_SAMPLER_2D_RECT_ARB, // 18 }; for (size_t tt = 0; tt < sizeof(types) / sizeof(types[0]); ++tt) { ShDataType type = types[tt]; int num_rows = VariablePacker::GetNumRows(type); int num_components_per_row = VariablePacker::GetNumComponentsPerRow(type); // Check 1 of the type. vars.clear(); vars.push_back(TVariableInfo(type, 1)); EXPECT_TRUE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars)); // Check exactly the right amount of 1 type as an array. int num_vars = kMaxRows / num_rows; vars.clear(); vars.push_back(TVariableInfo(type, num_vars)); EXPECT_TRUE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars)); // test too many vars.clear(); vars.push_back(TVariableInfo(type, num_vars + 1)); EXPECT_FALSE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars)); // Check exactly the right amount of 1 type as individual vars. num_vars = kMaxRows / num_rows * ((num_components_per_row > 2) ? 1 : (4 / num_components_per_row)); vars.clear(); for (int ii = 0; ii < num_vars; ++ii) { vars.push_back(TVariableInfo(type, 1)); } EXPECT_TRUE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars)); // Check 1 too many. vars.push_back(TVariableInfo( type, 1)); EXPECT_FALSE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars)); } // Test example from GLSL ES 3.0 spec chapter 11. vars.clear(); vars.push_back(TVariableInfo(SH_FLOAT_VEC4, 1)); vars.push_back(TVariableInfo(SH_FLOAT_MAT3, 1)); vars.push_back(TVariableInfo(SH_FLOAT_MAT3, 1)); vars.push_back(TVariableInfo(SH_FLOAT_VEC2, 6)); vars.push_back(TVariableInfo(SH_FLOAT_VEC2, 4)); vars.push_back(TVariableInfo(SH_FLOAT_VEC2, 1)); vars.push_back(TVariableInfo(SH_FLOAT, 3)); vars.push_back(TVariableInfo(SH_FLOAT, 2)); vars.push_back(TVariableInfo(SH_FLOAT, 1)); EXPECT_TRUE(packer.CheckVariablesWithinPackingLimits(kMaxRows, vars)); }
C++