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
9 #include "CNullDriver.h"
10 #include "IContextManager.h"
12 #include "COpenGLCoreTexture.h"
13 #include "COpenGLCoreRenderTarget.h"
14 #include "COpenGLCoreCacheHandler.h"
16 #include "MaterialRenderer.h"
17 #include "FixedPipelineRenderer.h"
18 #include "Renderer2D.h"
20 #include "EVertexAttributes.h"
24 #ifdef _IRR_COMPILE_WITH_ANDROID_DEVICE_
25 #include "android_native_app_glue.h"
28 #include "mt_opengl.h"
34 struct VertexAttribute {
50 VertexAttribute Attributes[];
52 VertexType(const VertexType &) = delete;
53 VertexType &operator= (const VertexType &) = delete;
56 static const VertexAttribute *begin(const VertexType &type)
58 return type.Attributes;
61 static const VertexAttribute *end(const VertexType &type)
63 return type.Attributes + type.AttributeCount;
66 static constexpr VertexType vtStandard = {
67 sizeof(S3DVertex), 4, {
68 {EVA_POSITION, 3, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertex, Pos)},
69 {EVA_NORMAL, 3, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertex, Normal)},
70 {EVA_COLOR, 4, GL_UNSIGNED_BYTE, VertexAttribute::Mode::Normalized, offsetof(S3DVertex, Color)},
71 {EVA_TCOORD0, 2, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertex, TCoords)},
75 #pragma GCC diagnostic push
76 #pragma GCC diagnostic ignored "-Winvalid-offsetof"
78 static constexpr VertexType vt2TCoords = {
79 sizeof(S3DVertex2TCoords), 5, {
80 {EVA_POSITION, 3, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertex2TCoords, Pos)},
81 {EVA_NORMAL, 3, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertex2TCoords, Normal)},
82 {EVA_COLOR, 4, GL_UNSIGNED_BYTE, VertexAttribute::Mode::Normalized, offsetof(S3DVertex2TCoords, Color)},
83 {EVA_TCOORD0, 2, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertex2TCoords, TCoords)},
84 {EVA_TCOORD1, 2, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertex2TCoords, TCoords2)},
88 static constexpr VertexType vtTangents = {
89 sizeof(S3DVertexTangents), 6, {
90 {EVA_POSITION, 3, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertexTangents, Pos)},
91 {EVA_NORMAL, 3, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertexTangents, Normal)},
92 {EVA_COLOR, 4, GL_UNSIGNED_BYTE, VertexAttribute::Mode::Normalized, offsetof(S3DVertexTangents, Color)},
93 {EVA_TCOORD0, 2, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertexTangents, TCoords)},
94 {EVA_TANGENT, 3, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertexTangents, Tangent)},
95 {EVA_BINORMAL, 3, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertexTangents, Binormal)},
99 #pragma GCC diagnostic pop
101 static const VertexType &getVertexTypeDescription(E_VERTEX_TYPE type)
104 case EVT_STANDARD: return vtStandard;
105 case EVT_2TCOORDS: return vt2TCoords;
106 case EVT_TANGENTS: return vtTangents;
107 default: assert(false);
111 static constexpr VertexType vt2DImage = {
112 sizeof(S3DVertex), 3, {
113 {EVA_POSITION, 3, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertex, Pos)},
114 {EVA_COLOR, 4, GL_UNSIGNED_BYTE, VertexAttribute::Mode::Normalized, offsetof(S3DVertex, Color)},
115 {EVA_TCOORD0, 2, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertex, TCoords)},
119 static constexpr VertexType vtPrimitive = {
120 sizeof(S3DVertex), 2, {
121 {EVA_POSITION, 3, GL_FLOAT, VertexAttribute::Mode::Regular, offsetof(S3DVertex, Pos)},
122 {EVA_COLOR, 4, GL_UNSIGNED_BYTE, VertexAttribute::Mode::Normalized, offsetof(S3DVertex, Color)},
127 void APIENTRY COpenGL3DriverBase::debugCb(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam)
129 ((COpenGL3DriverBase *)userParam)->debugCb(source, type, id, severity, length, message);
132 void COpenGL3DriverBase::debugCb(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message)
134 printf("%04x %04x %x %x %.*s\n", source, type, id, severity, length, message);
137 COpenGL3DriverBase::COpenGL3DriverBase(const SIrrlichtCreationParameters& params, io::IFileSystem* io, IContextManager* contextManager) :
138 CNullDriver(io, params.WindowSize), COpenGL3ExtensionHandler(), CacheHandler(0),
139 Params(params), ResetRenderStates(true), LockRenderStateMode(false), AntiAlias(params.AntiAlias),
140 MaterialRenderer2DActive(0), MaterialRenderer2DTexture(0), MaterialRenderer2DNoTexture(0),
141 CurrentRenderMode(ERM_NONE), Transformation3DChanged(true),
142 OGLES2ShaderPath(params.OGLES2ShaderPath),
143 ColorFormat(ECF_R8G8B8), ContextManager(contextManager)
146 setDebugName("Driver");
152 ContextManager->grab();
153 ContextManager->generateSurface();
154 ContextManager->generateContext();
155 ExposedData = ContextManager->getContext();
156 ContextManager->activateContext(ExposedData, false);
157 GL.LoadAllProcedures(ContextManager);
158 GL.DebugMessageCallback(debugCb, this);
162 COpenGL3DriverBase::~COpenGL3DriverBase()
164 deleteMaterialRenders();
166 CacheHandler->getTextureCache().clear();
168 removeAllRenderTargets();
170 removeAllOcclusionQueries();
171 removeAllHardwareBuffers();
173 delete MaterialRenderer2DTexture;
174 delete MaterialRenderer2DNoTexture;
179 ContextManager->destroyContext();
180 ContextManager->destroySurface();
181 ContextManager->terminate();
182 ContextManager->drop();
186 void COpenGL3DriverBase::initQuadsIndices(int max_vertex_count)
188 int max_quad_count = max_vertex_count / 4;
189 QuadsIndices.reserve(6 * max_quad_count);
190 for (int k = 0; k < max_quad_count; k++) {
191 QuadsIndices.push_back(4 * k + 0);
192 QuadsIndices.push_back(4 * k + 1);
193 QuadsIndices.push_back(4 * k + 2);
194 QuadsIndices.push_back(4 * k + 0);
195 QuadsIndices.push_back(4 * k + 2);
196 QuadsIndices.push_back(4 * k + 3);
200 bool COpenGL3DriverBase::genericDriverInit(const core::dimension2d<u32>& screenSize, bool stencilBuffer)
202 Name = glGetString(GL_VERSION);
205 // print renderer information
206 VendorName = glGetString(GL_VENDOR);
207 os::Printer::log(VendorName.c_str(), ELL_INFORMATION);
212 // reset cache handler
214 CacheHandler = new COpenGL3CacheHandler(this);
216 StencilBuffer = stencilBuffer;
218 DriverAttributes->setAttribute("MaxTextures", (s32)Feature.MaxTextureUnits);
219 DriverAttributes->setAttribute("MaxSupportedTextures", (s32)Feature.MaxTextureUnits);
220 // DriverAttributes->setAttribute("MaxLights", MaxLights);
221 DriverAttributes->setAttribute("MaxAnisotropy", MaxAnisotropy);
222 // DriverAttributes->setAttribute("MaxUserClipPlanes", MaxUserClipPlanes);
223 // DriverAttributes->setAttribute("MaxAuxBuffers", MaxAuxBuffers);
224 // DriverAttributes->setAttribute("MaxMultipleRenderTargets", MaxMultipleRenderTargets);
225 DriverAttributes->setAttribute("MaxIndices", (s32)MaxIndices);
226 DriverAttributes->setAttribute("MaxTextureSize", (s32)MaxTextureSize);
227 DriverAttributes->setAttribute("MaxTextureLODBias", MaxTextureLODBias);
228 DriverAttributes->setAttribute("Version", Version);
229 DriverAttributes->setAttribute("AntiAlias", AntiAlias);
231 glPixelStorei(GL_PACK_ALIGNMENT, 1);
233 UserClipPlane.reallocate(0);
235 for (s32 i = 0; i < ETS_COUNT; ++i)
236 setTransform(static_cast<E_TRANSFORMATION_STATE>(i), core::IdentityMatrix);
238 setAmbientLight(SColorf(0.0f, 0.0f, 0.0f, 0.0f));
241 glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
244 // create material renderers
245 createMaterialRenderers();
247 // set the renderstates
248 setRenderStates3DMode();
251 setFog(FogColor, FogType, FogStart, FogEnd, FogDensity, PixelFog, RangeFog);
253 // create matrix for flipping textures
254 TextureFlipMatrix.buildTextureTransform(0.0f, core::vector2df(0, 0), core::vector2df(0, 1.0f), core::vector2df(1.0f, -1.0f));
256 // We need to reset once more at the beginning of the first rendering.
257 // This fixes problems with intermediate changes to the material during texture load.
258 ResetRenderStates = true;
260 testGLError(__LINE__);
265 void COpenGL3DriverBase::loadShaderData(const io::path& vertexShaderName, const io::path& fragmentShaderName, c8** vertexShaderData, c8** fragmentShaderData)
267 io::path vsPath(OGLES2ShaderPath);
268 vsPath += vertexShaderName;
270 io::path fsPath(OGLES2ShaderPath);
271 fsPath += fragmentShaderName;
273 *vertexShaderData = 0;
274 *fragmentShaderData = 0;
276 io::IReadFile* vsFile = FileSystem->createAndOpenFile(vsPath);
279 core::stringw warning(L"Warning: Missing shader files needed to simulate fixed function materials:\n");
280 warning += core::stringw(vsPath) + L"\n";
281 warning += L"Shaderpath can be changed in SIrrCreationParamters::OGLES2ShaderPath";
282 os::Printer::log(warning.c_str(), ELL_WARNING);
286 io::IReadFile* fsFile = FileSystem->createAndOpenFile(fsPath);
289 core::stringw warning(L"Warning: Missing shader files needed to simulate fixed function materials:\n");
290 warning += core::stringw(fsPath) + L"\n";
291 warning += L"Shaderpath can be changed in SIrrCreationParamters::OGLES2ShaderPath";
292 os::Printer::log(warning.c_str(), ELL_WARNING);
296 long size = vsFile->getSize();
299 *vertexShaderData = new c8[size+1];
300 vsFile->read(*vertexShaderData, size);
301 (*vertexShaderData)[size] = 0;
304 size = fsFile->getSize();
307 // if both handles are the same we must reset the file
308 if (fsFile == vsFile)
311 *fragmentShaderData = new c8[size+1];
312 fsFile->read(*fragmentShaderData, size);
313 (*fragmentShaderData)[size] = 0;
320 void COpenGL3DriverBase::createMaterialRenderers()
324 COpenGL3MaterialSolidCB* SolidCB = new COpenGL3MaterialSolidCB();
325 COpenGL3MaterialSolid2CB* Solid2LayerCB = new COpenGL3MaterialSolid2CB();
326 COpenGL3MaterialLightmapCB* LightmapCB = new COpenGL3MaterialLightmapCB(1.f);
327 COpenGL3MaterialLightmapCB* LightmapAddCB = new COpenGL3MaterialLightmapCB(1.f);
328 COpenGL3MaterialLightmapCB* LightmapM2CB = new COpenGL3MaterialLightmapCB(2.f);
329 COpenGL3MaterialLightmapCB* LightmapM4CB = new COpenGL3MaterialLightmapCB(4.f);
330 COpenGL3MaterialLightmapCB* LightmapLightingCB = new COpenGL3MaterialLightmapCB(1.f);
331 COpenGL3MaterialLightmapCB* LightmapLightingM2CB = new COpenGL3MaterialLightmapCB(2.f);
332 COpenGL3MaterialLightmapCB* LightmapLightingM4CB = new COpenGL3MaterialLightmapCB(4.f);
333 COpenGL3MaterialSolid2CB* DetailMapCB = new COpenGL3MaterialSolid2CB();
334 COpenGL3MaterialReflectionCB* SphereMapCB = new COpenGL3MaterialReflectionCB();
335 COpenGL3MaterialReflectionCB* Reflection2LayerCB = new COpenGL3MaterialReflectionCB();
336 COpenGL3MaterialSolidCB* TransparentAddColorCB = new COpenGL3MaterialSolidCB();
337 COpenGL3MaterialSolidCB* TransparentAlphaChannelCB = new COpenGL3MaterialSolidCB();
338 COpenGL3MaterialSolidCB* TransparentAlphaChannelRefCB = new COpenGL3MaterialSolidCB();
339 COpenGL3MaterialSolidCB* TransparentVertexAlphaCB = new COpenGL3MaterialSolidCB();
340 COpenGL3MaterialReflectionCB* TransparentReflection2LayerCB = new COpenGL3MaterialReflectionCB();
341 COpenGL3MaterialOneTextureBlendCB* OneTextureBlendCB = new COpenGL3MaterialOneTextureBlendCB();
343 // Create built-in materials.
345 core::stringc VertexShader = OGLES2ShaderPath + "Solid.vsh";
346 core::stringc FragmentShader = OGLES2ShaderPath + "Solid.fsh";
348 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
349 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, SolidCB, EMT_SOLID, 0);
351 VertexShader = OGLES2ShaderPath + "Solid2.vsh";
352 FragmentShader = OGLES2ShaderPath + "Solid2Layer.fsh";
354 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
355 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, Solid2LayerCB, EMT_SOLID, 0);
357 VertexShader = OGLES2ShaderPath + "Solid2.vsh";
358 FragmentShader = OGLES2ShaderPath + "LightmapModulate.fsh";
360 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
361 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, LightmapCB, EMT_SOLID, 0);
363 FragmentShader = OGLES2ShaderPath + "LightmapAdd.fsh";
365 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
366 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, LightmapAddCB, EMT_SOLID, 0);
368 FragmentShader = OGLES2ShaderPath + "LightmapModulate.fsh";
370 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
371 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, LightmapM2CB, EMT_SOLID, 0);
373 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
374 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, LightmapM4CB, EMT_SOLID, 0);
376 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
377 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, LightmapLightingCB, EMT_SOLID, 0);
379 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
380 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, LightmapLightingM2CB, EMT_SOLID, 0);
382 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
383 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, LightmapLightingM4CB, EMT_SOLID, 0);
385 VertexShader = OGLES2ShaderPath + "Solid2.vsh";
386 FragmentShader = OGLES2ShaderPath + "DetailMap.fsh";
388 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
389 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, DetailMapCB, EMT_SOLID, 0);
391 VertexShader = OGLES2ShaderPath + "SphereMap.vsh";
392 FragmentShader = OGLES2ShaderPath + "SphereMap.fsh";
394 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
395 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, SphereMapCB, EMT_SOLID, 0);
397 VertexShader = OGLES2ShaderPath + "Reflection2Layer.vsh";
398 FragmentShader = OGLES2ShaderPath + "Reflection2Layer.fsh";
400 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
401 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, Reflection2LayerCB, EMT_SOLID, 0);
403 VertexShader = OGLES2ShaderPath + "Solid.vsh";
404 FragmentShader = OGLES2ShaderPath + "Solid.fsh";
406 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
407 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, TransparentAddColorCB, EMT_TRANSPARENT_ADD_COLOR, 0);
409 FragmentShader = OGLES2ShaderPath + "TransparentAlphaChannel.fsh";
410 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
411 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, TransparentAlphaChannelCB, EMT_TRANSPARENT_ALPHA_CHANNEL, 0);
413 FragmentShader = OGLES2ShaderPath + "TransparentAlphaChannelRef.fsh";
415 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
416 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, TransparentAlphaChannelRefCB, EMT_SOLID, 0);
418 FragmentShader = OGLES2ShaderPath + "TransparentVertexAlpha.fsh";
420 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
421 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, TransparentVertexAlphaCB, EMT_TRANSPARENT_ALPHA_CHANNEL, 0);
423 VertexShader = OGLES2ShaderPath + "Reflection2Layer.vsh";
424 FragmentShader = OGLES2ShaderPath + "Reflection2Layer.fsh";
426 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
427 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, TransparentReflection2LayerCB, EMT_TRANSPARENT_ALPHA_CHANNEL, 0);
429 VertexShader = OGLES2ShaderPath + "Solid.vsh";
430 FragmentShader = OGLES2ShaderPath + "OneTextureBlend.fsh";
432 addHighLevelShaderMaterialFromFiles(VertexShader, "main", EVST_VS_2_0, FragmentShader, "main", EPST_PS_2_0, "", "main",
433 EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLE_STRIP, 0, OneTextureBlendCB, EMT_ONETEXTURE_BLEND, 0);
438 Solid2LayerCB->drop();
440 LightmapAddCB->drop();
441 LightmapM2CB->drop();
442 LightmapM4CB->drop();
443 LightmapLightingCB->drop();
444 LightmapLightingM2CB->drop();
445 LightmapLightingM4CB->drop();
448 Reflection2LayerCB->drop();
449 TransparentAddColorCB->drop();
450 TransparentAlphaChannelCB->drop();
451 TransparentAlphaChannelRefCB->drop();
452 TransparentVertexAlphaCB->drop();
453 TransparentReflection2LayerCB->drop();
454 OneTextureBlendCB->drop();
456 // Create 2D material renderers
460 loadShaderData(io::path("Renderer2D.vsh"), io::path("Renderer2D.fsh"), &vs2DData, &fs2DData);
461 MaterialRenderer2DTexture = new COpenGL3Renderer2D(vs2DData, fs2DData, this, true);
467 loadShaderData(io::path("Renderer2D.vsh"), io::path("Renderer2D_noTex.fsh"), &vs2DData, &fs2DData);
468 MaterialRenderer2DNoTexture = new COpenGL3Renderer2D(vs2DData, fs2DData, this, false);
473 bool COpenGL3DriverBase::setMaterialTexture(irr::u32 layerIdx, const irr::video::ITexture* texture)
475 Material.TextureLayer[layerIdx].Texture = const_cast<ITexture*>(texture); // function uses const-pointer for texture because all draw functions use const-pointers already
476 return CacheHandler->getTextureCache().set(0, texture);
479 bool COpenGL3DriverBase::beginScene(u16 clearFlag, SColor clearColor, f32 clearDepth, u8 clearStencil, const SExposedVideoData& videoData, core::rect<s32>* sourceRect)
481 CNullDriver::beginScene(clearFlag, clearColor, clearDepth, clearStencil, videoData, sourceRect);
484 ContextManager->activateContext(videoData, true);
486 clearBuffers(clearFlag, clearColor, clearDepth, clearStencil);
491 bool COpenGL3DriverBase::endScene()
493 CNullDriver::endScene();
498 return ContextManager->swapBuffers();
504 //! Returns the transformation set by setTransform
505 const core::matrix4& COpenGL3DriverBase::getTransform(E_TRANSFORMATION_STATE state) const
507 return Matrices[state];
511 //! sets transformation
512 void COpenGL3DriverBase::setTransform(E_TRANSFORMATION_STATE state, const core::matrix4& mat)
514 Matrices[state] = mat;
515 Transformation3DChanged = true;
519 bool COpenGL3DriverBase::updateVertexHardwareBuffer(SHWBufferLink_opengl *HWBuffer)
524 const scene::IMeshBuffer* mb = HWBuffer->MeshBuffer;
525 const void* vertices = mb->getVertices();
526 const u32 vertexCount = mb->getVertexCount();
527 const E_VERTEX_TYPE vType = mb->getVertexType();
528 const u32 vertexSize = getVertexPitchFromType(vType);
530 const void *buffer = vertices;
531 size_t bufferSize = vertexSize * vertexCount;
533 //get or create buffer
534 bool newBuffer = false;
535 if (!HWBuffer->vbo_verticesID)
537 glGenBuffers(1, &HWBuffer->vbo_verticesID);
538 if (!HWBuffer->vbo_verticesID) return false;
541 else if (HWBuffer->vbo_verticesSize < bufferSize)
546 glBindBuffer(GL_ARRAY_BUFFER, HWBuffer->vbo_verticesID);
548 // copy data to graphics card
550 glBufferSubData(GL_ARRAY_BUFFER, 0, bufferSize, buffer);
553 HWBuffer->vbo_verticesSize = bufferSize;
555 if (HWBuffer->Mapped_Vertex == scene::EHM_STATIC)
556 glBufferData(GL_ARRAY_BUFFER, bufferSize, buffer, GL_STATIC_DRAW);
558 glBufferData(GL_ARRAY_BUFFER, bufferSize, buffer, GL_DYNAMIC_DRAW);
561 glBindBuffer(GL_ARRAY_BUFFER, 0);
563 return (!testGLError(__LINE__));
567 bool COpenGL3DriverBase::updateIndexHardwareBuffer(SHWBufferLink_opengl *HWBuffer)
572 const scene::IMeshBuffer* mb = HWBuffer->MeshBuffer;
574 const void* indices = mb->getIndices();
575 u32 indexCount = mb->getIndexCount();
578 switch (mb->getIndexType())
582 indexSize = sizeof(u16);
587 indexSize = sizeof(u32);
596 //get or create buffer
597 bool newBuffer = false;
598 if (!HWBuffer->vbo_indicesID)
600 glGenBuffers(1, &HWBuffer->vbo_indicesID);
601 if (!HWBuffer->vbo_indicesID) return false;
604 else if (HWBuffer->vbo_indicesSize < indexCount*indexSize)
609 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, HWBuffer->vbo_indicesID);
611 // copy data to graphics card
613 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexCount * indexSize, indices);
616 HWBuffer->vbo_indicesSize = indexCount * indexSize;
618 if (HWBuffer->Mapped_Index == scene::EHM_STATIC)
619 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * indexSize, indices, GL_STATIC_DRAW);
621 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * indexSize, indices, GL_DYNAMIC_DRAW);
624 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
626 return (!testGLError(__LINE__));
630 //! updates hardware buffer if needed
631 bool COpenGL3DriverBase::updateHardwareBuffer(SHWBufferLink *HWBuffer)
636 if (HWBuffer->Mapped_Vertex != scene::EHM_NEVER)
638 if (HWBuffer->ChangedID_Vertex != HWBuffer->MeshBuffer->getChangedID_Vertex()
639 || !static_cast<SHWBufferLink_opengl*>(HWBuffer)->vbo_verticesID)
642 HWBuffer->ChangedID_Vertex = HWBuffer->MeshBuffer->getChangedID_Vertex();
644 if (!updateVertexHardwareBuffer(static_cast<SHWBufferLink_opengl*>(HWBuffer)))
649 if (HWBuffer->Mapped_Index != scene::EHM_NEVER)
651 if (HWBuffer->ChangedID_Index != HWBuffer->MeshBuffer->getChangedID_Index()
652 || !static_cast<SHWBufferLink_opengl*>(HWBuffer)->vbo_indicesID)
655 HWBuffer->ChangedID_Index = HWBuffer->MeshBuffer->getChangedID_Index();
657 if (!updateIndexHardwareBuffer((SHWBufferLink_opengl*)HWBuffer))
666 //! Create hardware buffer from meshbuffer
667 COpenGL3DriverBase::SHWBufferLink *COpenGL3DriverBase::createHardwareBuffer(const scene::IMeshBuffer* mb)
669 if (!mb || (mb->getHardwareMappingHint_Index() == scene::EHM_NEVER && mb->getHardwareMappingHint_Vertex() == scene::EHM_NEVER))
672 SHWBufferLink_opengl *HWBuffer = new SHWBufferLink_opengl(mb);
675 HWBuffer->listPosition = HWBufferList.insert(HWBufferList.end(), HWBuffer);
677 HWBuffer->ChangedID_Vertex = HWBuffer->MeshBuffer->getChangedID_Vertex();
678 HWBuffer->ChangedID_Index = HWBuffer->MeshBuffer->getChangedID_Index();
679 HWBuffer->Mapped_Vertex = mb->getHardwareMappingHint_Vertex();
680 HWBuffer->Mapped_Index = mb->getHardwareMappingHint_Index();
681 HWBuffer->vbo_verticesID = 0;
682 HWBuffer->vbo_indicesID = 0;
683 HWBuffer->vbo_verticesSize = 0;
684 HWBuffer->vbo_indicesSize = 0;
686 if (!updateHardwareBuffer(HWBuffer))
688 deleteHardwareBuffer(HWBuffer);
696 void COpenGL3DriverBase::deleteHardwareBuffer(SHWBufferLink *_HWBuffer)
701 SHWBufferLink_opengl *HWBuffer = static_cast<SHWBufferLink_opengl*>(_HWBuffer);
702 if (HWBuffer->vbo_verticesID)
704 glDeleteBuffers(1, &HWBuffer->vbo_verticesID);
705 HWBuffer->vbo_verticesID = 0;
707 if (HWBuffer->vbo_indicesID)
709 glDeleteBuffers(1, &HWBuffer->vbo_indicesID);
710 HWBuffer->vbo_indicesID = 0;
713 CNullDriver::deleteHardwareBuffer(_HWBuffer);
717 //! Draw hardware buffer
718 void COpenGL3DriverBase::drawHardwareBuffer(SHWBufferLink *_HWBuffer)
723 SHWBufferLink_opengl *HWBuffer = static_cast<SHWBufferLink_opengl*>(_HWBuffer);
725 updateHardwareBuffer(HWBuffer); //check if update is needed
727 const scene::IMeshBuffer* mb = HWBuffer->MeshBuffer;
728 const void *vertices = mb->getVertices();
729 const void *indexList = mb->getIndices();
731 if (HWBuffer->Mapped_Vertex != scene::EHM_NEVER)
733 glBindBuffer(GL_ARRAY_BUFFER, HWBuffer->vbo_verticesID);
737 if (HWBuffer->Mapped_Index != scene::EHM_NEVER)
739 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, HWBuffer->vbo_indicesID);
744 drawVertexPrimitiveList(vertices, mb->getVertexCount(),
745 indexList, mb->getPrimitiveCount(),
746 mb->getVertexType(), mb->getPrimitiveType(),
749 if (HWBuffer->Mapped_Vertex != scene::EHM_NEVER)
750 glBindBuffer(GL_ARRAY_BUFFER, 0);
752 if (HWBuffer->Mapped_Index != scene::EHM_NEVER)
753 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
757 IRenderTarget* COpenGL3DriverBase::addRenderTarget()
759 COpenGL3RenderTarget* renderTarget = new COpenGL3RenderTarget(this);
760 RenderTargets.push_back(renderTarget);
766 // small helper function to create vertex buffer object adress offsets
767 static inline u8* buffer_offset(const long offset)
769 return ((u8*)0 + offset);
773 //! draws a vertex primitive list
774 void COpenGL3DriverBase::drawVertexPrimitiveList(const void* vertices, u32 vertexCount,
775 const void* indexList, u32 primitiveCount,
776 E_VERTEX_TYPE vType, scene::E_PRIMITIVE_TYPE pType, E_INDEX_TYPE iType)
778 if (!primitiveCount || !vertexCount)
781 if (!checkPrimitiveCount(primitiveCount))
784 CNullDriver::drawVertexPrimitiveList(vertices, vertexCount, indexList, primitiveCount, vType, pType, iType);
786 setRenderStates3DMode();
788 auto &vTypeDesc = getVertexTypeDescription(vType);
789 beginDraw(vTypeDesc, reinterpret_cast<uintptr_t>(vertices));
790 GLenum indexSize = 0;
796 indexSize = GL_UNSIGNED_SHORT;
801 #ifdef GL_OES_element_index_uint
802 #ifndef GL_UNSIGNED_INT
803 #define GL_UNSIGNED_INT 0x1405
805 if (FeatureAvailable[COGLESCoreExtensionHandler::IRR_GL_OES_element_index_uint])
806 indexSize = GL_UNSIGNED_INT;
809 indexSize = GL_UNSIGNED_SHORT;
816 case scene::EPT_POINTS:
817 case scene::EPT_POINT_SPRITES:
818 glDrawArrays(GL_POINTS, 0, primitiveCount);
820 case scene::EPT_LINE_STRIP:
821 glDrawElements(GL_LINE_STRIP, primitiveCount + 1, indexSize, indexList);
823 case scene::EPT_LINE_LOOP:
824 glDrawElements(GL_LINE_LOOP, primitiveCount, indexSize, indexList);
826 case scene::EPT_LINES:
827 glDrawElements(GL_LINES, primitiveCount*2, indexSize, indexList);
829 case scene::EPT_TRIANGLE_STRIP:
830 glDrawElements(GL_TRIANGLE_STRIP, primitiveCount + 2, indexSize, indexList);
832 case scene::EPT_TRIANGLE_FAN:
833 glDrawElements(GL_TRIANGLE_FAN, primitiveCount + 2, indexSize, indexList);
835 case scene::EPT_TRIANGLES:
836 glDrawElements((LastMaterial.Wireframe) ? GL_LINES : (LastMaterial.PointCloud) ? GL_POINTS : GL_TRIANGLES, primitiveCount*3, indexSize, indexList);
846 void COpenGL3DriverBase::draw2DImage(const video::ITexture* texture, const core::position2d<s32>& destPos,
847 const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect, SColor color,
848 bool useAlphaChannelOfTexture)
853 if (!sourceRect.isValid())
856 SColor colors[4] = {color, color, color, color};
857 draw2DImage(texture, {destPos, sourceRect.getSize()}, sourceRect, clipRect, colors, useAlphaChannelOfTexture);
861 void COpenGL3DriverBase::draw2DImage(const video::ITexture* texture, const core::rect<s32>& destRect,
862 const core::rect<s32>& sourceRect, const core::rect<s32>* clipRect,
863 const video::SColor* const colors, bool useAlphaChannelOfTexture)
868 // texcoords need to be flipped horizontally for RTTs
869 const bool isRTT = texture->isRenderTarget();
870 const core::dimension2du& ss = texture->getOriginalSize();
871 const f32 invW = 1.f / static_cast<f32>(ss.Width);
872 const f32 invH = 1.f / static_cast<f32>(ss.Height);
873 const core::rect<f32> tcoords(
874 sourceRect.UpperLeftCorner.X * invW,
875 (isRTT ? sourceRect.LowerRightCorner.Y : sourceRect.UpperLeftCorner.Y) * invH,
876 sourceRect.LowerRightCorner.X * invW,
877 (isRTT ? sourceRect.UpperLeftCorner.Y : sourceRect.LowerRightCorner.Y) *invH);
879 const video::SColor temp[4] =
887 const video::SColor* const useColor = colors ? colors : temp;
890 if (!setMaterialTexture(0, texture ))
893 setRenderStates2DMode(useColor[0].getAlpha() < 255 || useColor[1].getAlpha() < 255 ||
894 useColor[2].getAlpha() < 255 || useColor[3].getAlpha() < 255,
895 true, useAlphaChannelOfTexture);
897 const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();
901 if (!clipRect->isValid())
904 glEnable(GL_SCISSOR_TEST);
905 glScissor(clipRect->UpperLeftCorner.X, renderTargetSize.Height - clipRect->LowerRightCorner.Y,
906 clipRect->getWidth(), clipRect->getHeight());
909 f32 left = (f32)destRect.UpperLeftCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;
910 f32 right = (f32)destRect.LowerRightCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;
911 f32 down = 2.f - (f32)destRect.LowerRightCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;
912 f32 top = 2.f - (f32)destRect.UpperLeftCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;
914 S3DVertex vertices[4];
915 vertices[0] = S3DVertex(left, top, 0, 0, 0, 1, useColor[0], tcoords.UpperLeftCorner.X, tcoords.UpperLeftCorner.Y);
916 vertices[1] = S3DVertex(right, top, 0, 0, 0, 1, useColor[3], tcoords.LowerRightCorner.X, tcoords.UpperLeftCorner.Y);
917 vertices[2] = S3DVertex(right, down, 0, 0, 0, 1, useColor[2], tcoords.LowerRightCorner.X, tcoords.LowerRightCorner.Y);
918 vertices[3] = S3DVertex(left, down, 0, 0, 0, 1, useColor[1], tcoords.UpperLeftCorner.X, tcoords.LowerRightCorner.Y);
920 drawQuad(vt2DImage, vertices);
923 glDisable(GL_SCISSOR_TEST);
925 testGLError(__LINE__);
928 void COpenGL3DriverBase::draw2DImage(const video::ITexture* texture, u32 layer, bool flip)
934 if (!setMaterialTexture(0, texture ))
937 setRenderStates2DMode(false, true, true);
939 S3DVertex quad2DVertices[4];
941 quad2DVertices[0].Pos = core::vector3df(-1.f, 1.f, 0.f);
942 quad2DVertices[1].Pos = core::vector3df(1.f, 1.f, 0.f);
943 quad2DVertices[2].Pos = core::vector3df(1.f, -1.f, 0.f);
944 quad2DVertices[3].Pos = core::vector3df(-1.f, -1.f, 0.f);
946 f32 modificator = (flip) ? 1.f : 0.f;
948 quad2DVertices[0].TCoords = core::vector2df(0.f, 0.f + modificator);
949 quad2DVertices[1].TCoords = core::vector2df(1.f, 0.f + modificator);
950 quad2DVertices[2].TCoords = core::vector2df(1.f, 1.f - modificator);
951 quad2DVertices[3].TCoords = core::vector2df(0.f, 1.f - modificator);
953 quad2DVertices[0].Color = SColor(0xFFFFFFFF);
954 quad2DVertices[1].Color = SColor(0xFFFFFFFF);
955 quad2DVertices[2].Color = SColor(0xFFFFFFFF);
956 quad2DVertices[3].Color = SColor(0xFFFFFFFF);
958 drawQuad(vt2DImage, quad2DVertices);
961 void COpenGL3DriverBase::draw2DImageBatch(const video::ITexture* texture,
962 const core::array<core::position2d<s32> >& positions,
963 const core::array<core::rect<s32> >& sourceRects,
964 const core::rect<s32>* clipRect,
965 SColor color, bool useAlphaChannelOfTexture)
971 if (!setMaterialTexture(0, texture))
974 setRenderStates2DMode(color.getAlpha() < 255, true, useAlphaChannelOfTexture);
976 const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();
980 if (!clipRect->isValid())
983 glEnable(GL_SCISSOR_TEST);
984 glScissor(clipRect->UpperLeftCorner.X, renderTargetSize.Height - clipRect->LowerRightCorner.Y,
985 clipRect->getWidth(), clipRect->getHeight());
988 const irr::u32 drawCount = core::min_<u32>(positions.size(), sourceRects.size());
989 assert(6 * std::size_t(drawCount) <= QuadsIndices.size());
991 core::array<S3DVertex> vtx(drawCount * 4);
993 for (u32 i = 0; i < drawCount; i++)
995 core::position2d<s32> targetPos = positions[i];
996 core::position2d<s32> sourcePos = sourceRects[i].UpperLeftCorner;
997 // This needs to be signed as it may go negative.
998 core::dimension2d<s32> sourceSize(sourceRects[i].getSize());
1002 core::rect<f32> tcoords;
1003 tcoords.UpperLeftCorner.X = (((f32)sourcePos.X)) / texture->getOriginalSize().Width ;
1004 tcoords.UpperLeftCorner.Y = (((f32)sourcePos.Y)) / texture->getOriginalSize().Height;
1005 tcoords.LowerRightCorner.X = tcoords.UpperLeftCorner.X + ((f32)(sourceSize.Width) / texture->getOriginalSize().Width);
1006 tcoords.LowerRightCorner.Y = tcoords.UpperLeftCorner.Y + ((f32)(sourceSize.Height) / texture->getOriginalSize().Height);
1008 const core::rect<s32> poss(targetPos, sourceSize);
1010 f32 left = (f32)poss.UpperLeftCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;
1011 f32 right = (f32)poss.LowerRightCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;
1012 f32 down = 2.f - (f32)poss.LowerRightCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;
1013 f32 top = 2.f - (f32)poss.UpperLeftCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;
1015 vtx.push_back(S3DVertex(left, top, 0.0f,
1016 0.0f, 0.0f, 0.0f, color,
1017 tcoords.UpperLeftCorner.X, tcoords.UpperLeftCorner.Y));
1018 vtx.push_back(S3DVertex(right, top, 0.0f,
1019 0.0f, 0.0f, 0.0f, color,
1020 tcoords.LowerRightCorner.X, tcoords.UpperLeftCorner.Y));
1021 vtx.push_back(S3DVertex(right, down, 0.0f,
1022 0.0f, 0.0f, 0.0f, color,
1023 tcoords.LowerRightCorner.X, tcoords.LowerRightCorner.Y));
1024 vtx.push_back(S3DVertex(left, down, 0.0f,
1025 0.0f, 0.0f, 0.0f, color,
1026 tcoords.UpperLeftCorner.X, tcoords.LowerRightCorner.Y));
1029 drawElements(GL_TRIANGLES, vt2DImage, vtx.const_pointer(), QuadsIndices.data(), 6 * drawCount);
1032 glDisable(GL_SCISSOR_TEST);
1036 //! draw a 2d rectangle
1037 void COpenGL3DriverBase::draw2DRectangle(SColor color,
1038 const core::rect<s32>& position,
1039 const core::rect<s32>* clip)
1042 setMaterialTexture(0, 0);
1044 setRenderStates2DMode(color.getAlpha() < 255, false, false);
1046 core::rect<s32> pos = position;
1049 pos.clipAgainst(*clip);
1054 const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();
1056 f32 left = (f32)pos.UpperLeftCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;
1057 f32 right = (f32)pos.LowerRightCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;
1058 f32 down = 2.f - (f32)pos.LowerRightCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;
1059 f32 top = 2.f - (f32)pos.UpperLeftCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;
1061 S3DVertex vertices[4];
1062 vertices[0] = S3DVertex(left, top, 0, 0, 0, 1, color, 0, 0);
1063 vertices[1] = S3DVertex(right, top, 0, 0, 0, 1, color, 0, 0);
1064 vertices[2] = S3DVertex(right, down, 0, 0, 0, 1, color, 0, 0);
1065 vertices[3] = S3DVertex(left, down, 0, 0, 0, 1, color, 0, 0);
1067 drawQuad(vtPrimitive, vertices);
1071 //! draw an 2d rectangle
1072 void COpenGL3DriverBase::draw2DRectangle(const core::rect<s32>& position,
1073 SColor colorLeftUp, SColor colorRightUp,
1074 SColor colorLeftDown, SColor colorRightDown,
1075 const core::rect<s32>* clip)
1077 core::rect<s32> pos = position;
1080 pos.clipAgainst(*clip);
1086 setMaterialTexture(0, 0);
1088 setRenderStates2DMode(colorLeftUp.getAlpha() < 255 ||
1089 colorRightUp.getAlpha() < 255 ||
1090 colorLeftDown.getAlpha() < 255 ||
1091 colorRightDown.getAlpha() < 255, false, false);
1093 const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();
1095 f32 left = (f32)pos.UpperLeftCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;
1096 f32 right = (f32)pos.LowerRightCorner.X / (f32)renderTargetSize.Width * 2.f - 1.f;
1097 f32 down = 2.f - (f32)pos.LowerRightCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;
1098 f32 top = 2.f - (f32)pos.UpperLeftCorner.Y / (f32)renderTargetSize.Height * 2.f - 1.f;
1100 S3DVertex vertices[4];
1101 vertices[0] = S3DVertex(left, top, 0, 0, 0, 1, colorLeftUp, 0, 0);
1102 vertices[1] = S3DVertex(right, top, 0, 0, 0, 1, colorRightUp, 0, 0);
1103 vertices[2] = S3DVertex(right, down, 0, 0, 0, 1, colorRightDown, 0, 0);
1104 vertices[3] = S3DVertex(left, down, 0, 0, 0, 1, colorLeftDown, 0, 0);
1106 drawQuad(vtPrimitive, vertices);
1110 //! Draws a 2d line.
1111 void COpenGL3DriverBase::draw2DLine(const core::position2d<s32>& start,
1112 const core::position2d<s32>& end, SColor color)
1115 drawPixel(start.X, start.Y, color);
1119 setMaterialTexture(0, 0);
1121 setRenderStates2DMode(color.getAlpha() < 255, false, false);
1123 const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();
1125 f32 startX = (f32)start.X / (f32)renderTargetSize.Width * 2.f - 1.f;
1126 f32 endX = (f32)end.X / (f32)renderTargetSize.Width * 2.f - 1.f;
1127 f32 startY = 2.f - (f32)start.Y / (f32)renderTargetSize.Height * 2.f - 1.f;
1128 f32 endY = 2.f - (f32)end.Y / (f32)renderTargetSize.Height * 2.f - 1.f;
1130 S3DVertex vertices[2];
1131 vertices[0] = S3DVertex(startX, startY, 0, 0, 0, 1, color, 0, 0);
1132 vertices[1] = S3DVertex(endX, endY, 0, 0, 0, 1, color, 1, 1);
1134 drawArrays(GL_LINES, vtPrimitive, vertices, 2);
1140 void COpenGL3DriverBase::drawPixel(u32 x, u32 y, const SColor &color)
1142 const core::dimension2d<u32>& renderTargetSize = getCurrentRenderTargetSize();
1143 if (x > (u32)renderTargetSize.Width || y > (u32)renderTargetSize.Height)
1147 setMaterialTexture(0, 0);
1149 setRenderStates2DMode(color.getAlpha() < 255, false, false);
1151 f32 X = (f32)x / (f32)renderTargetSize.Width * 2.f - 1.f;
1152 f32 Y = 2.f - (f32)y / (f32)renderTargetSize.Height * 2.f - 1.f;
1154 S3DVertex vertices[1];
1155 vertices[0] = S3DVertex(X, Y, 0, 0, 0, 1, color, 0, 0);
1157 drawArrays(GL_POINTS, vtPrimitive, vertices, 1);
1160 void COpenGL3DriverBase::drawQuad(const VertexType &vertexType, const S3DVertex (&vertices)[4])
1162 drawArrays(GL_TRIANGLE_FAN, vertexType, vertices, 4);
1165 void COpenGL3DriverBase::drawArrays(GLenum primitiveType, const VertexType &vertexType, const void *vertices, int vertexCount)
1167 beginDraw(vertexType, reinterpret_cast<uintptr_t>(vertices));
1168 glDrawArrays(primitiveType, 0, vertexCount);
1169 endDraw(vertexType);
1172 void COpenGL3DriverBase::drawElements(GLenum primitiveType, const VertexType &vertexType, const void *vertices, const u16 *indices, int indexCount)
1174 beginDraw(vertexType, reinterpret_cast<uintptr_t>(vertices));
1175 glDrawElements(primitiveType, indexCount, GL_UNSIGNED_SHORT, indices);
1176 endDraw(vertexType);
1179 void COpenGL3DriverBase::beginDraw(const VertexType &vertexType, uintptr_t verticesBase)
1181 for (auto attr: vertexType) {
1182 glEnableVertexAttribArray(attr.Index);
1183 switch (attr.mode) {
1184 case VertexAttribute::Mode::Regular: glVertexAttribPointer(attr.Index, attr.ComponentCount, attr.ComponentType, GL_FALSE, vertexType.VertexSize, reinterpret_cast<void *>(verticesBase + attr.Offset)); break;
1185 case VertexAttribute::Mode::Normalized: glVertexAttribPointer(attr.Index, attr.ComponentCount, attr.ComponentType, GL_TRUE, vertexType.VertexSize, reinterpret_cast<void *>(verticesBase + attr.Offset)); break;
1186 case VertexAttribute::Mode::Integral: glVertexAttribIPointer(attr.Index, attr.ComponentCount, attr.ComponentType, vertexType.VertexSize, reinterpret_cast<void *>(verticesBase + attr.Offset)); break;
1191 void COpenGL3DriverBase::endDraw(const VertexType &vertexType)
1193 for (auto attr: vertexType)
1194 glDisableVertexAttribArray(attr.Index);
1197 ITexture* COpenGL3DriverBase::createDeviceDependentTexture(const io::path& name, IImage* image)
1199 core::array<IImage*> imageArray(1);
1200 imageArray.push_back(image);
1202 COpenGL3Texture* texture = new COpenGL3Texture(name, imageArray, ETT_2D, this);
1207 ITexture* COpenGL3DriverBase::createDeviceDependentTextureCubemap(const io::path& name, const core::array<IImage*>& image)
1209 COpenGL3Texture* texture = new COpenGL3Texture(name, image, ETT_CUBEMAP, this);
1214 //! Sets a material.
1215 void COpenGL3DriverBase::setMaterial(const SMaterial& material)
1217 Material = material;
1218 OverrideMaterial.apply(Material);
1220 for (u32 i = 0; i < Feature.MaxTextureUnits; ++i)
1222 CacheHandler->getTextureCache().set(i, material.getTexture(i));
1223 setTransform((E_TRANSFORMATION_STATE)(ETS_TEXTURE_0 + i), material.getTextureMatrix(i));
1227 //! prints error if an error happened.
1228 bool COpenGL3DriverBase::testGLError(int code)
1231 GLenum g = glGetError();
1236 case GL_INVALID_ENUM:
1237 os::Printer::log("GL_INVALID_ENUM", core::stringc(code).c_str(), ELL_ERROR);
1239 case GL_INVALID_VALUE:
1240 os::Printer::log("GL_INVALID_VALUE", core::stringc(code).c_str(), ELL_ERROR);
1242 case GL_INVALID_OPERATION:
1243 os::Printer::log("GL_INVALID_OPERATION", core::stringc(code).c_str(), ELL_ERROR);
1245 case GL_OUT_OF_MEMORY:
1246 os::Printer::log("GL_OUT_OF_MEMORY", core::stringc(code).c_str(), ELL_ERROR);
1255 //! prints error if an error happened.
1256 bool COpenGL3DriverBase::testEGLError()
1258 #if defined(EGL_VERSION_1_0) && defined(_DEBUG)
1259 EGLint g = eglGetError();
1264 case EGL_NOT_INITIALIZED :
1265 os::Printer::log("Not Initialized", ELL_ERROR);
1267 case EGL_BAD_ACCESS:
1268 os::Printer::log("Bad Access", ELL_ERROR);
1271 os::Printer::log("Bad Alloc", ELL_ERROR);
1273 case EGL_BAD_ATTRIBUTE:
1274 os::Printer::log("Bad Attribute", ELL_ERROR);
1276 case EGL_BAD_CONTEXT:
1277 os::Printer::log("Bad Context", ELL_ERROR);
1279 case EGL_BAD_CONFIG:
1280 os::Printer::log("Bad Config", ELL_ERROR);
1282 case EGL_BAD_CURRENT_SURFACE:
1283 os::Printer::log("Bad Current Surface", ELL_ERROR);
1285 case EGL_BAD_DISPLAY:
1286 os::Printer::log("Bad Display", ELL_ERROR);
1288 case EGL_BAD_SURFACE:
1289 os::Printer::log("Bad Surface", ELL_ERROR);
1292 os::Printer::log("Bad Match", ELL_ERROR);
1294 case EGL_BAD_PARAMETER:
1295 os::Printer::log("Bad Parameter", ELL_ERROR);
1297 case EGL_BAD_NATIVE_PIXMAP:
1298 os::Printer::log("Bad Native Pixmap", ELL_ERROR);
1300 case EGL_BAD_NATIVE_WINDOW:
1301 os::Printer::log("Bad Native Window", ELL_ERROR);
1303 case EGL_CONTEXT_LOST:
1304 os::Printer::log("Context Lost", ELL_ERROR);
1314 void COpenGL3DriverBase::setRenderStates3DMode()
1316 if ( LockRenderStateMode )
1319 if (CurrentRenderMode != ERM_3D)
1321 // Reset Texture Stages
1322 CacheHandler->setBlend(false);
1323 CacheHandler->setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1325 ResetRenderStates = true;
1328 if (ResetRenderStates || LastMaterial != Material)
1330 // unset old material
1332 // unset last 3d material
1333 if (CurrentRenderMode == ERM_2D && MaterialRenderer2DActive)
1335 MaterialRenderer2DActive->OnUnsetMaterial();
1336 MaterialRenderer2DActive = 0;
1338 else if (LastMaterial.MaterialType != Material.MaterialType &&
1339 static_cast<u32>(LastMaterial.MaterialType) < MaterialRenderers.size())
1340 MaterialRenderers[LastMaterial.MaterialType].Renderer->OnUnsetMaterial();
1342 // set new material.
1343 if (static_cast<u32>(Material.MaterialType) < MaterialRenderers.size())
1344 MaterialRenderers[Material.MaterialType].Renderer->OnSetMaterial(
1345 Material, LastMaterial, ResetRenderStates, this);
1347 LastMaterial = Material;
1348 CacheHandler->correctCacheMaterial(LastMaterial);
1349 ResetRenderStates = false;
1352 if (static_cast<u32>(Material.MaterialType) < MaterialRenderers.size())
1353 MaterialRenderers[Material.MaterialType].Renderer->OnRender(this, video::EVT_STANDARD);
1355 CurrentRenderMode = ERM_3D;
1358 //! Can be called by an IMaterialRenderer to make its work easier.
1359 void COpenGL3DriverBase::setBasicRenderStates(const SMaterial& material, const SMaterial& lastmaterial, bool resetAllRenderStates)
1362 switch (material.ZBuffer)
1365 CacheHandler->setDepthTest(false);
1367 case ECFN_LESSEQUAL:
1368 CacheHandler->setDepthTest(true);
1369 CacheHandler->setDepthFunc(GL_LEQUAL);
1372 CacheHandler->setDepthTest(true);
1373 CacheHandler->setDepthFunc(GL_EQUAL);
1376 CacheHandler->setDepthTest(true);
1377 CacheHandler->setDepthFunc(GL_LESS);
1380 CacheHandler->setDepthTest(true);
1381 CacheHandler->setDepthFunc(GL_NOTEQUAL);
1383 case ECFN_GREATEREQUAL:
1384 CacheHandler->setDepthTest(true);
1385 CacheHandler->setDepthFunc(GL_GEQUAL);
1388 CacheHandler->setDepthTest(true);
1389 CacheHandler->setDepthFunc(GL_GREATER);
1392 CacheHandler->setDepthTest(true);
1393 CacheHandler->setDepthFunc(GL_ALWAYS);
1396 CacheHandler->setDepthTest(true);
1397 CacheHandler->setDepthFunc(GL_NEVER);
1404 if (getWriteZBuffer(material))
1406 CacheHandler->setDepthMask(true);
1410 CacheHandler->setDepthMask(false);
1413 // Back face culling
1414 if ((material.FrontfaceCulling) && (material.BackfaceCulling))
1416 CacheHandler->setCullFaceFunc(GL_FRONT_AND_BACK);
1417 CacheHandler->setCullFace(true);
1419 else if (material.BackfaceCulling)
1421 CacheHandler->setCullFaceFunc(GL_BACK);
1422 CacheHandler->setCullFace(true);
1424 else if (material.FrontfaceCulling)
1426 CacheHandler->setCullFaceFunc(GL_FRONT);
1427 CacheHandler->setCullFace(true);
1431 CacheHandler->setCullFace(false);
1435 CacheHandler->setColorMask(material.ColorMask);
1438 if (material.BlendOperation == EBO_NONE)
1439 CacheHandler->setBlend(false);
1442 CacheHandler->setBlend(true);
1444 switch (material.BlendOperation)
1447 CacheHandler->setBlendEquation(GL_FUNC_ADD);
1450 CacheHandler->setBlendEquation(GL_FUNC_SUBTRACT);
1452 case EBO_REVSUBTRACT:
1453 CacheHandler->setBlendEquation(GL_FUNC_REVERSE_SUBTRACT);
1461 if (IR(material.BlendFactor) & 0xFFFFFFFF // TODO: why the & 0xFFFFFFFF?
1462 && material.MaterialType != EMT_ONETEXTURE_BLEND
1465 E_BLEND_FACTOR srcRGBFact = EBF_ZERO;
1466 E_BLEND_FACTOR dstRGBFact = EBF_ZERO;
1467 E_BLEND_FACTOR srcAlphaFact = EBF_ZERO;
1468 E_BLEND_FACTOR dstAlphaFact = EBF_ZERO;
1469 E_MODULATE_FUNC modulo = EMFN_MODULATE_1X;
1470 u32 alphaSource = 0;
1472 unpack_textureBlendFuncSeparate(srcRGBFact, dstRGBFact, srcAlphaFact, dstAlphaFact, modulo, alphaSource, material.BlendFactor);
1474 CacheHandler->setBlendFuncSeparate(getGLBlend(srcRGBFact), getGLBlend(dstRGBFact),
1475 getGLBlend(srcAlphaFact), getGLBlend(dstAlphaFact));
1478 // TODO: Polygon Offset. Not sure if it was left out deliberately or if it won't work with this driver.
1480 if (resetAllRenderStates || lastmaterial.Thickness != material.Thickness)
1481 glLineWidth(core::clamp(static_cast<GLfloat>(material.Thickness), DimAliasedLine[0], DimAliasedLine[1]));
1484 if (resetAllRenderStates || lastmaterial.AntiAliasing != material.AntiAliasing)
1486 if (material.AntiAliasing & EAAM_ALPHA_TO_COVERAGE)
1487 glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
1488 else if (lastmaterial.AntiAliasing & EAAM_ALPHA_TO_COVERAGE)
1489 glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
1492 // Texture parameters
1493 setTextureRenderStates(material, resetAllRenderStates);
1496 //! Compare in SMaterial doesn't check texture parameters, so we should call this on each OnRender call.
1497 void COpenGL3DriverBase::setTextureRenderStates(const SMaterial& material, bool resetAllRenderstates)
1499 // Set textures to TU/TIU and apply filters to them
1501 for (s32 i = Feature.MaxTextureUnits - 1; i >= 0; --i)
1503 const COpenGL3Texture* tmpTexture = CacheHandler->getTextureCache()[i];
1508 GLenum tmpTextureType = tmpTexture->getOpenGLTextureType();
1510 CacheHandler->setActiveTexture(GL_TEXTURE0 + i);
1512 if (resetAllRenderstates)
1513 tmpTexture->getStatesCache().IsCached = false;
1515 if (!tmpTexture->getStatesCache().IsCached || material.TextureLayer[i].BilinearFilter != tmpTexture->getStatesCache().BilinearFilter ||
1516 material.TextureLayer[i].TrilinearFilter != tmpTexture->getStatesCache().TrilinearFilter)
1518 glTexParameteri(tmpTextureType, GL_TEXTURE_MAG_FILTER,
1519 (material.TextureLayer[i].BilinearFilter || material.TextureLayer[i].TrilinearFilter) ? GL_LINEAR : GL_NEAREST);
1521 tmpTexture->getStatesCache().BilinearFilter = material.TextureLayer[i].BilinearFilter;
1522 tmpTexture->getStatesCache().TrilinearFilter = material.TextureLayer[i].TrilinearFilter;
1525 if (material.UseMipMaps && tmpTexture->hasMipMaps())
1527 if (!tmpTexture->getStatesCache().IsCached || material.TextureLayer[i].BilinearFilter != tmpTexture->getStatesCache().BilinearFilter ||
1528 material.TextureLayer[i].TrilinearFilter != tmpTexture->getStatesCache().TrilinearFilter || !tmpTexture->getStatesCache().MipMapStatus)
1530 glTexParameteri(tmpTextureType, GL_TEXTURE_MIN_FILTER,
1531 material.TextureLayer[i].TrilinearFilter ? GL_LINEAR_MIPMAP_LINEAR :
1532 material.TextureLayer[i].BilinearFilter ? GL_LINEAR_MIPMAP_NEAREST :
1533 GL_NEAREST_MIPMAP_NEAREST);
1535 tmpTexture->getStatesCache().BilinearFilter = material.TextureLayer[i].BilinearFilter;
1536 tmpTexture->getStatesCache().TrilinearFilter = material.TextureLayer[i].TrilinearFilter;
1537 tmpTexture->getStatesCache().MipMapStatus = true;
1542 if (!tmpTexture->getStatesCache().IsCached || material.TextureLayer[i].BilinearFilter != tmpTexture->getStatesCache().BilinearFilter ||
1543 material.TextureLayer[i].TrilinearFilter != tmpTexture->getStatesCache().TrilinearFilter || tmpTexture->getStatesCache().MipMapStatus)
1545 glTexParameteri(tmpTextureType, GL_TEXTURE_MIN_FILTER,
1546 (material.TextureLayer[i].BilinearFilter || material.TextureLayer[i].TrilinearFilter) ? GL_LINEAR : GL_NEAREST);
1548 tmpTexture->getStatesCache().BilinearFilter = material.TextureLayer[i].BilinearFilter;
1549 tmpTexture->getStatesCache().TrilinearFilter = material.TextureLayer[i].TrilinearFilter;
1550 tmpTexture->getStatesCache().MipMapStatus = false;
1554 #ifdef GL_EXT_texture_filter_anisotropic
1555 if (FeatureAvailable[COGLESCoreExtensionHandler::IRR_GL_EXT_texture_filter_anisotropic] &&
1556 (!tmpTexture->getStatesCache().IsCached || material.TextureLayer[i].AnisotropicFilter != tmpTexture->getStatesCache().AnisotropicFilter))
1558 glTexParameteri(tmpTextureType, GL_TEXTURE_MAX_ANISOTROPY_EXT,
1559 material.TextureLayer[i].AnisotropicFilter>1 ? core::min_(MaxAnisotropy, material.TextureLayer[i].AnisotropicFilter) : 1);
1561 tmpTexture->getStatesCache().AnisotropicFilter = material.TextureLayer[i].AnisotropicFilter;
1565 if (!tmpTexture->getStatesCache().IsCached || material.TextureLayer[i].TextureWrapU != tmpTexture->getStatesCache().WrapU)
1567 glTexParameteri(tmpTextureType, GL_TEXTURE_WRAP_S, getTextureWrapMode(material.TextureLayer[i].TextureWrapU));
1568 tmpTexture->getStatesCache().WrapU = material.TextureLayer[i].TextureWrapU;
1571 if (!tmpTexture->getStatesCache().IsCached || material.TextureLayer[i].TextureWrapV != tmpTexture->getStatesCache().WrapV)
1573 glTexParameteri(tmpTextureType, GL_TEXTURE_WRAP_T, getTextureWrapMode(material.TextureLayer[i].TextureWrapV));
1574 tmpTexture->getStatesCache().WrapV = material.TextureLayer[i].TextureWrapV;
1577 tmpTexture->getStatesCache().IsCached = true;
1582 // Get OpenGL ES2.0 texture wrap mode from Irrlicht wrap mode.
1583 GLint COpenGL3DriverBase::getTextureWrapMode(u8 clamp) const
1588 case ETC_CLAMP_TO_EDGE:
1589 case ETC_CLAMP_TO_BORDER:
1590 return GL_CLAMP_TO_EDGE;
1599 //! sets the needed renderstates
1600 void COpenGL3DriverBase::setRenderStates2DMode(bool alpha, bool texture, bool alphaChannel)
1602 if ( LockRenderStateMode )
1605 COpenGL3Renderer2D* nextActiveRenderer = texture ? MaterialRenderer2DTexture : MaterialRenderer2DNoTexture;
1607 if (CurrentRenderMode != ERM_2D)
1609 // unset last 3d material
1610 if (CurrentRenderMode == ERM_3D)
1612 if (static_cast<u32>(LastMaterial.MaterialType) < MaterialRenderers.size())
1613 MaterialRenderers[LastMaterial.MaterialType].Renderer->OnUnsetMaterial();
1616 CurrentRenderMode = ERM_2D;
1618 else if ( MaterialRenderer2DActive && MaterialRenderer2DActive != nextActiveRenderer)
1620 MaterialRenderer2DActive->OnUnsetMaterial();
1623 MaterialRenderer2DActive = nextActiveRenderer;
1625 MaterialRenderer2DActive->OnSetMaterial(Material, LastMaterial, true, 0);
1626 LastMaterial = Material;
1627 CacheHandler->correctCacheMaterial(LastMaterial);
1629 // no alphaChannel without texture
1630 alphaChannel &= texture;
1632 if (alphaChannel || alpha)
1634 CacheHandler->setBlend(true);
1635 CacheHandler->setBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1636 CacheHandler->setBlendEquation(GL_FUNC_ADD);
1639 CacheHandler->setBlend(false);
1641 Material.setTexture(0, const_cast<COpenGL3Texture*>(CacheHandler->getTextureCache().get(0)));
1642 setTransform(ETS_TEXTURE_0, core::IdentityMatrix);
1646 if (OverrideMaterial2DEnabled)
1647 setTextureRenderStates(OverrideMaterial2D, false);
1649 setTextureRenderStates(InitMaterial2D, false);
1652 MaterialRenderer2DActive->OnRender(this, video::EVT_STANDARD);
1656 void COpenGL3DriverBase::chooseMaterial2D()
1658 if (!OverrideMaterial2DEnabled)
1659 Material = InitMaterial2D;
1661 if (OverrideMaterial2DEnabled)
1663 OverrideMaterial2D.Lighting=false;
1664 OverrideMaterial2D.ZWriteEnable=EZW_OFF;
1665 OverrideMaterial2D.ZBuffer=ECFN_DISABLED; // it will be ECFN_DISABLED after merge
1666 OverrideMaterial2D.Lighting=false;
1668 Material = OverrideMaterial2D;
1673 //! \return Returns the name of the video driver.
1674 const wchar_t* COpenGL3DriverBase::getName() const
1676 return Name.c_str();
1679 void COpenGL3DriverBase::setViewPort(const core::rect<s32>& area)
1681 core::rect<s32> vp = area;
1682 core::rect<s32> rendert(0, 0, getCurrentRenderTargetSize().Width, getCurrentRenderTargetSize().Height);
1683 vp.clipAgainst(rendert);
1685 if (vp.getHeight() > 0 && vp.getWidth() > 0)
1686 CacheHandler->setViewport(vp.UpperLeftCorner.X, getCurrentRenderTargetSize().Height - vp.UpperLeftCorner.Y - vp.getHeight(), vp.getWidth(), vp.getHeight());
1692 void COpenGL3DriverBase::setViewPortRaw(u32 width, u32 height)
1694 CacheHandler->setViewport(0, 0, width, height);
1695 ViewPort = core::recti(0, 0, width, height);
1699 //! Draws a 3d line.
1700 void COpenGL3DriverBase::draw3DLine(const core::vector3df& start,
1701 const core::vector3df& end, SColor color)
1703 setRenderStates3DMode();
1705 S3DVertex vertices[2];
1706 vertices[0] = S3DVertex(start.X, start.Y, start.Z, 0, 0, 1, color, 0, 0);
1707 vertices[1] = S3DVertex(end.X, end.Y, end.Z, 0, 0, 1, color, 0, 0);
1709 drawArrays(GL_LINES, vtPrimitive, vertices, 2);
1713 //! Only used by the internal engine. Used to notify the driver that
1714 //! the window was resized.
1715 void COpenGL3DriverBase::OnResize(const core::dimension2d<u32>& size)
1717 CNullDriver::OnResize(size);
1718 CacheHandler->setViewport(0, 0, size.Width, size.Height);
1719 Transformation3DChanged = true;
1723 //! Returns type of video driver
1724 E_DRIVER_TYPE COpenGL3DriverBase::getDriverType() const
1730 //! returns color format
1731 ECOLOR_FORMAT COpenGL3DriverBase::getColorFormat() const
1737 //! Get a vertex shader constant index.
1738 s32 COpenGL3DriverBase::getVertexShaderConstantID(const c8* name)
1740 return getPixelShaderConstantID(name);
1743 //! Get a pixel shader constant index.
1744 s32 COpenGL3DriverBase::getPixelShaderConstantID(const c8* name)
1746 os::Printer::log("Error: Please call services->getPixelShaderConstantID(), not VideoDriver->getPixelShaderConstantID().");
1750 //! Sets a vertex shader constant.
1751 void COpenGL3DriverBase::setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount)
1753 os::Printer::log("Error: Please call services->setVertexShaderConstant(), not VideoDriver->setPixelShaderConstant().");
1756 //! Sets a pixel shader constant.
1757 void COpenGL3DriverBase::setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount)
1759 os::Printer::log("Error: Please call services->setPixelShaderConstant(), not VideoDriver->setPixelShaderConstant().");
1762 //! Sets a constant for the vertex shader based on an index.
1763 bool COpenGL3DriverBase::setVertexShaderConstant(s32 index, const f32* floats, int count)
1765 os::Printer::log("Error: Please call services->setVertexShaderConstant(), not VideoDriver->setVertexShaderConstant().");
1769 //! Int interface for the above.
1770 bool COpenGL3DriverBase::setVertexShaderConstant(s32 index, const s32* ints, int count)
1772 os::Printer::log("Error: Please call services->setVertexShaderConstant(), not VideoDriver->setVertexShaderConstant().");
1776 bool COpenGL3DriverBase::setVertexShaderConstant(s32 index, const u32* ints, int count)
1778 os::Printer::log("Error: Please call services->setVertexShaderConstant(), not VideoDriver->setVertexShaderConstant().");
1782 //! Sets a constant for the pixel shader based on an index.
1783 bool COpenGL3DriverBase::setPixelShaderConstant(s32 index, const f32* floats, int count)
1785 os::Printer::log("Error: Please call services->setPixelShaderConstant(), not VideoDriver->setPixelShaderConstant().");
1789 //! Int interface for the above.
1790 bool COpenGL3DriverBase::setPixelShaderConstant(s32 index, const s32* ints, int count)
1792 os::Printer::log("Error: Please call services->setPixelShaderConstant(), not VideoDriver->setPixelShaderConstant().");
1796 bool COpenGL3DriverBase::setPixelShaderConstant(s32 index, const u32* ints, int count)
1798 os::Printer::log("Error: Please call services->setPixelShaderConstant(), not VideoDriver->setPixelShaderConstant().");
1802 //! Adds a new material renderer to the VideoDriver, using pixel and/or
1803 //! vertex shaders to render geometry.
1804 s32 COpenGL3DriverBase::addShaderMaterial(const c8* vertexShaderProgram,
1805 const c8* pixelShaderProgram,
1806 IShaderConstantSetCallBack* callback,
1807 E_MATERIAL_TYPE baseMaterial, s32 userData)
1809 os::Printer::log("No shader support.");
1814 //! Adds a new material renderer to the VideoDriver, using GLSL to render geometry.
1815 s32 COpenGL3DriverBase::addHighLevelShaderMaterial(
1816 const c8* vertexShaderProgram,
1817 const c8* vertexShaderEntryPointName,
1818 E_VERTEX_SHADER_TYPE vsCompileTarget,
1819 const c8* pixelShaderProgram,
1820 const c8* pixelShaderEntryPointName,
1821 E_PIXEL_SHADER_TYPE psCompileTarget,
1822 const c8* geometryShaderProgram,
1823 const c8* geometryShaderEntryPointName,
1824 E_GEOMETRY_SHADER_TYPE gsCompileTarget,
1825 scene::E_PRIMITIVE_TYPE inType,
1826 scene::E_PRIMITIVE_TYPE outType,
1828 IShaderConstantSetCallBack* callback,
1829 E_MATERIAL_TYPE baseMaterial,
1833 COpenGL3MaterialRenderer* r = new COpenGL3MaterialRenderer(
1834 this, nr, vertexShaderProgram,
1836 callback, baseMaterial, userData);
1842 //! Returns a pointer to the IVideoDriver interface. (Implementation for
1843 //! IMaterialRendererServices)
1844 IVideoDriver* COpenGL3DriverBase::getVideoDriver()
1850 //! Returns pointer to the IGPUProgrammingServices interface.
1851 IGPUProgrammingServices* COpenGL3DriverBase::getGPUProgrammingServices()
1856 ITexture* COpenGL3DriverBase::addRenderTargetTexture(const core::dimension2d<u32>& size,
1857 const io::path& name, const ECOLOR_FORMAT format)
1859 //disable mip-mapping
1860 bool generateMipLevels = getTextureCreationFlag(ETCF_CREATE_MIP_MAPS);
1861 setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, false);
1863 COpenGL3Texture* renderTargetTexture = new COpenGL3Texture(name, size, ETT_2D, format, this);
1864 addTexture(renderTargetTexture);
1865 renderTargetTexture->drop();
1867 //restore mip-mapping
1868 setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, generateMipLevels);
1870 return renderTargetTexture;
1873 ITexture* COpenGL3DriverBase::addRenderTargetTextureCubemap(const irr::u32 sideLen, const io::path& name, const ECOLOR_FORMAT format)
1875 //disable mip-mapping
1876 bool generateMipLevels = getTextureCreationFlag(ETCF_CREATE_MIP_MAPS);
1877 setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, false);
1879 bool supportForFBO = (Feature.ColorAttachment > 0);
1881 const core::dimension2d<u32> size(sideLen, sideLen);
1882 core::dimension2du destSize(size);
1886 destSize = core::dimension2d<u32>(core::min_(size.Width, ScreenSize.Width), core::min_(size.Height, ScreenSize.Height));
1887 destSize = destSize.getOptimalSize((size == size.getOptimalSize()), false, false);
1890 COpenGL3Texture* renderTargetTexture = new COpenGL3Texture(name, destSize, ETT_CUBEMAP, format, this);
1891 addTexture(renderTargetTexture);
1892 renderTargetTexture->drop();
1894 //restore mip-mapping
1895 setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, generateMipLevels);
1897 return renderTargetTexture;
1901 //! Returns the maximum amount of primitives
1902 u32 COpenGL3DriverBase::getMaximalPrimitiveCount() const
1907 bool COpenGL3DriverBase::setRenderTargetEx(IRenderTarget* target, u16 clearFlag, SColor clearColor, f32 clearDepth, u8 clearStencil)
1909 if (target && target->getDriverType() != getDriverType())
1911 os::Printer::log("Fatal Error: Tried to set a render target not owned by OpenGL 3 driver.", ELL_ERROR);
1915 core::dimension2d<u32> destRenderTargetSize(0, 0);
1919 COpenGL3RenderTarget* renderTarget = static_cast<COpenGL3RenderTarget*>(target);
1921 CacheHandler->setFBO(renderTarget->getBufferID());
1922 renderTarget->update();
1924 destRenderTargetSize = renderTarget->getSize();
1926 setViewPortRaw(destRenderTargetSize.Width, destRenderTargetSize.Height);
1930 CacheHandler->setFBO(0);
1932 destRenderTargetSize = core::dimension2d<u32>(0, 0);
1934 setViewPortRaw(ScreenSize.Width, ScreenSize.Height);
1937 if (CurrentRenderTargetSize != destRenderTargetSize)
1939 CurrentRenderTargetSize = destRenderTargetSize;
1941 Transformation3DChanged = true;
1944 CurrentRenderTarget = target;
1946 clearBuffers(clearFlag, clearColor, clearDepth, clearStencil);
1951 void COpenGL3DriverBase::clearBuffers(u16 flag, SColor color, f32 depth, u8 stencil)
1953 GLbitfield mask = 0;
1955 bool depthMask = false;
1957 CacheHandler->getColorMask(colorMask);
1958 CacheHandler->getDepthMask(depthMask);
1960 if (flag & ECBF_COLOR)
1962 CacheHandler->setColorMask(ECP_ALL);
1964 const f32 inv = 1.0f / 255.0f;
1965 glClearColor(color.getRed() * inv, color.getGreen() * inv,
1966 color.getBlue() * inv, color.getAlpha() * inv);
1968 mask |= GL_COLOR_BUFFER_BIT;
1971 if (flag & ECBF_DEPTH)
1973 CacheHandler->setDepthMask(true);
1974 glClearDepthf(depth);
1975 mask |= GL_DEPTH_BUFFER_BIT;
1978 if (flag & ECBF_STENCIL)
1980 glClearStencil(stencil);
1981 mask |= GL_STENCIL_BUFFER_BIT;
1987 CacheHandler->setColorMask(colorMask);
1988 CacheHandler->setDepthMask(depthMask);
1992 //! Returns an image created from the last rendered frame.
1993 // We want to read the front buffer to get the latest render finished.
1994 // This is not possible under ogl-es, though, so one has to call this method
1995 // outside of the render loop only.
1996 IImage* COpenGL3DriverBase::createScreenShot(video::ECOLOR_FORMAT format, video::E_RENDER_TARGET target)
1998 if (target==video::ERT_MULTI_RENDER_TEXTURES || target==video::ERT_RENDER_TEXTURE || target==video::ERT_STEREO_BOTH_BUFFERS)
2001 GLint internalformat = GL_RGBA;
2002 GLint type = GL_UNSIGNED_BYTE;
2004 // glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &internalformat);
2005 // glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &type);
2006 // there's a format we don't support ATM
2007 if (GL_UNSIGNED_SHORT_4_4_4_4 == type)
2009 internalformat = GL_RGBA;
2010 type = GL_UNSIGNED_BYTE;
2014 IImage* newImage = 0;
2015 if (GL_RGBA == internalformat)
2017 if (GL_UNSIGNED_BYTE == type)
2018 newImage = new CImage(ECF_A8R8G8B8, ScreenSize);
2020 newImage = new CImage(ECF_A1R5G5B5, ScreenSize);
2024 if (GL_UNSIGNED_BYTE == type)
2025 newImage = new CImage(ECF_R8G8B8, ScreenSize);
2027 newImage = new CImage(ECF_R5G6B5, ScreenSize);
2033 u8* pixels = static_cast<u8*>(newImage->getData());
2040 glReadPixels(0, 0, ScreenSize.Width, ScreenSize.Height, internalformat, type, pixels);
2041 testGLError(__LINE__);
2043 // opengl images are horizontally flipped, so we have to fix that here.
2044 const s32 pitch = newImage->getPitch();
2045 u8* p2 = pixels + (ScreenSize.Height - 1) * pitch;
2046 u8* tmpBuffer = new u8[pitch];
2047 for (u32 i = 0; i < ScreenSize.Height; i += 2)
2049 memcpy(tmpBuffer, pixels, pitch);
2050 memcpy(pixels, p2, pitch);
2051 memcpy(p2, tmpBuffer, pitch);
2055 delete [] tmpBuffer;
2057 // also GL_RGBA doesn't match the internal encoding of the image (which is BGRA)
2058 if (GL_RGBA == internalformat && GL_UNSIGNED_BYTE == type)
2060 pixels = static_cast<u8*>(newImage->getData());
2061 for (u32 i = 0; i < ScreenSize.Height; i++)
2063 for (u32 j = 0; j < ScreenSize.Width; j++)
2065 u32 c = *(u32*) (pixels + 4 * j);
2066 *(u32*) (pixels + 4 * j) = (c & 0xFF00FF00) |
2067 ((c & 0x00FF0000) >> 16) | ((c & 0x000000FF) << 16);
2073 if (testGLError(__LINE__))
2078 testGLError(__LINE__);
2082 void COpenGL3DriverBase::removeTexture(ITexture* texture)
2084 CacheHandler->getTextureCache().remove(texture);
2085 CNullDriver::removeTexture(texture);
2088 //! Set/unset a clipping plane.
2089 bool COpenGL3DriverBase::setClipPlane(u32 index, const core::plane3df& plane, bool enable)
2091 if (index >= UserClipPlane.size())
2092 UserClipPlane.push_back(SUserClipPlane());
2094 UserClipPlane[index].Plane = plane;
2095 UserClipPlane[index].Enabled = enable;
2099 //! Enable/disable a clipping plane.
2100 void COpenGL3DriverBase::enableClipPlane(u32 index, bool enable)
2102 UserClipPlane[index].Enabled = enable;
2105 //! Get the ClipPlane Count
2106 u32 COpenGL3DriverBase::getClipPlaneCount() const
2108 return UserClipPlane.size();
2111 const core::plane3df& COpenGL3DriverBase::getClipPlane(irr::u32 index) const
2113 if (index < UserClipPlane.size())
2114 return UserClipPlane[index].Plane;
2117 _IRR_DEBUG_BREAK_IF(true) // invalid index
2118 static const core::plane3df dummy;
2123 core::dimension2du COpenGL3DriverBase::getMaxTextureSize() const
2125 return core::dimension2du(MaxTextureSize, MaxTextureSize);
2128 GLenum COpenGL3DriverBase::getGLBlend(E_BLEND_FACTOR factor) const
2130 static GLenum const blendTable[] =
2135 GL_ONE_MINUS_DST_COLOR,
2137 GL_ONE_MINUS_SRC_COLOR,
2139 GL_ONE_MINUS_SRC_ALPHA,
2141 GL_ONE_MINUS_DST_ALPHA,
2142 GL_SRC_ALPHA_SATURATE
2145 return blendTable[factor];
2148 bool COpenGL3DriverBase::getColorFormatParameters(ECOLOR_FORMAT format, GLint& internalFormat, GLenum& pixelFormat,
2149 GLenum& pixelType, void(**converter)(const void*, s32, void*)) const
2151 bool supported = false;
2152 pixelFormat = GL_RGBA;
2153 pixelType = GL_UNSIGNED_BYTE;
2160 pixelFormat = GL_RGBA;
2161 pixelType = GL_UNSIGNED_SHORT_5_5_5_1;
2162 *converter = CColorConverter::convert_A1R5G5B5toR5G5B5A1;
2166 pixelFormat = GL_RGB;
2167 pixelType = GL_UNSIGNED_SHORT_5_6_5;
2171 pixelFormat = GL_RGB;
2172 pixelType = GL_UNSIGNED_BYTE;
2176 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_IMG_texture_format_BGRA8888) ||
2177 queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_EXT_texture_format_BGRA8888) ||
2178 queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_APPLE_texture_format_BGRA8888))
2180 pixelFormat = GL_BGRA;
2184 pixelFormat = GL_RGBA;
2185 *converter = CColorConverter::convert_A8R8G8B8toA8B8G8R8;
2187 pixelType = GL_UNSIGNED_BYTE;
2189 #ifdef GL_EXT_texture_compression_s3tc
2192 pixelFormat = GL_RGBA;
2193 pixelType = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
2198 pixelFormat = GL_RGBA;
2199 pixelType = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
2204 pixelFormat = GL_RGBA;
2205 pixelType = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
2208 #ifdef GL_OES_compressed_ETC1_RGB8_texture
2211 pixelFormat = GL_RGB;
2212 pixelType = GL_ETC1_RGB8_OES;
2215 #ifdef GL_ES_VERSION_3_0 // TO-DO - fix when extension name will be available
2218 pixelFormat = GL_RGB;
2219 pixelType = GL_COMPRESSED_RGB8_ETC2;
2222 #ifdef GL_ES_VERSION_3_0 // TO-DO - fix when extension name will be available
2225 pixelFormat = GL_RGBA;
2226 pixelType = GL_COMPRESSED_RGBA8_ETC2_EAC;
2231 pixelFormat = GL_DEPTH_COMPONENT;
2232 pixelType = GL_UNSIGNED_SHORT;
2235 #if defined(GL_OES_depth32)
2236 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_OES_depth32))
2239 pixelFormat = GL_DEPTH_COMPONENT;
2240 pixelType = GL_UNSIGNED_INT;
2245 #ifdef GL_OES_packed_depth_stencil
2246 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_OES_packed_depth_stencil))
2249 pixelFormat = GL_DEPTH_STENCIL_OES;
2250 pixelType = GL_UNSIGNED_INT_24_8_OES;
2255 #if defined(GL_EXT_texture_rg)
2256 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_EXT_texture_rg))
2259 pixelFormat = GL_RED_EXT;
2260 pixelType = GL_UNSIGNED_BYTE;
2265 #if defined(GL_EXT_texture_rg)
2266 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_EXT_texture_rg))
2269 pixelFormat = GL_RG_EXT;
2270 pixelType = GL_UNSIGNED_BYTE;
2279 #if defined(GL_OES_texture_half_float) && defined(GL_EXT_texture_rg)
2280 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_EXT_texture_rg)
2281 && queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_OES_texture_half_float)
2285 pixelFormat = GL_RED_EXT;
2286 pixelType = GL_HALF_FLOAT_OES ;
2291 #if defined(GL_OES_texture_half_float) && defined(GL_EXT_texture_rg)
2292 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_EXT_texture_rg)
2293 && queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_OES_texture_half_float)
2297 pixelFormat = GL_RG_EXT;
2298 pixelType = GL_HALF_FLOAT_OES ;
2302 case ECF_A16B16G16R16F:
2303 #if defined(GL_OES_texture_half_float)
2304 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_OES_texture_half_float))
2307 pixelFormat = GL_RGBA;
2308 pixelType = GL_HALF_FLOAT_OES ;
2313 #if defined(GL_OES_texture_float) && defined(GL_EXT_texture_rg)
2314 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_EXT_texture_rg)
2315 && queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_OES_texture_float)
2319 pixelFormat = GL_RED_EXT;
2320 pixelType = GL_FLOAT;
2325 #if defined(GL_OES_texture_float) && defined(GL_EXT_texture_rg)
2326 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_EXT_texture_rg)
2327 && queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_OES_texture_float)
2331 pixelFormat = GL_RG_EXT;
2332 pixelType = GL_FLOAT;
2336 case ECF_A32B32G32R32F:
2337 #if defined(GL_OES_texture_float)
2338 if (queryGLESFeature(COGLESCoreExtensionHandler::IRR_GL_OES_texture_half_float))
2341 pixelFormat = GL_RGBA;
2342 pixelType = GL_FLOAT ;
2350 // ES 2.0 says internalFormat must match pixelFormat (chapter 3.7.1 in Spec).
2351 // Doesn't mention if "match" means "equal" or some other way of matching, but
2352 // some bug on Emscripten and browsing discussions by others lead me to believe
2353 // it means they have to be equal. Note that this was different in OpenGL.
2354 internalFormat = pixelFormat;
2356 #ifdef _IRR_IOS_PLATFORM_
2357 if (internalFormat == GL_BGRA)
2358 internalFormat = GL_RGBA;
2364 bool COpenGL3DriverBase::queryTextureFormat(ECOLOR_FORMAT format) const
2366 GLint dummyInternalFormat;
2367 GLenum dummyPixelFormat;
2368 GLenum dummyPixelType;
2369 void (*dummyConverter)(const void*, s32, void*);
2370 return getColorFormatParameters(format, dummyInternalFormat, dummyPixelFormat, dummyPixelType, &dummyConverter);
2373 bool COpenGL3DriverBase::needsTransparentRenderPass(const irr::video::SMaterial& material) const
2375 return CNullDriver::needsTransparentRenderPass(material) || material.isAlphaBlendOperation();
2378 const SMaterial& COpenGL3DriverBase::getCurrentMaterial() const
2383 COpenGL3CacheHandler* COpenGL3DriverBase::getCacheHandler() const
2385 return CacheHandler;