Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / libGLESv2 / renderer / d3d / d3d9 / Renderer9.h
1 //
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.
5 //
6
7 // Renderer9.h: Defines a back-end specific class for the D3D9 renderer.
8
9 #ifndef LIBGLESV2_RENDERER_RENDERER9_H_
10 #define LIBGLESV2_RENDERER_RENDERER9_H_
11
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"
19
20 namespace gl
21 {
22 class FramebufferAttachment;
23 }
24
25 namespace rx
26 {
27 class VertexDataManager;
28 class IndexDataManager;
29 class StreamingIndexBufferInterface;
30 struct TranslatedAttribute;
31 class Blit9;
32
33 class Renderer9 : public Renderer
34 {
35   public:
36     Renderer9(egl::Display *display, EGLNativeDisplayType hDc, EGLint requestedDisplay);
37     virtual ~Renderer9();
38
39     static Renderer9 *makeRenderer9(Renderer *renderer);
40
41     virtual EGLint initialize();
42     virtual bool resetDevice();
43
44     virtual int generateConfigs(ConfigDesc **configDescList);
45     virtual void deleteConfigs(ConfigDesc *configDescList);
46
47     void startScene();
48     void endScene();
49
50     virtual void sync(bool block);
51
52     virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat);
53
54     IDirect3DQuery9* allocateEventQuery();
55     void freeEventQuery(IDirect3DQuery9* query);
56
57     // resource creation
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);
65
66     virtual bool setUniformBuffers(const gl::Buffer *vertexUniformBuffers[], const gl::Buffer *fragmentUniformBuffers[]);
67
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);
73
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,
76                              bool ignoreViewport);
77
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);
86
87     virtual void applyTransformFeedbackBuffers(gl::Buffer *transformFeedbackBuffers[], GLintptr offsets[]);
88
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);
92
93     virtual void clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer);
94
95     virtual void markAllStateDirty();
96
97     // lost device
98     void notifyDeviceLost();
99     virtual bool isDeviceLost();
100     virtual bool testDeviceLost(bool notify);
101     virtual bool testDeviceResettable();
102
103     IDirect3DDevice9 *getDevice() { return mDevice; }
104     virtual DWORD getAdapterVendor() const;
105     virtual std::string getRendererDescription() const;
106     virtual GUID getAdapterIdentifier() const;
107
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;
127
128     virtual int getMajorShaderModel() const;
129     DWORD getCapsDeclTypes() const;
130     virtual int getMinSwapInterval() const;
131     virtual int getMaxSwapInterval() const;
132
133     // Pixel operations
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);
138
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);
147
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);
152
153     // RenderTarget creation
154     virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth);
155     virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples);
156
157     // Shader operations
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);
165
166     // Image operations
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);
174
175     // Texture creation
176     virtual Texture2DImpl *createTexture2D();
177     virtual TextureCubeImpl *createTextureCube();
178     virtual Texture3DImpl *createTexture3D();
179     virtual Texture2DArrayImpl *createTexture2DArray();
180
181     // Buffer creation
182     virtual BufferImpl *createBuffer();
183     virtual VertexBuffer *createVertexBuffer();
184     virtual IndexBuffer *createIndexBuffer();
185
186     // Vertex Array creation
187     virtual VertexArrayImpl *createVertexArray();
188
189     // Query and Fence creation
190     virtual QueryImpl *createQuery(GLenum type);
191     virtual FenceImpl *createFence();
192
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);
197
198     // D3D9-renderer specific methods
199     bool boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest);
200
201     D3DPOOL getTexturePool(DWORD usage) const;
202
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;
206
207   private:
208     DISALLOW_COPY_AND_ASSIGN(Renderer9);
209
210     virtual void generateCaps(gl::Caps *outCaps, gl::TextureCapsMap *outTextureCaps, gl::Extensions *outExtensions) const;
211
212     void release();
213
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);
217
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);
220
221     bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged);
222     gl::FramebufferAttachment *getNullColorbuffer(gl::FramebufferAttachment *depthbuffer);
223
224     D3DPOOL getBufferPool(DWORD usage) const;
225
226     HMODULE mD3d9Module;
227     HDC mDc;
228
229     void initializeDevice();
230     D3DPRESENT_PARAMETERS getDefaultPresentParameters();
231     void releaseDeviceResources();
232
233     HRESULT getDeviceStatusCode();
234     bool isRemovedDeviceResettable() const;
235     bool resetRemovedDevice();
236
237     UINT mAdapter;
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.
243
244     HLSLCompiler mCompiler;
245
246     Blit9 *mBlit;
247
248     HWND mDeviceWindow;
249
250     bool mDeviceLost;
251     D3DCAPS9 mDeviceCaps;
252     D3DADAPTER_IDENTIFIER9 mAdapterIdentifier;
253
254     D3DPRIMITIVETYPE mPrimitiveType;
255     int mPrimitiveCount;
256     GLsizei mRepeatDraw;
257
258     bool mSceneStarted;
259     int mMinSwapInterval;
260     int mMaxSwapInterval;
261
262     bool mVertexTextureSupport;
263
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;
273
274     IDirect3DStateBlock9 *mMaskedClearSavedState;
275
276     // previously set render states
277     bool mForceSetDepthStencilState;
278     gl::DepthStencilState mCurDepthStencilState;
279     int mCurStencilRef;
280     int mCurStencilBackRef;
281     bool mCurFrontFaceCCW;
282
283     bool mForceSetRasterState;
284     gl::RasterizerState mCurRasterState;
285
286     bool mForceSetScissor;
287     gl::Rectangle mCurScissor;
288     bool mScissorEnabled;
289
290     bool mForceSetViewport;
291     gl::Rectangle mCurViewport;
292     float mCurNear;
293     float mCurFar;
294     float mCurDepthFront;
295
296     bool mForceSetBlendState;
297     gl::BlendState mCurBlendState;
298     gl::ColorF mCurBlendColor;
299     GLuint mCurSampleMask;
300
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];
304
305     bool mForceSetPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
306     gl::SamplerState mCurPixelSamplerStates[gl::MAX_TEXTURE_IMAGE_UNITS];
307
308     // Currently applied textures
309     unsigned int mCurVertexTextureSerials[gl::IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
310     unsigned int mCurPixelTextureSerials[gl::MAX_TEXTURE_IMAGE_UNITS];
311
312     unsigned int mAppliedIBSerial;
313     IDirect3DVertexShader9 *mAppliedVertexShader;
314     IDirect3DPixelShader9 *mAppliedPixelShader;
315     unsigned int mAppliedProgramSerial;
316
317     rx::dx_VertexConstants mVertexConstants;
318     rx::dx_PixelConstants mPixelConstants;
319     bool mDxUniformsDirty;
320
321     // A pool of event queries that are currently unused.
322     std::vector<IDirect3DQuery9*> mEventQueryPool;
323     VertexShaderCache mVertexShaderCache;
324     PixelShaderCache mPixelShaderCache;
325
326     VertexDataManager *mVertexDataManager;
327     VertexDeclarationCache mVertexDeclarationCache;
328
329     IndexDataManager *mIndexDataManager;
330     StreamingIndexBufferInterface *mLineLoopIB;
331
332     enum { NUM_NULL_COLORBUFFER_CACHE_ENTRIES = 12 };
333     struct NullColorbufferCacheEntry
334     {
335         UINT lruCount;
336         int width;
337         int height;
338         gl::FramebufferAttachment *buffer;
339     } mNullColorbufferCache[NUM_NULL_COLORBUFFER_CACHE_ENTRIES];
340     UINT mMaxNullColorbufferLRU;
341
342 };
343
344 }
345 #endif // LIBGLESV2_RENDERER_RENDERER9_H_