blob: 1c1227972b4c80e4a67b3aff7fb8ccac55137ee4 [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),
daniel@transgaming.comb1154552013-01-11 04:11:02 +000024 mLodOffset(0),
25 mMipLevels(0)
daniel@transgaming.com4834ee22013-01-11 04:06:16 +000026{
27 mRenderer = Renderer11::makeRenderer11(renderer);
28}
29
30TextureStorage11::~TextureStorage11()
31{
32}
33
34TextureStorage11 *TextureStorage11::makeTextureStorage11(TextureStorage *storage)
35{
36 ASSERT(dynamic_cast<TextureStorage11*>(storage) != NULL);
37 return static_cast<TextureStorage11*>(storage);
38}
39
40DWORD TextureStorage11::GetTextureBindFlags(DXGI_FORMAT format, GLenum glusage, bool forceRenderable)
41{
42 UINT bindFlags = D3D11_BIND_SHADER_RESOURCE;
43
44 if (format == DXGI_FORMAT_D24_UNORM_S8_UINT)
45 {
46 bindFlags |= D3D11_BIND_DEPTH_STENCIL;
47 }
48 else if(forceRenderable || (TextureStorage11::IsTextureFormatRenderable(format) && (glusage == GL_FRAMEBUFFER_ATTACHMENT_ANGLE)))
49 {
50 bindFlags |= D3D11_BIND_RENDER_TARGET;
51 }
52 return bindFlags;
53}
54
55bool TextureStorage11::IsTextureFormatRenderable(DXGI_FORMAT format)
56{
57 switch(format)
58 {
59 case DXGI_FORMAT_R8G8B8A8_UNORM:
60 case DXGI_FORMAT_A8_UNORM:
61 case DXGI_FORMAT_R32G32B32A32_FLOAT:
62 case DXGI_FORMAT_R32G32B32_FLOAT:
63 case DXGI_FORMAT_R16G16B16A16_FLOAT:
64 case DXGI_FORMAT_B8G8R8A8_UNORM:
65 case DXGI_FORMAT_R8_UNORM:
66 case DXGI_FORMAT_R8G8_UNORM:
67 case DXGI_FORMAT_R16_FLOAT:
68 case DXGI_FORMAT_R16G16_FLOAT:
69 return true;
70 case DXGI_FORMAT_BC1_UNORM:
71 case DXGI_FORMAT_BC2_UNORM:
72 case DXGI_FORMAT_BC3_UNORM:
73 return false;
74 default:
75 UNREACHABLE();
76 return false;
77 }
78}
79
80UINT TextureStorage11::getBindFlags() const
81{
82 return mBindFlags;
83}
84
85int TextureStorage11::getLodOffset() const
86{
87 return mLodOffset;
88}
89
90bool TextureStorage11::isRenderTarget() const
91{
92 return (mBindFlags & (D3D11_BIND_RENDER_TARGET | D3D11_BIND_DEPTH_STENCIL)) != 0;
93}
94
95bool TextureStorage11::isManaged() const
96{
97 return false;
98}
99
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000100int TextureStorage11::levelCount()
101{
102 int levels = 0;
103 if (getBaseTexture())
104 {
daniel@transgaming.comb1154552013-01-11 04:11:02 +0000105 levels = mMipLevels - getLodOffset();
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000106 }
107 return levels;
108}
109
daniel@transgaming.com9a2f54d2013-01-11 04:06:36 +0000110UINT TextureStorage11::getSubresourceIndex(int level, int faceIndex)
111{
112 UINT index = 0;
113 if (getBaseTexture())
114 {
daniel@transgaming.comb1154552013-01-11 04:11:02 +0000115 index = D3D11CalcSubresource(level, faceIndex, mMipLevels);
daniel@transgaming.com9a2f54d2013-01-11 04:06:36 +0000116 }
117 return index;
118}
119
120bool TextureStorage11::updateSubresourceLevel(ID3D11Texture2D *srcTexture, int level, int face, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height)
121{
122 if (srcTexture)
123 {
124 D3D11_BOX srcBox;
125 srcBox.left = xoffset;
126 srcBox.top = yoffset;
127 srcBox.right = xoffset + width;
128 srcBox.bottom = yoffset + height;
129 srcBox.front = 0;
130 srcBox.back = 1;
131
132 ID3D11DeviceContext *context = mRenderer->getDeviceContext();
133
134 ASSERT(getBaseTexture());
135 context->CopySubresourceRegion(getBaseTexture(), getSubresourceIndex(level, face), xoffset, yoffset, 0, srcTexture, 0, &srcBox);
136 return true;
137 }
138
139 return false;
140}
141
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000142TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, SwapChain11 *swapchain)
143 : TextureStorage11(renderer, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE)
144{
145 ID3D11Texture2D *surfaceTexture = swapchain->getOffscreenTexture();
146 mTexture = surfaceTexture;
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000147 mSRV = NULL;
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000148 mRenderTarget = NULL;
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000149
150 D3D11_TEXTURE2D_DESC desc;
151 surfaceTexture->GetDesc(&desc);
152
daniel@transgaming.comb1154552013-01-11 04:11:02 +0000153 mMipLevels = desc.MipLevels;
154
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000155 initializeSRV(desc.Format, desc.MipLevels);
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000156 initializeRenderTarget(desc.Format, desc.Width, desc.Height);
157}
158
159TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
160 : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable))
161{
162 mTexture = NULL;
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000163 mSRV = NULL;
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000164 mRenderTarget = NULL;
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000165 DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat);
daniel@transgaming.comb1154552013-01-11 04:11:02 +0000166
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000167 // if the width or height is not positive this should be treated as an incomplete texture
168 // we handle that here by skipping the d3d texture creation
169 if (width > 0 && height > 0)
170 {
171 // adjust size if needed for compressed textures
172 gl::MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset);
173
174 ID3D11Device *device = mRenderer->getDevice();
175
176 D3D11_TEXTURE2D_DESC desc;
177 desc.Width = width; // Compressed texture size constraints?
178 desc.Height = height;
179 desc.MipLevels = levels + mLodOffset;
180 desc.ArraySize = 1;
181 desc.Format = format;
182 desc.SampleDesc.Count = 1;
183 desc.SampleDesc.Quality = 0;
184 desc.Usage = D3D11_USAGE_DEFAULT;
185 desc.BindFlags = getBindFlags();
186 desc.CPUAccessFlags = 0;
187 desc.MiscFlags = 0;
188
189 HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture);
190
191 if (FAILED(result))
192 {
193 ASSERT(result == E_OUTOFMEMORY);
194 ERR("Creating image failed.");
195 error(GL_OUT_OF_MEMORY);
196 }
daniel@transgaming.comb1154552013-01-11 04:11:02 +0000197 else
198 {
199 mTexture->GetDesc(&desc);
200 mMipLevels = desc.MipLevels;
201 }
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000202 }
203
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000204 initializeSRV(format, levels + mLodOffset);
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000205 initializeRenderTarget(format, width, height);
206}
207
208TextureStorage11_2D::~TextureStorage11_2D()
209{
210 if (mTexture)
211 mTexture->Release();
212}
213
214TextureStorage11_2D *TextureStorage11_2D::makeTextureStorage11_2D(TextureStorage *storage)
215{
216 ASSERT(dynamic_cast<TextureStorage11_2D*>(storage) != NULL);
217 return static_cast<TextureStorage11_2D*>(storage);
218}
219
220RenderTarget *TextureStorage11_2D::getRenderTarget() const
221{
222 return mRenderTarget;
223}
224
225ID3D11Texture2D *TextureStorage11_2D::getBaseTexture() const
226{
227 return mTexture;
228}
229
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000230ID3D11ShaderResourceView *TextureStorage11_2D::getSRV() const
231{
232 return mSRV;
233}
234
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000235void TextureStorage11_2D::generateMipmap(int level)
236{
237 // TODO
238 UNIMPLEMENTED();
239}
240
241void TextureStorage11_2D::initializeRenderTarget(DXGI_FORMAT format, int width, int height)
242{
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000243 ASSERT(mRenderTarget == NULL);
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000244
245 if (mTexture != NULL && isRenderTarget())
246 {
247 if (getBindFlags() & D3D11_BIND_RENDER_TARGET)
248 {
249 // Create render target view -- texture should already be created with
250 // BIND_RENDER_TARGET flag.
251 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
daniel@transgaming.com442cdc12013-01-11 04:09:49 +0000252 rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000253 rtvDesc.Format = format;
254 rtvDesc.Texture2D.MipSlice = 0;
255
256 ID3D11RenderTargetView *renderTargetView;
257 ID3D11Device *device = mRenderer->getDevice();
258 HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
259
260 if (result == E_OUTOFMEMORY)
261 return;
262
daniel@transgaming.com11861ce2013-01-11 04:07:00 +0000263 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000264
265 mRenderTarget = new RenderTarget11(mRenderer, renderTargetView, width, height);
266 }
267 else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL)
268 {
269 // TODO
270 UNIMPLEMENTED();
271 }
272 else
273 UNREACHABLE();
274 }
275}
276
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000277void TextureStorage11_2D::initializeSRV(DXGI_FORMAT format, int levels)
278{
279 ASSERT(mSRV == NULL);
280
281 if (mTexture)
282 {
283 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
284 srvDesc.Format = format;
285 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
286 srvDesc.Texture2D.MostDetailedMip = 0;
daniel@transgaming.coma0b868a2013-01-11 04:08:27 +0000287 srvDesc.Texture2D.MipLevels = (levels == 0 ? -1 : levels);
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000288
289 ID3D11Device *device = mRenderer->getDevice();
290 HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV);
291
292 if (result == E_OUTOFMEMORY)
293 {
294 return error(GL_OUT_OF_MEMORY);
295 }
296
297 ASSERT(SUCCEEDED(result));
298 }
299}
300
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000301TextureStorage11_Cube::TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
302 : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable))
303{
304 mTexture = NULL;
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000305 mSRV = NULL;
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000306
307 for (int i = 0; i < 6; ++i)
308 {
309 mRenderTarget[i] = NULL;
310 }
311
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000312 DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat);
313 // if the size is not positive this should be treated as an incomplete texture
314 // we handle that here by skipping the d3d texture creation
315 if (size > 0)
316 {
317 // adjust size if needed for compressed textures
318 int height = size;
319 gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset);
320
321 ID3D11Device *device = mRenderer->getDevice();
322
323 D3D11_TEXTURE2D_DESC desc;
324 desc.Width = size;
325 desc.Height = size;
326 desc.MipLevels = levels + mLodOffset;
327 desc.ArraySize = 6;
328 desc.Format = format;
329 desc.SampleDesc.Count = 1;
330 desc.SampleDesc.Quality = 0;
331 desc.Usage = D3D11_USAGE_DEFAULT;
332 desc.BindFlags = getBindFlags();
333 desc.CPUAccessFlags = 0;
334 desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
335
336 HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture);
337
338 if (FAILED(result))
339 {
340 ASSERT(result == E_OUTOFMEMORY);
341 ERR("Creating image failed.");
342 error(GL_OUT_OF_MEMORY);
343 }
daniel@transgaming.comb1154552013-01-11 04:11:02 +0000344 else
345 {
346 mTexture->GetDesc(&desc);
347 mMipLevels = desc.MipLevels;
348 }
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000349 }
350
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000351 initializeSRV(format, levels + mLodOffset);
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000352 initializeRenderTarget(format, size);
353}
354
355TextureStorage11_Cube::~TextureStorage11_Cube()
356{
357 if (mTexture)
358 mTexture->Release();
359}
360
361TextureStorage11_Cube *TextureStorage11_Cube::makeTextureStorage11_Cube(TextureStorage *storage)
362{
363 ASSERT(dynamic_cast<TextureStorage11_Cube*>(storage) != NULL);
364 return static_cast<TextureStorage11_Cube*>(storage);
365}
366
367RenderTarget *TextureStorage11_Cube::getRenderTarget(GLenum faceTarget) const
368{
369 return mRenderTarget[gl::TextureCubeMap::faceIndex(faceTarget)];
370}
371
372ID3D11Texture2D *TextureStorage11_Cube::getBaseTexture() const
373{
374 return mTexture;
375}
376
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000377ID3D11ShaderResourceView *TextureStorage11_Cube::getSRV() const
378{
379 return mSRV;
380}
381
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000382void TextureStorage11_Cube::generateMipmap(int face, int level)
383{
384 // TODO
385 UNIMPLEMENTED();
386}
387
388void TextureStorage11_Cube::initializeRenderTarget(DXGI_FORMAT format, int size)
389{
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000390 if (mTexture != NULL && isRenderTarget())
391 {
392 if (getBindFlags() & D3D11_BIND_RENDER_TARGET)
393 {
394 // Create render target view -- texture should already be created with
395 // BIND_RENDER_TARGET flag.
396
397 for (int i = 0; i < 6; ++i)
398 {
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000399 ASSERT(mRenderTarget[i] == NULL);
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000400 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
401 rtvDesc.Format = format;
402 rtvDesc.Texture2DArray.MipSlice = 0;
403 rtvDesc.Texture2DArray.FirstArraySlice = i;
404 rtvDesc.Texture2DArray.ArraySize = 1;
405
406 ID3D11RenderTargetView *renderTargetView;
407 ID3D11Device *device = mRenderer->getDevice();
408 HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
409
410 if (result == E_OUTOFMEMORY)
411 return;
412
daniel@transgaming.com11861ce2013-01-11 04:07:00 +0000413 ASSERT(SUCCEEDED(result));
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000414
415 mRenderTarget[i] = new RenderTarget11(mRenderer, renderTargetView, size, size);
416 }
417 }
418 else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL)
419 {
420 // TODO
421 UNIMPLEMENTED();
422 }
423 else
424 UNREACHABLE();
425 }
426}
427
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000428void TextureStorage11_Cube::initializeSRV(DXGI_FORMAT format, int levels)
429{
430 ASSERT(mSRV == NULL);
431
432 if (mTexture)
433 {
434 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
435 srvDesc.Format = format;
436 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
daniel@transgaming.coma0b868a2013-01-11 04:08:27 +0000437 srvDesc.TextureCube.MipLevels = (levels == 0 ? -1 : levels);
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000438 srvDesc.TextureCube.MostDetailedMip = 0;
439
440 ID3D11Device *device = mRenderer->getDevice();
441 HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV);
442
443 if (result == E_OUTOFMEMORY)
444 {
445 return error(GL_OUT_OF_MEMORY);
446 }
447
448 ASSERT(SUCCEEDED(result));
449 }
450}
451
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000452}