blob: 61d2429e8fc18d861c4bd1d9f48e7a2669d89066 [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.com5cdd0582013-01-11 04:07:35 +0000155 mRenderTarget = NULL;
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000156
157 D3D11_TEXTURE2D_DESC desc;
158 surfaceTexture->GetDesc(&desc);
159
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000160 initializeSRV(desc.Format, desc.MipLevels);
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000161 initializeRenderTarget(desc.Format, desc.Width, desc.Height);
162}
163
164TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height)
165 : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable))
166{
167 mTexture = NULL;
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000168 mSRV = NULL;
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000169 mRenderTarget = NULL;
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000170 DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat);
171 // if the width or height is not positive this should be treated as an incomplete texture
172 // we handle that here by skipping the d3d texture creation
173 if (width > 0 && height > 0)
174 {
175 // adjust size if needed for compressed textures
176 gl::MakeValidSize(false, gl::IsCompressed(internalformat), &width, &height, &mLodOffset);
177
178 ID3D11Device *device = mRenderer->getDevice();
179
180 D3D11_TEXTURE2D_DESC desc;
181 desc.Width = width; // Compressed texture size constraints?
182 desc.Height = height;
183 desc.MipLevels = levels + mLodOffset;
184 desc.ArraySize = 1;
185 desc.Format = format;
186 desc.SampleDesc.Count = 1;
187 desc.SampleDesc.Quality = 0;
188 desc.Usage = D3D11_USAGE_DEFAULT;
189 desc.BindFlags = getBindFlags();
190 desc.CPUAccessFlags = 0;
191 desc.MiscFlags = 0;
192
193 HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture);
194
195 if (FAILED(result))
196 {
197 ASSERT(result == E_OUTOFMEMORY);
198 ERR("Creating image failed.");
199 error(GL_OUT_OF_MEMORY);
200 }
201 }
202
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000203 initializeSRV(format, levels + mLodOffset);
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000204 initializeRenderTarget(format, width, height);
205}
206
207TextureStorage11_2D::~TextureStorage11_2D()
208{
209 if (mTexture)
210 mTexture->Release();
211}
212
213TextureStorage11_2D *TextureStorage11_2D::makeTextureStorage11_2D(TextureStorage *storage)
214{
215 ASSERT(dynamic_cast<TextureStorage11_2D*>(storage) != NULL);
216 return static_cast<TextureStorage11_2D*>(storage);
217}
218
219RenderTarget *TextureStorage11_2D::getRenderTarget() const
220{
221 return mRenderTarget;
222}
223
224ID3D11Texture2D *TextureStorage11_2D::getBaseTexture() const
225{
226 return mTexture;
227}
228
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000229ID3D11ShaderResourceView *TextureStorage11_2D::getSRV() const
230{
231 return mSRV;
232}
233
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000234void TextureStorage11_2D::generateMipmap(int level)
235{
236 // TODO
237 UNIMPLEMENTED();
238}
239
240void TextureStorage11_2D::initializeRenderTarget(DXGI_FORMAT format, int width, int height)
241{
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000242 ASSERT(mRenderTarget == NULL);
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000243
244 if (mTexture != NULL && isRenderTarget())
245 {
246 if (getBindFlags() & D3D11_BIND_RENDER_TARGET)
247 {
248 // Create render target view -- texture should already be created with
249 // BIND_RENDER_TARGET flag.
250 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
251 rtvDesc.Format = format;
252 rtvDesc.Texture2D.MipSlice = 0;
253
254 ID3D11RenderTargetView *renderTargetView;
255 ID3D11Device *device = mRenderer->getDevice();
256 HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
257
258 if (result == E_OUTOFMEMORY)
259 return;
260
daniel@transgaming.com11861ce2013-01-11 04:07:00 +0000261 ASSERT(SUCCEEDED(result));
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000262
263 mRenderTarget = new RenderTarget11(mRenderer, renderTargetView, width, height);
264 }
265 else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL)
266 {
267 // TODO
268 UNIMPLEMENTED();
269 }
270 else
271 UNREACHABLE();
272 }
273}
274
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000275void TextureStorage11_2D::initializeSRV(DXGI_FORMAT format, int levels)
276{
277 ASSERT(mSRV == NULL);
278
279 if (mTexture)
280 {
281 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
282 srvDesc.Format = format;
283 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
284 srvDesc.Texture2D.MostDetailedMip = 0;
285 srvDesc.Texture2D.MipLevels = levels;
286
287 ID3D11Device *device = mRenderer->getDevice();
288 HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV);
289
290 if (result == E_OUTOFMEMORY)
291 {
292 return error(GL_OUT_OF_MEMORY);
293 }
294
295 ASSERT(SUCCEEDED(result));
296 }
297}
298
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000299TextureStorage11_Cube::TextureStorage11_Cube(Renderer *renderer, int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size)
300 : TextureStorage11(renderer, GetTextureBindFlags(gl_d3d11::ConvertTextureFormat(internalformat), usage, forceRenderable))
301{
302 mTexture = NULL;
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000303 mSRV = NULL;
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000304
305 for (int i = 0; i < 6; ++i)
306 {
307 mRenderTarget[i] = NULL;
308 }
309
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000310 DXGI_FORMAT format = gl_d3d11::ConvertTextureFormat(internalformat);
311 // if the size is not positive this should be treated as an incomplete texture
312 // we handle that here by skipping the d3d texture creation
313 if (size > 0)
314 {
315 // adjust size if needed for compressed textures
316 int height = size;
317 gl::MakeValidSize(false, gl::IsCompressed(internalformat), &size, &height, &mLodOffset);
318
319 ID3D11Device *device = mRenderer->getDevice();
320
321 D3D11_TEXTURE2D_DESC desc;
322 desc.Width = size;
323 desc.Height = size;
324 desc.MipLevels = levels + mLodOffset;
325 desc.ArraySize = 6;
326 desc.Format = format;
327 desc.SampleDesc.Count = 1;
328 desc.SampleDesc.Quality = 0;
329 desc.Usage = D3D11_USAGE_DEFAULT;
330 desc.BindFlags = getBindFlags();
331 desc.CPUAccessFlags = 0;
332 desc.MiscFlags = D3D11_RESOURCE_MISC_TEXTURECUBE;
333
334 HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture);
335
336 if (FAILED(result))
337 {
338 ASSERT(result == E_OUTOFMEMORY);
339 ERR("Creating image failed.");
340 error(GL_OUT_OF_MEMORY);
341 }
342 }
343
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000344 initializeSRV(format, levels + mLodOffset);
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000345 initializeRenderTarget(format, size);
346}
347
348TextureStorage11_Cube::~TextureStorage11_Cube()
349{
350 if (mTexture)
351 mTexture->Release();
352}
353
354TextureStorage11_Cube *TextureStorage11_Cube::makeTextureStorage11_Cube(TextureStorage *storage)
355{
356 ASSERT(dynamic_cast<TextureStorage11_Cube*>(storage) != NULL);
357 return static_cast<TextureStorage11_Cube*>(storage);
358}
359
360RenderTarget *TextureStorage11_Cube::getRenderTarget(GLenum faceTarget) const
361{
362 return mRenderTarget[gl::TextureCubeMap::faceIndex(faceTarget)];
363}
364
365ID3D11Texture2D *TextureStorage11_Cube::getBaseTexture() const
366{
367 return mTexture;
368}
369
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000370ID3D11ShaderResourceView *TextureStorage11_Cube::getSRV() const
371{
372 return mSRV;
373}
374
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000375void TextureStorage11_Cube::generateMipmap(int face, int level)
376{
377 // TODO
378 UNIMPLEMENTED();
379}
380
381void TextureStorage11_Cube::initializeRenderTarget(DXGI_FORMAT format, int size)
382{
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000383 if (mTexture != NULL && isRenderTarget())
384 {
385 if (getBindFlags() & D3D11_BIND_RENDER_TARGET)
386 {
387 // Create render target view -- texture should already be created with
388 // BIND_RENDER_TARGET flag.
389
390 for (int i = 0; i < 6; ++i)
391 {
daniel@transgaming.com5cdd0582013-01-11 04:07:35 +0000392 ASSERT(mRenderTarget[i] == NULL);
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000393 D3D11_RENDER_TARGET_VIEW_DESC rtvDesc;
394 rtvDesc.Format = format;
395 rtvDesc.Texture2DArray.MipSlice = 0;
396 rtvDesc.Texture2DArray.FirstArraySlice = i;
397 rtvDesc.Texture2DArray.ArraySize = 1;
398
399 ID3D11RenderTargetView *renderTargetView;
400 ID3D11Device *device = mRenderer->getDevice();
401 HRESULT result = device->CreateRenderTargetView(mTexture, &rtvDesc, &renderTargetView);
402
403 if (result == E_OUTOFMEMORY)
404 return;
405
daniel@transgaming.com11861ce2013-01-11 04:07:00 +0000406 ASSERT(SUCCEEDED(result));
daniel@transgaming.comb2151e52013-01-11 04:06:24 +0000407
408 mRenderTarget[i] = new RenderTarget11(mRenderer, renderTargetView, size, size);
409 }
410 }
411 else if (getBindFlags() & D3D11_BIND_DEPTH_STENCIL)
412 {
413 // TODO
414 UNIMPLEMENTED();
415 }
416 else
417 UNREACHABLE();
418 }
419}
420
daniel@transgaming.comb50d5302013-01-11 04:07:29 +0000421void TextureStorage11_Cube::initializeSRV(DXGI_FORMAT format, int levels)
422{
423 ASSERT(mSRV == NULL);
424
425 if (mTexture)
426 {
427 D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
428 srvDesc.Format = format;
429 srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE;
430 srvDesc.TextureCube.MipLevels = levels;
431 srvDesc.TextureCube.MostDetailedMip = 0;
432
433 ID3D11Device *device = mRenderer->getDevice();
434 HRESULT result = device->CreateShaderResourceView(mTexture, &srvDesc, &mSRV);
435
436 if (result == E_OUTOFMEMORY)
437 {
438 return error(GL_OUT_OF_MEMORY);
439 }
440
441 ASSERT(SUCCEEDED(result));
442 }
443}
444
daniel@transgaming.com4834ee22013-01-11 04:06:16 +0000445}