2 // Copyright (c) 2012-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 // 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 "libGLESv2/mathutil.h"
14 #include "libGLESv2/renderer/ShaderCache.h"
15 #include "libGLESv2/renderer/d3d9/VertexDeclarationCache.h"
16 #include "libGLESv2/renderer/Renderer.h"
17 #include "libGLESv2/renderer/RenderTarget.h"
26 class VertexDataManager;
27 class IndexDataManager;
28 class StreamingIndexBufferInterface;
29 struct TranslatedAttribute;
31 class Renderer9 : public Renderer
34 Renderer9(egl::Display *display, HDC hDc, bool softwareDevice);
37 static Renderer9 *makeRenderer9(Renderer *renderer);
39 virtual EGLint initialize();
40 virtual bool resetDevice();
42 virtual int generateConfigs(ConfigDesc **configDescList);
43 virtual void deleteConfigs(ConfigDesc *configDescList);
48 virtual void sync(bool block);
50 virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat);
52 IDirect3DQuery9* allocateEventQuery();
53 void freeEventQuery(IDirect3DQuery9* query);
56 IDirect3DVertexShader9 *createVertexShader(const DWORD *function, size_t length);
57 IDirect3DPixelShader9 *createPixelShader(const DWORD *function, size_t length);
58 HRESULT createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer);
59 HRESULT createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer);
61 void *createTexture2D();
62 void *createTextureCube();
64 void *createIndexBuffer();
65 void *createVertexbuffer();
69 void applyConstants();
71 virtual void setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &sampler);
72 virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture);
74 virtual void setRasterizerState(const gl::RasterizerState &rasterState);
75 virtual void setBlendState(gl::Framebuffer *framebuffer, const gl::BlendState &blendState, const gl::Color &blendColor,
76 unsigned int sampleMask);
77 virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
78 int stencilBackRef, bool frontFaceCCW);
80 virtual void setScissorRectangle(const gl::Rectangle &scissor, bool enabled);
81 virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, GLenum drawMode, GLenum frontFace,
84 virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer);
85 virtual void applyShaders(gl::ProgramBinary *programBinary);
86 virtual void applyUniforms(gl::ProgramBinary *programBinary, gl::UniformArray *uniformArray);
87 virtual bool applyPrimitiveType(GLenum primitiveType, GLsizei elementCount);
88 virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances);
89 virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo);
91 virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances);
92 virtual void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const TranslatedIndexData &indexInfo, GLsizei instances);
94 virtual void clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer);
96 virtual void markAllStateDirty();
99 void notifyDeviceLost();
100 virtual bool isDeviceLost();
101 virtual bool testDeviceLost(bool notify);
102 virtual bool testDeviceResettable();
104 // Renderer capabilities
105 IDirect3DDevice9 *getDevice() { return mDevice; }
106 virtual DWORD getAdapterVendor() const;
107 virtual std::string getRendererDescription() const;
108 virtual GUID getAdapterIdentifier() const;
110 virtual bool getBGRATextureSupport() const;
111 virtual bool getDXT1TextureSupport();
112 virtual bool getDXT3TextureSupport();
113 virtual bool getDXT5TextureSupport();
114 virtual bool getEventQuerySupport();
115 virtual bool getFloat32TextureSupport(bool *filtering, bool *renderable);
116 virtual bool getFloat16TextureSupport(bool *filtering, bool *renderable);
117 virtual bool getLuminanceTextureSupport();
118 virtual bool getLuminanceAlphaTextureSupport();
119 virtual unsigned int getMaxVertexTextureImageUnits() const;
120 virtual unsigned int getMaxCombinedTextureImageUnits() const;
121 virtual unsigned int getReservedVertexUniformVectors() const;
122 virtual unsigned int getReservedFragmentUniformVectors() const;
123 virtual unsigned int getMaxVertexUniformVectors() const;
124 virtual unsigned int getMaxFragmentUniformVectors() const;
125 virtual unsigned int getMaxVaryingVectors() const;
126 virtual bool getNonPower2TextureSupport() const;
127 virtual bool getDepthTextureSupport() const;
128 virtual bool getOcclusionQuerySupport() const;
129 virtual bool getInstancingSupport() const;
130 virtual bool getTextureFilterAnisotropySupport() const;
131 virtual float getTextureMaxAnisotropy() const;
132 virtual bool getShareHandleSupport() const;
133 virtual bool getDerivativeInstructionSupport() const;
134 virtual bool getPostSubBufferSupport() const;
136 virtual int getMajorShaderModel() const;
137 virtual float getMaxPointSize() const;
138 virtual int getMaxViewportDimension() const;
139 virtual int getMaxTextureWidth() const;
140 virtual int getMaxTextureHeight() const;
141 virtual bool get32BitIndexSupport() const;
142 DWORD getCapsDeclTypes() const;
143 virtual int getMinSwapInterval() const;
144 virtual int getMaxSwapInterval() const;
146 virtual GLsizei getMaxSupportedSamples() const;
147 int getNearestSupportedSamples(D3DFORMAT format, int requested) const;
149 virtual unsigned int getMaxRenderTargets() const;
151 D3DFORMAT ConvertTextureInternalFormat(GLint internalformat);
154 virtual bool copyToRenderTarget(TextureStorageInterface2D *dest, TextureStorageInterface2D *source);
155 virtual bool copyToRenderTarget(TextureStorageInterfaceCube *dest, TextureStorageInterfaceCube *source);
157 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
158 GLint xoffset, GLint yoffset, TextureStorageInterface2D *storage, GLint level);
159 virtual bool copyImage(gl::Framebuffer *framebuffer, const gl::Rectangle &sourceRect, GLenum destFormat,
160 GLint xoffset, GLint yoffset, TextureStorageInterfaceCube *storage, GLenum target, GLint level);
162 virtual bool blitRect(gl::Framebuffer *readTarget, const gl::Rectangle &readRect, gl::Framebuffer *drawTarget, const gl::Rectangle &drawRect,
163 bool blitRenderTarget, bool blitDepthStencil);
164 virtual void readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
165 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels);
167 // RenderTarget creation
168 virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth);
169 virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth);
172 virtual ShaderExecutable *loadExecutable(const void *function, size_t length, rx::ShaderType type);
173 virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, rx::ShaderType type, D3DWorkaroundType workaround);
176 virtual Image *createImage();
177 virtual void generateMipmap(Image *dest, Image *source);
178 virtual TextureStorage *createTextureStorage2D(SwapChain *swapChain);
179 virtual TextureStorage *createTextureStorage2D(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, GLsizei width, GLsizei height);
180 virtual TextureStorage *createTextureStorageCube(int levels, GLenum internalformat, GLenum usage, bool forceRenderable, int size);
183 virtual VertexBuffer *createVertexBuffer();
184 virtual IndexBuffer *createIndexBuffer();
185 virtual BufferStorage *createBufferStorage();
187 // Query and Fence creation
188 virtual QueryImpl *createQuery(GLenum type);
189 virtual FenceImpl *createFence();
191 // D3D9-renderer specific methods
192 bool boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest);
194 D3DPOOL getTexturePool(DWORD usage) const;
196 virtual bool getLUID(LUID *adapterLuid) const;
199 DISALLOW_COPY_AND_ASSIGN(Renderer9);
203 void applyUniformnfv(gl::Uniform *targetUniform, const GLfloat *v);
204 void applyUniformniv(gl::Uniform *targetUniform, const GLint *v);
205 void applyUniformnbv(gl::Uniform *targetUniform, const GLint *v);
207 void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
208 void drawIndexedPoints(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
210 void getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray);
211 bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged);
212 gl::Renderbuffer *getNullColorbuffer(gl::Renderbuffer *depthbuffer);
214 D3DPOOL getBufferPool(DWORD usage) const;
219 void initializeDevice();
220 D3DPRESENT_PARAMETERS getDefaultPresentParameters();
221 void releaseDeviceResources();
223 HRESULT getDeviceStatusCode();
224 bool isRemovedDeviceResettable() const;
225 bool resetRemovedDevice();
228 D3DDEVTYPE mDeviceType;
229 bool mSoftwareDevice; // FIXME: Deprecate
230 IDirect3D9 *mD3d9; // Always valid after successful initialization.
231 IDirect3D9Ex *mD3d9Ex; // Might be null if D3D9Ex is not supported.
232 IDirect3DDevice9 *mDevice;
233 IDirect3DDevice9Ex *mDeviceEx; // Might be null if D3D9Ex is not supported.
240 D3DCAPS9 mDeviceCaps;
241 D3DADAPTER_IDENTIFIER9 mAdapterIdentifier;
243 D3DPRIMITIVETYPE mPrimitiveType;
248 bool mSupportsNonPower2Textures;
249 bool mSupportsTextureFilterAnisotropy;
250 int mMinSwapInterval;
251 int mMaxSwapInterval;
253 bool mOcclusionQuerySupport;
254 bool mEventQuerySupport;
255 bool mVertexTextureSupport;
257 bool mDepthTextureSupport;
259 bool mFloat32TextureSupport;
260 bool mFloat32FilterSupport;
261 bool mFloat32RenderSupport;
263 bool mFloat16TextureSupport;
264 bool mFloat16FilterSupport;
265 bool mFloat16RenderSupport;
267 bool mDXT1TextureSupport;
268 bool mDXT3TextureSupport;
269 bool mDXT5TextureSupport;
271 bool mLuminanceTextureSupport;
272 bool mLuminanceAlphaTextureSupport;
274 std::map<D3DFORMAT, bool *> mMultiSampleSupport;
275 GLsizei mMaxSupportedSamples;
277 // current render target states
278 unsigned int mAppliedRenderTargetSerial;
279 unsigned int mAppliedDepthbufferSerial;
280 unsigned int mAppliedStencilbufferSerial;
281 bool mDepthStencilInitialized;
282 bool mRenderTargetDescInitialized;
283 rx::RenderTarget::Desc mRenderTargetDesc;
284 unsigned int mCurStencilSize;
285 unsigned int mCurDepthSize;
287 IDirect3DStateBlock9 *mMaskedClearSavedState;
289 // previously set render states
290 bool mForceSetDepthStencilState;
291 gl::DepthStencilState mCurDepthStencilState;
293 int mCurStencilBackRef;
294 bool mCurFrontFaceCCW;
296 bool mForceSetRasterState;
297 gl::RasterizerState mCurRasterState;
299 bool mForceSetScissor;
300 gl::Rectangle mCurScissor;
301 bool mScissorEnabled;
303 bool mForceSetViewport;
304 gl::Rectangle mCurViewport;
307 float mCurDepthFront;
309 bool mForceSetBlendState;
310 gl::BlendState mCurBlendState;
311 gl::Color mCurBlendColor;
312 GLuint mCurSampleMask;
314 // Currently applied sampler states
315 bool mForceSetVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
316 gl::SamplerState mCurVertexSamplerStates[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
318 bool mForceSetPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
319 gl::SamplerState mCurPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
321 // Currently applied textures
322 unsigned int mCurVertexTextureSerials[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
323 unsigned int mCurPixelTextureSerials[gl::MAX_TEXTURE_IMAGE_UNITS];
325 unsigned int mAppliedIBSerial;
326 unsigned int mAppliedProgramBinarySerial;
328 rx::dx_VertexConstants mVertexConstants;
329 rx::dx_PixelConstants mPixelConstants;
330 bool mDxUniformsDirty;
332 // A pool of event queries that are currently unused.
333 std::vector<IDirect3DQuery9*> mEventQueryPool;
334 VertexShaderCache mVertexShaderCache;
335 PixelShaderCache mPixelShaderCache;
337 VertexDataManager *mVertexDataManager;
338 VertexDeclarationCache mVertexDeclarationCache;
340 IndexDataManager *mIndexDataManager;
341 StreamingIndexBufferInterface *mLineLoopIB;
343 enum { NUM_NULL_COLORBUFFER_CACHE_ENTRIES = 12 };
344 struct NullColorbufferCacheEntry
349 gl::Renderbuffer *buffer;
350 } mNullColorbufferCache[NUM_NULL_COLORBUFFER_CACHE_ENTRIES];
351 UINT mMaxNullColorbufferLRU;
356 #endif // LIBGLESV2_RENDERER_RENDERER9_H_