2 // Copyright (c) 2012-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.
7 // Renderer9.h: Defines a back-end specific class for the D3D9 renderer.
9 #ifndef LIBGLESV2_RENDERER_RENDERER9_H_
10 #define LIBGLESV2_RENDERER_RENDERER9_H_
12 #include "common/angleutils.h"
13 #include "common/mathutil.h"
14 #include "libGLESv2/renderer/d3d/HLSLCompiler.h"
15 #include "libGLESv2/renderer/d3d/d3d9/ShaderCache.h"
16 #include "libGLESv2/renderer/d3d/d3d9/VertexDeclarationCache.h"
17 #include "libGLESv2/renderer/Renderer.h"
18 #include "libGLESv2/renderer/RenderTarget.h"
22 class FramebufferAttachment;
27 class VertexDataManager;
28 class IndexDataManager;
29 class StreamingIndexBufferInterface;
30 struct TranslatedAttribute;
33 class Renderer9 : public Renderer
36 Renderer9(egl::Display *display, EGLNativeDisplayType hDc, EGLint requestedDisplay);
39 static Renderer9 *makeRenderer9(Renderer *renderer);
41 virtual EGLint initialize();
42 virtual bool resetDevice();
44 virtual int generateConfigs(ConfigDesc **configDescList);
45 virtual void deleteConfigs(ConfigDesc *configDescList);
50 virtual void sync(bool block);
52 virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat);
54 IDirect3DQuery9* allocateEventQuery();
55 void freeEventQuery(IDirect3DQuery9* query);
58 IDirect3DVertexShader9 *createVertexShader(const DWORD *function, size_t length);
59 IDirect3DPixelShader9 *createPixelShader(const DWORD *function, size_t length);
60 HRESULT createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer);
61 HRESULT createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer);
62 virtual void generateSwizzle(gl::Texture *texture);
63 virtual void setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &sampler);
64 virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture);
66 virtual bool setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[]);
68 virtual void setRasterizerState(const gl::RasterizerState &rasterState);
69 virtual void setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::ColorF &blendColor,
70 unsigned int sampleMask);
71 virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
72 int stencilBackRef, bool frontFaceCCW);
74 virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled);
75 virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
78 virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer);
79 virtual void applyShaders(gl::ProgramBinary *programBinary, const gl::VertexFormat inputLayout[], const gl::Framebuffer *framebuffer,
80 bool rasterizerDiscard, bool transformFeedbackActive);
81 virtual void applyUniforms(const gl::ProgramBinary &programBinary);
82 virtual bool applyPrimitiveType(GLenum primitiveType, GLsizei elementCount);
83 virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, const gl::VertexAttribute vertexAttributes[], const gl::VertexAttribCurrentValueData currentValues[],
84 GLint first, GLsizei count, GLsizei instances);
85 virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
87 virtual void applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[]);
89 virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances, bool transformFeedbackActive);
90 virtual void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices,
91 gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances);
93 virtual void clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer);
95 virtual void markAllStateDirty();
98 void notifyDeviceLost();
99 virtual bool isDeviceLost();
100 virtual bool testDeviceLost(bool notify);
101 virtual bool testDeviceResettable();
103 IDirect3DDevice9 *getDevice() { return mDevice; }
104 virtual DWORD getAdapterVendor() const;
105 virtual std::string getRendererDescription() const;
106 virtual GUID getAdapterIdentifier() const;
108 virtual unsigned int getMaxVertexTextureImageUnits() const;
109 virtual unsigned int getMaxCombinedTextureImageUnits() const;
110 virtual unsigned int getReservedVertexUniformVectors() const;
111 virtual unsigned int getReservedFragmentUniformVectors() const;
112 virtual unsigned int getMaxVertexUniformVectors() const;
113 virtual unsigned int getMaxFragmentUniformVectors() const;
114 virtual unsigned int getMaxVaryingVectors() const;
115 virtual unsigned int getMaxVertexShaderUniformBuffers() const;
116 virtual unsigned int getMaxFragmentShaderUniformBuffers() const;
117 virtual unsigned int getReservedVertexUniformBuffers() const;
118 virtual unsigned int getReservedFragmentUniformBuffers() const;
119 virtual unsigned int getMaxTransformFeedbackBuffers() const;
120 virtual unsigned int getMaxTransformFeedbackSeparateComponents() const;
121 virtual unsigned int getMaxTransformFeedbackInterleavedComponents() const;
122 virtual unsigned int getMaxUniformBufferSize() const;
123 virtual bool getShareHandleSupport() const;
124 virtual bool getPostSubBufferSupport() const;
125 virtual int getMaxRecommendedElementsIndices() const;
126 virtual int getMaxRecommendedElementsVertices() const;
128 virtual int getMajorShaderModel() const;
129 DWORD getCapsDeclTypes() const;
130 virtual int getMinSwapInterval() const;
131 virtual int getMaxSwapInterval() const;
134 virtual bool copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source);
135 virtual bool copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source);
136 virtual bool copyToRenderTarget(TextureStorageInterface3D *dest, TextureStorageInterface3D *source);
137 virtual bool copyToRenderTarget(TextureStorageInterface2DArray *dest, TextureStorageInterface2DArray *source);
139 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
140 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level);
141 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
142 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level);
143 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
144 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface3D *storage, GLint level);
145 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
146 GLint xoffset, GLint yoffset, GLint zOffset, TextureStorageInterface2DArray *storage, GLint level);
148 virtual bool blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
149 const gl::Rectangle *scissor, bool blitRenderTarget, bool blitDepth, bool blitStencil, GLenum filter);
150 virtual void readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format,
151 GLenum type, GLuint outputPitch, const gl::PixelPackState &pack, void* pixels);
153 // RenderTarget creation
154 virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth);
155 virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples);
158 virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type,
159 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
160 bool separatedOutputBuffers);
161 virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type,
162 const std::vector<gl::LinkedVarying> &transformFeedbackVaryings,
163 bool separatedOutputBuffers, D3DWorkaroundType workaround);
164 virtual UniformStorage *createUniformStorage(size_t storageSize);
167 virtual Image *createImage();
168 virtual void generateMipmap(Image *dest, Image *source);
169 virtual TextureStorage *createTextureStorage2D(SwapChain *swapChain);
170 virtual TextureStorage *createTextureStorage2D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, int levels);
171 virtual TextureStorage *createTextureStorageCube(GLenum internalformat, bool renderTarget, int size, int levels);
172 virtual TextureStorage *createTextureStorage3D(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels);
173 virtual TextureStorage *createTextureStorage2DArray(GLenum internalformat, bool renderTarget, GLsizei width, GLsizei height, GLsizei depth, int levels);
176 virtual Texture2DImpl *createTexture2D();
177 virtual TextureCubeImpl *createTextureCube();
178 virtual Texture3DImpl *createTexture3D();
179 virtual Texture2DArrayImpl *createTexture2DArray();
182 virtual BufferImpl *createBuffer();
183 virtual VertexBuffer *createVertexBuffer();
184 virtual IndexBuffer *createIndexBuffer();
186 // Vertex Array creation
187 virtual VertexArrayImpl *createVertexArray();
189 // Query and Fence creation
190 virtual QueryImpl *createQuery(GLenum type);
191 virtual FenceImpl *createFence();
193 // Buffer-to-texture and Texture-to-buffer copies
194 virtual bool supportsFastCopyBufferToTexture(GLenum internalFormat) const;
195 virtual bool fastCopyBufferToTexture(const gl::PixelUnpackState &unpack, unsigned int offset, RenderTarget *destRenderTarget,
196 GLenum destinationFormat, GLenum sourcePixelsType, const gl::Box &destArea);
198 // D3D9-renderer specific methods
199 bool boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest);
201 D3DPOOL getTexturePool(DWORD usage) const;
203 virtual bool getLUID(LUID *adapterLuid) const;
204 virtual rx::VertexConversionType getVertexConversionType(const gl::VertexFormat &vertexFormat) const;
205 virtual GLenum getVertexComponentType(const gl::VertexFormat &vertexFormat) const;
208 DISALLOW_COPY_AND_ASSIGN(Renderer9);
210 virtual void generateCaps(gl::Caps *outCaps, gl::TextureCapsMap *outTextureCaps, gl::Extensions *outExtensions) const;
214 void applyUniformnfv(gl::LinkedUniform *targetUniform, const GLfloat *v);
215 void applyUniformniv(gl::LinkedUniform *targetUniform, const GLint *v);
216 void applyUniformnbv(gl::LinkedUniform *targetUniform, const GLint *v);
218 void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
219 void drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
221 bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged);
222 gl::FramebufferAttachment *getNullColorbuffer(gl::FramebufferAttachment *depthbuffer);
224 D3DPOOL getBufferPool(DWORD usage) const;
229 void initializeDevice();
230 D3DPRESENT_PARAMETERS getDefaultPresentParameters();
231 void releaseDeviceResources();
233 HRESULT getDeviceStatusCode();
234 bool isRemovedDeviceResettable() const;
235 bool resetRemovedDevice();
238 D3DDEVTYPE mDeviceType;
239 IDirect3D9 *mD3d9; // Always valid after successful initialization.
240 IDirect3D9Ex *mD3d9Ex; // Might be null if D3D9Ex is not supported.
241 IDirect3DDevice9 *mDevice;
242 IDirect3DDevice9Ex *mDeviceEx; // Might be null if D3D9Ex is not supported.
244 HLSLCompiler mCompiler;
251 D3DCAPS9 mDeviceCaps;
252 D3DADAPTER_IDENTIFIER9 mAdapterIdentifier;
254 D3DPRIMITIVETYPE mPrimitiveType;
259 int mMinSwapInterval;
260 int mMaxSwapInterval;
262 bool mVertexTextureSupport;
264 // current render target states
265 unsigned int mAppliedRenderTargetSerial;
266 unsigned int mAppliedDepthbufferSerial;
267 unsigned int mAppliedStencilbufferSerial;
268 bool mDepthStencilInitialized;
269 bool mRenderTargetDescInitialized;
270 rx::RenderTarget::Desc mRenderTargetDesc;
271 unsigned int mCurStencilSize;
272 unsigned int mCurDepthSize;
274 IDirect3DStateBlock9 *mMaskedClearSavedState;
276 // previously set render states
277 bool mForceSetDepthStencilState;
278 gl::DepthStencilState mCurDepthStencilState;
280 int mCurStencilBackRef;
281 bool mCurFrontFaceCCW;
283 bool mForceSetRasterState;
284 gl::RasterizerState mCurRasterState;
286 bool mForceSetScissor;
287 gl::Rectangle mCurScissor;
288 bool mScissorEnabled;
290 bool mForceSetViewport;
291 gl::Rectangle mCurViewport;
294 float mCurDepthFront;
296 bool mForceSetBlendState;
297 gl::BlendState mCurBlendState;
298 gl::ColorF mCurBlendColor;
299 GLuint mCurSampleMask;
301 // Currently applied sampler states
302 bool mForceSetVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
303 gl::SamplerState mCurVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
305 bool mForceSetPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
306 gl::SamplerState mCurPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
308 // Currently applied textures
309 unsigned int mCurVertexTextureSerials[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
310 unsigned int mCurPixelTextureSerials[gl::MAX_TEXTURE_IMAGE_UNITS];
312 unsigned int mAppliedIBSerial;
313 IDirect3DVertexShader9 *mAppliedVertexShader;
314 IDirect3DPixelShader9 *mAppliedPixelShader;
315 unsigned int mAppliedProgramSerial;
317 rx::dx_VertexConstants mVertexConstants;
318 rx::dx_PixelConstants mPixelConstants;
319 bool mDxUniformsDirty;
321 // A pool of event queries that are currently unused.
322 std::vector<IDirect3DQuery9*> mEventQueryPool;
323 VertexShaderCache mVertexShaderCache;
324 PixelShaderCache mPixelShaderCache;
326 VertexDataManager *mVertexDataManager;
327 VertexDeclarationCache mVertexDeclarationCache;
329 IndexDataManager *mIndexDataManager;
330 StreamingIndexBufferInterface *mLineLoopIB;
332 enum { NUM_NULL_COLORBUFFER_CACHE_ENTRIES = 12 };
333 struct NullColorbufferCacheEntry
338 gl::FramebufferAttachment *buffer;
339 } mNullColorbufferCache[NUM_NULL_COLORBUFFER_CACHE_ENTRIES];
340 UINT mMaxNullColorbufferLRU;
345 #endif // LIBGLESV2_RENDERER_RENDERER9_H_