Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / libGLESv2 / renderer / d3d / d3d11 / PixelTransfer11.cpp
1 //
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.
5 //
6
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.
10 //
11
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"
23
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"
30
31 namespace rx
32 {
33
34 PixelTransfer11::PixelTransfer11(Renderer11 *renderer)
35     : mRenderer(renderer),
36       mBufferToTextureVS(NULL),
37       mBufferToTextureGS(NULL),
38       mParamsConstantBuffer(NULL),
39       mCopyRasterizerState(NULL),
40       mCopyDepthStencilState(NULL)
41 {
42     HRESULT result = S_OK;
43     ID3D11Device *device = mRenderer->getDevice();
44
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;
56
57     result = device->CreateRasterizerState(&rasterDesc, &mCopyRasterizerState);
58     ASSERT(SUCCEEDED(result));
59
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;
75
76     result = device->CreateDepthStencilState(&depthStencilDesc, &mCopyDepthStencilState);
77     ASSERT(SUCCEEDED(result));
78
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;
86
87     result = device->CreateBuffer(&constantBufferDesc, NULL, &mParamsConstantBuffer);
88     ASSERT(SUCCEEDED(result));
89     d3d11::SetDebugName(mParamsConstantBuffer, "PixelTransfer11 constant buffer");
90
91     // init shaders
92     mBufferToTextureVS = d3d11::CompileVS(device, g_VS_BufferToTexture, "BufferToTexture VS");
93     mBufferToTextureGS = d3d11::CompileGS(device, g_GS_BufferToTexture, "BufferToTexture GS");
94
95     buildShaderMap();
96
97     StructZero(&mParamsData);
98 }
99
100 PixelTransfer11::~PixelTransfer11()
101 {
102     for (auto shaderMapIt = mBufferToTexturePSMap.begin(); shaderMapIt != mBufferToTexturePSMap.end(); shaderMapIt++)
103     {
104         SafeRelease(shaderMapIt->second);
105     }
106
107     mBufferToTexturePSMap.clear();
108
109     SafeRelease(mBufferToTextureVS);
110     SafeRelease(mBufferToTextureGS);
111     SafeRelease(mParamsConstantBuffer);
112     SafeRelease(mCopyRasterizerState);
113     SafeRelease(mCopyDepthStencilState);
114 }
115
116 void PixelTransfer11::setBufferToTextureCopyParams(const gl::Box &destArea, const gl::Extents &destSize, GLenum internalFormat,
117                                                    const gl::PixelUnpackState &unpack, unsigned int offset, CopyShaderParams *parametersOut)
118 {
119     StructZero(parametersOut);
120
121     float texelCenterX = 0.5f / static_cast<float>(destSize.width - 1);
122     float texelCenterY = 0.5f / static_cast<float>(destSize.height - 1);
123
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);
127
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);
136 }
137
138 bool PixelTransfer11::copyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
139                                           GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea)
140 {
141     gl::Extents destSize = destRenderTarget->getExtents();
142
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    )
146     {
147         return false;
148     }
149
150     const gl::Buffer &sourceBuffer = *unpack.pixelBuffer.get();
151
152     ASSERT(mRenderer->supportsFastCopyBufferToTexture(destinationFormat));
153
154     ID3D11PixelShader *pixelShader = findBufferToTexturePS(destinationFormat);
155     ASSERT(pixelShader);
156
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;
161
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);
168
169     ID3D11RenderTargetView *textureRTV = RenderTarget11::makeRenderTarget11(destRenderTarget)->getRenderTargetView();
170     ASSERT(textureRTV != NULL);
171
172     CopyShaderParams shaderParams;
173     setBufferToTextureCopyParams(destArea, destSize, sourceFormat, unpack, offset, &shaderParams);
174
175     ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
176
177     ID3D11ShaderResourceView *nullSRV = NULL;
178     ID3D11Buffer *nullBuffer = NULL;
179     UINT zero = 0;
180
181     // Are we doing a 2D or 3D copy?
182     ID3D11GeometryShader *geometryShader = ((destSize.depth > 1) ? mBufferToTextureGS : NULL);
183
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);
190
191     deviceContext->IASetVertexBuffers(0, 1, &nullBuffer, &zero, &zero);
192     deviceContext->OMSetBlendState(NULL, NULL, 0xFFFFFFF);
193     deviceContext->OMSetDepthStencilState(mCopyDepthStencilState, 0xFFFFFFFF);
194     deviceContext->RSSetState(mCopyRasterizerState);
195
196     mRenderer->setOneTimeRenderTarget(textureRTV);
197
198     if (!StructEquals(mParamsData, shaderParams))
199     {
200         d3d11::SetBufferData(deviceContext, mParamsConstantBuffer, shaderParams);
201         mParamsData = shaderParams;
202     }
203
204     deviceContext->VSSetConstantBuffers(0, 1, &mParamsConstantBuffer);
205
206     // Set the viewport
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);
215
216     UINT numPixels = (destArea.width * destArea.height * destArea.depth);
217     deviceContext->Draw(numPixels, 0);
218
219     // Unbind textures and render targets and vertex buffer
220     deviceContext->PSSetShaderResources(0, 1, &nullSRV);
221     deviceContext->VSSetConstantBuffers(0, 1, &nullBuffer);
222
223     mRenderer->markAllStateDirty();
224
225     return true;
226 }
227
228 void PixelTransfer11::buildShaderMap()
229 {
230     ID3D11Device *device = mRenderer->getDevice();
231
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");
235 }
236
237 ID3D11PixelShader *PixelTransfer11::findBufferToTexturePS(GLenum internalFormat) const
238 {
239     GLenum componentType = gl::GetInternalFormatInfo(internalFormat).componentType;
240     if (componentType == GL_SIGNED_NORMALIZED || componentType == GL_UNSIGNED_NORMALIZED)
241     {
242         componentType = GL_FLOAT;
243     }
244
245     auto shaderMapIt = mBufferToTexturePSMap.find(componentType);
246     return (shaderMapIt == mBufferToTexturePSMap.end() ? NULL : shaderMapIt->second);
247 }
248
249 }