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
6 #ifndef __C_OGLES2_DRIVER_H_INCLUDED__
\r
7 #define __C_OGLES2_DRIVER_H_INCLUDED__
\r
10 #include "SIrrCreationParameters.h"
\r
12 #ifdef _IRR_COMPILE_WITH_OGLES2_
\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
26 class COGLES2FixedPipelineRenderer;
\r
27 class COGLES2Renderer2D;
\r
29 class COGLES2Driver : public CNullDriver, public IMaterialRendererServices, public COGLES2ExtensionHandler
\r
31 friend class COpenGLCoreTexture<COGLES2Driver>;
\r
32 friend IVideoDriver* createOGLES2Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);
\r
35 //! constructor (use createOGLES2Driver instead)
\r
36 COGLES2Driver(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager);
\r
41 virtual ~COGLES2Driver();
\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
46 bool endScene() override;
\r
48 //! sets transformation
\r
49 void setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat) override;
\r
51 struct SHWBufferLink_opengl : public SHWBufferLink
\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
58 u32 vbo_verticesID; //tmp
\r
59 u32 vbo_indicesID; //tmp
\r
61 u32 vbo_verticesSize; //tmp
\r
62 u32 vbo_indicesSize; //tmp
\r
65 bool updateVertexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);
\r
66 bool updateIndexHardwareBuffer(SHWBufferLink_opengl *HWBuffer);
\r
68 //! updates hardware buffer if needed
\r
69 bool updateHardwareBuffer(SHWBufferLink *HWBuffer) override;
\r
71 //! Create hardware buffer from mesh
\r
72 SHWBufferLink *createHardwareBuffer(const scene::IMeshBuffer* mb) override;
\r
74 //! Delete hardware buffer (only some drivers can)
\r
75 void deleteHardwareBuffer(SHWBufferLink *HWBuffer) override;
\r
77 //! Draw hardware buffer
\r
78 void drawHardwareBuffer(SHWBufferLink *HWBuffer) override;
\r
80 IRenderTarget* addRenderTarget() override;
\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
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
90 return FeatureEnabled[feature] && COGLES2ExtensionHandler::queryFeature(feature);
\r
93 //! Sets a material.
\r
94 void setMaterial(const SMaterial& material) override;
\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
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
106 virtual void draw2DImage(const video::ITexture* texture, u32 layer, bool flip);
\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
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
122 bool useAlphaChannelOfTexture) override;
\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
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
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
138 //! Draws a single pixel
\r
139 void drawPixel(u32 x, u32 y, const SColor & color) override;
\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
147 // virtual void drawPixel(u32 x, u32 y, const SColor & color);
\r
149 //! Returns the name of the video driver.
\r
150 const wchar_t* getName() const override;
\r
152 //! Returns the maximum texture size supported.
\r
153 core::dimension2du getMaxTextureSize() const override;
\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
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
165 //! sets a viewport
\r
166 void setViewPort(const core::rect<s32>& area) override;
\r
168 //! Only used internally by the engine
\r
169 void OnResize(const core::dimension2d<u32>& size) override;
\r
171 //! Returns type of video driver
\r
172 E_DRIVER_TYPE getDriverType() const override;
\r
174 //! get color format of the current color buffer
\r
175 ECOLOR_FORMAT getColorFormat() const override;
\r
177 //! Returns the transformation set by setTransform
\r
178 const core::matrix4& getTransform(E_TRANSFORMATION_STATE state) const override;
\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
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
186 //! Get a vertex shader constant index.
\r
187 s32 getVertexShaderConstantID(const c8* name) override;
\r
189 //! Get a pixel shader constant index.
\r
190 s32 getPixelShaderConstantID(const c8* name) override;
\r
192 //! Sets a vertex shader constant.
\r
193 void setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount = 1) override;
\r
195 //! Sets a pixel shader constant.
\r
196 void setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount = 1) override;
\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
201 //! Int interface for the above.
\r
202 bool setVertexShaderConstant(s32 index, const s32* ints, int count) override;
\r
204 //! Uint interface for the above.
\r
205 bool setVertexShaderConstant(s32 index, const u32* ints, int count) override;
\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
210 //! Int interface for the above.
\r
211 bool setPixelShaderConstant(s32 index, const s32* ints, int count) override;
\r
213 //! Uint interface for the above.
\r
214 bool setPixelShaderConstant(s32 index, const u32* ints, int count) override;
\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
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
238 //! Returns pointer to the IGPUProgrammingServices interface.
\r
239 IGPUProgrammingServices* getGPUProgrammingServices() override;
\r
241 //! Returns a pointer to the IVideoDriver interface.
\r
242 IVideoDriver* getVideoDriver() override;
\r
244 //! Returns the maximum amount of primitives
\r
245 u32 getMaximalPrimitiveCount() const override;
\r
247 virtual ITexture* addRenderTargetTexture(const core::dimension2d<u32>& size,
\r
248 const io::path& name, const ECOLOR_FORMAT format = ECF_UNKNOWN) override;
\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
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
257 void clearBuffers(u16 flag, SColor color = SColor(255, 0, 0, 0), f32 depth = 1.f, u8 stencil = 0) override;
\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
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
265 //! checks if an OGLES1 error has happened and prints it
\r
266 bool testEGLError();
\r
268 //! Set/unset a clipping plane.
\r
269 bool setClipPlane(u32 index, const core::plane3df& plane, bool enable = false) override;
\r
271 //! returns the current amount of user clip planes set.
\r
272 u32 getClipPlaneCount() const;
\r
274 //! returns the 0 indexed Plane
\r
275 const core::plane3df& getClipPlane(u32 index) const;
\r
277 //! Enable/disable a clipping plane.
\r
278 void enableClipPlane(u32 index, bool enable) override;
\r
280 //! Returns the graphics card vendor name.
\r
281 core::stringc getVendorInfo() override
\r
286 void removeTexture(ITexture* texture) override;
\r
288 //! Check if the driver supports creating textures with the given color format
\r
289 bool queryTextureFormat(ECOLOR_FORMAT format) const override;
\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
294 //! Convert E_BLEND_FACTOR to OpenGL equivalent
\r
295 GLenum getGLBlend(E_BLEND_FACTOR factor) const;
\r
297 //! Get ZBuffer bits.
\r
298 virtual GLenum getZBufferBits() const;
\r
300 virtual bool getColorFormatParameters(ECOLOR_FORMAT format, GLint& internalFormat, GLenum& pixelFormat,
\r
301 GLenum& pixelType, void(**converter)(const void*, s32, void*)) const;
\r
303 //! Get current material.
\r
304 const SMaterial& getCurrentMaterial() const;
\r
306 COGLES2CacheHandler* getCacheHandler() const;
\r
309 //! inits the opengl-es driver
\r
310 virtual bool genericDriverInit(const core::dimension2d<u32>& screenSize, bool stencilBuffer);
\r
312 void chooseMaterial2D();
\r
314 ITexture* createDeviceDependentTexture(const io::path& name, IImage* image) override;
\r
316 ITexture* createDeviceDependentTextureCubemap(const io::path& name, const core::array<IImage*>& image) override;
\r
318 //! Map Irrlicht wrap mode to OpenGL enum
\r
319 GLint getTextureWrapMode(u8 clamp) const;
\r
321 //! sets the needed renderstates
\r
322 void setRenderStates3DMode();
\r
324 //! sets the needed renderstates
\r
325 void setRenderStates2DMode(bool alpha, bool texture, bool alphaChannel);
\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
333 LockRenderStateMode = true;
\r
336 //! Allow setRenderStateMode calls to work again
\r
337 void unlockRenderStateMode()
\r
339 LockRenderStateMode = false;
\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
347 void createMaterialRenderers();
\r
349 void loadShaderData(const io::path& vertexShaderName, const io::path& fragmentShaderName, c8** vertexShaderData, c8** fragmentShaderData);
\r
351 bool setMaterialTexture(irr::u32 layerIdx, const irr::video::ITexture* texture);
\r
353 //! Same as `CacheHandler->setViewport`, but also sets `ViewPort`
\r
354 virtual void setViewPortRaw(u32 width, u32 height);
\r
356 COGLES2CacheHandler* CacheHandler;
\r
357 core::stringw Name;
\r
358 core::stringc VendorName;
\r
359 SIrrlichtCreationParameters Params;
\r
361 //! bool to make all renderstates reset if set to true.
\r
362 bool ResetRenderStates;
\r
363 bool LockRenderStateMode;
\r
366 struct SUserClipPlane
\r
368 core::plane3df Plane;
\r
372 core::array<SUserClipPlane> UserClipPlane;
\r
374 core::matrix4 TextureFlipMatrix;
\r
378 COGLES2Renderer2D* MaterialRenderer2DActive;
\r
379 COGLES2Renderer2D* MaterialRenderer2DTexture;
\r
380 COGLES2Renderer2D* MaterialRenderer2DNoTexture;
\r
382 core::matrix4 Matrices[ETS_COUNT];
\r
384 //! enumeration for rendering modes such as 2d and 3d for minimizing the switching of renderStates.
\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
392 E_RENDER_MODE CurrentRenderMode;
\r
393 bool Transformation3DChanged;
\r
394 irr::io::path OGLES2ShaderPath;
\r
396 SMaterial Material, LastMaterial;
\r
398 //! Color buffer format
\r
399 ECOLOR_FORMAT ColorFormat;
\r
401 IContextManager* ContextManager;
\r
404 } // end namespace video
\r
405 } // end namespace irr
\r
407 #endif // _IRR_COMPILE_WITH_OGLES2_
\r
409 #endif // __C_OGLES2_DRIVER_H_INCLUDED__
\r