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