]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/COGLES2Driver.h
Drop IrrCompileConfig (#163)
[irrlicht.git] / source / Irrlicht / COGLES2Driver.h
1 // Copyright (C) 2014 Patryk Nadrowski\r
2 // Copyright (C) 2009-2010 Amundis\r
3 // This file is part of the "Irrlicht Engine".\r
4 // For conditions of distribution and use, see copyright notice in Irrlicht.h\r
5 \r
6 #ifndef __C_OGLES2_DRIVER_H_INCLUDED__\r
7 #define __C_OGLES2_DRIVER_H_INCLUDED__\r
8 \r
9 \r
10 #include "SIrrCreationParameters.h"\r
11 \r
12 #ifdef _IRR_COMPILE_WITH_OGLES2_\r
13 \r
14 #include "CNullDriver.h"\r
15 #include "IMaterialRendererServices.h"\r
16 #include "EDriverFeatures.h"\r
17 #include "fast_atof.h"\r
18 #include "COGLES2ExtensionHandler.h"\r
19 #include "IContextManager.h"\r
20 \r
21 namespace irr\r
22 {\r
23 namespace video\r
24 {\r
25 \r
26         class COGLES2FixedPipelineRenderer;\r
27         class COGLES2Renderer2D;\r
28 \r
29         class COGLES2Driver : public CNullDriver, public IMaterialRendererServices, public COGLES2ExtensionHandler\r
30         {\r
31                 friend class COpenGLCoreTexture<COGLES2Driver>;\r
32                 friend IVideoDriver* createOGLES2Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);\r
33 \r
34         protected:\r
35                 //! constructor (use createOGLES2Driver instead)\r
36                 COGLES2Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);\r
37 \r
38         public:\r
39 \r
40                 //! destructor\r
41                 virtual ~COGLES2Driver();\r
42 \r
43                 virtual bool beginScene(u16 clearFlag, SColor clearColor = SColor(255, 0, 0, 0), f32 clearDepth = 1.f, u8 clearStencil = 0,\r
44                         const SExposedVideoData& videoData = SExposedVideoData(), core::rect<s32>* sourceRect = 0) override;\r
45 \r
46                 bool endScene() override;\r
47 \r
48                 //! sets transformation\r
49                 void setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat) override;\r
50 \r
51                 struct SHWBufferLink_opengl : public SHWBufferLink\r
52                 {\r
53                         SHWBufferLink_opengl(const scene::IMeshBuffer *meshBuffer)\r
54                         : SHWBufferLink(meshBuffer), vbo_verticesID(0), vbo_indicesID(0)\r
55                         , vbo_verticesSize(0), vbo_indicesSize(0)\r
56                         {}\r
57 \r
58                         u32 vbo_verticesID; //tmp\r
59                         u32 vbo_indicesID; //tmp\r
60 \r
61                         u32 vbo_verticesSize; //tmp\r
62                         u32 vbo_indicesSize; //tmp\r
63                 };\r
64 \r
65                 bool updateVertexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);\r
66                 bool updateIndexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);\r
67 \r
68                 //! updates hardware buffer if needed\r
69                 bool updateHardwareBuffer(SHWBufferLink *HWBuffer) override;\r
70 \r
71                 //! Create hardware buffer from mesh\r
72                 SHWBufferLink *createHardwareBuffer(const scene::IMeshBuffer* mb) override;\r
73 \r
74                 //! Delete hardware buffer (only some drivers can)\r
75                 void deleteHardwareBuffer(SHWBufferLink *HWBuffer) override;\r
76 \r
77                 //! Draw hardware buffer\r
78                 void drawHardwareBuffer(SHWBufferLink *HWBuffer) override;\r
79 \r
80                 IRenderTarget* addRenderTarget() override;\r
81 \r
82                 //! draws a vertex primitive list\r
83                 virtual void drawVertexPrimitiveList(const void* vertices, u32 vertexCount,\r
84                                 const void* indexList, u32 primitiveCount,\r
85                                 E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType) override;\r
86 \r
87                 //! queries the features of the driver, returns true if feature is available\r
88                 bool queryFeature(E_VIDEO_DRIVER_FEATURE feature) const override\r
89                 {\r
90                         return FeatureEnabled[feature] && COGLES2ExtensionHandler::queryFeature(feature);\r
91                 }\r
92 \r
93                 //! Sets a material.\r
94                 void setMaterial(const SMaterial& material) override;\r
95 \r
96                 virtual void draw2DImage(const video::ITexture* texture,\r
97                                 const core::position2d<s32>& destPos,\r
98                                 const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,\r
99                                 SColor color = SColor(255, 255, 255, 255), bool useAlphaChannelOfTexture = false) override;\r
100 \r
101                 virtual void draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,\r
102                         const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect = 0,\r
103                         const video::SColor* const colors = 0, bool useAlphaChannelOfTexture = false) override;\r
104 \r
105                 // internally used\r
106                 virtual void draw2DImage(const video::ITexture* texture, u32 layer, bool flip);\r
107 \r
108                 //! draws a set of 2d images\r
109                 virtual void draw2DImageBatch(const video::ITexture* texture,\r
110                                 const core::position2d<s32>& pos,\r
111                                 const core::array<core::rect<s32> >& sourceRects,\r
112                                 const core::array<s32>& indices, s32 kerningWidth = 0,\r
113                                 const core::rect<s32>* clipRect = 0,\r
114                                 SColor color = SColor(255, 255, 255, 255),\r
115                                 bool useAlphaChannelOfTexture = false) override;\r
116 \r
117                 void draw2DImageBatch(const video::ITexture* texture,\r
118                                 const core::array<core::position2d<s32> >& positions,\r
119                                 const core::array<core::rect<s32> >& sourceRects,\r
120                                 const core::rect<s32>* clipRect,\r
121                                 SColor color,\r
122                                 bool useAlphaChannelOfTexture) override;\r
123 \r
124                 //! draw an 2d rectangle\r
125                 virtual void draw2DRectangle(SColor color, const core::rect<s32>& pos,\r
126                                 const core::rect<s32>* clip = 0) override;\r
127 \r
128                 //!Draws an 2d rectangle with a gradient.\r
129                 virtual void draw2DRectangle(const core::rect<s32>& pos,\r
130                                 SColor colorLeftUp, SColor colorRightUp, SColor colorLeftDown, SColor colorRightDown,\r
131                                 const core::rect<s32>* clip = 0) override;\r
132 \r
133                 //! Draws a 2d line.\r
134                 virtual void draw2DLine(const core::position2d<s32>& start,\r
135                                 const core::position2d<s32>& end,\r
136                                 SColor color = SColor(255, 255, 255, 255)) override;\r
137 \r
138                 //! Draws a single pixel\r
139                 void drawPixel(u32 x, u32 y, const SColor & color) override;\r
140 \r
141                 //! Draws a 3d line.\r
142                 virtual void draw3DLine(const core::vector3df& start,\r
143                                 const core::vector3df& end,\r
144                                 SColor color = SColor(255, 255, 255, 255)) override;\r
145 \r
146                 //! Draws a pixel\r
147 //                      virtual void drawPixel(u32 x, u32 y, const SColor & color);\r
148 \r
149                 //! Returns the name of the video driver.\r
150                 const wchar_t* getName() const override;\r
151 \r
152                 //! Returns the maximum texture size supported.\r
153                 core::dimension2du getMaxTextureSize() const override;\r
154 \r
155                 //! Draws a shadow volume into the stencil buffer.\r
156                 void drawStencilShadowVolume(const core::array<core::vector3df>& triangles, bool zfail, u32 debugDataVisible=0) override;\r
157 \r
158                 //! Fills the stencil shadow with color.\r
159                 virtual void drawStencilShadow(bool clearStencilBuffer=false,\r
160                         video::SColor leftUpEdge = video::SColor(0,0,0,0),\r
161                         video::SColor rightUpEdge = video::SColor(0,0,0,0),\r
162                         video::SColor leftDownEdge = video::SColor(0,0,0,0),\r
163                         video::SColor rightDownEdge = video::SColor(0,0,0,0)) override;\r
164 \r
165                 //! sets a viewport\r
166                 void setViewPort(const core::rect<s32>& area) override;\r
167 \r
168                 //! Only used internally by the engine\r
169                 void OnResize(const core::dimension2d<u32>& size) override;\r
170 \r
171                 //! Returns type of video driver\r
172                 E_DRIVER_TYPE getDriverType() const override;\r
173 \r
174                 //! get color format of the current color buffer\r
175                 ECOLOR_FORMAT getColorFormat() const override;\r
176 \r
177                 //! Returns the transformation set by setTransform\r
178                 const core::matrix4& getTransform(E_TRANSFORMATION_STATE state) const override;\r
179 \r
180                 //! Can be called by an IMaterialRenderer to make its work easier.\r
181                 void setBasicRenderStates(const SMaterial& material, const SMaterial& lastmaterial, bool resetAllRenderstates) override;\r
182 \r
183                 //! Compare in SMaterial doesn't check texture parameters, so we should call this on each OnRender call.\r
184                 void setTextureRenderStates(const SMaterial& material, bool resetAllRenderstates);\r
185 \r
186                 //! Get a vertex shader constant index.\r
187                 s32 getVertexShaderConstantID(const c8* name) override;\r
188 \r
189                 //! Get a pixel shader constant index.\r
190                 s32 getPixelShaderConstantID(const c8* name) override;\r
191 \r
192                 //! Sets a vertex shader constant.\r
193                 void setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount = 1) override;\r
194 \r
195                 //! Sets a pixel shader constant.\r
196                 void setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount = 1) override;\r
197 \r
198                 //! Sets a constant for the vertex shader based on an index.\r
199                 bool setVertexShaderConstant(s32 index, const f32* floats, int count) override;\r
200 \r
201                 //! Int interface for the above.\r
202                 bool setVertexShaderConstant(s32 index, const s32* ints, int count) override;\r
203 \r
204                 //! Uint interface for the above.\r
205                 bool setVertexShaderConstant(s32 index, const u32* ints, int count) override;\r
206 \r
207                 //! Sets a constant for the pixel shader based on an index.\r
208                 bool setPixelShaderConstant(s32 index, const f32* floats, int count) override;\r
209 \r
210                 //! Int interface for the above.\r
211                 bool setPixelShaderConstant(s32 index, const s32* ints, int count) override;\r
212 \r
213                 //! Uint interface for the above.\r
214                 bool setPixelShaderConstant(s32 index, const u32* ints, int count) override;\r
215 \r
216                 //! Adds a new material renderer to the VideoDriver\r
217                 virtual s32 addShaderMaterial(const c8* vertexShaderProgram, const c8* pixelShaderProgram,\r
218                                 IShaderConstantSetCallBack* callback, E_MATERIAL_TYPE baseMaterial, s32 userData) override;\r
219 \r
220                 //! Adds a new material renderer to the VideoDriver\r
221                 virtual s32 addHighLevelShaderMaterial(\r
222                                 const c8* vertexShaderProgram,\r
223                                 const c8* vertexShaderEntryPointName = 0,\r
224                                 E_VERTEX_SHADER_TYPE vsCompileTarget = EVST_VS_1_1,\r
225                                 const c8* pixelShaderProgram = 0,\r
226                                 const c8* pixelShaderEntryPointName = 0,\r
227                                 E_PIXEL_SHADER_TYPE psCompileTarget = EPST_PS_1_1,\r
228                                 const c8* geometryShaderProgram = 0,\r
229                                 const c8* geometryShaderEntryPointName = "main",\r
230                                 E_GEOMETRY_SHADER_TYPE gsCompileTarget = EGST_GS_4_0,\r
231                                 scene::E_PRIMITIVE_TYPE inType = scene::EPT_TRIANGLES,\r
232                                 scene::E_PRIMITIVE_TYPE outType = scene::EPT_TRIANGLE_STRIP,\r
233                                 u32 verticesOut = 0,\r
234                                 IShaderConstantSetCallBack* callback = 0,\r
235                                 E_MATERIAL_TYPE baseMaterial = video::EMT_SOLID,\r
236                                 s32 userData=0) override;\r
237 \r
238                 //! Returns pointer to the IGPUProgrammingServices interface.\r
239                 IGPUProgrammingServices* getGPUProgrammingServices() override;\r
240 \r
241                 //! Returns a pointer to the IVideoDriver interface.\r
242                 IVideoDriver* getVideoDriver() override;\r
243 \r
244                 //! Returns the maximum amount of primitives\r
245                 u32 getMaximalPrimitiveCount() const override;\r
246 \r
247                 virtual ITexture* addRenderTargetTexture(const core::dimension2d<u32>& size,\r
248                                 const io::path& name, const ECOLOR_FORMAT format = ECF_UNKNOWN) override;\r
249 \r
250                 //! Creates a render target texture for a cubemap\r
251                 ITexture* addRenderTargetTextureCubemap(const irr::u32 sideLen,\r
252                                 const io::path& name, const ECOLOR_FORMAT format) override;\r
253 \r
254                 virtual bool setRenderTargetEx(IRenderTarget* target, u16 clearFlag, SColor clearColor = SColor(255, 0, 0, 0),\r
255                         f32 clearDepth = 1.f, u8 clearStencil = 0) override;\r
256 \r
257                 void clearBuffers(u16 flag, SColor color = SColor(255, 0, 0, 0), f32 depth = 1.f, u8 stencil = 0) override;\r
258 \r
259                 //! Returns an image created from the last rendered frame.\r
260                 IImage* createScreenShot(video::ECOLOR_FORMAT format=video::ECF_UNKNOWN, video::E_RENDER_TARGET target=video::ERT_FRAME_BUFFER) override;\r
261 \r
262                 //! checks if an OpenGL error has happened and prints it (+ some internal code which is usually the line number)\r
263                 bool testGLError(int code=0);\r
264 \r
265                 //! checks if an OGLES1 error has happened and prints it\r
266                 bool testEGLError();\r
267 \r
268                 //! Set/unset a clipping plane.\r
269                 bool setClipPlane(u32 index, const core::plane3df& plane, bool enable = false) override;\r
270 \r
271                 //! returns the current amount of user clip planes set.\r
272                 u32 getClipPlaneCount() const;\r
273 \r
274                 //! returns the 0 indexed Plane\r
275                 const core::plane3df& getClipPlane(u32 index) const;\r
276 \r
277                 //! Enable/disable a clipping plane.\r
278                 void enableClipPlane(u32 index, bool enable) override;\r
279 \r
280                 //! Returns the graphics card vendor name.\r
281                 core::stringc getVendorInfo() override\r
282                 {\r
283                         return VendorName;\r
284                 };\r
285 \r
286                 void removeTexture(ITexture* texture) override;\r
287 \r
288                 //! Check if the driver supports creating textures with the given color format\r
289                 bool queryTextureFormat(ECOLOR_FORMAT format) const override;\r
290 \r
291                 //! Used by some SceneNodes to check if a material should be rendered in the transparent render pass\r
292                 bool needsTransparentRenderPass(const irr::video::SMaterial& material) const override;\r
293 \r
294                 //! Convert E_BLEND_FACTOR to OpenGL equivalent\r
295                 GLenum getGLBlend(E_BLEND_FACTOR factor) const;\r
296 \r
297                 //! Get ZBuffer bits.\r
298                 virtual GLenum getZBufferBits() const;\r
299 \r
300                 virtual bool getColorFormatParameters(ECOLOR_FORMAT format, GLint& internalFormat, GLenum& pixelFormat,\r
301                         GLenum& pixelType, void(**converter)(const void*, s32, void*)) const;\r
302 \r
303                 //! Get current material.\r
304                 const SMaterial& getCurrentMaterial() const;\r
305 \r
306                 COGLES2CacheHandler* getCacheHandler() const;\r
307 \r
308         protected:\r
309                 //! inits the opengl-es driver\r
310                 virtual bool genericDriverInit(const core::dimension2d<u32>& screenSize, bool stencilBuffer);\r
311 \r
312                 void chooseMaterial2D();\r
313 \r
314                 ITexture* createDeviceDependentTexture(const io::path& name, IImage* image) override;\r
315 \r
316                 ITexture* createDeviceDependentTextureCubemap(const io::path& name, const core::array<IImage*>& image) override;\r
317 \r
318                 //! Map Irrlicht wrap mode to OpenGL enum\r
319                 GLint getTextureWrapMode(u8 clamp) const;\r
320 \r
321                 //! sets the needed renderstates\r
322                 void setRenderStates3DMode();\r
323 \r
324                 //! sets the needed renderstates\r
325                 void setRenderStates2DMode(bool alpha, bool texture, bool alphaChannel);\r
326 \r
327                 //! Prevent setRenderStateMode calls to do anything.\r
328                 // hack to allow drawing meshbuffers in 2D mode.\r
329                 // Better solution would be passing this flag through meshbuffers,\r
330                 // but the way this is currently implemented in Irrlicht makes this tricky to implement\r
331                 void lockRenderStateMode()\r
332                 {\r
333                         LockRenderStateMode = true;\r
334                 }\r
335 \r
336                 //! Allow setRenderStateMode calls to work again\r
337                 void unlockRenderStateMode()\r
338                 {\r
339                         LockRenderStateMode = false;\r
340                 }\r
341 \r
342                 void draw2D3DVertexPrimitiveList(const void* vertices,\r
343                                 u32 vertexCount, const void* indexList, u32 primitiveCount,\r
344                                 E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType,\r
345                                 E_INDEX_TYPE iType, bool is3D);\r
346 \r
347                 void createMaterialRenderers();\r
348 \r
349                 void loadShaderData(const io::path& vertexShaderName, const io::path& fragmentShaderName, c8** vertexShaderData, c8** fragmentShaderData);\r
350 \r
351                 bool setMaterialTexture(irr::u32 layerIdx, const irr::video::ITexture* texture);\r
352 \r
353                 //! Same as `CacheHandler->setViewport`, but also sets `ViewPort`\r
354                 virtual void setViewPortRaw(u32 width, u32 height);\r
355 \r
356                 COGLES2CacheHandler* CacheHandler;\r
357                 core::stringw Name;\r
358                 core::stringc VendorName;\r
359                 SIrrlichtCreationParameters Params;\r
360 \r
361                 //! bool to make all renderstates reset if set to true.\r
362                 bool ResetRenderStates;\r
363                 bool LockRenderStateMode;\r
364                 u8 AntiAlias;\r
365 \r
366                 struct SUserClipPlane\r
367                 {\r
368                         core::plane3df Plane;\r
369                         bool Enabled;\r
370                 };\r
371 \r
372                 core::array<SUserClipPlane> UserClipPlane;\r
373 \r
374                 core::matrix4 TextureFlipMatrix;\r
375 \r
376 private:\r
377 \r
378                 COGLES2Renderer2D* MaterialRenderer2DActive;\r
379                 COGLES2Renderer2D* MaterialRenderer2DTexture;\r
380                 COGLES2Renderer2D* MaterialRenderer2DNoTexture;\r
381 \r
382                 core::matrix4 Matrices[ETS_COUNT];\r
383 \r
384                 //! enumeration for rendering modes such as 2d and 3d for minimizing the switching of renderStates.\r
385                 enum E_RENDER_MODE\r
386                 {\r
387                         ERM_NONE = 0, // no render state has been set yet.\r
388                         ERM_2D, // 2d drawing rendermode\r
389                         ERM_3D // 3d rendering mode\r
390                 };\r
391 \r
392                 E_RENDER_MODE CurrentRenderMode;\r
393                 bool Transformation3DChanged;\r
394                 irr::io::path OGLES2ShaderPath;\r
395 \r
396                 SMaterial Material, LastMaterial;\r
397 \r
398                 //! Color buffer format\r
399                 ECOLOR_FORMAT ColorFormat;\r
400 \r
401                 IContextManager* ContextManager;\r
402         };\r
403 \r
404 } // end namespace video\r
405 } // end namespace irr\r
406 \r
407 #endif // _IRR_COMPILE_WITH_OGLES2_\r
408 \r
409 #endif // __C_OGLES2_DRIVER_H_INCLUDED__\r