2 // Copyright (c) 2013 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.
7 // PixelTransfer11.cpp:
8 // Implementation for buffer-to-texture and texture-to-buffer copies.
9 // Used to implement pixel transfers from unpack and to pack buffers.
12 #include "libGLESv2/renderer/d3d/d3d11/PixelTransfer11.h"
13 #include "libGLESv2/renderer/d3d/d3d11/Renderer11.h"
14 #include "libGLESv2/renderer/d3d/d3d11/renderer11_utils.h"
15 #include "libGLESv2/renderer/d3d/d3d11/formatutils11.h"
16 #include "libGLESv2/renderer/d3d/d3d11/Buffer11.h"
17 #include "libGLESv2/renderer/d3d/d3d11/TextureStorage11.h"
18 #include "libGLESv2/renderer/d3d/d3d11/RenderTarget11.h"
19 #include "libGLESv2/formatutils.h"
20 #include "libGLESv2/Texture.h"
21 #include "libGLESv2/Buffer.h"
22 #include "libGLESv2/Context.h"
24 // Precompiled shaders
25 #include "libGLESv2/renderer/d3d/d3d11/shaders/compiled/buffertotexture11_vs.h"
26 #include "libGLESv2/renderer/d3d/d3d11/shaders/compiled/buffertotexture11_gs.h"
27 #include "libGLESv2/renderer/d3d/d3d11/shaders/compiled/buffertotexture11_ps_4f.h"
28 #include "libGLESv2/renderer/d3d/d3d11/shaders/compiled/buffertotexture11_ps_4i.h"
29 #include "libGLESv2/renderer/d3d/d3d11/shaders/compiled/buffertotexture11_ps_4ui.h"
34 PixelTransfer11::PixelTransfer11(Renderer11 *renderer)
35 : mRenderer(renderer),
36 mBufferToTextureVS(NULL),
37 mBufferToTextureGS(NULL),
38 mParamsConstantBuffer(NULL),
39 mCopyRasterizerState(NULL),
40 mCopyDepthStencilState(NULL)
42 HRESULT result = S_OK;
43 ID3D11Device *device = mRenderer->getDevice();
45 D3D11_RASTERIZER_DESC rasterDesc;
46 rasterDesc.FillMode = D3D11_FILL_SOLID;
47 rasterDesc.CullMode = D3D11_CULL_NONE;
48 rasterDesc.FrontCounterClockwise = FALSE;
49 rasterDesc.DepthBias = 0;
50 rasterDesc.SlopeScaledDepthBias = 0.0f;
51 rasterDesc.DepthBiasClamp = 0.0f;
52 rasterDesc.DepthClipEnable = TRUE;
53 rasterDesc.ScissorEnable = FALSE;
54 rasterDesc.MultisampleEnable = FALSE;
55 rasterDesc.AntialiasedLineEnable = FALSE;
57 result = device->CreateRasterizerState(&rasterDesc, &mCopyRasterizerState);
58 ASSERT(SUCCEEDED(result));
60 D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
61 depthStencilDesc.DepthEnable = true;
62 depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
63 depthStencilDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
64 depthStencilDesc.StencilEnable = FALSE;
65 depthStencilDesc.StencilReadMask = D3D11_DEFAULT_STENCIL_READ_MASK;
66 depthStencilDesc.StencilWriteMask = D3D11_DEFAULT_STENCIL_WRITE_MASK;
67 depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
68 depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
69 depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
70 depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
71 depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
72 depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_KEEP;
73 depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
74 depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
76 result = device->CreateDepthStencilState(&depthStencilDesc, &mCopyDepthStencilState);
77 ASSERT(SUCCEEDED(result));
79 D3D11_BUFFER_DESC constantBufferDesc = { 0 };
80 constantBufferDesc.ByteWidth = rx::roundUp<UINT>(sizeof(CopyShaderParams), 32u);
81 constantBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
82 constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
83 constantBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
84 constantBufferDesc.MiscFlags = 0;
85 constantBufferDesc.StructureByteStride = 0;
87 result = device->CreateBuffer(&constantBufferDesc, NULL, &mParamsConstantBuffer);
88 ASSERT(SUCCEEDED(result));
89 d3d11::SetDebugName(mParamsConstantBuffer, "PixelTransfer11 constant buffer");
92 mBufferToTextureVS = d3d11::CompileVS(device, g_VS_BufferToTexture, "BufferToTexture VS");
93 mBufferToTextureGS = d3d11::CompileGS(device, g_GS_BufferToTexture, "BufferToTexture GS");
97 StructZero(&mParamsData);
100 PixelTransfer11::~PixelTransfer11()
102 for (auto shaderMapIt = mBufferToTexturePSMap.begin(); shaderMapIt != mBufferToTexturePSMap.end(); shaderMapIt++)
104 SafeRelease(shaderMapIt->second);
107 mBufferToTexturePSMap.clear();
109 SafeRelease(mBufferToTextureVS);
110 SafeRelease(mBufferToTextureGS);
111 SafeRelease(mParamsConstantBuffer);
112 SafeRelease(mCopyRasterizerState);
113 SafeRelease(mCopyDepthStencilState);
116 void PixelTransfer11::setBufferToTextureCopyParams(const gl::Box &destArea, const gl::Extents &destSize, GLenum internalFormat,
117 const gl::PixelUnpackState &unpack, unsigned int offset, CopyShaderParams *parametersOut)
119 StructZero(parametersOut);
121 float texelCenterX = 0.5f / static_cast<float>(destSize.width - 1);
122 float texelCenterY = 0.5f / static_cast<float>(destSize.height - 1);
124 unsigned int bytesPerPixel = gl::GetInternalFormatInfo(internalFormat).pixelBytes;
125 unsigned int alignmentBytes = static_cast<unsigned int>(unpack.alignment);
126 unsigned int alignmentPixels = (alignmentBytes <= bytesPerPixel ? 1 : alignmentBytes / bytesPerPixel);
128 parametersOut->FirstPixelOffset = offset;
129 parametersOut->PixelsPerRow = static_cast<unsigned int>(destArea.width);
130 parametersOut->RowStride = roundUp(parametersOut->PixelsPerRow, alignmentPixels);
131 parametersOut->RowsPerSlice = static_cast<unsigned int>(destArea.height);
132 parametersOut->PositionOffset[0] = texelCenterX + (destArea.x / float(destSize.width)) * 2.0f - 1.0f;
133 parametersOut->PositionOffset[1] = texelCenterY + ((destSize.height - destArea.y - 1) / float(destSize.height)) * 2.0f - 1.0f;
134 parametersOut->PositionScale[0] = 2.0f / static_cast<float>(destSize.width);
135 parametersOut->PositionScale[1] = -2.0f / static_cast<float>(destSize.height);
138 bool PixelTransfer11::copyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
139 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
141 gl::Extents destSize = destRenderTarget->getExtents();
143 if (destArea.x < 0 || destArea.x + destArea.width > destSize.width ||
144 destArea.y < 0 || destArea.y + destArea.height > destSize.height ||
145 destArea.z < 0 || destArea.z + destArea.depth > destSize.depth )
150 const gl::Buffer &sourceBuffer = *unpack.pixelBuffer.get();
152 ASSERT(mRenderer->supportsFastCopyBufferToTexture(destinationFormat));
154 ID3D11PixelShader *pixelShader = findBufferToTexturePS(destinationFormat);
157 // The SRV must be in the proper read format, which may be different from the destination format
158 // EG: for half float data, we can load full precision floats with implicit conversion
159 GLenum unsizedFormat = gl::GetInternalFormatInfo(destinationFormat).format;
160 GLenum sourceFormat = gl::GetFormatTypeInfo(unsizedFormat, sourcePixelsType).internalFormat;
162 const d3d11::TextureFormat &sourceFormatInfo = d3d11::GetTextureFormatInfo(sourceFormat);
163 DXGI_FORMAT srvFormat = sourceFormatInfo.srvFormat;
164 ASSERT(srvFormat != DXGI_FORMAT_UNKNOWN);
165 Buffer11 *bufferStorage11 = Buffer11::makeBuffer11(sourceBuffer.getImplementation());
166 ID3D11ShaderResourceView *bufferSRV = bufferStorage11->getSRV(srvFormat);
167 ASSERT(bufferSRV != NULL);
169 ID3D11RenderTargetView *textureRTV = RenderTarget11::makeRenderTarget11(destRenderTarget)->getRenderTargetView();
170 ASSERT(textureRTV != NULL);
172 CopyShaderParams shaderParams;
173 setBufferToTextureCopyParams(destArea, destSize, sourceFormat, unpack, offset, &shaderParams);
175 ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
177 ID3D11ShaderResourceView *nullSRV = NULL;
178 ID3D11Buffer *nullBuffer = NULL;
181 // Are we doing a 2D or 3D copy?
182 ID3D11GeometryShader *geometryShader = ((destSize.depth > 1) ? mBufferToTextureGS : NULL);
184 deviceContext->VSSetShader(mBufferToTextureVS, NULL, 0);
185 deviceContext->GSSetShader(geometryShader, NULL, 0);
186 deviceContext->PSSetShader(pixelShader, NULL, 0);
187 deviceContext->PSSetShaderResources(0, 1, &bufferSRV);
188 deviceContext->IASetInputLayout(NULL);
189 deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_POINTLIST);
191 deviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
192 deviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF);
193 deviceContext->OMSetDepthStencilState(mCopyDepthStencilState, 0xFFFFFFFF);
194 deviceContext->RSSetState(mCopyRasterizerState);
196 mRenderer->setOneTimeRenderTarget(textureRTV);
198 if (!StructEquals(mParamsData, shaderParams))
200 d3d11::SetBufferData(deviceContext, mParamsConstantBuffer, shaderParams);
201 mParamsData = shaderParams;
204 deviceContext->VSSetConstantBuffers(0, 1, &mParamsConstantBuffer);
207 D3D11_VIEWPORT viewport;
208 viewport.TopLeftX = 0;
209 viewport.TopLeftY = 0;
210 viewport.Width = destSize.width;
211 viewport.Height = destSize.height;
212 viewport.MinDepth = 0.0f;
213 viewport.MaxDepth = 1.0f;
214 deviceContext->RSSetViewports(1, &viewport);
216 UINT numPixels = (destArea.width * destArea.height * destArea.depth);
217 deviceContext->Draw(numPixels, 0);
219 // Unbind textures and render targets and vertex buffer
220 deviceContext->PSSetShaderResources(0, 1, &nullSRV);
221 deviceContext->VSSetConstantBuffers(0, 1, &nullBuffer);
223 mRenderer->markAllStateDirty();
228 void PixelTransfer11::buildShaderMap()
230 ID3D11Device *device = mRenderer->getDevice();
232 mBufferToTexturePSMap[GL_FLOAT] = d3d11::CompilePS(device, g_PS_BufferToTexture_4F, "BufferToTexture RGBA ps");
233 mBufferToTexturePSMap[GL_INT] = d3d11::CompilePS(device, g_PS_BufferToTexture_4I, "BufferToTexture RGBA-I ps");
234 mBufferToTexturePSMap[GL_UNSIGNED_INT] = d3d11::CompilePS(device, g_PS_BufferToTexture_4UI, "BufferToTexture RGBA-UI ps");
237 ID3D11PixelShader *PixelTransfer11::findBufferToTexturePS(GLenum internalFormat) const
239 GLenum componentType = gl::GetInternalFormatInfo(internalFormat).componentType;
240 if (componentType == GL_SIGNED_NORMALIZED || componentType == GL_UNSIGNED_NORMALIZED)
242 componentType = GL_FLOAT;
245 auto shaderMapIt = mBufferToTexturePSMap.find(componentType);
246 return (shaderMapIt == mBufferToTexturePSMap.end() ? NULL : shaderMapIt->second);