blob: ce0eb02833d576f8b5964365a8f53089521de2d7 [file] [log] [blame]
daniel@transgaming.com2507f412012-10-31 18:46:48 +00001//
2// Copyright (c) 2012 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
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +00007// Renderer9.h: Defines a back-end specific class for the D3D9 renderer.
daniel@transgaming.com2507f412012-10-31 18:46:48 +00008
9#ifndef LIBGLESV2_RENDERER_RENDERER9_H_
10#define LIBGLESV2_RENDERER_RENDERER9_H_
11
12#include <set>
13#include <map>
14#include <vector>
15
16#define GL_APICALL
17#include <GLES2/gl2.h>
18#include <GLES2/gl2ext.h>
19#define EGLAPI
20#include <EGL/egl.h>
21
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +000022#include <d3d9.h>
daniel@transgaming.com2507f412012-10-31 18:46:48 +000023
24#include "common/angleutils.h"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000025#include "libGLESv2/Context.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000026#include "libGLESv2/renderer/ShaderCache.h"
daniel@transgaming.com4a186ed2012-11-28 20:56:15 +000027#include "libGLESv2/renderer/VertexDeclarationCache.h"
daniel@transgaming.com2507f412012-10-31 18:46:48 +000028#include "libGLESv2/renderer/Renderer.h"
daniel@transgaming.com91207b72012-11-28 20:56:43 +000029#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000030
31namespace gl
32{
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000033class D3DConstantTable;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000034class VertexDataManager;
daniel@transgaming.com91207b72012-11-28 20:56:43 +000035class StreamingIndexBuffer;
36struct TranslatedAttribute;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +000037}
daniel@transgaming.com2507f412012-10-31 18:46:48 +000038
daniel@transgaming.com76d3e6e2012-10-31 19:55:33 +000039namespace rx
daniel@transgaming.com2507f412012-10-31 18:46:48 +000040{
41
42class Renderer9 : public Renderer
43{
44 public:
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +000045 Renderer9(egl::Display *display, HDC hDc, bool softwareDevice);
daniel@transgaming.com2507f412012-10-31 18:46:48 +000046 virtual ~Renderer9();
47
daniel@transgaming.comb64ed282012-11-28 20:54:02 +000048 static Renderer9 *makeRenderer9(Renderer *renderer);
49
daniel@transgaming.com2507f412012-10-31 18:46:48 +000050 virtual EGLint initialize();
51 virtual bool resetDevice();
52
53 virtual int generateConfigs(ConfigDesc **configDescList);
54 virtual void deleteConfigs(ConfigDesc *configDescList);
55
daniel@transgaming.com91207b72012-11-28 20:56:43 +000056 void startScene();
57 void endScene();
daniel@transgaming.com2507f412012-10-31 18:46:48 +000058
59 virtual void sync(bool block);
daniel@transgaming.comb9bb2792012-11-28 19:36:49 +000060
61 virtual SwapChain *createSwapChain(HWND window, HANDLE shareHandle, GLenum backBufferFormat, GLenum depthBufferFormat);
62
daniel@transgaming.com2507f412012-10-31 18:46:48 +000063 IDirect3DQuery9* allocateEventQuery();
64 void freeEventQuery(IDirect3DQuery9* query);
65
66 // resource creation
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +000067 IDirect3DVertexShader9 *createVertexShader(const DWORD *function, size_t length);
68 IDirect3DPixelShader9 *createPixelShader(const DWORD *function, size_t length);
69 HRESULT createVertexBuffer(UINT Length, DWORD Usage, IDirect3DVertexBuffer9 **ppVertexBuffer);
70 HRESULT createIndexBuffer(UINT Length, DWORD Usage, D3DFORMAT Format, IDirect3DIndexBuffer9 **ppIndexBuffer);
daniel@transgaming.com2507f412012-10-31 18:46:48 +000071#if 0
72 void *createTexture2D();
73 void *createTextureCube();
daniel@transgaming.com4f0af572012-10-31 19:55:46 +000074 void *createQuery();
daniel@transgaming.com2507f412012-10-31 18:46:48 +000075 void *createIndexBuffer();
76 void *createVertexbuffer();
77
78 // state setup
79 void applyShaders();
80 void applyConstants();
daniel@transgaming.com2507f412012-10-31 18:46:48 +000081#endif
82 virtual void setSamplerState(gl::SamplerType type, int index, const gl::SamplerState &sampler);
83 virtual void setTexture(gl::SamplerType type, int index, gl::Texture *texture);
84
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +000085 virtual void setRasterizerState(const gl::RasterizerState &rasterState);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000086 virtual void setBlendState(const gl::BlendState &blendState, const gl::Color &blendColor,
87 unsigned int sampleMask);
daniel@transgaming.com08c331d2012-11-28 19:38:39 +000088 virtual void setDepthStencilState(const gl::DepthStencilState &depthStencilState, int stencilRef,
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +000089 int stencilBackRef, bool frontFaceCCW);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000090
daniel@transgaming.com04f1b332012-11-28 21:00:40 +000091 virtual void setScissorRectangle(const gl::Rectangle &scissor);
daniel@transgaming.com4c4ce232012-11-28 21:01:40 +000092 virtual bool setViewport(const gl::Rectangle &viewport, float zNear, float zFar, bool ignoreViewport,
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +000093 gl::ProgramBinary *currentProgram, bool forceSetUniforms);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +000094
daniel@transgaming.comae39ee22012-11-28 19:42:02 +000095 virtual bool applyRenderTarget(gl::Framebuffer *frameBuffer);
daniel@transgaming.com5fbf1772012-11-28 20:54:43 +000096 virtual void applyShaders(gl::ProgramBinary *programBinary);
daniel@transgaming.com91207b72012-11-28 20:56:43 +000097 virtual bool applyPrimitiveType(GLenum primitiveType, GLsizei elementCount);
98 virtual GLenum applyVertexBuffer(gl::ProgramBinary *programBinary, gl::VertexAttribute vertexAttributes[], GLint first, GLsizei count, GLsizei instances);
99 virtual GLenum applyIndexBuffer(const GLvoid *indices, gl::Buffer *elementArrayBuffer, GLsizei count, GLenum mode, GLenum type, gl::TranslatedIndexData *indexInfo);
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000100
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000101 virtual void drawArrays(GLenum mode, GLsizei count, GLsizei instances);
daniel@transgaming.com97400dd2012-11-28 20:57:00 +0000102 virtual void drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, gl::Buffer *elementArrayBuffer, const gl::TranslatedIndexData &indexInfo);
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000103
daniel@transgaming.com084a2572012-11-28 20:55:17 +0000104 virtual void clear(const gl::ClearParameters &clearParams, gl::Framebuffer *frameBuffer);
daniel@transgaming.comd084c622012-11-28 19:36:05 +0000105
daniel@transgaming.comc43a6052012-11-28 19:41:51 +0000106 virtual void markAllStateDirty();
107
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000108 // lost device
109 virtual void markDeviceLost();
110 virtual bool isDeviceLost();
111 virtual bool testDeviceLost(bool notify);
112 virtual bool testDeviceResettable();
113
114 // Renderer capabilities
daniel@transgaming.com1d6aff22012-11-28 19:30:42 +0000115 IDirect3DDevice9 *getDevice() { return mDevice; }
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000116 virtual DWORD getAdapterVendor() const;
117 virtual const char *getAdapterDescription() const;
118 virtual GUID getAdapterIdentifier() const;
119
120 virtual bool getDXT1TextureSupport();
121 virtual bool getDXT3TextureSupport();
122 virtual bool getDXT5TextureSupport();
123 virtual bool getEventQuerySupport();
124 virtual bool getFloat32TextureSupport(bool *filtering, bool *renderable);
125 virtual bool getFloat16TextureSupport(bool *filtering, bool *renderable);
126 virtual bool getLuminanceTextureSupport();
127 virtual bool getLuminanceAlphaTextureSupport();
128 virtual bool getVertexTextureSupport() const;
129 virtual bool getNonPower2TextureSupport() const;
130 virtual bool getDepthTextureSupport() const;
131 virtual bool getOcclusionQuerySupport() const;
132 virtual bool getInstancingSupport() const;
133 virtual bool getTextureFilterAnisotropySupport() const;
134 virtual float getTextureMaxAnisotropy() const;
135 virtual bool getShareHandleSupport() const;
136
daniel@transgaming.com9549bea2012-11-28 20:57:23 +0000137 virtual int getMajorShaderModel() const;
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000138 virtual float getMaxPointSize() const;
139 virtual int getMaxTextureWidth() const;
140 virtual int getMaxTextureHeight() const;
141 virtual bool get32BitIndexSupport() const;
142 DWORD getCapsDeclTypes() const; // D3D9_REPLACE
143 virtual int getMinSwapInterval() const;
144 virtual int getMaxSwapInterval() const;
145
146 virtual GLsizei getMaxSupportedSamples() const;
147 int getNearestSupportedSamples(D3DFORMAT format, int requested) const;
daniel@transgaming.coma9571682012-11-28 19:33:08 +0000148
149 D3DFORMAT ConvertTextureInternalFormat(GLint internalformat);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +0000150
daniel@transgaming.coma9c71422012-11-28 20:58:45 +0000151 // Pixel operations
daniel@transgaming.com31b13e12012-11-28 19:34:30 +0000152 virtual bool copyToRenderTarget(TextureStorage2D *dest, TextureStorage2D *source);
153 virtual bool copyToRenderTarget(TextureStorageCubeMap *dest, TextureStorageCubeMap *source);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +0000154
daniel@transgaming.com38380882012-11-28 19:36:39 +0000155 virtual bool copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
156 GLint xoffset, GLint yoffset, TextureStorage2D *storage, GLint level);
157 virtual bool copyImage(gl::Framebuffer *framebuffer, const RECT &sourceRect, GLenum destFormat,
158 GLint xoffset, GLint yoffset, TextureStorageCubeMap *storage, GLenum target, GLint level);
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000159
daniel@transgaming.com6c872172012-11-28 19:39:33 +0000160 virtual bool blitRect(gl::Framebuffer *readTarget, gl::Rectangle *readRect, gl::Framebuffer *drawTarget, gl::Rectangle *drawRect,
161 bool blitRenderTarget, bool blitDepthStencil);
162 virtual void readPixels(gl::Framebuffer *framebuffer, GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type,
163 GLsizei outputPitch, bool packReverseRowOrder, GLint packAlignment, void* pixels);
164
daniel@transgaming.coma9c71422012-11-28 20:58:45 +0000165 // RenderTarget creation
daniel@transgaming.comf2423652012-11-28 20:53:50 +0000166 virtual RenderTarget *createRenderTarget(SwapChain *swapChain, bool depth);
167 virtual RenderTarget *createRenderTarget(int width, int height, GLenum format, GLsizei samples, bool depth);
168
daniel@transgaming.coma9c71422012-11-28 20:58:45 +0000169 // Shader operations
daniel@transgaming.com7b18d0c2012-11-28 21:04:10 +0000170 virtual ShaderExecutable *loadExecutable(const void *function, size_t length, GLenum type, void *data);
daniel@transgaming.coma9c71422012-11-28 20:58:45 +0000171 virtual ShaderExecutable *compileToExecutable(gl::InfoLog &infoLog, const char *shaderHLSL, GLenum type);
172
173 // D3D9-renderer specific methods
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000174 bool boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest);
175
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000176 D3DPOOL getTexturePool(DWORD usage) const;
177
178 private:
179 DISALLOW_COPY_AND_ASSIGN(Renderer9);
180
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000181 void drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex, gl::Buffer *elementArrayBuffer);
182
daniel@transgaming.com222ee082012-11-28 19:31:49 +0000183 void getMultiSampleSupport(D3DFORMAT format, bool *multiSampleArray);
daniel@transgaming.com1d80eee2012-11-28 19:33:31 +0000184 bool copyToRenderTarget(IDirect3DSurface9 *dest, IDirect3DSurface9 *source, bool fromManaged);
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000185
daniel@transgaming.com113f0eb2012-10-31 18:46:58 +0000186 D3DPOOL getBufferPool(DWORD usage) const;
187
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000188 HMODULE mD3d9Module;
189 HDC mDc;
190
191 void initializeDevice();
192 D3DPRESENT_PARAMETERS getDefaultPresentParameters();
193 void releaseDeviceResources();
194
195 UINT mAdapter;
196 D3DDEVTYPE mDeviceType;
daniel@transgaming.com95ffbc12012-10-31 19:55:27 +0000197 bool mSoftwareDevice; // FIXME: Deprecate
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000198 IDirect3D9 *mD3d9; // Always valid after successful initialization.
199 IDirect3D9Ex *mD3d9Ex; // Might be null if D3D9Ex is not supported.
200 IDirect3DDevice9 *mDevice;
201 IDirect3DDevice9Ex *mDeviceEx; // Might be null if D3D9Ex is not supported.
202
daniel@transgaming.comde8a7ff2012-11-28 19:34:13 +0000203 Blit *mBlit;
204
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000205 HWND mDeviceWindow;
206
207 bool mDeviceLost;
208 D3DCAPS9 mDeviceCaps;
209 D3DADAPTER_IDENTIFIER9 mAdapterIdentifier;
210
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000211 D3DPRIMITIVETYPE mPrimitiveType;
212 int mPrimitiveCount;
213 GLsizei mRepeatDraw;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000214
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000215 bool mSceneStarted;
216 bool mSupportsNonPower2Textures;
217 bool mSupportsTextureFilterAnisotropy;
218 int mMinSwapInterval;
219 int mMaxSwapInterval;
220
221 std::map<D3DFORMAT, bool *> mMultiSampleSupport;
222 GLsizei mMaxSupportedSamples;
223
daniel@transgaming.com220e79a2012-11-28 19:42:11 +0000224 // current render target states
225 unsigned int mAppliedRenderTargetSerial;
226 unsigned int mAppliedDepthbufferSerial;
227 unsigned int mAppliedStencilbufferSerial;
228 bool mDepthStencilInitialized;
229 bool mRenderTargetDescInitialized;
230 rx::RenderTarget::Desc mRenderTargetDesc;
daniel@transgaming.com3a0ef482012-11-28 21:01:20 +0000231 unsigned int mCurStencilSize;
daniel@transgaming.com237bc7e2012-11-28 21:01:06 +0000232 unsigned int mCurDepthSize;
daniel@transgaming.com220e79a2012-11-28 19:42:11 +0000233
daniel@transgaming.com0393e5b2012-11-28 20:55:33 +0000234 IDirect3DStateBlock9 *mMaskedClearSavedState;
235
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000236 // previously set render states
237 bool mForceSetDepthStencilState;
238 gl::DepthStencilState mCurDepthStencilState;
daniel@transgaming.com08c331d2012-11-28 19:38:39 +0000239 int mCurStencilRef;
240 int mCurStencilBackRef;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000241 bool mCurFrontFaceCCW;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000242
243 bool mForceSetRasterState;
244 gl::RasterizerState mCurRasterState;
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000245
246 bool mForceSetScissor;
247 gl::Rectangle mCurScissor;
248 unsigned int mCurRenderTargetWidth;
249 unsigned int mCurRenderTargetHeight;
250
daniel@transgaming.com83e80ee2012-11-28 19:40:53 +0000251 bool mForceSetViewport;
252 gl::Rectangle mCurViewport;
253 float mCurNear;
254 float mCurFar;
255
daniel@transgaming.com493d4f82012-11-28 19:35:45 +0000256 bool mForceSetBlendState;
257 gl::BlendState mCurBlendState;
258 gl::Color mCurBlendColor;
259 GLuint mCurSampleMask;
260
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000261 unsigned int mAppliedIBSerial;
262
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000263 // A pool of event queries that are currently unused.
264 std::vector<IDirect3DQuery9*> mEventQueryPool;
265 VertexShaderCache mVertexShaderCache;
266 PixelShaderCache mPixelShaderCache;
daniel@transgaming.com67094ee2012-11-28 20:53:04 +0000267
268 gl::VertexDataManager *mVertexDataManager;
269 VertexDeclarationCache mVertexDeclarationCache;
daniel@transgaming.com91207b72012-11-28 20:56:43 +0000270
271 gl::IndexDataManager *mIndexDataManager;
272 gl::StreamingIndexBuffer *mLineLoopIB;
daniel@transgaming.com2507f412012-10-31 18:46:48 +0000273};
274
275}
276#endif // LIBGLESV2_RENDERER_RENDERER9_H_