blob: 4afa3d185b2063d959913e335be90a7f5a466a22 [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
daniel@transgaming.com9a2f54d2013-01-11 04:06:36 +0000113// TODO - Once we're storing level count internally, we should no longer
114// need to look up the texture description to determine the number of mip levels.
115UINT TextureStorage11::getSubresourceIndex(int level, int faceIndex)
116{
117 UINT index = 0;
118 if (getBaseTexture())
119 {
120 D3D11_TEXTURE2D_DESC desc;
121 getBaseTexture()->GetDesc(&desc);
122 index = D3D11CalcSubresource(level, faceIndex, desc.MipLevels);
123 }
124 return index;
125}
126
127bool TextureStorage11::updateSubresourceLevel(ID3D11Texture2D *srcTexture, int level, int face, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
128{
129 if (srcTexture)
130 {
131 D3D11_BOX srcBox;
132 srcBox.left = xoffset;
133 srcBox.top = yoffset;
134 srcBox.right = xoffset + width;
135 srcBox.bottom = yoffset + height;
136 srcBox.front = 0;
137 srcBox.back = 1;
138
139 ID3D11DeviceContext *context = mRenderer->getDeviceContext();
140
141 ASSERT(getBaseTexture());
142 context->CopySubresourceRegion(getBaseTexture(), getSubresourceIndex(level, face), xoffset, yoffset, 0, srcTexture, 0, &srcBox);
143 return true;
144 }
145
146 return false;
147}
148
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000149TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, SwapChain11 *swapchain)
150 : TextureStorage11(renderer, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE)
151{
152 ID3D11Texture2D *surfaceTexture = swapchain->getOffscreenTexture();
153 mTexture = surfaceTexture;
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000154 mSRV = NULL;
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000155
156 D3D11_TEXTURE2D_DESC desc;
157 surfaceTexture->GetDesc(&desc);
158
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000159 initializeSRV(desc.Format, desc.MipLevels);
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000160 initializeRenderTarget(desc.Format, desc.Width, desc.Height);
161}
162
163TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
164 : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable))
165{
166 mTexture = NULL;
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000167 mSRV = NULL;
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000168 DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat);
169 // if the width or height is not positive this should be treated as an incomplete texture
170 // we handle that here by skipping the d3d texture creation
171 if (width > 0 && height > 0)
172 {
173 // adjust size if needed for compressed textures
174 gl::MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset);
175
176 ID3D11Device *device = mRenderer->getDevice();
177
178 D3D11_TEXTURE2D_DESC desc;
179 desc.Width = width; // Compressed texture size constraints?
180 desc.Height = height;
181 desc.MipLevels = levels + mLodOffset;
182 desc.ArraySize = 1;
183 desc.Format = format;
184 desc.SampleDesc.Count = 1;
185 desc.SampleDesc.Quality = 0;
186 desc.Usage = D3D11_USAGE_DEFAULT;
187 desc.BindFlags = getBindFlags();
188 desc.CPUAccessFlags = 0;
189 desc.MiscFlags = 0;
190
191 HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture);
192
193 if (FAILED(result))
194 {
195 ASSERT(result == E_OUTOFMEMORY);
196 ERR("Creating image failed.");
197 error(GL_OUT_OF_MEMORY);
198 }
199 }
200
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000201 initializeSRV(format, levels + mLodOffset);
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000202 initializeRenderTarget(format, width, height);
203}
204
205TextureStorage11_2D::~TextureStorage11_2D()
206{
207 if (mTexture)
208 mTexture->Release();
209}
210
211TextureStorage11_2D *TextureStorage11_2D::makeTextureStorage11_2D(TextureStorage *storage)
212{
213 ASSERT(dynamic_cast<TextureStorage11_2D*>(storage) != NULL);
214 return static_cast<TextureStorage11_2D*>(storage);
215}
216
217RenderTarget *TextureStorage11_2D::getRenderTarget() const
218{
219 return mRenderTarget;
220}
221
222ID3D11Texture2D *TextureStorage11_2D::getBaseTexture() const
223{
224 return mTexture;
225}
226
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000227ID3D11ShaderResourceView *TextureStorage11_2D::getSRV() const
228{
229 return mSRV;
230}
231
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000232void TextureStorage11_2D::generateMipmap(int level)
233{
234 // TODO
235 UNIMPLEMENTED();
236}
237
238void TextureStorage11_2D::initializeRenderTarget(DXGI_FORMAT format, int width, int height)
239{
240 mRenderTarget = NULL;
241
242 if (mTexture != NULL && isRenderTarget())
243 {
244 if (getBindFlags() & D3D11_BIND_RENDER_TARGET)
245 {
246 // Create render target view -- texture should already be created with
247 // BIND_RENDER_TARGET flag.
248 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
249 rtvDesc.Format = format;
250 rtvDesc.Texture2D.MipSlice = 0;
251
252 ID3D11RenderTargetView *renderTargetView;
253 ID3D11Device *device = mRenderer->getDevice();
254 HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
255
256 if (result == E_OUTOFMEMORY)
257 return;
258
daniel@transgaming.com11861ce2013-01-11 04:07:00 +0000259 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000260
261 mRenderTarget = new RenderTarget11(mRenderer, renderTargetView, width, height);
262 }
263 else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL)
264 {
265 // TODO
266 UNIMPLEMENTED();
267 }
268 else
269 UNREACHABLE();
270 }
271}
272
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000273void TextureStorage11_2D::initializeSRV(DXGI_FORMAT format, int levels)
274{
275 ASSERT(mSRV == NULL);
276
277 if (mTexture)
278 {
279 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
280 srvDesc.Format = format;
281 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
282 srvDesc.Texture2D.MostDetailedMip = 0;
283 srvDesc.Texture2D.MipLevels = levels;
284
285 ID3D11Device *device = mRenderer->getDevice();
286 HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV);
287
288 if (result == E_OUTOFMEMORY)
289 {
290 return error(GL_OUT_OF_MEMORY);
291 }
292
293 ASSERT(SUCCEEDED(result));
294 }
295}
296
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000297TextureStorage11_Cube::TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
298 : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable))
299{
300 mTexture = NULL;
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000301 mSRV = NULL;
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000302 DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat);
303 // if the size is not positive this should be treated as an incomplete texture
304 // we handle that here by skipping the d3d texture creation
305 if (size > 0)
306 {
307 // adjust size if needed for compressed textures
308 int height = size;
309 gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset);
310
311 ID3D11Device *device = mRenderer->getDevice();
312
313 D3D11_TEXTURE2D_DESC desc;
314 desc.Width = size;
315 desc.Height = size;
316 desc.MipLevels = levels + mLodOffset;
317 desc.ArraySize = 6;
318 desc.Format = format;
319 desc.SampleDesc.Count = 1;
320 desc.SampleDesc.Quality = 0;
321 desc.Usage = D3D11_USAGE_DEFAULT;
322 desc.BindFlags = getBindFlags();
323 desc.CPUAccessFlags = 0;
324 desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
325
326 HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture);
327
328 if (FAILED(result))
329 {
330 ASSERT(result == E_OUTOFMEMORY);
331 ERR("Creating image failed.");
332 error(GL_OUT_OF_MEMORY);
333 }
334 }
335
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000336 initializeSRV(format, levels + mLodOffset);
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000337 initializeRenderTarget(format, size);
338}
339
340TextureStorage11_Cube::~TextureStorage11_Cube()
341{
342 if (mTexture)
343 mTexture->Release();
344}
345
346TextureStorage11_Cube *TextureStorage11_Cube::makeTextureStorage11_Cube(TextureStorage *storage)
347{
348 ASSERT(dynamic_cast<TextureStorage11_Cube*>(storage) != NULL);
349 return static_cast<TextureStorage11_Cube*>(storage);
350}
351
352RenderTarget *TextureStorage11_Cube::getRenderTarget(GLenum faceTarget) const
353{
354 return mRenderTarget[gl::TextureCubeMap::faceIndex(faceTarget)];
355}
356
357ID3D11Texture2D *TextureStorage11_Cube::getBaseTexture() const
358{
359 return mTexture;
360}
361
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000362ID3D11ShaderResourceView *TextureStorage11_Cube::getSRV() const
363{
364 return mSRV;
365}
366
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000367void TextureStorage11_Cube::generateMipmap(int face, int level)
368{
369 // TODO
370 UNIMPLEMENTED();
371}
372
373void TextureStorage11_Cube::initializeRenderTarget(DXGI_FORMAT format, int size)
374{
375 for (int i = 0; i < 6; ++i)
376 {
377 mRenderTarget[i] = NULL;
378 }
379
380 if (mTexture != NULL && isRenderTarget())
381 {
382 if (getBindFlags() & D3D11_BIND_RENDER_TARGET)
383 {
384 // Create render target view -- texture should already be created with
385 // BIND_RENDER_TARGET flag.
386
387 for (int i = 0; i < 6; ++i)
388 {
389 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
390 rtvDesc.Format = format;
391 rtvDesc.Texture2DArray.MipSlice = 0;
392 rtvDesc.Texture2DArray.FirstArraySlice = i;
393 rtvDesc.Texture2DArray.ArraySize = 1;
394
395 ID3D11RenderTargetView *renderTargetView;
396 ID3D11Device *device = mRenderer->getDevice();
397 HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
398
399 if (result == E_OUTOFMEMORY)
400 return;
401
daniel@transgaming.com11861ce2013-01-11 04:07:00 +0000402 ASSERT(SUCCEEDED(result));
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000403
404 mRenderTarget[i] = new RenderTarget11(mRenderer, renderTargetView, size, size);
405 }
406 }
407 else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL)
408 {
409 // TODO
410 UNIMPLEMENTED();
411 }
412 else
413 UNREACHABLE();
414 }
415}
416
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000417void TextureStorage11_Cube::initializeSRV(DXGI_FORMAT format, int levels)
418{
419 ASSERT(mSRV == NULL);
420
421 if (mTexture)
422 {
423 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
424 srvDesc.Format = format;
425 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
426 srvDesc.TextureCube.MipLevels = levels;
427 srvDesc.TextureCube.MostDetailedMip = 0;
428
429 ID3D11Device *device = mRenderer->getDevice();
430 HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV);
431
432 if (result == E_OUTOFMEMORY)
433 {
434 return error(GL_OUT_OF_MEMORY);
435 }
436
437 ASSERT(SUCCEEDED(result));
438 }
439}
440
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000441}