blob: 9a4171393017ce2ab900a263062af38c187941cb [file] [log] [blame]
Geoff Langb86b9792013-06-04 16:32:05 -04001#include "precompiled.h"
2//
3// Copyright (c) 2013 The ANGLE Project Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style license that can be
5// found in the LICENSE file.
6//
7
8// Blit11.cpp: Texture copy utility class.
9
10#include "libGLESv2/main.h"
11#include "libGLESv2/formatutils.h"
Geoff Langd47e0fc2013-08-29 11:40:43 -040012#include "libGLESv2/renderer/d3d11/Blit11.h"
13#include "libGLESv2/renderer/d3d11/Renderer11.h"
14#include "libGLESv2/renderer/d3d11/renderer11_utils.h"
15#include "libGLESv2/renderer/d3d11/formatutils11.h"
Geoff Langb86b9792013-06-04 16:32:05 -040016
Geoff Langd47e0fc2013-08-29 11:40:43 -040017#include "libGLESv2/renderer/d3d11/shaders/compiled/passthrough2d11vs.h"
18#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughdepth2d11ps.h"
19#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgba2d11ps.h"
20#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgba2dui11ps.h"
21#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgba2di11ps.h"
22#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgb2d11ps.h"
23#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgb2dui11ps.h"
24#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgb2di11ps.h"
25#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrg2d11ps.h"
26#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrg2dui11ps.h"
27#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrg2di11ps.h"
28#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughr2d11ps.h"
29#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughr2dui11ps.h"
30#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughr2di11ps.h"
31#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughlum2d11ps.h"
32#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughlumalpha2d11ps.h"
Geoff Langb86b9792013-06-04 16:32:05 -040033
Geoff Langd47e0fc2013-08-29 11:40:43 -040034#include "libGLESv2/renderer/d3d11/shaders/compiled/passthrough3d11vs.h"
35#include "libGLESv2/renderer/d3d11/shaders/compiled/passthrough3d11gs.h"
36#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgba3d11ps.h"
37#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgba3dui11ps.h"
38#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgba3di11ps.h"
39#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgb3d11ps.h"
40#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgb3dui11ps.h"
41#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrgb3di11ps.h"
42#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrg3d11ps.h"
43#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrg3dui11ps.h"
44#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughrg3di11ps.h"
45#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughr3d11ps.h"
46#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughr3dui11ps.h"
47#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughr3di11ps.h"
48#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughlum3d11ps.h"
49#include "libGLESv2/renderer/d3d11/shaders/compiled/passthroughlumalpha3d11ps.h"
Geoff Langb86b9792013-06-04 16:32:05 -040050
Geoff Langed883f52013-09-10 17:20:54 -040051#include "libGLESv2/renderer/d3d11/shaders/compiled/swizzlef2dps.h"
52#include "libGLESv2/renderer/d3d11/shaders/compiled/swizzlei2dps.h"
53#include "libGLESv2/renderer/d3d11/shaders/compiled/swizzleui2dps.h"
54#include "libGLESv2/renderer/d3d11/shaders/compiled/swizzlef3dps.h"
55#include "libGLESv2/renderer/d3d11/shaders/compiled/swizzlei3dps.h"
56#include "libGLESv2/renderer/d3d11/shaders/compiled/swizzleui3dps.h"
57#include "libGLESv2/renderer/d3d11/shaders/compiled/swizzlef2darrayps.h"
58#include "libGLESv2/renderer/d3d11/shaders/compiled/swizzlei2darrayps.h"
59#include "libGLESv2/renderer/d3d11/shaders/compiled/swizzleui2darrayps.h"
60
Geoff Langb86b9792013-06-04 16:32:05 -040061namespace rx
62{
63
Jamie Madillbdfa2282013-09-18 14:36:21 -040064static DXGI_FORMAT GetTextureFormat(ID3D11Resource *resource)
65{
66 ID3D11Texture2D *texture = d3d11::DynamicCastComObject<ID3D11Texture2D>(resource);
67 if (!texture)
68 {
69 return DXGI_FORMAT_UNKNOWN;
70 }
71
72 D3D11_TEXTURE2D_DESC desc;
73 texture->GetDesc(&desc);
74
75 SafeRelease(texture);
76
77 return desc.Format;
78}
79
80static ID3D11Resource *CreateStagingTexture(ID3D11Device *device, ID3D11DeviceContext *context,
81 ID3D11Resource *source, unsigned int subresource,
82 const gl::Extents &size, unsigned int cpuAccessFlags)
83{
84 D3D11_TEXTURE2D_DESC stagingDesc;
85 stagingDesc.Width = size.width;
86 stagingDesc.Height = size.height;
87 stagingDesc.MipLevels = 1;
88 stagingDesc.ArraySize = 1;
89 stagingDesc.Format = GetTextureFormat(source);
90 stagingDesc.SampleDesc.Count = 1;
91 stagingDesc.SampleDesc.Quality = 0;
92 stagingDesc.Usage = D3D11_USAGE_STAGING;
93 stagingDesc.CPUAccessFlags = cpuAccessFlags;
94 stagingDesc.MiscFlags = 0;
95 stagingDesc.BindFlags = 0;
96
97 ID3D11Texture2D *stagingTexture = NULL;
98 HRESULT result = device->CreateTexture2D(&stagingDesc, NULL, &stagingTexture);
99 if (FAILED(result))
100 {
101 ERR("Failed to create staging texture for depth stencil blit. HRESULT: 0x%X.", result);
102 return NULL;
103 }
104
105 context->CopySubresourceRegion(stagingTexture, 0, 0, 0, 0, source, subresource, NULL);
106
107 return stagingTexture;
108}
109
110inline static void GenerateVertexCoords(const gl::Box &sourceArea, const gl::Extents &sourceSize,
111 const gl::Box &destArea, const gl::Extents &destSize,
112 float *x1, float *y1, float *x2, float *y2,
113 float *u1, float *v1, float *u2, float *v2)
114{
115 *x1 = (destArea.x / float(destSize.width)) * 2.0f - 1.0f;
116 *y1 = ((destSize.height - destArea.y - destArea.height) / float(destSize.height)) * 2.0f - 1.0f;
117 *x2 = ((destArea.x + destArea.width) / float(destSize.width)) * 2.0f - 1.0f;
118 *y2 = ((destSize.height - destArea.y) / float(destSize.height)) * 2.0f - 1.0f;
119
120 *u1 = sourceArea.x / float(sourceSize.width);
121 *v1 = sourceArea.y / float(sourceSize.height);
122 *u2 = (sourceArea.x + sourceArea.width) / float(sourceSize.width);
123 *v2 = (sourceArea.y + sourceArea.height) / float(sourceSize.height);
124}
125
126static void Write2DVertices(const gl::Box &sourceArea, const gl::Extents &sourceSize,
127 const gl::Box &destArea, const gl::Extents &destSize,
128 void *outVertices, unsigned int *outStride, unsigned int *outVertexCount,
129 D3D11_PRIMITIVE_TOPOLOGY *outTopology)
130{
131 float x1, y1, x2, y2, u1, v1, u2, v2;
132 GenerateVertexCoords(sourceArea, sourceSize, destArea, destSize, &x1, &y1, &x2, &y2, &u1, &v1, &u2, &v2);
133
134 d3d11::PositionTexCoordVertex *vertices = static_cast<d3d11::PositionTexCoordVertex*>(outVertices);
135
136 d3d11::SetPositionTexCoordVertex(&vertices[0], x1, y1, u1, v2);
137 d3d11::SetPositionTexCoordVertex(&vertices[1], x1, y2, u1, v1);
138 d3d11::SetPositionTexCoordVertex(&vertices[2], x2, y1, u2, v2);
139 d3d11::SetPositionTexCoordVertex(&vertices[3], x2, y2, u2, v1);
140
141 *outStride = sizeof(d3d11::PositionTexCoordVertex);
142 *outVertexCount = 4;
143 *outTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;
144}
145
146static void Write3DVertices(const gl::Box &sourceArea, const gl::Extents &sourceSize,
147 const gl::Box &destArea, const gl::Extents &destSize,
148 void *outVertices, unsigned int *outStride, unsigned int *outVertexCount,
149 D3D11_PRIMITIVE_TOPOLOGY *outTopology)
150{
Geoff Langed883f52013-09-10 17:20:54 -0400151 ASSERT(sourceSize.depth > 0 && destSize.depth > 0);
152
Jamie Madillbdfa2282013-09-18 14:36:21 -0400153 float x1, y1, x2, y2, u1, v1, u2, v2;
154 GenerateVertexCoords(sourceArea, sourceSize, destArea, destSize, &x1, &y1, &x2, &y2, &u1, &v1, &u2, &v2);
155
156 d3d11::PositionLayerTexCoord3DVertex *vertices = static_cast<d3d11::PositionLayerTexCoord3DVertex*>(outVertices);
157
158 for (int i = 0; i < destSize.depth; i++)
159 {
Geoff Langed883f52013-09-10 17:20:54 -0400160 float readDepth = (float)i / std::max(destSize.depth - 1, 1);
Jamie Madillbdfa2282013-09-18 14:36:21 -0400161
162 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 0], x1, y1, i, u1, v2, readDepth);
163 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 1], x1, y2, i, u1, v1, readDepth);
164 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 2], x2, y1, i, u2, v2, readDepth);
165
166 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 3], x1, y2, i, u1, v1, readDepth);
167 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 4], x2, y2, i, u2, v1, readDepth);
168 d3d11::SetPositionLayerTexCoord3DVertex(&vertices[i * 6 + 5], x2, y1, i, u2, v2, readDepth);
169 }
170
171 *outStride = sizeof(d3d11::PositionLayerTexCoord3DVertex);
172 *outVertexCount = destSize.depth * 6;
173 *outTopology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
174}
175
Geoff Langb86b9792013-06-04 16:32:05 -0400176Blit11::Blit11(rx::Renderer11 *renderer)
Geoff Langed883f52013-09-10 17:20:54 -0400177 : mRenderer(renderer), mBlitShaderMap(compareBlitParameters), mSwizzleShaderMap(compareSwizzleParameters),
178 mVertexBuffer(NULL), mPointSampler(NULL), mLinearSampler(NULL), mScissorEnabledRasterizerState(NULL),
Geoff Lang125deab2013-08-09 13:34:16 -0400179 mScissorDisabledRasterizerState(NULL), mDepthStencilState(NULL),
Geoff Lang975af372013-06-12 11:19:22 -0400180 mQuad2DIL(NULL), mQuad2DVS(NULL), mDepthPS(NULL),
Geoff Langed883f52013-09-10 17:20:54 -0400181 mQuad3DIL(NULL), mQuad3DVS(NULL), mQuad3DGS(NULL),
182 mSwizzleCB(NULL)
Geoff Langb86b9792013-06-04 16:32:05 -0400183{
184 HRESULT result;
185 ID3D11Device *device = mRenderer->getDevice();
186
187 D3D11_BUFFER_DESC vbDesc;
Geoff Langed883f52013-09-10 17:20:54 -0400188 vbDesc.ByteWidth = std::max(sizeof(d3d11::PositionLayerTexCoord3DVertex), sizeof(d3d11::PositionTexCoordVertex)) *
189 6 * renderer->getMaxTextureDepth();
Geoff Langb86b9792013-06-04 16:32:05 -0400190 vbDesc.Usage = D3D11_USAGE_DYNAMIC;
191 vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
192 vbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
193 vbDesc.MiscFlags = 0;
194 vbDesc.StructureByteStride = 0;
195
196 result = device->CreateBuffer(&vbDesc, NULL, &mVertexBuffer);
197 ASSERT(SUCCEEDED(result));
198 d3d11::SetDebugName(mVertexBuffer, "Blit11 vertex buffer");
199
200 D3D11_SAMPLER_DESC pointSamplerDesc;
201 pointSamplerDesc.Filter = D3D11_FILTER_MIN_MAG_POINT_MIP_LINEAR;
202 pointSamplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
203 pointSamplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
204 pointSamplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
205 pointSamplerDesc.MipLODBias = 0.0f;
206 pointSamplerDesc.MaxAnisotropy = 0;
207 pointSamplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
208 pointSamplerDesc.BorderColor[0] = 0.0f;
209 pointSamplerDesc.BorderColor[1] = 0.0f;
210 pointSamplerDesc.BorderColor[2] = 0.0f;
211 pointSamplerDesc.BorderColor[3] = 0.0f;
212 pointSamplerDesc.MinLOD = 0.0f;
213 pointSamplerDesc.MaxLOD = 0.0f;
214
215 result = device->CreateSamplerState(&pointSamplerDesc, &mPointSampler);
216 ASSERT(SUCCEEDED(result));
217 d3d11::SetDebugName(mPointSampler, "Blit11 point sampler");
218
219 D3D11_SAMPLER_DESC linearSamplerDesc;
220 linearSamplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
221 linearSamplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
222 linearSamplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
223 linearSamplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
224 linearSamplerDesc.MipLODBias = 0.0f;
225 linearSamplerDesc.MaxAnisotropy = 0;
226 linearSamplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
227 linearSamplerDesc.BorderColor[0] = 0.0f;
228 linearSamplerDesc.BorderColor[1] = 0.0f;
229 linearSamplerDesc.BorderColor[2] = 0.0f;
230 linearSamplerDesc.BorderColor[3] = 0.0f;
231 linearSamplerDesc.MinLOD = 0.0f;
232 linearSamplerDesc.MaxLOD = 0.0f;
233
234 result = device->CreateSamplerState(&linearSamplerDesc, &mLinearSampler);
235 ASSERT(SUCCEEDED(result));
236 d3d11::SetDebugName(mLinearSampler, "Blit11 linear sampler");
237
Geoff Lang77e39002013-06-11 11:28:19 -0400238 // Use a rasterizer state that will not cull so that inverted quads will not be culled
239 D3D11_RASTERIZER_DESC rasterDesc;
240 rasterDesc.FillMode = D3D11_FILL_SOLID;
241 rasterDesc.CullMode = D3D11_CULL_NONE;
242 rasterDesc.FrontCounterClockwise = FALSE;
243 rasterDesc.DepthBias = 0;
244 rasterDesc.SlopeScaledDepthBias = 0.0f;
245 rasterDesc.DepthBiasClamp = 0.0f;
246 rasterDesc.DepthClipEnable = TRUE;
Geoff Lang77e39002013-06-11 11:28:19 -0400247 rasterDesc.MultisampleEnable = FALSE;
248 rasterDesc.AntialiasedLineEnable = FALSE;
249
Geoff Lang125deab2013-08-09 13:34:16 -0400250 rasterDesc.ScissorEnable = TRUE;
251 result = device->CreateRasterizerState(&rasterDesc, &mScissorEnabledRasterizerState);
Geoff Lang77e39002013-06-11 11:28:19 -0400252 ASSERT(SUCCEEDED(result));
Geoff Lang125deab2013-08-09 13:34:16 -0400253 d3d11::SetDebugName(mScissorEnabledRasterizerState, "Blit11 scissoring rasterizer state");
254
255 rasterDesc.ScissorEnable = FALSE;
256 result = device->CreateRasterizerState(&rasterDesc, &mScissorDisabledRasterizerState);
257 ASSERT(SUCCEEDED(result));
258 d3d11::SetDebugName(mScissorDisabledRasterizerState, "Blit11 no scissoring rasterizer state");
Geoff Lang77e39002013-06-11 11:28:19 -0400259
Geoff Lang975af372013-06-12 11:19:22 -0400260 D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
261 depthStencilDesc.DepthEnable = true;
262 depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
263 depthStencilDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
264 depthStencilDesc.StencilEnable = FALSE;
265 depthStencilDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
266 depthStencilDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
267 depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
268 depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
269 depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
270 depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
271 depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
272 depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
273 depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
274 depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
275
276 result = device->CreateDepthStencilState(&depthStencilDesc, &mDepthStencilState);
277 ASSERT(SUCCEEDED(result));
278 d3d11::SetDebugName(mDepthStencilState, "Blit11 depth stencil state");
279
Geoff Langb86b9792013-06-04 16:32:05 -0400280 D3D11_INPUT_ELEMENT_DESC quad2DLayout[] =
281 {
282 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
283 { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
284 };
285
286 result = device->CreateInputLayout(quad2DLayout, ArraySize(quad2DLayout), g_VS_Passthrough2D, ArraySize(g_VS_Passthrough2D), &mQuad2DIL);
287 ASSERT(SUCCEEDED(result));
288 d3d11::SetDebugName(mQuad2DIL, "Blit11 2D input layout");
289
290 result = device->CreateVertexShader(g_VS_Passthrough2D, ArraySize(g_VS_Passthrough2D), NULL, &mQuad2DVS);
291 ASSERT(SUCCEEDED(result));
292 d3d11::SetDebugName(mQuad2DVS, "Blit11 2D vertex shader");
293
Geoff Lang975af372013-06-12 11:19:22 -0400294 result = device->CreatePixelShader(g_PS_PassthroughDepth2D, ArraySize(g_PS_PassthroughDepth2D), NULL, &mDepthPS);
295 ASSERT(SUCCEEDED(result));
296 d3d11::SetDebugName(mDepthPS, "Blit11 2D depth pixel shader");
297
Geoff Langb86b9792013-06-04 16:32:05 -0400298 D3D11_INPUT_ELEMENT_DESC quad3DLayout[] =
299 {
300 { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
301 { "LAYER", 0, DXGI_FORMAT_R32_UINT, 0, 8, D3D11_INPUT_PER_VERTEX_DATA, 0 },
302 { "TEXCOORD", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
303 };
304
305 result = device->CreateInputLayout(quad3DLayout, ArraySize(quad3DLayout), g_VS_Passthrough3D, ArraySize(g_VS_Passthrough3D), &mQuad3DIL);
306 ASSERT(SUCCEEDED(result));
307 d3d11::SetDebugName(mQuad3DIL, "Blit11 3D input layout");
308
309 result = device->CreateVertexShader(g_VS_Passthrough3D, ArraySize(g_VS_Passthrough3D), NULL, &mQuad3DVS);
310 ASSERT(SUCCEEDED(result));
311 d3d11::SetDebugName(mQuad3DVS, "Blit11 3D vertex shader");
312
313 result = device->CreateGeometryShader(g_GS_Passthrough3D, ArraySize(g_GS_Passthrough3D), NULL, &mQuad3DGS);
314 ASSERT(SUCCEEDED(result));
315 d3d11::SetDebugName(mQuad3DGS, "Renderer11 copy 3D texture geometry shader");
316
317 buildShaderMap();
Geoff Langed883f52013-09-10 17:20:54 -0400318
319 D3D11_BUFFER_DESC swizzleBufferDesc;
320 swizzleBufferDesc.ByteWidth = sizeof(unsigned int) * 4;
321 swizzleBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
322 swizzleBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
323 swizzleBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
324 swizzleBufferDesc.MiscFlags = 0;
325 swizzleBufferDesc.StructureByteStride = 0;
326
327 result = device->CreateBuffer(&swizzleBufferDesc, NULL, &mSwizzleCB);
328 ASSERT(SUCCEEDED(result));
329 d3d11::SetDebugName(mSwizzleCB, "Blit11 swizzle constant buffer");
Geoff Langb86b9792013-06-04 16:32:05 -0400330}
331
332Blit11::~Blit11()
333{
334 SafeRelease(mVertexBuffer);
335 SafeRelease(mPointSampler);
336 SafeRelease(mLinearSampler);
Geoff Lang125deab2013-08-09 13:34:16 -0400337 SafeRelease(mScissorEnabledRasterizerState);
338 SafeRelease(mScissorDisabledRasterizerState);
Geoff Lang975af372013-06-12 11:19:22 -0400339 SafeRelease(mDepthStencilState);
Geoff Langb86b9792013-06-04 16:32:05 -0400340
341 SafeRelease(mQuad2DIL);
342 SafeRelease(mQuad2DVS);
Geoff Lang975af372013-06-12 11:19:22 -0400343 SafeRelease(mDepthPS);
Geoff Langb86b9792013-06-04 16:32:05 -0400344
345 SafeRelease(mQuad3DIL);
346 SafeRelease(mQuad3DVS);
347 SafeRelease(mQuad3DGS);
348
Geoff Langed883f52013-09-10 17:20:54 -0400349 SafeRelease(mSwizzleCB);
350
Geoff Langb86b9792013-06-04 16:32:05 -0400351 clearShaderMap();
352}
353
Geoff Langed883f52013-09-10 17:20:54 -0400354static inline unsigned int GetSwizzleIndex(GLenum swizzle)
355{
356 unsigned int colorIndex = 0;
357
358 switch (swizzle)
359 {
360 case GL_RED: colorIndex = 0; break;
361 case GL_GREEN: colorIndex = 1; break;
362 case GL_BLUE: colorIndex = 2; break;
363 case GL_ALPHA: colorIndex = 3; break;
364 case GL_ZERO: colorIndex = 4; break;
365 case GL_ONE: colorIndex = 5; break;
366 default: UNREACHABLE(); break;
367 }
368
369 return colorIndex;
370}
371
372bool Blit11::swizzleTexture(ID3D11ShaderResourceView *source, ID3D11RenderTargetView *dest, const gl::Extents &size,
373 GLenum swizzleRed, GLenum swizzleGreen, GLenum swizzleBlue, GLenum swizzleAlpha)
374{
375 HRESULT result;
376 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
377
378 D3D11_SHADER_RESOURCE_VIEW_DESC sourceSRVDesc;
379 source->GetDesc(&sourceSRVDesc);
380 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(sourceSRVDesc.Format, mRenderer->getCurrentClientVersion());
381
382 GLenum shaderType = GL_NONE;
383 switch (gl::GetComponentType(sourceInternalFormat, mRenderer->getCurrentClientVersion()))
384 {
385 case GL_UNSIGNED_NORMALIZED:
386 case GL_SIGNED_NORMALIZED:
387 case GL_FLOAT:
388 shaderType = GL_FLOAT;
389 break;
390 case GL_INT:
391 shaderType = GL_INT;
392 break;
393 case GL_UNSIGNED_INT:
394 shaderType = GL_UNSIGNED_INT;
395 break;
396 default:
397 UNREACHABLE();
398 break;
399 }
400
401 SwizzleParameters parameters = { 0 };
402 parameters.mDestinationType = shaderType;
403 parameters.mViewDimension = sourceSRVDesc.ViewDimension;
404
405 SwizzleShaderMap::const_iterator i = mSwizzleShaderMap.find(parameters);
406 if (i == mSwizzleShaderMap.end())
407 {
408 UNREACHABLE();
409 return false;
410 }
411
412 const Shader &shader = i->second;
413
414 // Set vertices
415 D3D11_MAPPED_SUBRESOURCE mappedResource;
416 result = deviceContext->Map(mVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
417 if (FAILED(result))
418 {
419 ERR("Failed to map vertex buffer for texture swizzle, HRESULT: 0x%X.", result);
420 return false;
421 }
422
423 UINT stride = 0;
424 UINT startIdx = 0;
425 UINT drawCount = 0;
426 D3D11_PRIMITIVE_TOPOLOGY topology;
427
428 gl::Box area(0, 0, 0, size.width, size.height, size.depth);
429 shader.mVertexWriteFunction(area, size, area, size, mappedResource.pData, &stride, &drawCount, &topology);
430
431 deviceContext->Unmap(mVertexBuffer, 0);
432
433 // Set constant buffer
434 result = deviceContext->Map(mSwizzleCB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
435 if (FAILED(result))
436 {
437 ERR("Failed to map constant buffer for texture swizzle, HRESULT: 0x%X.", result);
438 return false;
439 }
440
441 unsigned int *swizzleIndices = reinterpret_cast<unsigned int*>(mappedResource.pData);
442 swizzleIndices[0] = GetSwizzleIndex(swizzleRed);
443 swizzleIndices[1] = GetSwizzleIndex(swizzleGreen);
444 swizzleIndices[2] = GetSwizzleIndex(swizzleBlue);
445 swizzleIndices[3] = GetSwizzleIndex(swizzleAlpha);
446
447 deviceContext->Unmap(mSwizzleCB, 0);
448
449 // Apply vertex buffer
450 deviceContext->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &startIdx);
451
452 // Apply constant buffer
453 deviceContext->PSSetConstantBuffers(0, 1, &mSwizzleCB);
454
455 // Apply state
456 deviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF);
457 deviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
458 deviceContext->RSSetState(mScissorDisabledRasterizerState);
459
460 // Apply shaders
461 deviceContext->IASetInputLayout(shader.mInputLayout);
462 deviceContext->IASetPrimitiveTopology(topology);
463 deviceContext->VSSetShader(shader.mVertexShader, NULL, 0);
464
465 deviceContext->PSSetShader(shader.mPixelShader, NULL, 0);
466 deviceContext->GSSetShader(shader.mGeometryShader, NULL, 0);
467
468 // Unset the currently bound shader resource to avoid conflicts
469 ID3D11ShaderResourceView *const nullSRV = NULL;
470 deviceContext->PSSetShaderResources(0, 1, &nullSRV);
471
472 // Apply render target
473 mRenderer->setOneTimeRenderTarget(dest);
474
475 // Set the viewport
476 D3D11_VIEWPORT viewport;
477 viewport.TopLeftX = 0;
478 viewport.TopLeftY = 0;
479 viewport.Width = size.width;
480 viewport.Height = size.height;
481 viewport.MinDepth = 0.0f;
482 viewport.MaxDepth = 1.0f;
483 deviceContext->RSSetViewports(1, &viewport);
484
485 // Apply textures
486 deviceContext->PSSetShaderResources(0, 1, &source);
487
488 // Apply samplers
489 deviceContext->PSSetSamplers(0, 1, &mPointSampler);
490
491 // Draw the quad
492 deviceContext->Draw(drawCount, 0);
493
494 // Unbind textures and render targets and vertex buffer
495 deviceContext->PSSetShaderResources(0, 1, &nullSRV);
496
497 mRenderer->unapplyRenderTargets();
498
499 UINT zero = 0;
500 ID3D11Buffer *const nullBuffer = NULL;
501 deviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
502
503 mRenderer->markAllStateDirty();
504
505 return true;
506}
507
Geoff Langb86b9792013-06-04 16:32:05 -0400508bool Blit11::copyTexture(ID3D11ShaderResourceView *source, const gl::Box &sourceArea, const gl::Extents &sourceSize,
509 ID3D11RenderTargetView *dest, const gl::Box &destArea, const gl::Extents &destSize,
Geoff Lang125deab2013-08-09 13:34:16 -0400510 const gl::Rectangle *scissor, GLenum destFormat, GLenum filter)
Geoff Langb86b9792013-06-04 16:32:05 -0400511{
Geoff Langb86b9792013-06-04 16:32:05 -0400512 HRESULT result;
513 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
514
Geoff Langb0c75cc2013-06-04 17:13:27 -0400515 // Determine if the source format is a signed integer format, the destFormat will already
516 // be GL_XXXX_INTEGER but it does not tell us if it is signed or unsigned.
517 D3D11_SHADER_RESOURCE_VIEW_DESC sourceSRVDesc;
518 source->GetDesc(&sourceSRVDesc);
Geoff Lang005df412013-10-16 14:12:50 -0400519 GLenum sourceInternalFormat = d3d11_gl::GetInternalFormat(sourceSRVDesc.Format, mRenderer->getCurrentClientVersion());
Geoff Langb0c75cc2013-06-04 17:13:27 -0400520
Geoff Langb86b9792013-06-04 16:32:05 -0400521 BlitParameters parameters = { 0 };
522 parameters.mDestinationFormat = destFormat;
Geoff Langb2f3d052013-08-13 12:49:27 -0400523 parameters.mSignedInteger = gl::GetComponentType(sourceInternalFormat, mRenderer->getCurrentClientVersion()) == GL_INT;
Geoff Langb86b9792013-06-04 16:32:05 -0400524 parameters.m3DBlit = sourceArea.depth > 1;
525
Geoff Lang26533602013-09-09 16:41:42 -0400526 BlitShaderMap::const_iterator i = mBlitShaderMap.find(parameters);
527 if (i == mBlitShaderMap.end())
Geoff Langb86b9792013-06-04 16:32:05 -0400528 {
529 UNREACHABLE();
530 return false;
531 }
532
Geoff Lang26533602013-09-09 16:41:42 -0400533 const Shader& shader = i->second;
Geoff Langb86b9792013-06-04 16:32:05 -0400534
535 // Set vertices
536 D3D11_MAPPED_SUBRESOURCE mappedResource;
537 result = deviceContext->Map(mVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
538 if (FAILED(result))
539 {
540 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
541 return false;
542 }
543
544 UINT stride = 0;
545 UINT startIdx = 0;
546 UINT drawCount = 0;
547 D3D11_PRIMITIVE_TOPOLOGY topology;
548
549 shader.mVertexWriteFunction(sourceArea, sourceSize, destArea, destSize, mappedResource.pData,
550 &stride, &drawCount, &topology);
551
552 deviceContext->Unmap(mVertexBuffer, 0);
553
554 // Apply vertex buffer
555 deviceContext->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &startIdx);
556
557 // Apply state
558 deviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF);
559 deviceContext->OMSetDepthStencilState(NULL, 0xFFFFFFFF);
Geoff Lang125deab2013-08-09 13:34:16 -0400560
561 if (scissor)
562 {
563 D3D11_RECT scissorRect;
564 scissorRect.left = scissor->x;
565 scissorRect.right = scissor->x + scissor->width;
566 scissorRect.top = scissor->y;
567 scissorRect.bottom = scissor->y + scissor->height;
568
569 deviceContext->RSSetScissorRects(1, &scissorRect);
570 deviceContext->RSSetState(mScissorEnabledRasterizerState);
571 }
572 else
573 {
574 deviceContext->RSSetState(mScissorDisabledRasterizerState);
575 }
Geoff Langb86b9792013-06-04 16:32:05 -0400576
577 // Apply shaders
578 deviceContext->IASetInputLayout(shader.mInputLayout);
579 deviceContext->IASetPrimitiveTopology(topology);
580 deviceContext->VSSetShader(shader.mVertexShader, NULL, 0);
581
582 deviceContext->PSSetShader(shader.mPixelShader, NULL, 0);
583 deviceContext->GSSetShader(shader.mGeometryShader, NULL, 0);
584
585 // Unset the currently bound shader resource to avoid conflicts
586 ID3D11ShaderResourceView *const nullSRV = NULL;
587 deviceContext->PSSetShaderResources(0, 1, &nullSRV);
588
589 // Apply render target
590 mRenderer->setOneTimeRenderTarget(dest);
591
592 // Set the viewport
593 D3D11_VIEWPORT viewport;
594 viewport.TopLeftX = 0;
595 viewport.TopLeftY = 0;
596 viewport.Width = destSize.width;
597 viewport.Height = destSize.height;
598 viewport.MinDepth = 0.0f;
599 viewport.MaxDepth = 1.0f;
600 deviceContext->RSSetViewports(1, &viewport);
601
602 // Apply textures
603 deviceContext->PSSetShaderResources(0, 1, &source);
604
605 // Apply samplers
606 ID3D11SamplerState *sampler = NULL;
607 switch (filter)
608 {
609 case GL_NEAREST: sampler = mPointSampler; break;
610 case GL_LINEAR: sampler = mLinearSampler; break;
611 default: UNREACHABLE(); return false;
612 }
613 deviceContext->PSSetSamplers(0, 1, &sampler);
614
615 // Draw the quad
616 deviceContext->Draw(drawCount, 0);
617
618 // Unbind textures and render targets and vertex buffer
619 deviceContext->PSSetShaderResources(0, 1, &nullSRV);
620
621 mRenderer->unapplyRenderTargets();
622
623 UINT zero = 0;
624 ID3D11Buffer *const nullBuffer = NULL;
625 deviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
626
627 mRenderer->markAllStateDirty();
628
629 return true;
630}
631
Geoff Lang975af372013-06-12 11:19:22 -0400632bool Blit11::copyStencil(ID3D11Resource *source, unsigned int sourceSubresource, const gl::Box &sourceArea, const gl::Extents &sourceSize,
Geoff Lang125deab2013-08-09 13:34:16 -0400633 ID3D11Resource *dest, unsigned int destSubresource, const gl::Box &destArea, const gl::Extents &destSize,
634 const gl::Rectangle *scissor)
Geoff Lang975af372013-06-12 11:19:22 -0400635{
636 return copyDepthStencil(source, sourceSubresource, sourceArea, sourceSize,
637 dest, destSubresource, destArea, destSize,
Geoff Lang125deab2013-08-09 13:34:16 -0400638 scissor, true);
Geoff Lang975af372013-06-12 11:19:22 -0400639}
640
641bool Blit11::copyDepth(ID3D11ShaderResourceView *source, const gl::Box &sourceArea, const gl::Extents &sourceSize,
Geoff Lang125deab2013-08-09 13:34:16 -0400642 ID3D11DepthStencilView *dest, const gl::Box &destArea, const gl::Extents &destSize,
643 const gl::Rectangle *scissor)
Geoff Lang975af372013-06-12 11:19:22 -0400644{
645 HRESULT result;
646 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
647
648 // Set vertices
649 D3D11_MAPPED_SUBRESOURCE mappedResource;
650 result = deviceContext->Map(mVertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
651 if (FAILED(result))
652 {
653 ERR("Failed to map vertex buffer for texture copy, HRESULT: 0x%X.", result);
654 return false;
655 }
656
657 UINT stride = 0;
658 UINT startIdx = 0;
659 UINT drawCount = 0;
660 D3D11_PRIMITIVE_TOPOLOGY topology;
661
Jamie Madillbdfa2282013-09-18 14:36:21 -0400662 Write2DVertices(sourceArea, sourceSize, destArea, destSize, mappedResource.pData,
Geoff Lang975af372013-06-12 11:19:22 -0400663 &stride, &drawCount, &topology);
664
665 deviceContext->Unmap(mVertexBuffer, 0);
666
667 // Apply vertex buffer
668 deviceContext->IASetVertexBuffers(0, 1, &mVertexBuffer, &stride, &startIdx);
669
670 // Apply state
671 deviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF);
672 deviceContext->OMSetDepthStencilState(mDepthStencilState, 0xFFFFFFFF);
Geoff Lang125deab2013-08-09 13:34:16 -0400673
674 if (scissor)
675 {
676 D3D11_RECT scissorRect;
677 scissorRect.left = scissor->x;
678 scissorRect.right = scissor->x + scissor->width;
679 scissorRect.top = scissor->y;
680 scissorRect.bottom = scissor->y + scissor->height;
681
682 deviceContext->RSSetScissorRects(1, &scissorRect);
683 deviceContext->RSSetState(mScissorEnabledRasterizerState);
684 }
685 else
686 {
687 deviceContext->RSSetState(mScissorDisabledRasterizerState);
688 }
Geoff Lang975af372013-06-12 11:19:22 -0400689
690 // Apply shaders
691 deviceContext->IASetInputLayout(mQuad2DIL);
692 deviceContext->IASetPrimitiveTopology(topology);
693 deviceContext->VSSetShader(mQuad2DVS, NULL, 0);
694
695 deviceContext->PSSetShader(mDepthPS, NULL, 0);
696 deviceContext->GSSetShader(NULL, NULL, 0);
697
698 // Unset the currently bound shader resource to avoid conflicts
699 ID3D11ShaderResourceView *const nullSRV = NULL;
700 deviceContext->PSSetShaderResources(0, 1, &nullSRV);
701
702 // Apply render target
703 deviceContext->OMSetRenderTargets(0, NULL, dest);
704
705 // Set the viewport
706 D3D11_VIEWPORT viewport;
707 viewport.TopLeftX = 0;
708 viewport.TopLeftY = 0;
709 viewport.Width = destSize.width;
710 viewport.Height = destSize.height;
711 viewport.MinDepth = 0.0f;
712 viewport.MaxDepth = 1.0f;
713 deviceContext->RSSetViewports(1, &viewport);
714
715 // Apply textures
716 deviceContext->PSSetShaderResources(0, 1, &source);
717
718 // Apply samplers
719 deviceContext->PSSetSamplers(0, 1, &mPointSampler);
720
721 // Draw the quad
722 deviceContext->Draw(drawCount, 0);
723
724 // Unbind textures and render targets and vertex buffer
725 deviceContext->PSSetShaderResources(0, 1, &nullSRV);
726
727 mRenderer->unapplyRenderTargets();
728
729 UINT zero = 0;
730 ID3D11Buffer *const nullBuffer = NULL;
731 deviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
732
733 mRenderer->markAllStateDirty();
734
735 return true;
736}
737
738bool Blit11::copyDepthStencil(ID3D11Resource *source, unsigned int sourceSubresource, const gl::Box &sourceArea, const gl::Extents &sourceSize,
Geoff Lang125deab2013-08-09 13:34:16 -0400739 ID3D11Resource *dest, unsigned int destSubresource, const gl::Box &destArea, const gl::Extents &destSize,
740 const gl::Rectangle *scissor)
Geoff Lang975af372013-06-12 11:19:22 -0400741{
742 return copyDepthStencil(source, sourceSubresource, sourceArea, sourceSize,
743 dest, destSubresource, destArea, destSize,
Geoff Lang125deab2013-08-09 13:34:16 -0400744 scissor, false);
Geoff Lang975af372013-06-12 11:19:22 -0400745}
746
747bool Blit11::copyDepthStencil(ID3D11Resource *source, unsigned int sourceSubresource, const gl::Box &sourceArea, const gl::Extents &sourceSize,
748 ID3D11Resource *dest, unsigned int destSubresource, const gl::Box &destArea, const gl::Extents &destSize,
Geoff Lang125deab2013-08-09 13:34:16 -0400749 const gl::Rectangle *scissor, bool stencilOnly)
Geoff Lang975af372013-06-12 11:19:22 -0400750{
751 ID3D11Device *device = mRenderer->getDevice();
752 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
753
Jamie Madillbdfa2282013-09-18 14:36:21 -0400754 ID3D11Resource *sourceStaging = CreateStagingTexture(device, deviceContext, source, sourceSubresource, sourceSize, D3D11_CPU_ACCESS_READ);
Geoff Lang921968c2013-09-04 16:59:22 -0400755 // HACK: Create the destination staging buffer as a read/write texture so ID3D11DevicContext::UpdateSubresource can be called
756 // using it's mapped data as a source
Jamie Madillbdfa2282013-09-18 14:36:21 -0400757 ID3D11Resource *destStaging = CreateStagingTexture(device, deviceContext, dest, destSubresource, destSize, D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE);
Geoff Lang975af372013-06-12 11:19:22 -0400758
Geoff Lang125deab2013-08-09 13:34:16 -0400759 if (!sourceStaging || !destStaging)
760 {
761 SafeRelease(sourceStaging);
762 SafeRelease(destStaging);
763 return false;
764 }
765
Jamie Madillbdfa2282013-09-18 14:36:21 -0400766 DXGI_FORMAT format = GetTextureFormat(source);
767 ASSERT(format == GetTextureFormat(dest));
Geoff Lang975af372013-06-12 11:19:22 -0400768
Jamie Madill9eeecfc2014-01-29 09:26:48 -0500769 unsigned int pixelSize = d3d11::GetFormatPixelBytes(format);
Geoff Lang975af372013-06-12 11:19:22 -0400770 unsigned int copyOffset = 0;
771 unsigned int copySize = pixelSize;
772 if (stencilOnly)
773 {
774 copyOffset = d3d11::GetStencilOffset(format) / 8;
775 copySize = d3d11::GetStencilBits(format) / 8;
776
777 // It would be expensive to have non-byte sized stencil sizes since it would
Geoff Lang125deab2013-08-09 13:34:16 -0400778 // require reading from the destination, currently there aren't any though.
Geoff Lang975af372013-06-12 11:19:22 -0400779 ASSERT(d3d11::GetStencilBits(format) % 8 == 0 &&
780 d3d11::GetStencilOffset(format) % 8 == 0);
781 }
782
783 D3D11_MAPPED_SUBRESOURCE sourceMapping, destMapping;
784 deviceContext->Map(sourceStaging, 0, D3D11_MAP_READ, 0, &sourceMapping);
785 deviceContext->Map(destStaging, 0, D3D11_MAP_WRITE, 0, &destMapping);
786
Geoff Lang125deab2013-08-09 13:34:16 -0400787 if (!sourceMapping.pData || !destMapping.pData)
Geoff Lang975af372013-06-12 11:19:22 -0400788 {
Geoff Lang125deab2013-08-09 13:34:16 -0400789 if (!sourceMapping.pData)
790 {
791 deviceContext->Unmap(sourceStaging, 0);
792 }
793 if (!destMapping.pData)
794 {
795 deviceContext->Unmap(destStaging, 0);
796 }
797 SafeRelease(sourceStaging);
798 SafeRelease(destStaging);
799 return false;
800 }
801
802 gl::Rectangle clippedDestArea(destArea.x, destArea.y, destArea.width, destArea.height);
803
804 // Clip dest area to the destination size
805 gl::ClipRectangle(clippedDestArea, gl::Rectangle(0, 0, destSize.width, destSize.height), &clippedDestArea);
806
807 // Clip dest area to the scissor
808 if (scissor)
809 {
810 gl::ClipRectangle(clippedDestArea, *scissor, &clippedDestArea);
811 }
812
813 // Determine if entire rows can be copied at once instead of each individual pixel, requires that there is
814 // no out of bounds lookups required, the entire pixel is copied and no stretching
815 bool wholeRowCopy = sourceArea.width == clippedDestArea.width &&
816 sourceArea.x >= 0 && sourceArea.x + sourceArea.width <= sourceSize.width &&
817 copySize == pixelSize;
818
819 for (int y = clippedDestArea.y; y < clippedDestArea.y + clippedDestArea.height; y++)
820 {
821 float yPerc = static_cast<float>(y - destArea.y) / (destArea.height - 1);
822
823 // Interpolate using the original source rectangle to determine which row to sample from while clamping to the edges
824 unsigned int readRow = gl::clamp(sourceArea.y + floor(yPerc * (sourceArea.height - 1) + 0.5f), 0, sourceSize.height - 1);
Geoff Lang975af372013-06-12 11:19:22 -0400825 unsigned int writeRow = y;
826
Geoff Lang125deab2013-08-09 13:34:16 -0400827 if (wholeRowCopy)
Geoff Lang975af372013-06-12 11:19:22 -0400828 {
829 void *sourceRow = reinterpret_cast<char*>(sourceMapping.pData) +
Geoff Lang125deab2013-08-09 13:34:16 -0400830 readRow * sourceMapping.RowPitch +
831 sourceArea.x * pixelSize;
Geoff Lang975af372013-06-12 11:19:22 -0400832
833 void *destRow = reinterpret_cast<char*>(destMapping.pData) +
Geoff Lang125deab2013-08-09 13:34:16 -0400834 writeRow * destMapping.RowPitch +
835 destArea.x * pixelSize;
Geoff Lang975af372013-06-12 11:19:22 -0400836
837 memcpy(destRow, sourceRow, pixelSize * destArea.width);
838 }
839 else
840 {
Geoff Lang125deab2013-08-09 13:34:16 -0400841 for (int x = clippedDestArea.x; x < clippedDestArea.x + clippedDestArea.width; x++)
Geoff Lang975af372013-06-12 11:19:22 -0400842 {
Geoff Lang125deab2013-08-09 13:34:16 -0400843 float xPerc = static_cast<float>(x - destArea.x) / (destArea.width - 1);
844
845 // Interpolate the original source rectangle to determine which column to sample from while clamping to the edges
846 unsigned int readColumn = gl::clamp(sourceArea.x + floor(xPerc * (sourceArea.width - 1) + 0.5f), 0, sourceSize.width - 1);
Geoff Lang975af372013-06-12 11:19:22 -0400847 unsigned int writeColumn = x;
848
849 void *sourcePixel = reinterpret_cast<char*>(sourceMapping.pData) +
850 readRow * sourceMapping.RowPitch +
851 readColumn * pixelSize +
852 copyOffset;
853
854 void *destPixel = reinterpret_cast<char*>(destMapping.pData) +
855 writeRow * destMapping.RowPitch +
856 writeColumn * pixelSize +
857 copyOffset;
858
859 memcpy(destPixel, sourcePixel, copySize);
860 }
861 }
862 }
863
Geoff Lang921968c2013-09-04 16:59:22 -0400864 // HACK: Use ID3D11DevicContext::UpdateSubresource which causes an extra copy compared to ID3D11DevicContext::CopySubresourceRegion
865 // according to MSDN.
866 deviceContext->UpdateSubresource(dest, destSubresource, NULL, destMapping.pData, destMapping.RowPitch, destMapping.DepthPitch);
867
Geoff Lang975af372013-06-12 11:19:22 -0400868 deviceContext->Unmap(sourceStaging, 0);
869 deviceContext->Unmap(destStaging, 0);
870
Geoff Lang921968c2013-09-04 16:59:22 -0400871 // TODO: Determine why this call to ID3D11DevicContext::CopySubresourceRegion causes a TDR timeout on some
872 // systems when called repeatedly.
873 // deviceContext->CopySubresourceRegion(dest, destSubresource, 0, 0, 0, destStaging, 0, NULL);
Geoff Lang975af372013-06-12 11:19:22 -0400874
875 SafeRelease(sourceStaging);
876 SafeRelease(destStaging);
877
878 return true;
879}
880
881bool Blit11::compareBlitParameters(const Blit11::BlitParameters &a, const Blit11::BlitParameters &b)
882{
883 return memcmp(&a, &b, sizeof(Blit11::BlitParameters)) < 0;
884}
885
Geoff Langed883f52013-09-10 17:20:54 -0400886bool Blit11::compareSwizzleParameters(const SwizzleParameters &a, const SwizzleParameters &b)
887{
888 return memcmp(&a, &b, sizeof(Blit11::SwizzleParameters)) < 0;
889}
890
Geoff Lang26533602013-09-09 16:41:42 -0400891void Blit11::add2DBlitShaderToMap(GLenum destFormat, bool signedInteger, ID3D11PixelShader *ps)
Geoff Langb86b9792013-06-04 16:32:05 -0400892{
893 BlitParameters params = { 0 };
894 params.mDestinationFormat = destFormat;
895 params.mSignedInteger = signedInteger;
896 params.m3DBlit = false;
897
Geoff Lang26533602013-09-09 16:41:42 -0400898 ASSERT(mBlitShaderMap.find(params) == mBlitShaderMap.end());
Geoff Langb86b9792013-06-04 16:32:05 -0400899 ASSERT(ps);
900
Geoff Lang26533602013-09-09 16:41:42 -0400901 Shader shader;
Jamie Madillbdfa2282013-09-18 14:36:21 -0400902 shader.mVertexWriteFunction = Write2DVertices;
Geoff Langb86b9792013-06-04 16:32:05 -0400903 shader.mInputLayout = mQuad2DIL;
904 shader.mVertexShader = mQuad2DVS;
905 shader.mGeometryShader = NULL;
906 shader.mPixelShader = ps;
907
Geoff Lang26533602013-09-09 16:41:42 -0400908 mBlitShaderMap[params] = shader;
Geoff Langb86b9792013-06-04 16:32:05 -0400909}
910
Geoff Lang26533602013-09-09 16:41:42 -0400911void Blit11::add3DBlitShaderToMap(GLenum destFormat, bool signedInteger, ID3D11PixelShader *ps)
Geoff Langb86b9792013-06-04 16:32:05 -0400912{
913 BlitParameters params = { 0 };
914 params.mDestinationFormat = destFormat;
915 params.mSignedInteger = signedInteger;
916 params.m3DBlit = true;
917
Geoff Lang26533602013-09-09 16:41:42 -0400918 ASSERT(mBlitShaderMap.find(params) == mBlitShaderMap.end());
Geoff Langb86b9792013-06-04 16:32:05 -0400919 ASSERT(ps);
920
Geoff Lang26533602013-09-09 16:41:42 -0400921 Shader shader;
Jamie Madillbdfa2282013-09-18 14:36:21 -0400922 shader.mVertexWriteFunction = Write3DVertices;
Geoff Langb86b9792013-06-04 16:32:05 -0400923 shader.mInputLayout = mQuad3DIL;
924 shader.mVertexShader = mQuad3DVS;
925 shader.mGeometryShader = mQuad3DGS;
926 shader.mPixelShader = ps;
927
Geoff Lang26533602013-09-09 16:41:42 -0400928 mBlitShaderMap[params] = shader;
Geoff Langb86b9792013-06-04 16:32:05 -0400929}
930
Geoff Langed883f52013-09-10 17:20:54 -0400931void Blit11::addSwizzleShaderToMap(GLenum destType, D3D11_SRV_DIMENSION viewDimension, ID3D11PixelShader *ps)
932{
933 SwizzleParameters params = { 0 };
934 params.mDestinationType = destType;
935 params.mViewDimension = viewDimension;
936
937 ASSERT(mSwizzleShaderMap.find(params) == mSwizzleShaderMap.end());
938 ASSERT(ps);
939
940 Shader shader;
941 switch (viewDimension)
942 {
943 case D3D_SRV_DIMENSION_TEXTURE2D:
944 shader.mVertexWriteFunction = Write2DVertices;
945 shader.mInputLayout = mQuad2DIL;
946 shader.mVertexShader = mQuad2DVS;
947 shader.mGeometryShader = NULL;
948 break;
949
950 case D3D_SRV_DIMENSION_TEXTURE3D:
951 case D3D_SRV_DIMENSION_TEXTURE2DARRAY:
952 case D3D_SRV_DIMENSION_TEXTURECUBE:
953 shader.mVertexWriteFunction = Write3DVertices;
954 shader.mInputLayout = mQuad3DIL;
955 shader.mVertexShader = mQuad3DVS;
956 shader.mGeometryShader = mQuad3DGS;
957 break;
958
959 default:
960 UNREACHABLE();
961 break;
962 }
963 shader.mPixelShader = ps;
964
965 mSwizzleShaderMap[params] = shader;
966}
967
Geoff Langb86b9792013-06-04 16:32:05 -0400968void Blit11::buildShaderMap()
969{
970 ID3D11Device *device = mRenderer->getDevice();
971
Geoff Lang26533602013-09-09 16:41:42 -0400972 add2DBlitShaderToMap(GL_RGBA, false, d3d11::CompilePS(device, g_PS_PassthroughRGBA2D, "Blit11 2D RGBA pixel shader" ));
973 add2DBlitShaderToMap(GL_RGBA_INTEGER, false, d3d11::CompilePS(device, g_PS_PassthroughRGBA2DUI, "Blit11 2D RGBA UI pixel shader" ));
974 add2DBlitShaderToMap(GL_RGBA_INTEGER, true, d3d11::CompilePS(device, g_PS_PassthroughRGBA2DI, "Blit11 2D RGBA I pixel shader" ));
975 add2DBlitShaderToMap(GL_BGRA_EXT, false, d3d11::CompilePS(device, g_PS_PassthroughRGBA2D, "Blit11 2D BGRA pixel shader" ));
976 add2DBlitShaderToMap(GL_RGB, false, d3d11::CompilePS(device, g_PS_PassthroughRGB2D, "Blit11 2D RGB pixel shader" ));
977 add2DBlitShaderToMap(GL_RGB_INTEGER, false, d3d11::CompilePS(device, g_PS_PassthroughRGB2DUI, "Blit11 2D RGB UI pixel shader" ));
978 add2DBlitShaderToMap(GL_RGB_INTEGER, true, d3d11::CompilePS(device, g_PS_PassthroughRGB2DI, "Blit11 2D RGB I pixel shader" ));
979 add2DBlitShaderToMap(GL_RG, false, d3d11::CompilePS(device, g_PS_PassthroughRG2D, "Blit11 2D RG pixel shader" ));
980 add2DBlitShaderToMap(GL_RG_INTEGER, false, d3d11::CompilePS(device, g_PS_PassthroughRG2DUI, "Blit11 2D RG UI pixel shader" ));
981 add2DBlitShaderToMap(GL_RG_INTEGER, true, d3d11::CompilePS(device, g_PS_PassthroughRG2DI, "Blit11 2D RG I pixel shader" ));
982 add2DBlitShaderToMap(GL_RED, false, d3d11::CompilePS(device, g_PS_PassthroughR2D, "Blit11 2D R pixel shader" ));
983 add2DBlitShaderToMap(GL_RED_INTEGER, false, d3d11::CompilePS(device, g_PS_PassthroughR2DUI, "Blit11 2D R UI pixel shader" ));
984 add2DBlitShaderToMap(GL_RED_INTEGER, true, d3d11::CompilePS(device, g_PS_PassthroughR2DI, "Blit11 2D R I pixel shader" ));
985 add2DBlitShaderToMap(GL_ALPHA, false, d3d11::CompilePS(device, g_PS_PassthroughRGBA2D, "Blit11 2D alpha pixel shader" ));
986 add2DBlitShaderToMap(GL_LUMINANCE, false, d3d11::CompilePS(device, g_PS_PassthroughLum2D, "Blit11 2D lum pixel shader" ));
987 add2DBlitShaderToMap(GL_LUMINANCE_ALPHA, false, d3d11::CompilePS(device, g_PS_PassthroughLumAlpha2D, "Blit11 2D luminance alpha pixel shader"));
Geoff Langb86b9792013-06-04 16:32:05 -0400988
Geoff Lang26533602013-09-09 16:41:42 -0400989 add3DBlitShaderToMap(GL_RGBA, false, d3d11::CompilePS(device, g_PS_PassthroughRGBA3D, "Blit11 3D RGBA pixel shader" ));
990 add3DBlitShaderToMap(GL_RGBA_INTEGER, false, d3d11::CompilePS(device, g_PS_PassthroughRGBA3DUI, "Blit11 3D UI RGBA pixel shader" ));
991 add3DBlitShaderToMap(GL_RGBA_INTEGER, true, d3d11::CompilePS(device, g_PS_PassthroughRGBA3DI, "Blit11 3D I RGBA pixel shader" ));
992 add3DBlitShaderToMap(GL_BGRA_EXT, false, d3d11::CompilePS(device, g_PS_PassthroughRGBA3D, "Blit11 3D BGRA pixel shader" ));
993 add3DBlitShaderToMap(GL_RGB, false, d3d11::CompilePS(device, g_PS_PassthroughRGB3D, "Blit11 3D RGB pixel shader" ));
994 add3DBlitShaderToMap(GL_RGB_INTEGER, false, d3d11::CompilePS(device, g_PS_PassthroughRGB3DUI, "Blit11 3D RGB UI pixel shader" ));
995 add3DBlitShaderToMap(GL_RGB_INTEGER, true, d3d11::CompilePS(device, g_PS_PassthroughRGB3DI, "Blit11 3D RGB I pixel shader" ));
996 add3DBlitShaderToMap(GL_RG, false, d3d11::CompilePS(device, g_PS_PassthroughRG3D, "Blit11 3D RG pixel shader" ));
997 add3DBlitShaderToMap(GL_RG_INTEGER, false, d3d11::CompilePS(device, g_PS_PassthroughRG3DUI, "Blit11 3D RG UI pixel shader" ));
998 add3DBlitShaderToMap(GL_RG_INTEGER, true, d3d11::CompilePS(device, g_PS_PassthroughRG3DI, "Blit11 3D RG I pixel shader" ));
999 add3DBlitShaderToMap(GL_RED, false, d3d11::CompilePS(device, g_PS_PassthroughR3D, "Blit11 3D R pixel shader" ));
1000 add3DBlitShaderToMap(GL_RED_INTEGER, false, d3d11::CompilePS(device, g_PS_PassthroughR3DUI, "Blit11 3D R UI pixel shader" ));
1001 add3DBlitShaderToMap(GL_RED_INTEGER, true, d3d11::CompilePS(device, g_PS_PassthroughR3DI, "Blit11 3D R I pixel shader" ));
1002 add3DBlitShaderToMap(GL_ALPHA, false, d3d11::CompilePS(device, g_PS_PassthroughRGBA3D, "Blit11 3D alpha pixel shader" ));
1003 add3DBlitShaderToMap(GL_LUMINANCE, false, d3d11::CompilePS(device, g_PS_PassthroughLum3D, "Blit11 3D luminance pixel shader" ));
1004 add3DBlitShaderToMap(GL_LUMINANCE_ALPHA, false, d3d11::CompilePS(device, g_PS_PassthroughLumAlpha3D, "Blit11 3D luminance alpha pixel shader"));
Geoff Langed883f52013-09-10 17:20:54 -04001005
1006 addSwizzleShaderToMap(GL_FLOAT, D3D_SRV_DIMENSION_TEXTURE2D, d3d11::CompilePS(device, g_PS_SwizzleF2D, "Blit11 2D F swizzle pixel shader" ));
1007 addSwizzleShaderToMap(GL_UNSIGNED_INT, D3D_SRV_DIMENSION_TEXTURE2D, d3d11::CompilePS(device, g_PS_SwizzleUI2D, "Blit11 2D UI swizzle pixel shader"));
1008 addSwizzleShaderToMap(GL_INT, D3D_SRV_DIMENSION_TEXTURE2D, d3d11::CompilePS(device, g_PS_SwizzleI2D, "Blit11 2D I swizzle pixel shader" ));
1009
1010 addSwizzleShaderToMap(GL_FLOAT, D3D_SRV_DIMENSION_TEXTURECUBE, d3d11::CompilePS(device, g_PS_SwizzleF2DArray, "Blit11 2D Cube F swizzle pixel shader" ));
1011 addSwizzleShaderToMap(GL_UNSIGNED_INT, D3D_SRV_DIMENSION_TEXTURECUBE, d3d11::CompilePS(device, g_PS_SwizzleUI2DArray, "Blit11 2D Cube UI swizzle pixel shader"));
1012 addSwizzleShaderToMap(GL_INT, D3D_SRV_DIMENSION_TEXTURECUBE, d3d11::CompilePS(device, g_PS_SwizzleI2DArray, "Blit11 2D Cube I swizzle pixel shader" ));
1013
1014 addSwizzleShaderToMap(GL_FLOAT, D3D_SRV_DIMENSION_TEXTURE3D, d3d11::CompilePS(device, g_PS_SwizzleF3D, "Blit11 3D F swizzle pixel shader" ));
1015 addSwizzleShaderToMap(GL_UNSIGNED_INT, D3D_SRV_DIMENSION_TEXTURE3D, d3d11::CompilePS(device, g_PS_SwizzleUI3D, "Blit11 3D UI swizzle pixel shader"));
1016 addSwizzleShaderToMap(GL_INT, D3D_SRV_DIMENSION_TEXTURE3D, d3d11::CompilePS(device, g_PS_SwizzleI3D, "Blit11 3D I swizzle pixel shader" ));
1017
1018 addSwizzleShaderToMap(GL_FLOAT, D3D_SRV_DIMENSION_TEXTURE2DARRAY, d3d11::CompilePS(device, g_PS_SwizzleF2DArray, "Blit11 2D Array F swizzle pixel shader" ));
1019 addSwizzleShaderToMap(GL_UNSIGNED_INT, D3D_SRV_DIMENSION_TEXTURE2DARRAY, d3d11::CompilePS(device, g_PS_SwizzleUI2DArray, "Blit11 2D Array UI swizzle pixel shader"));
1020 addSwizzleShaderToMap(GL_INT, D3D_SRV_DIMENSION_TEXTURE2DARRAY, d3d11::CompilePS(device, g_PS_SwizzleI2DArray, "Blit11 2D Array I swizzle pixel shader" ));
Geoff Langb86b9792013-06-04 16:32:05 -04001021}
1022
1023void Blit11::clearShaderMap()
1024{
Geoff Lang26533602013-09-09 16:41:42 -04001025 for (BlitShaderMap::iterator i = mBlitShaderMap.begin(); i != mBlitShaderMap.end(); ++i)
Geoff Langb86b9792013-06-04 16:32:05 -04001026 {
Geoff Lang26533602013-09-09 16:41:42 -04001027 Shader &shader = i->second;
Geoff Langb86b9792013-06-04 16:32:05 -04001028 SafeRelease(shader.mPixelShader);
1029 }
Geoff Lang26533602013-09-09 16:41:42 -04001030 mBlitShaderMap.clear();
Geoff Langed883f52013-09-10 17:20:54 -04001031
1032 for (SwizzleShaderMap::iterator i = mSwizzleShaderMap.begin(); i != mSwizzleShaderMap.end(); ++i)
1033 {
1034 Shader &shader = i->second;
1035 SafeRelease(shader.mPixelShader);
1036 }
1037 mSwizzleShaderMap.clear();
Geoff Langb86b9792013-06-04 16:32:05 -04001038}
1039
1040}