blob: 797b8dbb59c41e6a79d4a8054fbae2a874b49942 [file] [log] [blame]
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +00001//
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
16namespace rx
17{
18
19RenderTarget11::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
39RenderTarget11::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.comc9a501d2013-01-11 04:08:46 +000059RenderTarget11::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);
daniel@transgaming.com168763a2013-01-11 04:09:56 +000066 int supportedSamples = 0; // TODO - Multisample support query
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +000067
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;
daniel@transgaming.com168763a2013-01-11 04:09:56 +000086 desc.SampleDesc.Count = (supportedSamples == 0 ? 1 : supportedSamples);
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +000087 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;
daniel@transgaming.com442cdc12013-01-11 04:09:49 +0000105 dsvDesc.Flags = 0;
daniel@transgaming.comc9a501d2013-01-11 04:08:46 +0000106 result = device->CreateDepthStencilView(rtTexture, &dsvDesc, &mDepthStencil);
107 }
108 else
109 {
110 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
111 rtvDesc.Format = requestedFormat;
112 rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
113 rtvDesc.Texture2D.MipSlice = 0;
114 result = device->CreateRenderTargetView(rtTexture, &rtvDesc, &mRenderTarget);
115 }
116
117 rtTexture->Release();
118 }
119
120 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
121 {
122 error(GL_OUT_OF_MEMORY);
123
124 return;
125 }
126
127 ASSERT(SUCCEEDED(result));
128 }
129
130 mWidth = width;
131 mHeight = height;
132 mInternalFormat = format;
133 mSamples = supportedSamples;
134 mActualFormat = format;
135}
136
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +0000137RenderTarget11::~RenderTarget11()
138{
139 if (mRenderTarget)
140 {
141 mRenderTarget->Release();
142 }
143
144 if (mDepthStencil)
145 {
146 mDepthStencil->Release();
147 }
148}
149
150RenderTarget11 *RenderTarget11::makeRenderTarget11(RenderTarget *target)
151{
152 ASSERT(dynamic_cast<rx::RenderTarget11*>(target) != NULL);
153 return static_cast<rx::RenderTarget11*>(target);
154}
155
daniel@transgaming.com816c7f32012-11-28 21:02:01 +0000156// Adds reference, caller must call Release
157ID3D11RenderTargetView *RenderTarget11::getRenderTargetView() const
158{
159 if (mRenderTarget)
160 {
161 mRenderTarget->AddRef();
162 }
163
164 return mRenderTarget;
165}
166
167// Adds reference, caller must call Release
168ID3D11DepthStencilView *RenderTarget11::getDepthStencilView() const
169{
170 if (mDepthStencil)
171 {
172 mDepthStencil->AddRef();
173 }
174
175 return mDepthStencil;
176}
177
daniel@transgaming.comb6b27bc2012-11-28 20:54:30 +0000178}