blob: a3902fb2384790b347df6676c8d2bbfeeb60c679 [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{
daniel@transgaming.com1e1b5e92013-01-11 04:11:27 +000032}
daniel@transgaming.com4834ee22013-01-11 04:06:16 +000033
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();
daniel@transgaming.com1e1b5e92013-01-11 04:11:27 +0000212
213 if (mSRV)
214 mSRV->Release();
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000215}
216
217TextureStorage11_2D *TextureStorage11_2D::makeTextureStorage11_2D(TextureStorage *storage)
218{
219 ASSERT(dynamic_cast<TextureStorage11_2D*>(storage) != NULL);
220 return static_cast<TextureStorage11_2D*>(storage);
221}
222
223RenderTarget *TextureStorage11_2D::getRenderTarget() const
224{
225 return mRenderTarget;
226}
227
228ID3D11Texture2D *TextureStorage11_2D::getBaseTexture() const
229{
230 return mTexture;
231}
232
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000233ID3D11ShaderResourceView *TextureStorage11_2D::getSRV() const
234{
235 return mSRV;
236}
237
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000238void TextureStorage11_2D::generateMipmap(int level)
239{
240 // TODO
241 UNIMPLEMENTED();
242}
243
244void TextureStorage11_2D::initializeRenderTarget(DXGI_FORMAT format, int width, int height)
245{
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000246 ASSERT(mRenderTarget == NULL);
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000247
248 if (mTexture != NULL && isRenderTarget())
249 {
250 if (getBindFlags() & D3D11_BIND_RENDER_TARGET)
251 {
252 // Create render target view -- texture should already be created with
253 // BIND_RENDER_TARGET flag.
254 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
daniel@transgaming.com442cdc12013-01-11 04:09:49 +0000255 rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000256 rtvDesc.Format = format;
257 rtvDesc.Texture2D.MipSlice = 0;
258
259 ID3D11RenderTargetView *renderTargetView;
260 ID3D11Device *device = mRenderer->getDevice();
261 HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
262
263 if (result == E_OUTOFMEMORY)
264 return;
265
daniel@transgaming.com11861ce2013-01-11 04:07:00 +0000266 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000267
268 mRenderTarget = new RenderTarget11(mRenderer, renderTargetView, width, height);
269 }
270 else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL)
271 {
272 // TODO
273 UNIMPLEMENTED();
274 }
275 else
276 UNREACHABLE();
277 }
278}
279
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000280void TextureStorage11_2D::initializeSRV(DXGI_FORMAT format, int levels)
281{
282 ASSERT(mSRV == NULL);
283
284 if (mTexture)
285 {
286 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
287 srvDesc.Format = format;
288 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
289 srvDesc.Texture2D.MostDetailedMip = 0;
daniel@transgaming.coma0b868a2013-01-11 04:08:27 +0000290 srvDesc.Texture2D.MipLevels = (levels == 0 ? -1 : levels);
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000291
292 ID3D11Device *device = mRenderer->getDevice();
293 HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV);
294
295 if (result == E_OUTOFMEMORY)
296 {
297 return error(GL_OUT_OF_MEMORY);
298 }
299
300 ASSERT(SUCCEEDED(result));
301 }
302}
303
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000304TextureStorage11_Cube::TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
305 : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable))
306{
307 mTexture = NULL;
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000308 mSRV = NULL;
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000309
310 for (int i = 0; i < 6; ++i)
311 {
312 mRenderTarget[i] = NULL;
313 }
314
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000315 DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat);
316 // if the size is not positive this should be treated as an incomplete texture
317 // we handle that here by skipping the d3d texture creation
318 if (size > 0)
319 {
320 // adjust size if needed for compressed textures
321 int height = size;
322 gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset);
323
324 ID3D11Device *device = mRenderer->getDevice();
325
326 D3D11_TEXTURE2D_DESC desc;
327 desc.Width = size;
328 desc.Height = size;
329 desc.MipLevels = levels + mLodOffset;
330 desc.ArraySize = 6;
331 desc.Format = format;
332 desc.SampleDesc.Count = 1;
333 desc.SampleDesc.Quality = 0;
334 desc.Usage = D3D11_USAGE_DEFAULT;
335 desc.BindFlags = getBindFlags();
336 desc.CPUAccessFlags = 0;
337 desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
338
339 HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture);
340
341 if (FAILED(result))
342 {
343 ASSERT(result == E_OUTOFMEMORY);
344 ERR("Creating image failed.");
345 error(GL_OUT_OF_MEMORY);
346 }
daniel@transgaming.comb1154552013-01-11 04:11:02 +0000347 else
348 {
349 mTexture->GetDesc(&desc);
350 mMipLevels = desc.MipLevels;
351 }
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000352 }
353
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000354 initializeSRV(format, levels + mLodOffset);
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000355 initializeRenderTarget(format, size);
356}
357
358TextureStorage11_Cube::~TextureStorage11_Cube()
359{
360 if (mTexture)
361 mTexture->Release();
daniel@transgaming.com1e1b5e92013-01-11 04:11:27 +0000362
363 if (mSRV)
364 mSRV->Release();
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000365}
366
367TextureStorage11_Cube *TextureStorage11_Cube::makeTextureStorage11_Cube(TextureStorage *storage)
368{
369 ASSERT(dynamic_cast<TextureStorage11_Cube*>(storage) != NULL);
370 return static_cast<TextureStorage11_Cube*>(storage);
371}
372
373RenderTarget *TextureStorage11_Cube::getRenderTarget(GLenum faceTarget) const
374{
375 return mRenderTarget[gl::TextureCubeMap::faceIndex(faceTarget)];
376}
377
378ID3D11Texture2D *TextureStorage11_Cube::getBaseTexture() const
379{
380 return mTexture;
381}
382
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000383ID3D11ShaderResourceView *TextureStorage11_Cube::getSRV() const
384{
385 return mSRV;
386}
387
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000388void TextureStorage11_Cube::generateMipmap(int face, int level)
389{
390 // TODO
391 UNIMPLEMENTED();
392}
393
394void TextureStorage11_Cube::initializeRenderTarget(DXGI_FORMAT format, int size)
395{
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000396 if (mTexture != NULL && isRenderTarget())
397 {
398 if (getBindFlags() & D3D11_BIND_RENDER_TARGET)
399 {
400 // Create render target view -- texture should already be created with
401 // BIND_RENDER_TARGET flag.
402
403 for (int i = 0; i < 6; ++i)
404 {
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000405 ASSERT(mRenderTarget[i] == NULL);
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000406 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
407 rtvDesc.Format = format;
408 rtvDesc.Texture2DArray.MipSlice = 0;
409 rtvDesc.Texture2DArray.FirstArraySlice = i;
410 rtvDesc.Texture2DArray.ArraySize = 1;
411
412 ID3D11RenderTargetView *renderTargetView;
413 ID3D11Device *device = mRenderer->getDevice();
414 HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
415
416 if (result == E_OUTOFMEMORY)
417 return;
418
daniel@transgaming.com11861ce2013-01-11 04:07:00 +0000419 ASSERT(SUCCEEDED(result));
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000420
421 mRenderTarget[i] = new RenderTarget11(mRenderer, renderTargetView, size, size);
422 }
423 }
424 else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL)
425 {
426 // TODO
427 UNIMPLEMENTED();
428 }
429 else
430 UNREACHABLE();
431 }
432}
433
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000434void TextureStorage11_Cube::initializeSRV(DXGI_FORMAT format, int levels)
435{
436 ASSERT(mSRV == NULL);
437
438 if (mTexture)
439 {
440 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
441 srvDesc.Format = format;
442 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
daniel@transgaming.coma0b868a2013-01-11 04:08:27 +0000443 srvDesc.TextureCube.MipLevels = (levels == 0 ? -1 : levels);
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000444 srvDesc.TextureCube.MostDetailedMip = 0;
445
446 ID3D11Device *device = mRenderer->getDevice();
447 HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV);
448
449 if (result == E_OUTOFMEMORY)
450 {
451 return error(GL_OUT_OF_MEMORY);
452 }
453
454 ASSERT(SUCCEEDED(result));
455 }
456}
457
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000458}