LCOV - code coverage report
Current view: top level - source/renderer/backend/gl - DeviceCommandContext.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 767 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 62 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "DeviceCommandContext.h"
      21             : 
      22             : #include "lib/config2.h"
      23             : #include "lib/debug.h"
      24             : #include "ps/CLogger.h"
      25             : #include "renderer/backend/Barrier.h"
      26             : #include "renderer/backend/Format.h"
      27             : #include "renderer/backend/IFramebuffer.h"
      28             : #include "renderer/backend/IShaderProgram.h"
      29             : #include "renderer/backend/ITexture.h"
      30             : #include "renderer/backend/PipelineState.h"
      31             : #include "renderer/backend/Sampler.h"
      32             : #include "renderer/backend/gl/Buffer.h"
      33             : #include "renderer/backend/gl/Device.h"
      34             : #include "renderer/backend/gl/Framebuffer.h"
      35             : #include "renderer/backend/gl/Mapping.h"
      36             : #include "renderer/backend/gl/PipelineState.h"
      37             : #include "renderer/backend/gl/ShaderProgram.h"
      38             : #include "renderer/backend/gl/Texture.h"
      39             : 
      40             : #include <algorithm>
      41             : #include <cstring>
      42             : #include <limits>
      43             : #include <vector>
      44             : 
      45             : namespace Renderer
      46             : {
      47             : 
      48             : namespace Backend
      49             : {
      50             : 
      51             : namespace GL
      52             : {
      53             : 
      54             : namespace
      55             : {
      56             : 
      57           0 : bool operator==(const SStencilOpState& lhs, const SStencilOpState& rhs)
      58             : {
      59           0 :     return
      60           0 :         lhs.failOp == rhs.failOp &&
      61           0 :         lhs.passOp == rhs.passOp &&
      62           0 :         lhs.depthFailOp == rhs.depthFailOp &&
      63           0 :         lhs.compareOp == rhs.compareOp;
      64             : }
      65           0 : bool operator!=(const SStencilOpState& lhs, const SStencilOpState& rhs)
      66             : {
      67           0 :     return !operator==(lhs, rhs);
      68             : }
      69             : 
      70           0 : bool operator==(
      71             :     const CDeviceCommandContext::Rect& lhs,
      72             :     const CDeviceCommandContext::Rect& rhs)
      73             : {
      74           0 :     return
      75           0 :         lhs.x == rhs.x && lhs.y == rhs.y &&
      76           0 :         lhs.width == rhs.width && lhs.height == rhs.height;
      77             : }
      78             : 
      79           0 : bool operator!=(
      80             :     const CDeviceCommandContext::Rect& lhs,
      81             :     const CDeviceCommandContext::Rect& rhs)
      82             : {
      83           0 :     return !operator==(lhs, rhs);
      84             : }
      85             : 
      86           0 : void ApplyDepthMask(const bool depthWriteEnabled)
      87             : {
      88           0 :     glDepthMask(depthWriteEnabled ? GL_TRUE : GL_FALSE);
      89           0 : }
      90             : 
      91           0 : void ApplyColorMask(const uint8_t colorWriteMask)
      92             : {
      93           0 :     glColorMask(
      94             :         (colorWriteMask & ColorWriteMask::RED) != 0 ? GL_TRUE : GL_FALSE,
      95           0 :         (colorWriteMask & ColorWriteMask::GREEN) != 0 ? GL_TRUE : GL_FALSE,
      96           0 :         (colorWriteMask & ColorWriteMask::BLUE) != 0 ? GL_TRUE : GL_FALSE,
      97           0 :         (colorWriteMask & ColorWriteMask::ALPHA) != 0 ? GL_TRUE : GL_FALSE);
      98           0 : }
      99             : 
     100           0 : void ApplyStencilMask(const uint32_t stencilWriteMask)
     101             : {
     102           0 :     glStencilMask(stencilWriteMask);
     103           0 : }
     104             : 
     105           0 : GLenum BufferTypeToGLTarget(const CBuffer::Type type)
     106             : {
     107           0 :     GLenum target = GL_ARRAY_BUFFER;
     108           0 :     switch (type)
     109             :     {
     110             :     case CBuffer::Type::VERTEX:
     111             :         target = GL_ARRAY_BUFFER;
     112             :         break;
     113             :     case CBuffer::Type::INDEX:
     114           0 :         target = GL_ELEMENT_ARRAY_BUFFER;
     115             :         break;
     116           0 :     case CBuffer::Type::UNIFORM:
     117             : #if !CONFIG2_GLES
     118           0 :         target = GL_UNIFORM_BUFFER;
     119             : #else
     120             :         debug_warn("Unsupported buffer type.");
     121             : #endif
     122           0 :         break;
     123           0 :     case CBuffer::Type::UPLOAD:
     124           0 :         debug_warn("Unsupported buffer type.");
     125             :         break;
     126           0 :     };
     127           0 :     return target;
     128             : }
     129             : 
     130           0 : void UploadDynamicBufferRegionImpl(
     131             :     const GLenum target, const uint32_t bufferSize,
     132             :     const uint32_t dataOffset, const uint32_t dataSize,
     133             :     const CDeviceCommandContext::UploadBufferFunction& uploadFunction)
     134             : {
     135           0 :     ENSURE(dataOffset < dataSize);
     136             :     // Tell the driver that it can reallocate the whole VBO
     137           0 :     glBufferData(target, bufferSize, nullptr, GL_DYNAMIC_DRAW);
     138             : 
     139           0 :     while (true)
     140             :     {
     141             :         // (In theory, glMapBufferRange with GL_MAP_INVALIDATE_BUFFER_BIT could be used
     142             :         // here instead of glBufferData(..., NULL, ...) plus glMapBuffer(), but with
     143             :         // current Intel Windows GPU drivers (as of 2015-01) it's much faster if you do
     144             :         // the explicit glBufferData.)
     145           0 :         void* mappedData = glMapBuffer(target, GL_WRITE_ONLY);
     146           0 :         if (mappedData == nullptr)
     147             :         {
     148             :             // This shouldn't happen unless we run out of virtual address space
     149           0 :             LOGERROR("glMapBuffer failed");
     150           0 :             break;
     151             :         }
     152             : 
     153           0 :         uploadFunction(static_cast<std::uint8_t*>(mappedData) + dataOffset);
     154             : 
     155           0 :         if (glUnmapBuffer(target) == GL_TRUE)
     156             :             break;
     157             : 
     158             :         // Unmap might fail on e.g. resolution switches, so just try again
     159             :         // and hope it will eventually succeed
     160           0 :         LOGMESSAGE("glUnmapBuffer failed, trying again...\n");
     161           0 :     }
     162           0 : }
     163             : 
     164             : /**
     165             :  * In case we don't need a framebuffer content (because of the following clear
     166             :  * or overwriting by a shader) we might give a hint to a driver via
     167             :  * glInvalidateFramebuffer.
     168             :  */
     169           0 : void InvalidateFramebuffer(
     170             :     CFramebuffer* framebuffer, const bool color, const bool depthStencil)
     171             : {
     172           0 :     GLsizei numberOfAttachments = 0;
     173           0 :     GLenum attachments[8];
     174           0 :     const bool isBackbuffer = framebuffer->GetHandle() == 0;
     175           0 :     if (color && (framebuffer->GetAttachmentMask() & GL_COLOR_BUFFER_BIT))
     176             :     {
     177           0 :         if (isBackbuffer)
     178             : #if CONFIG2_GLES
     179             :             attachments[numberOfAttachments++] = GL_COLOR_EXT;
     180             : #else
     181           0 :             attachments[numberOfAttachments++] = GL_COLOR;
     182             : #endif
     183             :         else
     184           0 :             attachments[numberOfAttachments++] = GL_COLOR_ATTACHMENT0;
     185             :     }
     186           0 :     if (depthStencil)
     187             :     {
     188           0 :         if (isBackbuffer)
     189             :         {
     190           0 :             if (framebuffer->GetAttachmentMask() & GL_DEPTH_BUFFER_BIT)
     191             : #if CONFIG2_GLES
     192             :                 attachments[numberOfAttachments++] = GL_DEPTH_EXT;
     193             : #else
     194           0 :                 attachments[numberOfAttachments++] = GL_DEPTH;
     195             : #endif
     196           0 :             if (framebuffer->GetAttachmentMask() & GL_STENCIL_BUFFER_BIT)
     197             : #if CONFIG2_GLES
     198             :                 attachments[numberOfAttachments++] = GL_STENCIL_EXT;
     199             : #else
     200           0 :                 attachments[numberOfAttachments++] = GL_STENCIL;
     201             : #endif
     202             :         }
     203             :         else
     204             :         {
     205           0 :             if (framebuffer->GetAttachmentMask() & GL_DEPTH_BUFFER_BIT)
     206           0 :                 attachments[numberOfAttachments++] = GL_DEPTH_ATTACHMENT;
     207           0 :             if (framebuffer->GetAttachmentMask() & GL_STENCIL_BUFFER_BIT)
     208           0 :                 attachments[numberOfAttachments++] = GL_STENCIL_ATTACHMENT;
     209             :         }
     210             :     }
     211             : 
     212           0 :     if (numberOfAttachments > 0)
     213             :     {
     214             : #if CONFIG2_GLES
     215             :         glDiscardFramebufferEXT(GL_FRAMEBUFFER_EXT, numberOfAttachments, attachments);
     216             : #else
     217           0 :         glInvalidateFramebuffer(GL_FRAMEBUFFER_EXT, numberOfAttachments, attachments);
     218             : #endif
     219             :     }
     220           0 : }
     221             : 
     222             : } // anonymous namespace
     223             : 
     224             : // static
     225           0 : std::unique_ptr<CDeviceCommandContext> CDeviceCommandContext::Create(CDevice* device)
     226             : {
     227           0 :     std::unique_ptr<CDeviceCommandContext> deviceCommandContext(new CDeviceCommandContext(device));
     228           0 :     deviceCommandContext->ResetStates();
     229           0 :     return deviceCommandContext;
     230           0 : }
     231             : 
     232           0 : CDeviceCommandContext::CDeviceCommandContext(CDevice* device)
     233           0 :     : m_Device(device)
     234             : {
     235           0 :     glActiveTexture(GL_TEXTURE0);
     236           0 :     glBindTexture(GL_TEXTURE_2D, 0);
     237           0 :     for (BindUnit& unit : m_BoundTextures)
     238             :     {
     239           0 :         unit.target = GL_TEXTURE_2D;
     240           0 :         unit.handle = 0;
     241             :     }
     242           0 :     for (size_t index = 0; index < m_VertexAttributeFormat.size(); ++index)
     243             :     {
     244           0 :         m_VertexAttributeFormat[index].active = false;
     245           0 :         m_VertexAttributeFormat[index].initialized = false;
     246           0 :         m_VertexAttributeFormat[index].bindingSlot = 0;
     247             :     }
     248             : 
     249           0 :     for (size_t index = 0; index < m_BoundBuffers.size(); ++index)
     250             :     {
     251           0 :         const CBuffer::Type type = static_cast<CBuffer::Type>(index);
     252             :         // Currently we don't support upload buffers for GL.
     253           0 :         if (type == CBuffer::Type::UPLOAD)
     254           0 :             continue;
     255             : #if CONFIG2_GLES
     256             :         if (type == CBuffer::Type::UNIFORM)
     257             :             continue;
     258             : #endif
     259           0 :         const GLenum target = BufferTypeToGLTarget(type);
     260           0 :         const GLuint handle = 0;
     261           0 :         m_BoundBuffers[index].first = target;
     262           0 :         m_BoundBuffers[index].second = handle;
     263             :     }
     264           0 : }
     265             : 
     266           0 : CDeviceCommandContext::~CDeviceCommandContext() = default;
     267             : 
     268           0 : IDevice* CDeviceCommandContext::GetDevice()
     269             : {
     270           0 :     return m_Device;
     271             : }
     272             : 
     273           0 : void CDeviceCommandContext::SetGraphicsPipelineState(
     274             :     const SGraphicsPipelineStateDesc& pipelineState)
     275             : {
     276           0 :     ENSURE(!pipelineState.shaderProgram || m_InsideFramebufferPass);
     277           0 :     SetGraphicsPipelineStateImpl(pipelineState, false);
     278           0 : }
     279             : 
     280           0 : void CDeviceCommandContext::SetGraphicsPipelineState(
     281             :     IGraphicsPipelineState* pipelineState)
     282             : {
     283           0 :     ENSURE(!pipelineState->GetShaderProgram() || m_InsideFramebufferPass);
     284           0 :     ENSURE(pipelineState);
     285           0 :     SetGraphicsPipelineStateImpl(
     286             :         pipelineState->As<CGraphicsPipelineState>()->GetDesc(), false);
     287           0 : }
     288             : 
     289           0 : void CDeviceCommandContext::SetComputePipelineState(
     290             :     IComputePipelineState* pipelineState)
     291             : {
     292           0 :     ENSURE(m_InsideComputePass);
     293           0 :     ENSURE(pipelineState);
     294           0 :     const SComputePipelineStateDesc& desc = pipelineState->As<CComputePipelineState>()->GetDesc();
     295           0 :     if (m_ComputePipelineStateDesc.shaderProgram != desc.shaderProgram)
     296             :     {
     297           0 :         CShaderProgram* currentShaderProgram = nullptr;
     298           0 :         if (m_ComputePipelineStateDesc.shaderProgram)
     299           0 :             currentShaderProgram = m_ComputePipelineStateDesc.shaderProgram->As<CShaderProgram>();
     300           0 :         CShaderProgram* nextShaderProgram = nullptr;
     301           0 :         if (desc.shaderProgram)
     302           0 :             nextShaderProgram = desc.shaderProgram->As<CShaderProgram>();
     303             : 
     304           0 :         if (nextShaderProgram)
     305           0 :             nextShaderProgram->Bind(currentShaderProgram);
     306           0 :         else if (currentShaderProgram)
     307           0 :             currentShaderProgram->Unbind();
     308             : 
     309           0 :         m_ShaderProgram = nextShaderProgram;
     310             :     }
     311           0 : }
     312             : 
     313           0 : void CDeviceCommandContext::UploadTexture(
     314             :     ITexture* texture, const Format format,
     315             :     const void* data, const size_t dataSize,
     316             :     const uint32_t level, const uint32_t layer)
     317             : {
     318           0 :     UploadTextureRegion(texture, format, data, dataSize,
     319             :         0, 0,
     320           0 :         std::max(1u, texture->GetWidth() >> level),
     321           0 :         std::max(1u, texture->GetHeight() >> level),
     322             :         level, layer);
     323           0 : }
     324             : 
     325           0 : void CDeviceCommandContext::UploadTextureRegion(
     326             :     ITexture* destinationTexture, const Format dataFormat,
     327             :     const void* data, const size_t dataSize,
     328             :     const uint32_t xOffset, const uint32_t yOffset,
     329             :     const uint32_t width, const uint32_t height,
     330             :     const uint32_t level, const uint32_t layer)
     331             : {
     332           0 :     ENSURE(destinationTexture);
     333           0 :     CTexture* texture = destinationTexture->As<CTexture>();
     334           0 :     ENSURE(texture->GetUsage() & Renderer::Backend::ITexture::Usage::TRANSFER_DST);
     335           0 :     ENSURE(width > 0 && height > 0);
     336           0 :     if (texture->GetType() == CTexture::Type::TEXTURE_2D)
     337             :     {
     338           0 :         ENSURE(layer == 0);
     339           0 :         if (texture->GetFormat() == Format::R8G8B8A8_UNORM ||
     340           0 :             texture->GetFormat() == Format::R8G8B8_UNORM ||
     341             : #if !CONFIG2_GLES
     342           0 :             texture->GetFormat() == Format::R8_UNORM ||
     343             : #endif
     344           0 :             texture->GetFormat() == Format::A8_UNORM)
     345             :         {
     346           0 :             ENSURE(texture->GetFormat() == dataFormat);
     347           0 :             size_t bytesPerPixel = 4;
     348           0 :             GLenum pixelFormat = GL_RGBA;
     349           0 :             switch (dataFormat)
     350             :             {
     351             :             case Format::R8G8B8A8_UNORM: [[fallthrough]];
     352             :             case Format::R8G8B8A8_SRGB:
     353             :                 break;
     354             :             case Format::R8G8B8_UNORM:
     355           0 :                 pixelFormat = GL_RGB;
     356           0 :                 bytesPerPixel = 3;
     357             :                 break;
     358             : #if !CONFIG2_GLES
     359           0 :             case Format::R8_UNORM:
     360           0 :                 pixelFormat = GL_RED;
     361           0 :                 bytesPerPixel = 1;
     362           0 :                 break;
     363             : #endif
     364           0 :             case Format::A8_UNORM:
     365           0 :                 pixelFormat = GL_ALPHA;
     366           0 :                 bytesPerPixel = 1;
     367           0 :                 break;
     368           0 :             case Format::L8_UNORM:
     369           0 :                 pixelFormat = GL_LUMINANCE;
     370           0 :                 bytesPerPixel = 1;
     371           0 :                 break;
     372           0 :             default:
     373           0 :                 debug_warn("Unexpected format.");
     374             :                 break;
     375             :             }
     376           0 :             ENSURE(dataSize == width * height * bytesPerPixel);
     377             : 
     378           0 :             ScopedBind scopedBind(this, GL_TEXTURE_2D, texture->GetHandle());
     379           0 :             glTexSubImage2D(GL_TEXTURE_2D, level,
     380             :                 xOffset, yOffset, width, height,
     381             :                 pixelFormat, GL_UNSIGNED_BYTE, data);
     382             :         }
     383           0 :         else if (
     384           0 :             texture->GetFormat() == Format::BC1_RGB_UNORM ||
     385           0 :             texture->GetFormat() == Format::BC1_RGB_SRGB ||
     386           0 :             texture->GetFormat() == Format::BC1_RGBA_UNORM ||
     387           0 :             texture->GetFormat() == Format::BC1_RGBA_SRGB ||
     388           0 :             texture->GetFormat() == Format::BC2_UNORM ||
     389           0 :             texture->GetFormat() == Format::BC2_SRGB ||
     390           0 :             texture->GetFormat() == Format::BC3_UNORM ||
     391           0 :             texture->GetFormat() == Format::BC3_SRGB)
     392             :         {
     393           0 :             ENSURE(xOffset == 0 && yOffset == 0);
     394           0 :             ENSURE(texture->GetFormat() == dataFormat);
     395             :             // TODO: add data size check.
     396             : 
     397           0 :             GLenum internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
     398           0 :             switch (texture->GetFormat())
     399             :             {
     400             :             case Format::BC1_RGB_SRGB:
     401             :                 internalFormat = GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
     402             :                 break;
     403             :             case Format::BC1_RGBA_UNORM:
     404             :                 internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
     405             :                 break;
     406             :             case Format::BC1_RGBA_SRGB:
     407             :                 internalFormat = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
     408             :                 break;
     409             :             case Format::BC2_UNORM:
     410             :                 internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
     411             :                 break;
     412             :             case Format::BC2_SRGB:
     413             :                 internalFormat = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
     414             :                 break;
     415             :             case Format::BC3_UNORM:
     416             :                 internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
     417             :                 break;
     418             :             case Format::BC3_SRGB:
     419             :                 internalFormat = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
     420             :                 break;
     421             :             default:
     422             :                 break;
     423             :             }
     424             : 
     425           0 :             ScopedBind scopedBind(this, GL_TEXTURE_2D, texture->GetHandle());
     426           0 :             glCompressedTexImage2D(GL_TEXTURE_2D, level, internalFormat, width, height, 0, dataSize, data);
     427             :         }
     428             :         else
     429           0 :             debug_warn("Unsupported format");
     430             :     }
     431           0 :     else if (texture->GetType() == CTexture::Type::TEXTURE_CUBE)
     432             :     {
     433           0 :         if (texture->GetFormat() == Format::R8G8B8A8_UNORM)
     434             :         {
     435           0 :             ENSURE(texture->GetFormat() == dataFormat);
     436           0 :             ENSURE(level == 0 && layer < 6);
     437           0 :             ENSURE(xOffset == 0 && yOffset == 0 && texture->GetWidth() == width && texture->GetHeight() == height);
     438           0 :             const size_t bpp = 4;
     439           0 :             ENSURE(dataSize == width * height * bpp);
     440             : 
     441             :             // The order of layers should be the following:
     442             :             //   front, back, top, bottom, right, left
     443           0 :             static const GLenum targets[6] =
     444             :             {
     445             :                 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
     446             :                 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
     447             :                 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
     448             :                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
     449             :                 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
     450             :                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
     451             :             };
     452             : 
     453           0 :             ScopedBind scopedBind(this, GL_TEXTURE_CUBE_MAP, texture->GetHandle());
     454           0 :             glTexImage2D(targets[layer], level, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
     455             :         }
     456             :         else
     457           0 :             debug_warn("Unsupported format");
     458             :     }
     459             :     else
     460           0 :         debug_warn("Unsupported type");
     461           0 : }
     462             : 
     463           0 : void CDeviceCommandContext::UploadBuffer(IBuffer* buffer, const void* data, const uint32_t dataSize)
     464             : {
     465           0 :     ENSURE(!m_InsideFramebufferPass);
     466           0 :     UploadBufferRegion(buffer, data, 0, dataSize);
     467           0 : }
     468             : 
     469           0 : void CDeviceCommandContext::UploadBuffer(
     470             :     IBuffer* buffer, const UploadBufferFunction& uploadFunction)
     471             : {
     472           0 :     ENSURE(!m_InsideFramebufferPass);
     473           0 :     UploadBufferRegion(buffer, 0, buffer->GetSize(), uploadFunction);
     474           0 : }
     475             : 
     476           0 : void CDeviceCommandContext::UploadBufferRegion(
     477             :     IBuffer* buffer, const void* data, const uint32_t dataOffset, const uint32_t dataSize)
     478             : {
     479           0 :     ENSURE(!m_InsideFramebufferPass);
     480           0 :     ENSURE(data);
     481           0 :     ENSURE(dataOffset + dataSize <= buffer->GetSize());
     482           0 :     const GLenum target = BufferTypeToGLTarget(buffer->GetType());
     483           0 :     ScopedBufferBind scopedBufferBind(this, buffer->As<CBuffer>());
     484             :     // Uniform buffers is a relatively new feature so we don't need to use a
     485             :     // dynamic upload.
     486           0 :     if (buffer->IsDynamic() && buffer->GetType() != IBuffer::Type::UNIFORM)
     487             :     {
     488           0 :         UploadDynamicBufferRegionImpl(target, buffer->GetSize(), dataOffset, dataSize,
     489           0 :             [data, dataSize](std::uint8_t* mappedData)
     490             :         {
     491           0 :             std::memcpy(mappedData, data, dataSize);
     492             :         });
     493             :     }
     494             :     else
     495             :     {
     496           0 :         glBufferSubData(target, dataOffset, dataSize, data);
     497             :     }
     498           0 : }
     499             : 
     500           0 : void CDeviceCommandContext::UploadBufferRegion(
     501             :     IBuffer* buffer, const uint32_t dataOffset, const uint32_t dataSize,
     502             :     const UploadBufferFunction& uploadFunction)
     503             : {
     504           0 :     ENSURE(!m_InsideFramebufferPass);
     505           0 :     ENSURE(dataOffset + dataSize <= buffer->GetSize());
     506           0 :     const GLenum target = BufferTypeToGLTarget(buffer->GetType());
     507           0 :     ScopedBufferBind scopedBufferBind(this, buffer->As<CBuffer>());
     508           0 :     ENSURE(buffer->IsDynamic());
     509           0 :     UploadDynamicBufferRegionImpl(target, buffer->GetSize(), dataOffset, dataSize, uploadFunction);
     510           0 : }
     511             : 
     512           0 : void CDeviceCommandContext::InsertTimestampQuery(const uint32_t handle, const bool /*isScopeBegin*/)
     513             : {
     514             :     // GL can have the only one command context so we can call commands on
     515             :     // the deivce side.
     516           0 :     m_Device->InsertTimestampQuery(handle);
     517           0 : }
     518             : 
     519           0 : void CDeviceCommandContext::BeginScopedLabel(const char* name)
     520             : {
     521           0 :     if (!m_Device->GetCapabilities().debugScopedLabels)
     522             :         return;
     523             : 
     524           0 :     ++m_ScopedLabelDepth;
     525           0 :     glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, 0x0AD, -1, name);
     526             : }
     527             : 
     528           0 : void CDeviceCommandContext::EndScopedLabel()
     529             : {
     530           0 :     if (!m_Device->GetCapabilities().debugScopedLabels)
     531             :         return;
     532             : 
     533           0 :     ENSURE(m_ScopedLabelDepth > 0);
     534           0 :     --m_ScopedLabelDepth;
     535           0 :     glPopDebugGroup();
     536             : }
     537             : 
     538           0 : void CDeviceCommandContext::BindTexture(
     539             :     const uint32_t unit, const GLenum target, const GLuint handle)
     540             : {
     541           0 :     ENSURE(unit < m_BoundTextures.size());
     542             : #if CONFIG2_GLES
     543             :     ENSURE(target == GL_TEXTURE_2D || target == GL_TEXTURE_CUBE_MAP);
     544             : #else
     545           0 :     ENSURE(target == GL_TEXTURE_2D || target == GL_TEXTURE_CUBE_MAP || target == GL_TEXTURE_2D_MULTISAMPLE);
     546             : #endif
     547           0 :     if (m_ActiveTextureUnit != unit)
     548             :     {
     549           0 :         glActiveTexture(GL_TEXTURE0 + unit);
     550           0 :         m_ActiveTextureUnit = unit;
     551             :     }
     552           0 :     if (m_BoundTextures[unit].target == target && m_BoundTextures[unit].handle == handle)
     553             :         return;
     554           0 :     if (m_BoundTextures[unit].target != target && m_BoundTextures[unit].target && m_BoundTextures[unit].handle)
     555           0 :         glBindTexture(m_BoundTextures[unit].target, 0);
     556           0 :     if (m_BoundTextures[unit].handle != handle)
     557           0 :         glBindTexture(target, handle);
     558           0 :     m_BoundTextures[unit] = {target, handle};
     559             : }
     560             : 
     561           0 : void CDeviceCommandContext::BindBuffer(const IBuffer::Type type, CBuffer* buffer)
     562             : {
     563           0 :     ENSURE(!buffer || buffer->GetType() == type);
     564           0 :     if (type == IBuffer::Type::VERTEX)
     565             :     {
     566           0 :         if (m_VertexBuffer == buffer)
     567             :             return;
     568           0 :         m_VertexBuffer = buffer;
     569             :     }
     570           0 :     else if (type == IBuffer::Type::INDEX)
     571             :     {
     572           0 :         if (!buffer)
     573           0 :             m_IndexBuffer = nullptr;
     574           0 :         m_IndexBufferData = nullptr;
     575             :     }
     576           0 :     const GLenum target = BufferTypeToGLTarget(type);
     577           0 :     const GLuint handle = buffer ? buffer->GetHandle() : 0;
     578           0 :     glBindBuffer(target, handle);
     579           0 :     const size_t cacheIndex = static_cast<size_t>(type);
     580           0 :     ENSURE(cacheIndex < m_BoundBuffers.size());
     581           0 :     m_BoundBuffers[cacheIndex].second = handle;
     582             : }
     583             : 
     584           0 : void CDeviceCommandContext::OnTextureDestroy(CTexture* texture)
     585             : {
     586           0 :     ENSURE(texture);
     587           0 :     for (size_t index = 0; index < m_BoundTextures.size(); ++index)
     588           0 :         if (m_BoundTextures[index].handle == texture->GetHandle())
     589           0 :             BindTexture(index, GL_TEXTURE_2D, 0);
     590           0 : }
     591             : 
     592           0 : void CDeviceCommandContext::Flush()
     593             : {
     594           0 :     ENSURE(m_ScopedLabelDepth == 0);
     595           0 :     ENSURE(!m_InsideFramebufferPass);
     596           0 :     ENSURE(!m_InsideComputePass);
     597             : 
     598           0 :     GPU_SCOPED_LABEL(this, "CDeviceCommandContext::Flush");
     599             : 
     600           0 :     ResetStates();
     601             : 
     602           0 :     m_IndexBuffer = nullptr;
     603           0 :     m_IndexBufferData = nullptr;
     604             : 
     605           0 :     for (size_t unit = 0; unit < m_BoundTextures.size(); ++unit)
     606             :     {
     607           0 :         if (m_BoundTextures[unit].handle)
     608           0 :             BindTexture(unit, GL_TEXTURE_2D, 0);
     609             :     }
     610           0 :     BindBuffer(CBuffer::Type::INDEX, nullptr);
     611           0 :     BindBuffer(CBuffer::Type::VERTEX, nullptr);
     612           0 : }
     613             : 
     614           0 : void CDeviceCommandContext::ResetStates()
     615             : {
     616           0 :     SetGraphicsPipelineStateImpl(MakeDefaultGraphicsPipelineStateDesc(), true);
     617           0 :     SetScissors(0, nullptr);
     618           0 :     m_Framebuffer = nullptr;
     619           0 :     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
     620           0 : }
     621             : 
     622           0 : void CDeviceCommandContext::SetGraphicsPipelineStateImpl(
     623             :     const SGraphicsPipelineStateDesc& pipelineStateDesc, const bool force)
     624             : {
     625           0 :     ENSURE(!m_InsidePass);
     626             : 
     627           0 :     if (m_GraphicsPipelineStateDesc.shaderProgram != pipelineStateDesc.shaderProgram)
     628             :     {
     629           0 :         CShaderProgram* currentShaderProgram = nullptr;
     630           0 :         if (m_GraphicsPipelineStateDesc.shaderProgram)
     631             :         {
     632             :             currentShaderProgram =
     633             :                 static_cast<CShaderProgram*>(m_GraphicsPipelineStateDesc.shaderProgram);
     634             :         }
     635           0 :         CShaderProgram* nextShaderProgram = nullptr;
     636           0 :         if (pipelineStateDesc.shaderProgram)
     637             :         {
     638           0 :             nextShaderProgram =
     639             :                 static_cast<CShaderProgram*>(pipelineStateDesc.shaderProgram);
     640           0 :             for (size_t index = 0; index < m_VertexAttributeFormat.size(); ++index)
     641             :             {
     642           0 :                 const VertexAttributeStream stream = static_cast<VertexAttributeStream>(index);
     643           0 :                 m_VertexAttributeFormat[index].active = nextShaderProgram->IsStreamActive(stream);
     644           0 :                 m_VertexAttributeFormat[index].initialized = false;
     645           0 :                 m_VertexAttributeFormat[index].bindingSlot = std::numeric_limits<uint32_t>::max();
     646             :             }
     647             :         }
     648           0 :         if (nextShaderProgram)
     649           0 :             nextShaderProgram->Bind(currentShaderProgram);
     650           0 :         else if (currentShaderProgram)
     651           0 :             currentShaderProgram->Unbind();
     652             : 
     653           0 :         m_ShaderProgram = nextShaderProgram;
     654             :     }
     655             : 
     656           0 :     const SDepthStencilStateDesc& currentDepthStencilStateDesc = m_GraphicsPipelineStateDesc.depthStencilState;
     657           0 :     const SDepthStencilStateDesc& nextDepthStencilStateDesc = pipelineStateDesc.depthStencilState;
     658           0 :     if (force || currentDepthStencilStateDesc.depthTestEnabled != nextDepthStencilStateDesc.depthTestEnabled)
     659             :     {
     660           0 :         if (nextDepthStencilStateDesc.depthTestEnabled)
     661           0 :             glEnable(GL_DEPTH_TEST);
     662             :         else
     663           0 :             glDisable(GL_DEPTH_TEST);
     664             :     }
     665           0 :     if (force || currentDepthStencilStateDesc.depthCompareOp != nextDepthStencilStateDesc.depthCompareOp)
     666             :     {
     667           0 :         glDepthFunc(Mapping::FromCompareOp(nextDepthStencilStateDesc.depthCompareOp));
     668             :     }
     669           0 :     if (force || currentDepthStencilStateDesc.depthWriteEnabled != nextDepthStencilStateDesc.depthWriteEnabled)
     670             :     {
     671           0 :         ApplyDepthMask(nextDepthStencilStateDesc.depthWriteEnabled);
     672             :     }
     673             : 
     674           0 :     if (force || currentDepthStencilStateDesc.stencilTestEnabled != nextDepthStencilStateDesc.stencilTestEnabled)
     675             :     {
     676           0 :         if (nextDepthStencilStateDesc.stencilTestEnabled)
     677           0 :             glEnable(GL_STENCIL_TEST);
     678             :         else
     679           0 :             glDisable(GL_STENCIL_TEST);
     680             :     }
     681           0 :     if (force ||
     682           0 :         currentDepthStencilStateDesc.stencilFrontFace != nextDepthStencilStateDesc.stencilFrontFace ||
     683           0 :         currentDepthStencilStateDesc.stencilBackFace != nextDepthStencilStateDesc.stencilBackFace)
     684             :     {
     685           0 :         if (nextDepthStencilStateDesc.stencilFrontFace == nextDepthStencilStateDesc.stencilBackFace)
     686             :         {
     687           0 :             glStencilOp(
     688           0 :                 Mapping::FromStencilOp(nextDepthStencilStateDesc.stencilFrontFace.failOp),
     689           0 :                 Mapping::FromStencilOp(nextDepthStencilStateDesc.stencilFrontFace.depthFailOp),
     690             :                 Mapping::FromStencilOp(nextDepthStencilStateDesc.stencilFrontFace.passOp));
     691             :         }
     692             :         else
     693             :         {
     694           0 :             if (force || currentDepthStencilStateDesc.stencilFrontFace != nextDepthStencilStateDesc.stencilFrontFace)
     695             :             {
     696           0 :                 glStencilOpSeparate(
     697             :                     GL_FRONT,
     698           0 :                     Mapping::FromStencilOp(nextDepthStencilStateDesc.stencilFrontFace.failOp),
     699           0 :                     Mapping::FromStencilOp(nextDepthStencilStateDesc.stencilFrontFace.depthFailOp),
     700           0 :                     Mapping::FromStencilOp(nextDepthStencilStateDesc.stencilFrontFace.passOp));
     701             :             }
     702           0 :             if (force || currentDepthStencilStateDesc.stencilBackFace != nextDepthStencilStateDesc.stencilBackFace)
     703             :             {
     704           0 :                 glStencilOpSeparate(
     705             :                     GL_BACK,
     706           0 :                     Mapping::FromStencilOp(nextDepthStencilStateDesc.stencilBackFace.failOp),
     707           0 :                     Mapping::FromStencilOp(nextDepthStencilStateDesc.stencilBackFace.depthFailOp),
     708           0 :                     Mapping::FromStencilOp(nextDepthStencilStateDesc.stencilBackFace.passOp));
     709             :             }
     710             :         }
     711             :     }
     712           0 :     if (force || currentDepthStencilStateDesc.stencilWriteMask != nextDepthStencilStateDesc.stencilWriteMask)
     713             :     {
     714           0 :         ApplyStencilMask(nextDepthStencilStateDesc.stencilWriteMask);
     715             :     }
     716           0 :     if (force ||
     717           0 :         currentDepthStencilStateDesc.stencilReference != nextDepthStencilStateDesc.stencilReference ||
     718           0 :         currentDepthStencilStateDesc.stencilReadMask != nextDepthStencilStateDesc.stencilReadMask ||
     719           0 :         currentDepthStencilStateDesc.stencilFrontFace.compareOp != nextDepthStencilStateDesc.stencilFrontFace.compareOp ||
     720           0 :         currentDepthStencilStateDesc.stencilBackFace.compareOp != nextDepthStencilStateDesc.stencilBackFace.compareOp)
     721             :     {
     722           0 :         if (nextDepthStencilStateDesc.stencilFrontFace.compareOp == nextDepthStencilStateDesc.stencilBackFace.compareOp)
     723             :         {
     724           0 :             glStencilFunc(
     725             :                 Mapping::FromCompareOp(nextDepthStencilStateDesc.stencilFrontFace.compareOp),
     726           0 :                 nextDepthStencilStateDesc.stencilReference,
     727           0 :                 nextDepthStencilStateDesc.stencilReadMask);
     728             :         }
     729             :         else
     730             :         {
     731           0 :             glStencilFuncSeparate(GL_FRONT,
     732             :                 Mapping::FromCompareOp(nextDepthStencilStateDesc.stencilFrontFace.compareOp),
     733           0 :                 nextDepthStencilStateDesc.stencilReference,
     734           0 :                 nextDepthStencilStateDesc.stencilReadMask);
     735           0 :             glStencilFuncSeparate(GL_BACK,
     736           0 :                 Mapping::FromCompareOp(nextDepthStencilStateDesc.stencilBackFace.compareOp),
     737           0 :                 nextDepthStencilStateDesc.stencilReference,
     738           0 :                 nextDepthStencilStateDesc.stencilReadMask);
     739             :         }
     740             :     }
     741             : 
     742           0 :     const SBlendStateDesc& currentBlendStateDesc = m_GraphicsPipelineStateDesc.blendState;
     743           0 :     const SBlendStateDesc& nextBlendStateDesc = pipelineStateDesc.blendState;
     744           0 :     if (force || currentBlendStateDesc.enabled != nextBlendStateDesc.enabled)
     745             :     {
     746           0 :         if (nextBlendStateDesc.enabled)
     747           0 :             glEnable(GL_BLEND);
     748             :         else
     749           0 :             glDisable(GL_BLEND);
     750             :     }
     751           0 :     if (force ||
     752           0 :         currentBlendStateDesc.srcColorBlendFactor != nextBlendStateDesc.srcColorBlendFactor ||
     753           0 :         currentBlendStateDesc.srcAlphaBlendFactor != nextBlendStateDesc.srcAlphaBlendFactor ||
     754           0 :         currentBlendStateDesc.dstColorBlendFactor != nextBlendStateDesc.dstColorBlendFactor ||
     755           0 :         currentBlendStateDesc.dstAlphaBlendFactor != nextBlendStateDesc.dstAlphaBlendFactor)
     756             :     {
     757           0 :         if (nextBlendStateDesc.srcColorBlendFactor == nextBlendStateDesc.srcAlphaBlendFactor &&
     758           0 :             nextBlendStateDesc.dstColorBlendFactor == nextBlendStateDesc.dstAlphaBlendFactor)
     759             :         {
     760           0 :             glBlendFunc(
     761           0 :                 Mapping::FromBlendFactor(nextBlendStateDesc.srcColorBlendFactor),
     762             :                 Mapping::FromBlendFactor(nextBlendStateDesc.dstColorBlendFactor));
     763             :         }
     764             :         else
     765             :         {
     766           0 :             glBlendFuncSeparate(
     767           0 :                 Mapping::FromBlendFactor(nextBlendStateDesc.srcColorBlendFactor),
     768           0 :                 Mapping::FromBlendFactor(nextBlendStateDesc.dstColorBlendFactor),
     769           0 :                 Mapping::FromBlendFactor(nextBlendStateDesc.srcAlphaBlendFactor),
     770           0 :                 Mapping::FromBlendFactor(nextBlendStateDesc.dstAlphaBlendFactor));
     771             :         }
     772             :     }
     773             : 
     774           0 :     if (force ||
     775           0 :         currentBlendStateDesc.colorBlendOp != nextBlendStateDesc.colorBlendOp ||
     776           0 :         currentBlendStateDesc.alphaBlendOp != nextBlendStateDesc.alphaBlendOp)
     777             :     {
     778           0 :         if (nextBlendStateDesc.colorBlendOp == nextBlendStateDesc.alphaBlendOp)
     779             :         {
     780           0 :             glBlendEquation(Mapping::FromBlendOp(nextBlendStateDesc.colorBlendOp));
     781             :         }
     782             :         else
     783             :         {
     784           0 :             glBlendEquationSeparate(
     785           0 :                 Mapping::FromBlendOp(nextBlendStateDesc.colorBlendOp),
     786             :                 Mapping::FromBlendOp(nextBlendStateDesc.alphaBlendOp));
     787             :         }
     788             :     }
     789             : 
     790           0 :     if (force ||
     791           0 :         currentBlendStateDesc.constant != nextBlendStateDesc.constant)
     792             :     {
     793           0 :         glBlendColor(
     794           0 :             nextBlendStateDesc.constant.r,
     795           0 :             nextBlendStateDesc.constant.g,
     796           0 :             nextBlendStateDesc.constant.b,
     797           0 :             nextBlendStateDesc.constant.a);
     798             :     }
     799             : 
     800           0 :     if (force ||
     801           0 :         currentBlendStateDesc.colorWriteMask != nextBlendStateDesc.colorWriteMask)
     802             :     {
     803           0 :         ApplyColorMask(nextBlendStateDesc.colorWriteMask);
     804             :     }
     805             : 
     806           0 :     const SRasterizationStateDesc& currentRasterizationStateDesc =
     807             :         m_GraphicsPipelineStateDesc.rasterizationState;
     808           0 :     const SRasterizationStateDesc& nextRasterizationStateDesc =
     809             :         pipelineStateDesc.rasterizationState;
     810           0 :     if (force ||
     811           0 :         currentRasterizationStateDesc.polygonMode != nextRasterizationStateDesc.polygonMode)
     812             :     {
     813             : #if !CONFIG2_GLES
     814           0 :         glPolygonMode(
     815             :             GL_FRONT_AND_BACK,
     816           0 :             nextRasterizationStateDesc.polygonMode == PolygonMode::LINE ? GL_LINE : GL_FILL);
     817             : #endif
     818             :     }
     819             : 
     820           0 :     if (force ||
     821           0 :         currentRasterizationStateDesc.cullMode != nextRasterizationStateDesc.cullMode)
     822             :     {
     823           0 :         if (nextRasterizationStateDesc.cullMode == CullMode::NONE)
     824             :         {
     825           0 :             glDisable(GL_CULL_FACE);
     826             :         }
     827             :         else
     828             :         {
     829           0 :             if (force || currentRasterizationStateDesc.cullMode == CullMode::NONE)
     830           0 :                 glEnable(GL_CULL_FACE);
     831           0 :             glCullFace(nextRasterizationStateDesc.cullMode == CullMode::FRONT ? GL_FRONT : GL_BACK);
     832             :         }
     833             :     }
     834             : 
     835           0 :     if (force ||
     836           0 :         currentRasterizationStateDesc.frontFace != nextRasterizationStateDesc.frontFace)
     837             :     {
     838           0 :         if (nextRasterizationStateDesc.frontFace == FrontFace::CLOCKWISE)
     839           0 :             glFrontFace(GL_CW);
     840             :         else
     841           0 :             glFrontFace(GL_CCW);
     842             :     }
     843             : 
     844             : #if !CONFIG2_GLES
     845           0 :     if (force ||
     846           0 :         currentRasterizationStateDesc.depthBiasEnabled != nextRasterizationStateDesc.depthBiasEnabled)
     847             :     {
     848           0 :         if (nextRasterizationStateDesc.depthBiasEnabled)
     849           0 :             glEnable(GL_POLYGON_OFFSET_FILL);
     850             :         else
     851           0 :             glDisable(GL_POLYGON_OFFSET_FILL);
     852             :     }
     853           0 :     if (force ||
     854           0 :         currentRasterizationStateDesc.depthBiasConstantFactor != nextRasterizationStateDesc.depthBiasConstantFactor ||
     855           0 :         currentRasterizationStateDesc.depthBiasSlopeFactor != nextRasterizationStateDesc.depthBiasSlopeFactor)
     856             :     {
     857           0 :         glPolygonOffset(
     858           0 :             nextRasterizationStateDesc.depthBiasSlopeFactor,
     859           0 :             nextRasterizationStateDesc.depthBiasConstantFactor);
     860             :     }
     861             : #endif
     862             : 
     863           0 :     m_GraphicsPipelineStateDesc = pipelineStateDesc;
     864           0 : }
     865             : 
     866           0 : void CDeviceCommandContext::BlitFramebuffer(
     867             :     IFramebuffer* srcFramebuffer, IFramebuffer* dstFramebuffer,
     868             :     [[maybe_unused]] const Rect& sourceRegion, [[maybe_unused]] const Rect& destinationRegion,
     869             :     [[maybe_unused]] const Sampler::Filter filter)
     870             : {
     871           0 :     ENSURE(!m_InsideFramebufferPass);
     872           0 :     [[maybe_unused]] CFramebuffer* destinationFramebuffer = dstFramebuffer->As<CFramebuffer>();
     873           0 :     [[maybe_unused]] CFramebuffer* sourceFramebuffer = srcFramebuffer->As<CFramebuffer>();
     874             : #if CONFIG2_GLES
     875             :     debug_warn("CDeviceCommandContext::BlitFramebuffer is not implemented for GLES");
     876             : #else
     877             :     // Source framebuffer should not be backbuffer.
     878           0 :     ENSURE(sourceFramebuffer->GetHandle() != 0);
     879           0 :     ENSURE(destinationFramebuffer != sourceFramebuffer);
     880           0 :     glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, sourceFramebuffer->GetHandle());
     881           0 :     glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, destinationFramebuffer->GetHandle());
     882             :     // TODO: add more check for internal formats.
     883           0 :     glBlitFramebufferEXT(
     884           0 :         sourceRegion.x, sourceRegion.y, sourceRegion.width, sourceRegion.height,
     885           0 :         destinationRegion.x, destinationRegion.y, destinationRegion.width, destinationRegion.height,
     886           0 :         (sourceFramebuffer->GetAttachmentMask() & destinationFramebuffer->GetAttachmentMask()),
     887             :         filter == Sampler::Filter::LINEAR ? GL_LINEAR : GL_NEAREST);
     888             : #endif
     889           0 : }
     890             : 
     891           0 : void CDeviceCommandContext::ResolveFramebuffer(
     892             :     IFramebuffer* srcFramebuffer, IFramebuffer* dstFramebuffer)
     893             : {
     894           0 :     ENSURE(!m_InsideFramebufferPass);
     895           0 :     CFramebuffer* destinationFramebuffer = dstFramebuffer->As<CFramebuffer>();
     896           0 :     CFramebuffer* sourceFramebuffer = srcFramebuffer->As<CFramebuffer>();
     897           0 :     ENSURE(destinationFramebuffer->GetWidth() == sourceFramebuffer->GetWidth());
     898           0 :     ENSURE(destinationFramebuffer->GetHeight() == sourceFramebuffer->GetHeight());
     899             : #if CONFIG2_GLES
     900             :     debug_warn("CDeviceCommandContext::ResolveFramebuffer is not implemented for GLES");
     901             : #else
     902             :     // Source framebuffer should not be backbuffer.
     903           0 :     ENSURE(sourceFramebuffer->GetHandle() != 0);
     904           0 :     ENSURE(destinationFramebuffer != sourceFramebuffer);
     905           0 :     glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, sourceFramebuffer->GetHandle());
     906           0 :     glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, destinationFramebuffer->GetHandle());
     907           0 :     glBlitFramebufferEXT(
     908           0 :         0, 0, sourceFramebuffer->GetWidth(), sourceFramebuffer->GetHeight(),
     909           0 :         0, 0, sourceFramebuffer->GetWidth(), sourceFramebuffer->GetHeight(),
     910           0 :         (sourceFramebuffer->GetAttachmentMask() & destinationFramebuffer->GetAttachmentMask()),
     911             :         GL_NEAREST);
     912             : #endif
     913           0 : }
     914             : 
     915           0 : void CDeviceCommandContext::ClearFramebuffer(const bool color, const bool depth, const bool stencil)
     916             : {
     917           0 :     ENSURE(m_InsideFramebufferPass);
     918           0 :     const bool needsColor = color && (m_Framebuffer->GetAttachmentMask() & GL_COLOR_BUFFER_BIT) != 0;
     919           0 :     const bool needsDepth = depth && (m_Framebuffer->GetAttachmentMask() & GL_DEPTH_BUFFER_BIT) != 0;
     920           0 :     const bool needsStencil = stencil && (m_Framebuffer->GetAttachmentMask() & GL_STENCIL_BUFFER_BIT) != 0;
     921           0 :     GLbitfield mask = 0;
     922           0 :     if (needsColor)
     923             :     {
     924           0 :         ApplyColorMask(ColorWriteMask::RED | ColorWriteMask::GREEN | ColorWriteMask::BLUE | ColorWriteMask::ALPHA);
     925           0 :         glClearColor(
     926           0 :             m_Framebuffer->GetClearColor().r,
     927           0 :             m_Framebuffer->GetClearColor().g,
     928           0 :             m_Framebuffer->GetClearColor().b,
     929           0 :             m_Framebuffer->GetClearColor().a);
     930           0 :         mask |= GL_COLOR_BUFFER_BIT;
     931             :     }
     932           0 :     if (needsDepth)
     933             :     {
     934           0 :         ApplyDepthMask(true);
     935           0 :         mask |= GL_DEPTH_BUFFER_BIT;
     936             :     }
     937           0 :     if (needsStencil)
     938             :     {
     939           0 :         ApplyStencilMask(std::numeric_limits<uint32_t>::max());
     940           0 :         mask |= GL_STENCIL_BUFFER_BIT;
     941             :     }
     942           0 :     glClear(mask);
     943           0 :     if (needsColor)
     944           0 :         ApplyColorMask(m_GraphicsPipelineStateDesc.blendState.colorWriteMask);
     945           0 :     if (needsDepth)
     946           0 :         ApplyDepthMask(m_GraphicsPipelineStateDesc.depthStencilState.depthWriteEnabled);
     947           0 :     if (needsStencil)
     948           0 :         ApplyStencilMask(m_GraphicsPipelineStateDesc.depthStencilState.stencilWriteMask);
     949           0 : }
     950             : 
     951           0 : void CDeviceCommandContext::BeginFramebufferPass(IFramebuffer* framebuffer)
     952             : {
     953           0 :     SetGraphicsPipelineStateImpl(
     954           0 :         MakeDefaultGraphicsPipelineStateDesc(), false);
     955             : 
     956           0 :     ENSURE(!m_InsideFramebufferPass);
     957           0 :     m_InsideFramebufferPass = true;
     958           0 :     ENSURE(framebuffer);
     959           0 :     m_Framebuffer = framebuffer->As<CFramebuffer>();
     960           0 :     ENSURE(m_Framebuffer->GetHandle() == 0 || (m_Framebuffer->GetWidth() > 0 && m_Framebuffer->GetHeight() > 0));
     961           0 :     glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_Framebuffer->GetHandle());
     962           0 :     if (m_Device->UseFramebufferInvalidating())
     963             :     {
     964           0 :         InvalidateFramebuffer(
     965             :             m_Framebuffer,
     966           0 :             m_Framebuffer->GetColorAttachmentLoadOp() != AttachmentLoadOp::LOAD,
     967           0 :             m_Framebuffer->GetDepthStencilAttachmentLoadOp() != AttachmentLoadOp::LOAD);
     968             :     }
     969           0 :     const bool needsClearColor =
     970           0 :         m_Framebuffer->GetColorAttachmentLoadOp() == AttachmentLoadOp::CLEAR;
     971           0 :     const bool needsClearDepthStencil =
     972           0 :         m_Framebuffer->GetDepthStencilAttachmentLoadOp() == AttachmentLoadOp::CLEAR;
     973           0 :     if (needsClearColor || needsClearDepthStencil)
     974             :     {
     975           0 :         ClearFramebuffer(
     976             :             needsClearColor, needsClearDepthStencil, needsClearDepthStencil);
     977             :     }
     978           0 : }
     979             : 
     980           0 : void CDeviceCommandContext::EndFramebufferPass()
     981             : {
     982           0 :     if (m_Device->UseFramebufferInvalidating())
     983             :     {
     984           0 :         InvalidateFramebuffer(
     985             :             m_Framebuffer,
     986           0 :             m_Framebuffer->GetColorAttachmentStoreOp() != AttachmentStoreOp::STORE,
     987           0 :             m_Framebuffer->GetDepthStencilAttachmentStoreOp() != AttachmentStoreOp::STORE);
     988             :     }
     989           0 :     ENSURE(m_InsideFramebufferPass);
     990           0 :     m_InsideFramebufferPass = false;
     991           0 :     if (m_Framebuffer->GetHandle() != 0)
     992             :     {
     993           0 :         glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
     994             :     }
     995           0 :     m_Framebuffer = nullptr;
     996             : 
     997           0 :     SetGraphicsPipelineStateImpl(MakeDefaultGraphicsPipelineStateDesc(), false);
     998           0 : }
     999             : 
    1000           0 : void CDeviceCommandContext::ReadbackFramebufferSync(
    1001             :     ISwapChain&, const uint32_t x, const uint32_t y,
    1002             :     const uint32_t width, const uint32_t height,
    1003             :     void* data)
    1004             : {
    1005           0 :     glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
    1006           0 : }
    1007             : 
    1008           0 : void CDeviceCommandContext::SetScissors(const uint32_t scissorCount, const Rect* scissors)
    1009             : {
    1010           0 :     ENSURE(scissorCount <= 1);
    1011           0 :     if (scissorCount == 0)
    1012             :     {
    1013           0 :         if (m_ScissorCount != scissorCount)
    1014           0 :             glDisable(GL_SCISSOR_TEST);
    1015             :     }
    1016             :     else
    1017             :     {
    1018           0 :         if (m_ScissorCount != scissorCount)
    1019           0 :             glEnable(GL_SCISSOR_TEST);
    1020           0 :         ENSURE(scissors);
    1021           0 :         if (m_ScissorCount != scissorCount || m_Scissors[0] != scissors[0])
    1022             :         {
    1023           0 :             m_Scissors[0] = scissors[0];
    1024           0 :             glScissor(m_Scissors[0].x, m_Scissors[0].y, m_Scissors[0].width, m_Scissors[0].height);
    1025             :         }
    1026             :     }
    1027           0 :     m_ScissorCount = scissorCount;
    1028           0 : }
    1029             : 
    1030           0 : void CDeviceCommandContext::SetViewports(const uint32_t viewportCount, const Rect* viewports)
    1031             : {
    1032           0 :     ENSURE(m_InsideFramebufferPass);
    1033           0 :     ENSURE(viewportCount == 1);
    1034           0 :     glViewport(viewports[0].x, viewports[0].y, viewports[0].width, viewports[0].height);
    1035           0 : }
    1036             : 
    1037           0 : void CDeviceCommandContext::SetVertexInputLayout(
    1038             :     IVertexInputLayout* vertexInputLayout)
    1039             : {
    1040           0 :     ENSURE(vertexInputLayout);
    1041           0 :     for (const SVertexAttributeFormat& attribute : vertexInputLayout->As<CVertexInputLayout>()->GetAttributes())
    1042             :     {
    1043           0 :         const uint32_t index = static_cast<uint32_t>(attribute.stream);
    1044           0 :         ENSURE(index < m_VertexAttributeFormat.size());
    1045           0 :         ENSURE(attribute.bindingSlot < m_VertexAttributeFormat.size());
    1046           0 :         if (!m_VertexAttributeFormat[index].active)
    1047           0 :             continue;
    1048             : 
    1049           0 :         m_VertexAttributeFormat[index].format = attribute.format;
    1050           0 :         m_VertexAttributeFormat[index].offset = attribute.offset;
    1051           0 :         m_VertexAttributeFormat[index].stride = attribute.stride;
    1052           0 :         m_VertexAttributeFormat[index].rate = attribute.rate;
    1053           0 :         m_VertexAttributeFormat[index].bindingSlot = attribute.bindingSlot;
    1054             : 
    1055           0 :         m_VertexAttributeFormat[index].initialized = true;
    1056             :     }
    1057           0 : }
    1058             : 
    1059           0 : void CDeviceCommandContext::SetVertexBuffer(
    1060             :     const uint32_t bindingSlot, IBuffer* buffer, const uint32_t offset)
    1061             : {
    1062           0 :     ENSURE(buffer);
    1063           0 :     ENSURE(buffer->GetType() == IBuffer::Type::VERTEX);
    1064           0 :     ENSURE(m_ShaderProgram);
    1065           0 :     BindBuffer(buffer->GetType(), buffer->As<CBuffer>());
    1066           0 :     for (size_t index = 0; index < m_VertexAttributeFormat.size(); ++index)
    1067             :     {
    1068           0 :         if (!m_VertexAttributeFormat[index].active || m_VertexAttributeFormat[index].bindingSlot != bindingSlot)
    1069           0 :             continue;
    1070           0 :         ENSURE(m_VertexAttributeFormat[index].initialized);
    1071           0 :         const VertexAttributeStream stream = static_cast<VertexAttributeStream>(index);
    1072           0 :         m_ShaderProgram->VertexAttribPointer(stream,
    1073           0 :             m_VertexAttributeFormat[index].format,
    1074           0 :             m_VertexAttributeFormat[index].offset + offset,
    1075           0 :             m_VertexAttributeFormat[index].stride,
    1076           0 :             m_VertexAttributeFormat[index].rate,
    1077             :             nullptr);
    1078             :     }
    1079           0 : }
    1080             : 
    1081           0 : void CDeviceCommandContext::SetVertexBufferData(
    1082             :     const uint32_t bindingSlot, const void* data, const uint32_t dataSize)
    1083             : {
    1084           0 :     ENSURE(data);
    1085           0 :     ENSURE(m_ShaderProgram);
    1086           0 :     ENSURE(dataSize > 0);
    1087           0 :     BindBuffer(CBuffer::Type::VERTEX, nullptr);
    1088           0 :     for (size_t index = 0; index < m_VertexAttributeFormat.size(); ++index)
    1089             :     {
    1090           0 :         if (!m_VertexAttributeFormat[index].active || m_VertexAttributeFormat[index].bindingSlot != bindingSlot)
    1091           0 :             continue;
    1092           0 :         ENSURE(m_VertexAttributeFormat[index].initialized);
    1093           0 :         const VertexAttributeStream stream = static_cast<VertexAttributeStream>(index);
    1094             :         // We don't know how many vertices will be used in a draw command, so we
    1095             :         // assume at least one vertex.
    1096           0 :         ENSURE(dataSize >= m_VertexAttributeFormat[index].offset + m_VertexAttributeFormat[index].stride);
    1097           0 :         m_ShaderProgram->VertexAttribPointer(stream,
    1098           0 :             m_VertexAttributeFormat[index].format,
    1099           0 :             m_VertexAttributeFormat[index].offset,
    1100           0 :             m_VertexAttributeFormat[index].stride,
    1101           0 :             m_VertexAttributeFormat[index].rate,
    1102             :             data);
    1103             :     }
    1104           0 : }
    1105             : 
    1106           0 : void CDeviceCommandContext::SetIndexBuffer(IBuffer* buffer)
    1107             : {
    1108           0 :     ENSURE(buffer->GetType() == CBuffer::Type::INDEX);
    1109           0 :     m_IndexBuffer = buffer->As<CBuffer>();
    1110           0 :     m_IndexBufferData = nullptr;
    1111           0 :     BindBuffer(CBuffer::Type::INDEX, m_IndexBuffer);
    1112           0 : }
    1113             : 
    1114           0 : void CDeviceCommandContext::SetIndexBufferData(const void* data, const uint32_t dataSize)
    1115             : {
    1116           0 :     ENSURE(dataSize > 0);
    1117           0 :     if (m_IndexBuffer)
    1118             :     {
    1119           0 :         BindBuffer(CBuffer::Type::INDEX, nullptr);
    1120           0 :         m_IndexBuffer = nullptr;
    1121             :     }
    1122           0 :     m_IndexBufferData = data;
    1123           0 : }
    1124             : 
    1125           0 : void CDeviceCommandContext::BeginPass()
    1126             : {
    1127           0 :     ENSURE(!m_InsidePass);
    1128           0 :     m_InsidePass = true;
    1129           0 : }
    1130             : 
    1131           0 : void CDeviceCommandContext::EndPass()
    1132             : {
    1133           0 :     ENSURE(m_InsidePass);
    1134           0 :     m_InsidePass = false;
    1135           0 : }
    1136             : 
    1137           0 : void CDeviceCommandContext::Draw(
    1138             :     const uint32_t firstVertex, const uint32_t vertexCount)
    1139             : {
    1140           0 :     ENSURE(m_ShaderProgram);
    1141           0 :     ENSURE(m_InsidePass);
    1142             :     // Some drivers apparently don't like count = 0 in glDrawArrays here, so skip
    1143             :     // all drawing in that case.
    1144           0 :     if (vertexCount == 0)
    1145             :         return;
    1146           0 :     m_ShaderProgram->AssertPointersBound();
    1147           0 :     glDrawArrays(GL_TRIANGLES, firstVertex, vertexCount);
    1148             : }
    1149             : 
    1150           0 : void CDeviceCommandContext::DrawIndexed(
    1151             :     const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset)
    1152             : {
    1153           0 :     ENSURE(m_ShaderProgram);
    1154           0 :     ENSURE(m_InsidePass);
    1155           0 :     if (indexCount == 0)
    1156             :         return;
    1157           0 :     ENSURE(m_IndexBuffer || m_IndexBufferData);
    1158           0 :     ENSURE(vertexOffset == 0);
    1159           0 :     if (m_IndexBuffer)
    1160             :     {
    1161           0 :         ENSURE(sizeof(uint16_t) * (firstIndex + indexCount) <= m_IndexBuffer->GetSize());
    1162             :     }
    1163           0 :     m_ShaderProgram->AssertPointersBound();
    1164             :     // Don't use glMultiDrawElements here since it doesn't have a significant
    1165             :     // performance impact and it suffers from various driver bugs (e.g. it breaks
    1166             :     // in Mesa 7.10 swrast with index VBOs).
    1167           0 :     glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT,
    1168           0 :         static_cast<const void*>((static_cast<const uint8_t*>(m_IndexBufferData) + sizeof(uint16_t) * firstIndex)));
    1169             : }
    1170             : 
    1171           0 : void CDeviceCommandContext::DrawInstanced(
    1172             :     [[maybe_unused]] const uint32_t firstVertex, const uint32_t vertexCount,
    1173             :     const uint32_t firstInstance, const uint32_t instanceCount)
    1174             : {
    1175           0 :     ENSURE(m_Device->GetCapabilities().instancing);
    1176           0 :     ENSURE(m_ShaderProgram);
    1177           0 :     ENSURE(m_InsidePass);
    1178           0 :     if (vertexCount == 0 || instanceCount == 0)
    1179             :         return;
    1180           0 :     ENSURE(firstInstance == 0);
    1181           0 :     m_ShaderProgram->AssertPointersBound();
    1182             : #if CONFIG2_GLES
    1183             :     ENSURE(!m_Device->GetCapabilities().instancing);
    1184             : #else
    1185           0 :     glDrawArraysInstancedARB(GL_TRIANGLES, firstVertex, vertexCount, instanceCount);
    1186             : #endif
    1187             : }
    1188             : 
    1189           0 : void CDeviceCommandContext::DrawIndexedInstanced(
    1190             :     const uint32_t firstIndex, const uint32_t indexCount,
    1191             :     const uint32_t firstInstance, [[maybe_unused]] const uint32_t instanceCount,
    1192             :     const int32_t vertexOffset)
    1193             : {
    1194           0 :     ENSURE(m_Device->GetCapabilities().instancing);
    1195           0 :     ENSURE(m_ShaderProgram);
    1196           0 :     ENSURE(m_InsidePass);
    1197           0 :     ENSURE(m_IndexBuffer || m_IndexBufferData);
    1198           0 :     if (indexCount == 0)
    1199             :         return;
    1200           0 :     ENSURE(firstInstance == 0 && vertexOffset == 0);
    1201           0 :     if (m_IndexBuffer)
    1202             :     {
    1203           0 :         ENSURE(sizeof(uint16_t) * (firstIndex + indexCount) <= m_IndexBuffer->GetSize());
    1204             :     }
    1205           0 :     m_ShaderProgram->AssertPointersBound();
    1206             :     // Don't use glMultiDrawElements here since it doesn't have a significant
    1207             :     // performance impact and it suffers from various driver bugs (e.g. it breaks
    1208             :     // in Mesa 7.10 swrast with index VBOs).
    1209             : #if CONFIG2_GLES
    1210             :     ENSURE(!m_Device->GetCapabilities().instancing);
    1211             : #else
    1212           0 :     glDrawElementsInstancedARB(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT,
    1213           0 :         static_cast<const void*>((static_cast<const uint8_t*>(m_IndexBufferData) + sizeof(uint16_t) * firstIndex)),
    1214             :         instanceCount);
    1215             : #endif
    1216             : }
    1217             : 
    1218           0 : void CDeviceCommandContext::DrawIndexedInRange(
    1219             :     const uint32_t firstIndex, const uint32_t indexCount,
    1220             :     [[maybe_unused]] const uint32_t start, [[maybe_unused]] const uint32_t end)
    1221             : {
    1222           0 :     ENSURE(m_ShaderProgram);
    1223           0 :     ENSURE(m_InsidePass);
    1224           0 :     if (indexCount == 0)
    1225             :         return;
    1226           0 :     ENSURE(m_IndexBuffer || m_IndexBufferData);
    1227           0 :     const void* indices =
    1228           0 :         static_cast<const void*>((static_cast<const uint8_t*>(m_IndexBufferData) + sizeof(uint16_t) * firstIndex));
    1229           0 :     m_ShaderProgram->AssertPointersBound();
    1230             :     // Draw with DrawRangeElements where available, since it might be more
    1231             :     // efficient for slow hardware.
    1232             : #if CONFIG2_GLES
    1233             :     glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, indices);
    1234             : #else
    1235           0 :     glDrawRangeElements(GL_TRIANGLES, start, end, indexCount, GL_UNSIGNED_SHORT, indices);
    1236             : #endif
    1237             : }
    1238             : 
    1239           0 : void CDeviceCommandContext::BeginComputePass()
    1240             : {
    1241           0 :     ENSURE(!m_InsideFramebufferPass);
    1242           0 :     ENSURE(!m_InsideComputePass);
    1243           0 :     m_InsideComputePass = true;
    1244           0 : }
    1245             : 
    1246           0 : void CDeviceCommandContext::EndComputePass()
    1247             : {
    1248           0 :     ENSURE(m_InsideComputePass);
    1249           0 :     m_InsideComputePass = false;
    1250           0 : }
    1251             : 
    1252           0 : void CDeviceCommandContext::Dispatch(
    1253             :     [[maybe_unused]] const uint32_t groupCountX,
    1254             :     [[maybe_unused]] const uint32_t groupCountY,
    1255             :     [[maybe_unused]] const uint32_t groupCountZ)
    1256             : {
    1257             : #if !CONFIG2_GLES
    1258           0 :     ENSURE(m_InsideComputePass);
    1259           0 :     glDispatchCompute(groupCountX, groupCountY, groupCountZ);
    1260             :     // Storage buffers should be managed explicitly by InsertMemoryBarrier.
    1261           0 :     if (m_ShaderProgram->HasImageUniforms())
    1262             :     {
    1263             :         // TODO: we might want to do binding tracking to avoid redundant barriers.
    1264           0 :         glMemoryBarrier(
    1265             :             GL_SHADER_IMAGE_ACCESS_BARRIER_BIT | GL_TEXTURE_FETCH_BARRIER_BIT | GL_TEXTURE_UPDATE_BARRIER_BIT | GL_FRAMEBUFFER_BARRIER_BIT );
    1266             :     }
    1267             : #endif
    1268           0 : }
    1269             : 
    1270           0 : void CDeviceCommandContext::InsertMemoryBarrier(
    1271             :     const uint32_t /*srcStageMask*/, [[maybe_unused]] const uint32_t dstStageMask,
    1272             :     [[maybe_unused]] const uint32_t srcAccessMask, [[maybe_unused]] const uint32_t dstAccessMask)
    1273             : {
    1274             : #if !CONFIG2_GLES
    1275           0 :     ENSURE(!m_InsideFramebufferPass);
    1276           0 :     GLbitfield barriers{0};
    1277           0 :     if (srcAccessMask & Access::SHADER_WRITE)
    1278             :     {
    1279           0 :         if (dstStageMask & PipelineStage::VERTEX_INPUT)
    1280             :         {
    1281           0 :             if (dstAccessMask & Access::VERTEX_ATTRIBUTE_READ)
    1282           0 :                 barriers |= GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT;
    1283           0 :             if (dstAccessMask & Access::INDEX_READ)
    1284           0 :                 barriers |= GL_ELEMENT_ARRAY_BARRIER_BIT;
    1285             :         }
    1286           0 :         if (dstStageMask & (PipelineStage::VERTEX_SHADER | PipelineStage::FRAGMENT_SHADER | PipelineStage::COMPUTE_SHADER))
    1287             :         {
    1288           0 :             if (dstAccessMask & (Access::SHADER_READ | Access::SHADER_WRITE))
    1289           0 :                 barriers |= GL_SHADER_STORAGE_BARRIER_BIT;
    1290           0 :             if (dstAccessMask & Access::UNIFORM_READ)
    1291           0 :                 barriers |= GL_UNIFORM_BARRIER_BIT;
    1292             :         }
    1293             :     }
    1294           0 :     if (barriers)
    1295           0 :         glMemoryBarrier(barriers);
    1296             : #endif
    1297           0 : }
    1298             : 
    1299           0 : void CDeviceCommandContext::SetTexture(const int32_t bindingSlot, ITexture* texture)
    1300             : {
    1301           0 :     ENSURE(m_ShaderProgram);
    1302           0 :     ENSURE(texture);
    1303           0 :     ENSURE(texture->GetUsage() & Renderer::Backend::ITexture::Usage::SAMPLED);
    1304             : 
    1305           0 :     const CShaderProgram::TextureUnit textureUnit =
    1306           0 :         m_ShaderProgram->GetTextureUnit(bindingSlot);
    1307           0 :     if (!textureUnit.type)
    1308           0 :         return;
    1309             : 
    1310           0 :     if (textureUnit.type != GL_SAMPLER_2D &&
    1311             : #if !CONFIG2_GLES
    1312           0 :         textureUnit.type != GL_SAMPLER_2D_SHADOW &&
    1313             : #endif
    1314             :         textureUnit.type != GL_SAMPLER_CUBE)
    1315             :     {
    1316           0 :         LOGERROR("CDeviceCommandContext::SetTexture: expected sampler at binding slot");
    1317           0 :         return;
    1318             :     }
    1319             : 
    1320             : #if !CONFIG2_GLES
    1321           0 :     if (textureUnit.type == GL_SAMPLER_2D_SHADOW)
    1322             :     {
    1323           0 :         if (!IsDepthFormat(texture->GetFormat()))
    1324             :         {
    1325           0 :             LOGERROR("CDeviceCommandContext::SetTexture: Invalid texture type (expected depth texture)");
    1326           0 :             return;
    1327             :         }
    1328             :     }
    1329             : #endif
    1330             : 
    1331           0 :     ENSURE(textureUnit.unit >= 0);
    1332           0 :     const uint32_t unit = textureUnit.unit;
    1333           0 :     if (unit >= m_BoundTextures.size())
    1334             :     {
    1335           0 :         LOGERROR("CDeviceCommandContext::SetTexture: Invalid texture unit (too big)");
    1336           0 :         return;
    1337             :     }
    1338           0 :     BindTexture(unit, textureUnit.target, texture->As<CTexture>()->GetHandle());
    1339             : }
    1340             : 
    1341           0 : void CDeviceCommandContext::SetStorageTexture([[maybe_unused]] const int32_t bindingSlot,
    1342             :     [[maybe_unused]] ITexture* texture)
    1343             : {
    1344             : #if !CONFIG2_GLES
    1345           0 :     ENSURE(m_ShaderProgram);
    1346           0 :     ENSURE(texture);
    1347           0 :     ENSURE(texture->GetUsage() & Renderer::Backend::ITexture::Usage::STORAGE);
    1348             : 
    1349           0 :     const CShaderProgram::TextureUnit textureUnit =
    1350           0 :         m_ShaderProgram->GetTextureUnit(bindingSlot);
    1351           0 :     if (!textureUnit.type)
    1352           0 :         return;
    1353           0 :     ENSURE(textureUnit.type == GL_IMAGE_2D);
    1354           0 :     ENSURE(texture->GetFormat() == Format::R8G8B8A8_UNORM);
    1355           0 :     glBindImageTexture(textureUnit.unit, texture->As<CTexture>()->GetHandle(), 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8);
    1356             : #endif
    1357             : }
    1358             : 
    1359           0 : void CDeviceCommandContext::SetStorageBuffer([[maybe_unused]] const int32_t bindingSlot,
    1360             :     [[maybe_unused]] IBuffer* buffer)
    1361             : {
    1362             : #if !CONFIG2_GLES
    1363           0 :     if (bindingSlot < 0)
    1364             :         return;
    1365           0 :     ENSURE(m_ShaderProgram);
    1366           0 :     ENSURE(buffer);
    1367           0 :     ENSURE(buffer->GetUsage() & Renderer::Backend::IBuffer::Usage::STORAGE);
    1368           0 :     glBindBufferBase(GL_SHADER_STORAGE_BUFFER, m_ShaderProgram->GetStorageBuffer(bindingSlot), buffer->As<CBuffer>()->GetHandle());
    1369             : #endif
    1370             : }
    1371             : 
    1372           0 : void CDeviceCommandContext::SetUniform(
    1373             :     const int32_t bindingSlot,
    1374             :     const float value)
    1375             : {
    1376           0 :     ENSURE(m_ShaderProgram);
    1377           0 :     m_ShaderProgram->SetUniform(bindingSlot, value);
    1378           0 : }
    1379             : 
    1380           0 : void CDeviceCommandContext::SetUniform(
    1381             :     const int32_t bindingSlot,
    1382             :     const float valueX, const float valueY)
    1383             : {
    1384           0 :     ENSURE(m_ShaderProgram);
    1385           0 :     m_ShaderProgram->SetUniform(bindingSlot, valueX, valueY);
    1386           0 : }
    1387             : 
    1388           0 : void CDeviceCommandContext::SetUniform(
    1389             :     const int32_t bindingSlot,
    1390             :     const float valueX, const float valueY,
    1391             :     const float valueZ)
    1392             : {
    1393           0 :     ENSURE(m_ShaderProgram);
    1394           0 :     m_ShaderProgram->SetUniform(bindingSlot, valueX, valueY, valueZ);
    1395           0 : }
    1396             : 
    1397           0 : void CDeviceCommandContext::SetUniform(
    1398             :     const int32_t bindingSlot,
    1399             :     const float valueX, const float valueY,
    1400             :     const float valueZ, const float valueW)
    1401             : {
    1402           0 :     ENSURE(m_ShaderProgram);
    1403           0 :     m_ShaderProgram->SetUniform(bindingSlot, valueX, valueY, valueZ, valueW);
    1404           0 : }
    1405             : 
    1406           0 : void CDeviceCommandContext::SetUniform(
    1407             :     const int32_t bindingSlot, std::span<const float> values)
    1408             : {
    1409           0 :     ENSURE(m_ShaderProgram);
    1410           0 :     m_ShaderProgram->SetUniform(bindingSlot, values);
    1411           0 : }
    1412             : 
    1413           0 : CDeviceCommandContext::ScopedBind::ScopedBind(
    1414             :     CDeviceCommandContext* deviceCommandContext,
    1415           0 :     const GLenum target, const GLuint handle)
    1416           0 :     : m_DeviceCommandContext(deviceCommandContext),
    1417           0 :     m_OldBindUnit(deviceCommandContext->m_BoundTextures[deviceCommandContext->m_ActiveTextureUnit]),
    1418           0 :     m_ActiveTextureUnit(deviceCommandContext->m_ActiveTextureUnit)
    1419             : {
    1420           0 :     const uint32_t unit = m_DeviceCommandContext->m_BoundTextures.size() - 1;
    1421           0 :     m_DeviceCommandContext->BindTexture(unit, target, handle);
    1422           0 : }
    1423             : 
    1424           0 : CDeviceCommandContext::ScopedBind::~ScopedBind()
    1425             : {
    1426           0 :     m_DeviceCommandContext->BindTexture(
    1427             :         m_ActiveTextureUnit, m_OldBindUnit.target, m_OldBindUnit.handle);
    1428           0 : }
    1429             : 
    1430           0 : CDeviceCommandContext::ScopedBufferBind::ScopedBufferBind(
    1431           0 :     CDeviceCommandContext* deviceCommandContext, CBuffer* buffer)
    1432           0 :     : m_DeviceCommandContext(deviceCommandContext)
    1433             : {
    1434           0 :     ENSURE(buffer);
    1435           0 :     m_CacheIndex = static_cast<size_t>(buffer->GetType());
    1436           0 :     ENSURE(m_CacheIndex < m_DeviceCommandContext->m_BoundBuffers.size());
    1437           0 :     const GLenum target = BufferTypeToGLTarget(buffer->GetType());
    1438           0 :     const GLuint handle = buffer->GetHandle();
    1439           0 :     if (m_DeviceCommandContext->m_BoundBuffers[m_CacheIndex].first == target &&
    1440           0 :         m_DeviceCommandContext->m_BoundBuffers[m_CacheIndex].second == handle)
    1441             :     {
    1442             :         // Use an invalid index as a sign that we don't need to restore the
    1443             :         // bound buffer.
    1444           0 :         m_CacheIndex = m_DeviceCommandContext->m_BoundBuffers.size();
    1445             :     }
    1446             :     else
    1447             :     {
    1448           0 :         glBindBuffer(target, handle);
    1449             :     }
    1450           0 : }
    1451             : 
    1452           0 : CDeviceCommandContext::ScopedBufferBind::~ScopedBufferBind()
    1453             : {
    1454           0 :     if (m_CacheIndex >= m_DeviceCommandContext->m_BoundBuffers.size())
    1455           0 :         return;
    1456           0 :     glBindBuffer(
    1457           0 :         m_DeviceCommandContext->m_BoundBuffers[m_CacheIndex].first,
    1458           0 :         m_DeviceCommandContext->m_BoundBuffers[m_CacheIndex].second);
    1459           0 : }
    1460             : 
    1461             : } // namespace GL
    1462             : 
    1463             : } // namespace Backend
    1464             : 
    1465             : } // namespace Renderer

Generated by: LCOV version 1.16