daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // RenderTarget11.cpp: Implements a DX11-specific wrapper for ID3D11View pointers |
| 8 | // retained by Renderbuffers. |
| 9 | |
| 10 | #include "libGLESv2/renderer/RenderTarget11.h" |
| 11 | #include "libGLESv2/renderer/Renderer11.h" |
| 12 | |
| 13 | #include "libGLESv2/renderer/renderer11_utils.h" |
| 14 | #include "libGLESv2/main.h" |
| 15 | |
| 16 | namespace rx |
| 17 | { |
| 18 | |
| 19 | RenderTarget11::RenderTarget11(Renderer *renderer, ID3D11RenderTargetView *view, GLsizei width, GLsizei height) |
| 20 | { |
| 21 | mRenderer = Renderer11::makeRenderer11(renderer); |
| 22 | mRenderTarget = view; |
| 23 | mDepthStencil = NULL; |
| 24 | |
| 25 | if (mRenderTarget) |
| 26 | { |
| 27 | D3D11_RENDER_TARGET_VIEW_DESC desc; |
| 28 | view->GetDesc(&desc); |
| 29 | |
| 30 | mWidth = width; |
| 31 | mHeight = height; |
| 32 | |
| 33 | mInternalFormat = d3d11_gl::ConvertRenderbufferFormat(desc.Format); |
| 34 | mActualFormat = d3d11_gl::ConvertRenderbufferFormat(desc.Format); |
| 35 | mSamples = 1; // TEMP? |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | RenderTarget11::RenderTarget11(Renderer *renderer, ID3D11DepthStencilView *view, GLsizei width, GLsizei height) |
| 40 | { |
| 41 | mRenderer = Renderer11::makeRenderer11(renderer); |
| 42 | mRenderTarget = NULL; |
| 43 | mDepthStencil = view; |
| 44 | |
| 45 | if (mDepthStencil) |
| 46 | { |
| 47 | D3D11_DEPTH_STENCIL_VIEW_DESC desc; |
| 48 | view->GetDesc(&desc); |
| 49 | |
| 50 | mWidth = width; |
| 51 | mHeight = height; |
| 52 | |
| 53 | mInternalFormat = d3d11_gl::ConvertRenderbufferFormat(desc.Format); |
| 54 | mActualFormat = d3d11_gl::ConvertRenderbufferFormat(desc.Format); |
| 55 | mSamples = 1; // TEMP? |
| 56 | } |
| 57 | } |
| 58 | |
daniel@transgaming.com | c9a501d | 2013-01-11 04:08:46 +0000 | [diff] [blame] | 59 | RenderTarget11::RenderTarget11(Renderer *renderer, GLsizei width, GLsizei height, GLenum format, GLsizei samples, bool depth) |
| 60 | { |
| 61 | mRenderer = Renderer11::makeRenderer11(renderer); |
| 62 | mRenderTarget = NULL; |
| 63 | mDepthStencil = NULL; |
| 64 | |
| 65 | DXGI_FORMAT requestedFormat = gl_d3d11::ConvertRenderbufferFormat(format); |
| 66 | int supportedSamples = 1; // TODO - Multisample support query |
| 67 | |
| 68 | if (supportedSamples == -1) |
| 69 | { |
| 70 | error(GL_OUT_OF_MEMORY); |
| 71 | |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | HRESULT result = D3DERR_INVALIDCALL; |
| 76 | |
| 77 | if (width > 0 && height > 0) |
| 78 | { |
| 79 | // Create texture resource |
| 80 | D3D11_TEXTURE2D_DESC desc; |
| 81 | desc.Width = width; |
| 82 | desc.Height = height; |
| 83 | desc.MipLevels = 1; |
| 84 | desc.ArraySize = 1; |
| 85 | desc.Format = requestedFormat; |
| 86 | desc.SampleDesc.Count = supportedSamples; |
| 87 | desc.SampleDesc.Quality = 0; |
| 88 | desc.Usage = D3D11_USAGE_DEFAULT; |
| 89 | desc.CPUAccessFlags = 0; |
| 90 | desc.MiscFlags = 0; |
| 91 | desc.BindFlags = (depth ? D3D11_BIND_DEPTH_STENCIL : D3D11_BIND_RENDER_TARGET); |
| 92 | ID3D11Texture2D *rtTexture = NULL; |
| 93 | |
| 94 | ID3D11Device *device = mRenderer->getDevice(); |
| 95 | HRESULT result = device->CreateTexture2D(&desc, NULL, &rtTexture); |
| 96 | |
| 97 | if (SUCCEEDED(result)) |
| 98 | { |
| 99 | if (depth) |
| 100 | { |
| 101 | D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc; |
| 102 | dsvDesc.Format = requestedFormat; |
| 103 | dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; |
| 104 | dsvDesc.Texture2D.MipSlice = 0; |
| 105 | result = device->CreateDepthStencilView(rtTexture, &dsvDesc, &mDepthStencil); |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | D3D11_RENDER_TARGET_VIEW_DESC rtvDesc; |
| 110 | rtvDesc.Format = requestedFormat; |
| 111 | rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D; |
| 112 | rtvDesc.Texture2D.MipSlice = 0; |
| 113 | result = device->CreateRenderTargetView(rtTexture, &rtvDesc, &mRenderTarget); |
| 114 | } |
| 115 | |
| 116 | rtTexture->Release(); |
| 117 | } |
| 118 | |
| 119 | if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY) |
| 120 | { |
| 121 | error(GL_OUT_OF_MEMORY); |
| 122 | |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | ASSERT(SUCCEEDED(result)); |
| 127 | } |
| 128 | |
| 129 | mWidth = width; |
| 130 | mHeight = height; |
| 131 | mInternalFormat = format; |
| 132 | mSamples = supportedSamples; |
| 133 | mActualFormat = format; |
| 134 | } |
| 135 | |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 136 | RenderTarget11::~RenderTarget11() |
| 137 | { |
| 138 | if (mRenderTarget) |
| 139 | { |
| 140 | mRenderTarget->Release(); |
| 141 | } |
| 142 | |
| 143 | if (mDepthStencil) |
| 144 | { |
| 145 | mDepthStencil->Release(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | RenderTarget11 *RenderTarget11::makeRenderTarget11(RenderTarget *target) |
| 150 | { |
| 151 | ASSERT(dynamic_cast<rx::RenderTarget11*>(target) != NULL); |
| 152 | return static_cast<rx::RenderTarget11*>(target); |
| 153 | } |
| 154 | |
daniel@transgaming.com | 816c7f3 | 2012-11-28 21:02:01 +0000 | [diff] [blame] | 155 | // Adds reference, caller must call Release |
| 156 | ID3D11RenderTargetView *RenderTarget11::getRenderTargetView() const |
| 157 | { |
| 158 | if (mRenderTarget) |
| 159 | { |
| 160 | mRenderTarget->AddRef(); |
| 161 | } |
| 162 | |
| 163 | return mRenderTarget; |
| 164 | } |
| 165 | |
| 166 | // Adds reference, caller must call Release |
| 167 | ID3D11DepthStencilView *RenderTarget11::getDepthStencilView() const |
| 168 | { |
| 169 | if (mDepthStencil) |
| 170 | { |
| 171 | mDepthStencil->AddRef(); |
| 172 | } |
| 173 | |
| 174 | return mDepthStencil; |
| 175 | } |
| 176 | |
daniel@transgaming.com | b6b27bc | 2012-11-28 20:54:30 +0000 | [diff] [blame] | 177 | } |