1 
2 // Copyright (c) 2014 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 // RendererD3D.h: Defines a back-end specific class for the DirectX renderer.
8 
9 #ifndef LIBANGLE_RENDERER_D3D_RENDERERD3D_H_
10 #define LIBANGLE_RENDERER_D3D_RENDERERD3D_H_
11 
12 #include "common/debug.h"
13 #include "common/MemoryBuffer.h"
14 #include "libANGLE/ContextState.h"
15 #include "libANGLE/Device.h"
16 #include "libANGLE/formatutils.h"
17 #include "libANGLE/renderer/d3d/VertexDataManager.h"
18 #include "libANGLE/renderer/d3d/formatutilsD3D.h"
19 #include "libANGLE/renderer/d3d/WorkaroundsD3D.h"
20 #include "libANGLE/Version.h"
21 
22 //FIXME(jmadill): std::array is currently prohibited by Chromium style guide
23 #include <array>
24 
25 namespace egl
26 {
27 class ConfigSet;
28 }
29 
30 namespace gl
31 {
32 class FramebufferState;
33 class InfoLog;
34 class Texture;
35 struct LinkedVarying;
36 }
37 
38 namespace rx
39 {
40 class ContextImpl;
41 struct D3DUniform;
42 struct D3DVarying;
43 class DeviceD3D;
44 class EGLImageD3D;
45 class FramebufferImpl;
46 class ImageD3D;
47 class IndexBuffer;
48 class NativeWindowD3D;
49 class ProgramD3D;
50 class RenderTargetD3D;
51 class ShaderExecutableD3D;
52 class SwapChainD3D;
53 class TextureStorage;
54 struct TranslatedIndexData;
55 class UniformStorageD3D;
56 class VertexBuffer;
57 
58 enum ShaderType
59 {
60     SHADER_VERTEX,
61     SHADER_PIXEL,
62     SHADER_GEOMETRY,
63     SHADER_TYPE_MAX
64 };
65 
66 struct DeviceIdentifier
67 {
68     UINT VendorId;
69     UINT DeviceId;
70     UINT SubSysId;
71     UINT Revision;
72     UINT FeatureLevel;
73 };
74 
75 enum RendererClass
76 {
77     RENDERER_D3D11,
78     RENDERER_D3D9
79 };
80 
81 // Useful for unit testing
82 class BufferFactoryD3D : angle::NonCopyable
83 {
84   public:
BufferFactoryD3D()85     BufferFactoryD3D() {}
~BufferFactoryD3D()86     virtual ~BufferFactoryD3D() {}
87 
88     virtual VertexBuffer *createVertexBuffer() = 0;
89     virtual IndexBuffer *createIndexBuffer() = 0;
90 
91     // TODO(jmadill): add VertexFormatCaps
92     virtual VertexConversionType getVertexConversionType(gl::VertexFormatType vertexFormatType) const = 0;
93     virtual GLenum getVertexComponentType(gl::VertexFormatType vertexFormatType) const = 0;
94     virtual gl::ErrorOrResult<unsigned int> getVertexSpaceRequired(
95         const gl::VertexAttribute &attrib,
96         GLsizei count,
97         GLsizei instances) const = 0;
98 };
99 
100 using AttribIndexArray = std::array<int, gl::MAX_VERTEX_ATTRIBS>;
101 
102 class RendererD3D : public BufferFactoryD3D
103 {
104   public:
105     explicit RendererD3D(egl::Display *display);
106     virtual ~RendererD3D();
107 
108     virtual egl::Error initialize() = 0;
109 
110     virtual egl::ConfigSet generateConfigs() = 0;
111     virtual void generateDisplayExtensions(egl::DisplayExtensions *outExtensions) const = 0;
112 
113     virtual ContextImpl *createContext(const gl::ContextState &state) = 0;
114 
115     std::string getVendorString() const;
116 
117     virtual int getMinorShaderModel() const = 0;
118     virtual std::string getShaderModelSuffix() const = 0;
119 
120     // Direct3D Specific methods
121     virtual DeviceIdentifier getAdapterIdentifier() const = 0;
122 
123     virtual bool isValidNativeWindow(EGLNativeWindowType window) const = 0;
124     virtual NativeWindowD3D *createNativeWindow(EGLNativeWindowType window,
125                                                 const egl::Config *config,
126                                                 const egl::AttributeMap &attribs) const = 0;
127 
128     virtual SwapChainD3D *createSwapChain(NativeWindowD3D *nativeWindow,
129                                           HANDLE shareHandle,
130                                           IUnknown *d3dTexture,
131                                           GLenum backBufferFormat,
132                                           GLenum depthBufferFormat,
133                                           EGLint orientation) = 0;
134     virtual egl::Error getD3DTextureInfo(IUnknown *d3dTexture,
135                                          EGLint *width,
136                                          EGLint *height,
137                                          GLenum *fboFormat) const = 0;
138     virtual egl::Error validateShareHandle(const egl::Config *config,
139                                            HANDLE shareHandle,
140                                            const egl::AttributeMap &attribs) const = 0;
141 
142     virtual gl::Error setSamplerState(gl::SamplerType type, int index, gl::Texture *texture, const gl::SamplerState &sampler) = 0;
143     virtual gl::Error setTexture(gl::SamplerType type, int index, gl::Texture *texture) = 0;
144 
145     virtual gl::Error setUniformBuffers(const gl::ContextState &data,
146                                         const std::vector<GLint> &vertexUniformBuffers,
147                                         const std::vector<GLint> &fragmentUniformBuffers) = 0;
148 
149     virtual gl::Error applyUniforms(const ProgramD3D &programD3D,
150                                     GLenum drawMode,
151                                     const std::vector<D3DUniform *> &uniformArray) = 0;
152 
153     virtual unsigned int getReservedVertexUniformBuffers() const = 0;
154     virtual unsigned int getReservedFragmentUniformBuffers() const = 0;
155 
156     virtual int getMajorShaderModel() const = 0;
157 
158     const WorkaroundsD3D &getWorkarounds() const;
159 
160     // Pixel operations
161     virtual gl::Error copyImage2D(const gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
162                                  const gl::Offset &destOffset, TextureStorage *storage, GLint level) = 0;
163     virtual gl::Error copyImageCube(const gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
164                                     const gl::Offset &destOffset, TextureStorage *storage, GLenum target, GLint level) = 0;
165     virtual gl::Error copyImage3D(const gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
166                                   const gl::Offset &destOffset, TextureStorage *storage, GLint level) = 0;
167     virtual gl::Error copyImage2DArray(const gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
168                                        const gl::Offset &destOffset, TextureStorage *storage, GLint level) = 0;
169 
170     virtual gl::Error copyTexture(const gl::Texture *source,
171                                   GLint sourceLevel,
172                                   const gl::Rectangle &sourceRect,
173                                   GLenum destFormat,
174                                   const gl::Offset &destOffset,
175                                   TextureStorage *storage,
176                                   GLint destLevel,
177                                   bool unpackFlipY,
178                                   bool unpackPremultiplyAlpha,
179                                   bool unpackUnmultiplyAlpha) = 0;
180     virtual gl::Error copyCompressedTexture(const gl::Texture *source,
181                                             GLint sourceLevel,
182                                             TextureStorage *storage,
183                                             GLint destLevel) = 0;
184 
185     // RenderTarget creation
186     virtual gl::Error createRenderTarget(int width, int height, GLenum format, GLsizei samples, RenderTargetD3D **outRT) = 0;
187     virtual gl::Error createRenderTargetCopy(RenderTargetD3D *source, RenderTargetD3D **outRT) = 0;
188 
189     // Shader operations
190     virtual gl::Error loadExecutable(const void *function,
191                                      size_t length,
192                                      ShaderType type,
193                                      const std::vector<D3DVarying> &streamOutVaryings,
194                                      bool separatedOutputBuffers,
195                                      ShaderExecutableD3D **outExecutable) = 0;
196     virtual gl::Error compileToExecutable(gl::InfoLog &infoLog,
197                                           const std::string &shaderHLSL,
198                                           ShaderType type,
199                                           const std::vector<D3DVarying> &streamOutVaryings,
200                                           bool separatedOutputBuffers,
201                                           const D3DCompilerWorkarounds &workarounds,
202                                           ShaderExecutableD3D **outExectuable) = 0;
203     virtual UniformStorageD3D *createUniformStorage(size_t storageSize) = 0;
204 
205     // Image operations
206     virtual ImageD3D *createImage() = 0;
207     virtual gl::Error generateMipmap(ImageD3D *dest, ImageD3D *source) = 0;
208     virtual gl::Error generateMipmapUsingD3D(TextureStorage *storage,
209                                              const gl::TextureState &textureState) = 0;
210     virtual TextureStorage *createTextureStorage2D(SwapChainD3D *swapChain) = 0;
211     virtual TextureStorage *createTextureStorageEGLImage(EGLImageD3D *eglImage,
212                                                          RenderTargetD3D *renderTargetD3D) = 0;
213     virtual TextureStorage *createTextureStorageExternal(
214         egl::Stream *stream,
215         const egl::Stream::GLTextureDescription &desc) = 0;
216     virtual TextureStorage *createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels, bool hintLevelZeroOnly) = 0;
217     virtual TextureStorage *createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels, bool hintLevelZeroOnly) = 0;
218     virtual TextureStorage *createTextureStorage3D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels) = 0;
219     virtual TextureStorage *createTextureStorage2DArray(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels) = 0;
220 
221     // Buffer-to-texture and Texture-to-buffer copies
222     virtual bool supportsFastCopyBufferToTexture(GLenum internalFormat) const = 0;
223     virtual gl::Error fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTargetD3D *destRenderTarget,
224                                               GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea) = 0;
225 
226     // Device lost
227     GLenum getResetStatus();
228     void notifyDeviceLost();
229     virtual bool resetDevice() = 0;
230     virtual bool testDeviceLost()       = 0;
231     virtual bool testDeviceResettable() = 0;
232 
233     virtual RendererClass getRendererClass() const = 0;
234     virtual void *getD3DDevice() = 0;
235 
236     void setGPUDisjoint();
237 
238     GLint getGPUDisjoint();
239     GLint64 getTimestamp();
240 
241     // In D3D11, faster than calling setTexture a jillion times
242     virtual gl::Error clearTextures(gl::SamplerType samplerType, size_t rangeStart, size_t rangeEnd) = 0;
243 
244     virtual egl::Error getEGLDevice(DeviceImpl **device) = 0;
245 
presentPathFastEnabled()246     bool presentPathFastEnabled() const { return mPresentPathFastEnabled; }
247 
248     // Stream creation
249     virtual StreamProducerImpl *createStreamProducerD3DTextureNV12(
250         egl::Stream::ConsumerType consumerType,
251         const egl::AttributeMap &attribs) = 0;
252 
253     const gl::Caps &getNativeCaps() const;
254     const gl::TextureCapsMap &getNativeTextureCaps() const;
255     const gl::Extensions &getNativeExtensions() const;
256     const gl::Limitations &getNativeLimitations() const;
257 
258     // Necessary hack for default framebuffers in D3D.
259     virtual FramebufferImpl *createDefaultFramebuffer(const gl::FramebufferState &state) = 0;
260 
261     virtual gl::Version getMaxSupportedESVersion() const = 0;
262 
263   protected:
264     virtual bool getLUID(LUID *adapterLuid) const = 0;
265     virtual void generateCaps(gl::Caps *outCaps,
266                               gl::TextureCapsMap *outTextureCaps,
267                               gl::Extensions *outExtensions,
268                               gl::Limitations *outLimitations) const = 0;
269 
270     void cleanup();
271 
272     static unsigned int GetBlendSampleMask(const gl::ContextState &data, int samples);
273     // dirtyPointer is a special value that will make the comparison with any valid pointer fail and force the renderer to re-apply the state.
274 
275     gl::Error applyTextures(GLImplFactory *implFactory, const gl::ContextState &data);
276     bool skipDraw(const gl::ContextState &data, GLenum drawMode);
277     gl::Error markTransformFeedbackUsage(const gl::ContextState &data);
278 
279     egl::Display *mDisplay;
280 
281     bool mPresentPathFastEnabled;
282 
283   private:
284     void ensureCapsInitialized() const;
285 
286     typedef std::array<gl::Texture*, gl::IMPLEMENTATION_MAX_FRAMEBUFFER_ATTACHMENTS> FramebufferTextureArray;
287 
288     gl::Error applyTextures(GLImplFactory *implFactory,
289                             const gl::ContextState &data,
290                             gl::SamplerType shaderType,
291                             const FramebufferTextureArray &framebufferTextures,
292                             size_t framebufferTextureCount);
293 
294     size_t getBoundFramebufferTextures(const gl::ContextState &data,
295                                        FramebufferTextureArray *outTextureArray);
296     gl::Texture *getIncompleteTexture(GLImplFactory *implFactory, GLenum type);
297 
298     virtual WorkaroundsD3D generateWorkarounds() const = 0;
299 
300     mutable bool mCapsInitialized;
301     mutable gl::Caps mNativeCaps;
302     mutable gl::TextureCapsMap mNativeTextureCaps;
303     mutable gl::Extensions mNativeExtensions;
304     mutable gl::Limitations mNativeLimitations;
305 
306     gl::TextureMap mIncompleteTextures;
307 
308     mutable bool mWorkaroundsInitialized;
309     mutable WorkaroundsD3D mWorkarounds;
310 
311     bool mDisjoint;
312     bool mDeviceLost;
313 };
314 
315 }  // namespace rx
316 
317 #endif // LIBANGLE_RENDERER_D3D_RENDERERD3D_H_
318