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