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 // Blit11.cpp: Texture copy utility class.
8 
9 #ifndef LIBANGLE_RENDERER_D3D_D3D11_BLIT11_H_
10 #define LIBANGLE_RENDERER_D3D_D3D11_BLIT11_H_
11 
12 #include "common/angleutils.h"
13 #include "libANGLE/Error.h"
14 #include "libANGLE/angletypes.h"
15 #include "libANGLE/renderer/d3d/d3d11/ResourceManager11.h"
16 #include "libANGLE/renderer/d3d/d3d11/renderer11_utils.h"
17 
18 #include <map>
19 
20 namespace rx
21 {
22 class Renderer11;
23 
24 class Blit11 : angle::NonCopyable
25 {
26   public:
27     explicit Blit11(Renderer11 *renderer);
28     ~Blit11();
29 
30     gl::Error swizzleTexture(const gl::Context *context,
31                              const d3d11::SharedSRV &source,
32                              const d3d11::RenderTargetView &dest,
33                              const gl::Extents &size,
34                              const gl::SwizzleState &swizzleTarget);
35 
36     gl::Error copyTexture(const gl::Context *context,
37                           const d3d11::SharedSRV &source,
38                           const gl::Box &sourceArea,
39                           const gl::Extents &sourceSize,
40                           GLenum sourceFormat,
41                           const d3d11::RenderTargetView &dest,
42                           const gl::Box &destArea,
43                           const gl::Extents &destSize,
44                           const gl::Rectangle *scissor,
45                           GLenum destFormat,
46                           GLenum filter,
47                           bool maskOffAlpha,
48                           bool unpackPremultiplyAlpha,
49                           bool unpackUnmultiplyAlpha);
50 
51     gl::Error copyStencil(const gl::Context *context,
52                           const TextureHelper11 &source,
53                           unsigned int sourceSubresource,
54                           const gl::Box &sourceArea,
55                           const gl::Extents &sourceSize,
56                           const TextureHelper11 &dest,
57                           unsigned int destSubresource,
58                           const gl::Box &destArea,
59                           const gl::Extents &destSize,
60                           const gl::Rectangle *scissor);
61 
62     gl::Error copyDepth(const gl::Context *context,
63                         const d3d11::SharedSRV &source,
64                         const gl::Box &sourceArea,
65                         const gl::Extents &sourceSize,
66                         const d3d11::DepthStencilView &dest,
67                         const gl::Box &destArea,
68                         const gl::Extents &destSize,
69                         const gl::Rectangle *scissor);
70 
71     gl::Error copyDepthStencil(const TextureHelper11 &source,
72                                unsigned int sourceSubresource,
73                                const gl::Box &sourceArea,
74                                const gl::Extents &sourceSize,
75                                const TextureHelper11 &dest,
76                                unsigned int destSubresource,
77                                const gl::Box &destArea,
78                                const gl::Extents &destSize,
79                                const gl::Rectangle *scissor);
80 
81     gl::ErrorOrResult<TextureHelper11> resolveDepth(const gl::Context *context,
82                                                     RenderTarget11 *depth);
83 
84     gl::ErrorOrResult<TextureHelper11> resolveStencil(const gl::Context *context,
85                                                       RenderTarget11 *depthStencil,
86                                                       bool alsoDepth);
87 
88     using BlitConvertFunction = void(const gl::Box &sourceArea,
89                                      const gl::Box &destArea,
90                                      const gl::Rectangle &clipRect,
91                                      const gl::Extents &sourceSize,
92                                      unsigned int sourceRowPitch,
93                                      unsigned int destRowPitch,
94                                      ptrdiff_t readOffset,
95                                      ptrdiff_t writeOffset,
96                                      size_t copySize,
97                                      size_t srcPixelStride,
98                                      size_t destPixelStride,
99                                      const uint8_t *sourceData,
100                                      uint8_t *destData);
101 
102   private:
103     enum BlitShaderType
104     {
105         BLITSHADER_INVALID,
106 
107         // Passthrough shaders
108         BLITSHADER_2D_RGBAF,
109         BLITSHADER_2D_BGRAF,
110         BLITSHADER_2D_RGBF,
111         BLITSHADER_2D_RGF,
112         BLITSHADER_2D_RF,
113         BLITSHADER_2D_ALPHA,
114         BLITSHADER_2D_LUMA,
115         BLITSHADER_2D_LUMAALPHA,
116         BLITSHADER_2D_RGBAUI,
117         BLITSHADER_2D_RGBAI,
118         BLITSHADER_2D_RGBUI,
119         BLITSHADER_2D_RGBI,
120         BLITSHADER_2D_RGUI,
121         BLITSHADER_2D_RGI,
122         BLITSHADER_2D_RUI,
123         BLITSHADER_2D_RI,
124         BLITSHADER_3D_RGBAF,
125         BLITSHADER_3D_RGBAUI,
126         BLITSHADER_3D_RGBAI,
127         BLITSHADER_3D_BGRAF,
128         BLITSHADER_3D_RGBF,
129         BLITSHADER_3D_RGBUI,
130         BLITSHADER_3D_RGBI,
131         BLITSHADER_3D_RGF,
132         BLITSHADER_3D_RGUI,
133         BLITSHADER_3D_RGI,
134         BLITSHADER_3D_RF,
135         BLITSHADER_3D_RUI,
136         BLITSHADER_3D_RI,
137         BLITSHADER_3D_ALPHA,
138         BLITSHADER_3D_LUMA,
139         BLITSHADER_3D_LUMAALPHA,
140 
141         // Multiply alpha shaders
142         BLITSHADER_2D_RGBAF_PREMULTIPLY,
143         BLITSHADER_2D_RGBAF_UNMULTIPLY,
144 
145         BLITSHADER_2D_RGBF_PREMULTIPLY,
146         BLITSHADER_2D_RGBF_UNMULTIPLY,
147 
148         BLITSHADER_2D_RGBAF_TOUI,
149         BLITSHADER_2D_RGBAF_TOUI_PREMULTIPLY,
150         BLITSHADER_2D_RGBAF_TOUI_UNMULTIPLY,
151 
152         BLITSHADER_2D_RGBF_TOUI,
153         BLITSHADER_2D_RGBF_TOUI_PREMULTIPLY,
154         BLITSHADER_2D_RGBF_TOUI_UNMULTIPLY,
155 
156         BLITSHADER_2D_LUMAF_PREMULTIPLY,
157         BLITSHADER_2D_LUMAF_UNMULTIPLY,
158 
159         BLITSHADER_2D_LUMAALPHAF_PREMULTIPLY,
160         BLITSHADER_2D_LUMAALPHAF_UNMULTIPLY
161     };
162 
163     enum SwizzleShaderType
164     {
165         SWIZZLESHADER_INVALID,
166         SWIZZLESHADER_2D_FLOAT,
167         SWIZZLESHADER_2D_UINT,
168         SWIZZLESHADER_2D_INT,
169         SWIZZLESHADER_CUBE_FLOAT,
170         SWIZZLESHADER_CUBE_UINT,
171         SWIZZLESHADER_CUBE_INT,
172         SWIZZLESHADER_3D_FLOAT,
173         SWIZZLESHADER_3D_UINT,
174         SWIZZLESHADER_3D_INT,
175         SWIZZLESHADER_ARRAY_FLOAT,
176         SWIZZLESHADER_ARRAY_UINT,
177         SWIZZLESHADER_ARRAY_INT,
178     };
179 
180     typedef void (*WriteVertexFunction)(const gl::Box &sourceArea,
181                                         const gl::Extents &sourceSize,
182                                         const gl::Box &destArea,
183                                         const gl::Extents &destSize,
184                                         void *outVertices,
185                                         unsigned int *outStride,
186                                         unsigned int *outVertexCount,
187                                         D3D11_PRIMITIVE_TOPOLOGY *outTopology);
188 
189     enum ShaderDimension
190     {
191         SHADER_2D,
192         SHADER_3D,
193     };
194 
195     struct Shader
196     {
197         Shader();
198         Shader(Shader &&other);
199         ~Shader();
200         Shader &operator=(Shader &&other);
201 
202         ShaderDimension dimension;
203         d3d11::PixelShader pixelShader;
204     };
205 
206     struct ShaderSupport
207     {
208         const d3d11::InputLayout *inputLayout;
209         const d3d11::VertexShader *vertexShader;
210         const d3d11::GeometryShader *geometryShader;
211         WriteVertexFunction vertexWriteFunction;
212     };
213 
214     gl::Error initResources();
215 
216     gl::Error getShaderSupport(const Shader &shader, ShaderSupport *supportOut);
217 
218     static BlitShaderType GetBlitShaderType(GLenum destinationFormat,
219                                             GLenum sourceFormat,
220                                             bool isSigned,
221                                             bool unpackPremultiplyAlpha,
222                                             bool unpackUnmultiplyAlpha,
223                                             ShaderDimension dimension);
224     static SwizzleShaderType GetSwizzleShaderType(GLenum type, D3D11_SRV_DIMENSION dimensionality);
225 
226     gl::Error copyDepthStencilImpl(const TextureHelper11 &source,
227                                    unsigned int sourceSubresource,
228                                    const gl::Box &sourceArea,
229                                    const gl::Extents &sourceSize,
230                                    const TextureHelper11 &dest,
231                                    unsigned int destSubresource,
232                                    const gl::Box &destArea,
233                                    const gl::Extents &destSize,
234                                    const gl::Rectangle *scissor,
235                                    bool stencilOnly);
236 
237     gl::Error copyAndConvertImpl(const TextureHelper11 &source,
238                                  unsigned int sourceSubresource,
239                                  const gl::Box &sourceArea,
240                                  const gl::Extents &sourceSize,
241                                  const TextureHelper11 &destStaging,
242                                  const gl::Box &destArea,
243                                  const gl::Extents &destSize,
244                                  const gl::Rectangle *scissor,
245                                  size_t readOffset,
246                                  size_t writeOffset,
247                                  size_t copySize,
248                                  size_t srcPixelStride,
249                                  size_t destPixelStride,
250                                  BlitConvertFunction *convertFunction);
251 
252     gl::Error copyAndConvert(const TextureHelper11 &source,
253                              unsigned int sourceSubresource,
254                              const gl::Box &sourceArea,
255                              const gl::Extents &sourceSize,
256                              const TextureHelper11 &dest,
257                              unsigned int destSubresource,
258                              const gl::Box &destArea,
259                              const gl::Extents &destSize,
260                              const gl::Rectangle *scissor,
261                              size_t readOffset,
262                              size_t writeOffset,
263                              size_t copySize,
264                              size_t srcPixelStride,
265                              size_t destPixelStride,
266                              BlitConvertFunction *convertFunction);
267 
268     gl::Error addBlitShaderToMap(BlitShaderType blitShaderType,
269                                  ShaderDimension dimension,
270                                  const ShaderData &shaderData,
271                                  const char *name);
272 
273     gl::Error getBlitShader(GLenum destFormat,
274                             GLenum sourceFormat,
275                             bool isSigned,
276                             bool unpackPremultiplyAlpha,
277                             bool unpackUnmultiplyAlpha,
278                             ShaderDimension dimension,
279                             const Shader **shaderOut);
280     gl::Error getSwizzleShader(GLenum type,
281                                D3D11_SRV_DIMENSION viewDimension,
282                                const Shader **shaderOut);
283 
284     gl::Error addSwizzleShaderToMap(SwizzleShaderType swizzleShaderType,
285                                     ShaderDimension dimension,
286                                     const ShaderData &shaderData,
287                                     const char *name);
288 
289     void clearShaderMap();
290     void releaseResolveDepthStencilResources();
291     gl::Error initResolveDepthOnly(const d3d11::Format &format, const gl::Extents &extents);
292     gl::Error initResolveDepthStencil(const gl::Extents &extents);
293 
294     Renderer11 *mRenderer;
295 
296     std::map<BlitShaderType, Shader> mBlitShaderMap;
297     std::map<SwizzleShaderType, Shader> mSwizzleShaderMap;
298 
299     bool mResourcesInitialized;
300     d3d11::Buffer mVertexBuffer;
301     d3d11::SamplerState mPointSampler;
302     d3d11::SamplerState mLinearSampler;
303     d3d11::RasterizerState mScissorEnabledRasterizerState;
304     d3d11::RasterizerState mScissorDisabledRasterizerState;
305     d3d11::DepthStencilState mDepthStencilState;
306 
307     d3d11::LazyInputLayout mQuad2DIL;
308     d3d11::LazyShader<ID3D11VertexShader> mQuad2DVS;
309     d3d11::LazyShader<ID3D11PixelShader> mDepthPS;
310 
311     d3d11::LazyInputLayout mQuad3DIL;
312     d3d11::LazyShader<ID3D11VertexShader> mQuad3DVS;
313     d3d11::LazyShader<ID3D11GeometryShader> mQuad3DGS;
314 
315     d3d11::LazyBlendState mAlphaMaskBlendState;
316 
317     d3d11::Buffer mSwizzleCB;
318 
319     d3d11::LazyShader<ID3D11VertexShader> mResolveDepthStencilVS;
320     d3d11::LazyShader<ID3D11PixelShader> mResolveDepthPS;
321     d3d11::LazyShader<ID3D11PixelShader> mResolveDepthStencilPS;
322     d3d11::LazyShader<ID3D11PixelShader> mResolveStencilPS;
323     d3d11::ShaderResourceView mStencilSRV;
324     TextureHelper11 mResolvedDepthStencil;
325     d3d11::RenderTargetView mResolvedDepthStencilRTView;
326     TextureHelper11 mResolvedDepth;
327     d3d11::DepthStencilView mResolvedDepthDSView;
328 };
329 
330 }  // namespace rx
331 
332 #endif  // LIBANGLE_RENDERER_D3D_D3D11_BLIT11_H_
333