blob: 8052bded081cd8ee0c97325feeec0e8f886f21e6 [file] [log] [blame]
daniel@transgaming.com4834ee22013-01-11 04:06:16 +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// TextureStorage11.cpp: Implements the abstract rx::TextureStorage11 class and its concrete derived
8// classes TextureStorage11_2D and TextureStorage11_Cube, which act as the interface to the D3D11 texture.
9
10#include "libGLESv2/renderer/TextureStorage11.h"
11
12#include "libGLESv2/renderer/Renderer11.h"
13#include "libGLESv2/renderer/RenderTarget11.h"
14#include "libGLESv2/renderer/SwapChain11.h"
15#include "libGLESv2/renderer/renderer11_utils.h"
16
17#include "libGLESv2/main.h"
18
19namespace rx
20{
21
22TextureStorage11::TextureStorage11(Renderer *renderer, UINT bindFlags)
23 : mBindFlags(bindFlags),
24 mLodOffset(0)
25{
26 mRenderer = Renderer11::makeRenderer11(renderer);
27}
28
29TextureStorage11::~TextureStorage11()
30{
31}
32
33TextureStorage11 *TextureStorage11::makeTextureStorage11(TextureStorage *storage)
34{
35 ASSERT(dynamic_cast<TextureStorage11*>(storage) != NULL);
36 return static_cast<TextureStorage11*>(storage);
37}
38
39DWORD TextureStorage11::GetTextureBindFlags(DXGI_FORMAT format, GLenum glusage, bool forceRenderable)
40{
41 UINT bindFlags = D3D11_BIND_SHADER_RESOURCE;
42
43 if (format == DXGI_FORMAT_D24_UNORM_S8_UINT)
44 {
45 bindFlags |= D3D11_BIND_DEPTH_STENCIL;
46 }
47 else if(forceRenderable || (TextureStorage11::IsTextureFormatRenderable(format) && (glusage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE)))
48 {
49 bindFlags |= D3D11_BIND_RENDER_TARGET;
50 }
51 return bindFlags;
52}
53
54bool TextureStorage11::IsTextureFormatRenderable(DXGI_FORMAT format)
55{
56 switch(format)
57 {
58 case DXGI_FORMAT_R8G8B8A8_UNORM:
59 case DXGI_FORMAT_A8_UNORM:
60 case DXGI_FORMAT_R32G32B32A32_FLOAT:
61 case DXGI_FORMAT_R32G32B32_FLOAT:
62 case DXGI_FORMAT_R16G16B16A16_FLOAT:
63 case DXGI_FORMAT_B8G8R8A8_UNORM:
64 case DXGI_FORMAT_R8_UNORM:
65 case DXGI_FORMAT_R8G8_UNORM:
66 case DXGI_FORMAT_R16_FLOAT:
67 case DXGI_FORMAT_R16G16_FLOAT:
68 return true;
69 case DXGI_FORMAT_BC1_UNORM:
70 case DXGI_FORMAT_BC2_UNORM:
71 case DXGI_FORMAT_BC3_UNORM:
72 return false;
73 default:
74 UNREACHABLE();
75 return false;
76 }
77}
78
79UINT TextureStorage11::getBindFlags() const
80{
81 return mBindFlags;
82}
83
84int TextureStorage11::getLodOffset() const
85{
86 return mLodOffset;
87}
88
89bool TextureStorage11::isRenderTarget() const
90{
91 return (mBindFlags & (D3D11_BIND_RENDER_TARGET | D3D11_BIND_DEPTH_STENCIL)) != 0;
92}
93
94bool TextureStorage11::isManaged() const
95{
96 return false;
97}
98
99// TODO - We should store level count internally at creation time instead
100// of making driver calls to determine it each time levelCount() is called.
101int TextureStorage11::levelCount()
102{
103 int levels = 0;
104 if (getBaseTexture())
105 {
106 D3D11_TEXTURE2D_DESC desc;
107 getBaseTexture()->GetDesc(&desc);
108 levels = desc.MipLevels - getLodOffset();
109 }
110 return levels;
111}
112
113TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, SwapChain11 *swapchain)
114 : TextureStorage11(renderer, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE)
115{
116 ID3D11Texture2D *surfaceTexture = swapchain->getOffscreenTexture();
117 mTexture = surfaceTexture;
118
119 D3D11_TEXTURE2D_DESC desc;
120 surfaceTexture->GetDesc(&desc);
121
122 initializeRenderTarget(desc.Format, desc.Width, desc.Height);
123}
124
125TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
126 : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable))
127{
128 mTexture = NULL;
129 DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat);
130 // if the width or height is not positive this should be treated as an incomplete texture
131 // we handle that here by skipping the d3d texture creation
132 if (width > 0 && height > 0)
133 {
134 // adjust size if needed for compressed textures
135 gl::MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset);
136
137 ID3D11Device *device = mRenderer->getDevice();
138
139 D3D11_TEXTURE2D_DESC desc;
140 desc.Width = width; // Compressed texture size constraints?
141 desc.Height = height;
142 desc.MipLevels = levels + mLodOffset;
143 desc.ArraySize = 1;
144 desc.Format = format;
145 desc.SampleDesc.Count = 1;
146 desc.SampleDesc.Quality = 0;
147 desc.Usage = D3D11_USAGE_DEFAULT;
148 desc.BindFlags = getBindFlags();
149 desc.CPUAccessFlags = 0;
150 desc.MiscFlags = 0;
151
152 HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture);
153
154 if (FAILED(result))
155 {
156 ASSERT(result == E_OUTOFMEMORY);
157 ERR("Creating image failed.");
158 error(GL_OUT_OF_MEMORY);
159 }
160 }
161
162 initializeRenderTarget(format, width, height);
163}
164
165TextureStorage11_2D::~TextureStorage11_2D()
166{
167 if (mTexture)
168 mTexture->Release();
169}
170
171TextureStorage11_2D *TextureStorage11_2D::makeTextureStorage11_2D(TextureStorage *storage)
172{
173 ASSERT(dynamic_cast<TextureStorage11_2D*>(storage) != NULL);
174 return static_cast<TextureStorage11_2D*>(storage);
175}
176
177RenderTarget *TextureStorage11_2D::getRenderTarget() const
178{
179 return mRenderTarget;
180}
181
182ID3D11Texture2D *TextureStorage11_2D::getBaseTexture() const
183{
184 return mTexture;
185}
186
187void TextureStorage11_2D::generateMipmap(int level)
188{
189 // TODO
190 UNIMPLEMENTED();
191}
192
193void TextureStorage11_2D::initializeRenderTarget(DXGI_FORMAT format, int width, int height)
194{
195 mRenderTarget = NULL;
196
197 if (mTexture != NULL && isRenderTarget())
198 {
199 if (getBindFlags() & D3D11_BIND_RENDER_TARGET)
200 {
201 // Create render target view -- texture should already be created with
202 // BIND_RENDER_TARGET flag.
203 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
204 rtvDesc.Format = format;
205 rtvDesc.Texture2D.MipSlice = 0;
206
207 ID3D11RenderTargetView *renderTargetView;
208 ID3D11Device *device = mRenderer->getDevice();
209 HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
210
211 if (result == E_OUTOFMEMORY)
212 return;
213
214 ASSERT(SUCCESS(result));
215
216 mRenderTarget = new RenderTarget11(mRenderer, renderTargetView, width, height);
217 }
218 else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL)
219 {
220 // TODO
221 UNIMPLEMENTED();
222 }
223 else
224 UNREACHABLE();
225 }
226}
227
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000228TextureStorage11_Cube::TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
229 : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable))
230{
231 mTexture = NULL;
232 DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat);
233 // if the size is not positive this should be treated as an incomplete texture
234 // we handle that here by skipping the d3d texture creation
235 if (size > 0)
236 {
237 // adjust size if needed for compressed textures
238 int height = size;
239 gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset);
240
241 ID3D11Device *device = mRenderer->getDevice();
242
243 D3D11_TEXTURE2D_DESC desc;
244 desc.Width = size;
245 desc.Height = size;
246 desc.MipLevels = levels + mLodOffset;
247 desc.ArraySize = 6;
248 desc.Format = format;
249 desc.SampleDesc.Count = 1;
250 desc.SampleDesc.Quality = 0;
251 desc.Usage = D3D11_USAGE_DEFAULT;
252 desc.BindFlags = getBindFlags();
253 desc.CPUAccessFlags = 0;
254 desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
255
256 HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture);
257
258 if (FAILED(result))
259 {
260 ASSERT(result == E_OUTOFMEMORY);
261 ERR("Creating image failed.");
262 error(GL_OUT_OF_MEMORY);
263 }
264 }
265
266 initializeRenderTarget(format, size);
267}
268
269TextureStorage11_Cube::~TextureStorage11_Cube()
270{
271 if (mTexture)
272 mTexture->Release();
273}
274
275TextureStorage11_Cube *TextureStorage11_Cube::makeTextureStorage11_Cube(TextureStorage *storage)
276{
277 ASSERT(dynamic_cast<TextureStorage11_Cube*>(storage) != NULL);
278 return static_cast<TextureStorage11_Cube*>(storage);
279}
280
281RenderTarget *TextureStorage11_Cube::getRenderTarget(GLenum faceTarget) const
282{
283 return mRenderTarget[gl::TextureCubeMap::faceIndex(faceTarget)];
284}
285
286ID3D11Texture2D *TextureStorage11_Cube::getBaseTexture() const
287{
288 return mTexture;
289}
290
291void TextureStorage11_Cube::generateMipmap(int face, int level)
292{
293 // TODO
294 UNIMPLEMENTED();
295}
296
297void TextureStorage11_Cube::initializeRenderTarget(DXGI_FORMAT format, int size)
298{
299 for (int i = 0; i < 6; ++i)
300 {
301 mRenderTarget[i] = NULL;
302 }
303
304 if (mTexture != NULL && isRenderTarget())
305 {
306 if (getBindFlags() & D3D11_BIND_RENDER_TARGET)
307 {
308 // Create render target view -- texture should already be created with
309 // BIND_RENDER_TARGET flag.
310
311 for (int i = 0; i < 6; ++i)
312 {
313 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
314 rtvDesc.Format = format;
315 rtvDesc.Texture2DArray.MipSlice = 0;
316 rtvDesc.Texture2DArray.FirstArraySlice = i;
317 rtvDesc.Texture2DArray.ArraySize = 1;
318
319 ID3D11RenderTargetView *renderTargetView;
320 ID3D11Device *device = mRenderer->getDevice();
321 HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
322
323 if (result == E_OUTOFMEMORY)
324 return;
325
326 ASSERT(SUCCESS(result));
327
328 mRenderTarget[i] = new RenderTarget11(mRenderer, renderTargetView, size, size);
329 }
330 }
331 else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL)
332 {
333 // TODO
334 UNIMPLEMENTED();
335 }
336 else
337 UNREACHABLE();
338 }
339}
340
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000341}