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