2022-01-05 15:49:54 +01:00
|
|
|
/* Copyright (C) 2022 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "DeviceCommandContext.h"
|
|
|
|
|
2022-01-19 18:28:47 +01:00
|
|
|
#include "renderer/backend/gl/Mapping.h"
|
2022-01-05 15:49:54 +01:00
|
|
|
#include "renderer/backend/gl/Texture.h"
|
|
|
|
|
|
|
|
namespace Renderer
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace Backend
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace GL
|
|
|
|
{
|
|
|
|
|
|
|
|
// static
|
|
|
|
std::unique_ptr<CDeviceCommandContext> CDeviceCommandContext::Create()
|
|
|
|
{
|
|
|
|
std::unique_ptr<CDeviceCommandContext> deviceCommandContext(new CDeviceCommandContext());
|
2022-01-19 18:28:47 +01:00
|
|
|
deviceCommandContext->ResetStates();
|
2022-01-05 15:49:54 +01:00
|
|
|
return deviceCommandContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDeviceCommandContext::CDeviceCommandContext() = default;
|
|
|
|
|
|
|
|
CDeviceCommandContext::~CDeviceCommandContext() = default;
|
|
|
|
|
2022-01-19 18:28:47 +01:00
|
|
|
void CDeviceCommandContext::SetGraphicsPipelineState(
|
|
|
|
const GraphicsPipelineStateDesc& pipelineStateDesc)
|
|
|
|
{
|
|
|
|
SetGraphicsPipelineStateImpl(pipelineStateDesc, false);
|
|
|
|
}
|
|
|
|
|
2022-01-05 15:49:54 +01:00
|
|
|
void CDeviceCommandContext::UploadTexture(
|
2022-01-07 21:00:41 +01:00
|
|
|
CTexture* texture, const Format format,
|
|
|
|
const void* data, const size_t dataSize,
|
|
|
|
const uint32_t level, const uint32_t layer)
|
2022-01-05 15:49:54 +01:00
|
|
|
{
|
2022-01-07 21:00:41 +01:00
|
|
|
UploadTextureRegion(texture, format, data, dataSize,
|
|
|
|
0, 0, texture->GetWidth(), texture->GetHeight(), level, layer);
|
2022-01-05 15:49:54 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 21:00:41 +01:00
|
|
|
void CDeviceCommandContext::UploadTextureRegion(
|
2022-01-08 14:44:40 +01:00
|
|
|
CTexture* texture, const Format dataFormat,
|
2022-01-07 21:00:41 +01:00
|
|
|
const void* data, const size_t dataSize,
|
|
|
|
const uint32_t xOffset, const uint32_t yOffset,
|
|
|
|
const uint32_t width, const uint32_t height,
|
|
|
|
const uint32_t level, const uint32_t layer)
|
2022-01-05 15:49:54 +01:00
|
|
|
{
|
|
|
|
if (texture->GetType() == CTexture::Type::TEXTURE_2D)
|
|
|
|
{
|
2022-01-07 21:00:41 +01:00
|
|
|
ENSURE(level == 0 && layer == 0);
|
2022-01-05 15:49:54 +01:00
|
|
|
if (texture->GetFormat() == Format::R8G8B8A8 || texture->GetFormat() == Format::A8)
|
|
|
|
{
|
|
|
|
ENSURE(width > 0 && height > 0);
|
2022-01-08 14:44:40 +01:00
|
|
|
ENSURE(texture->GetFormat() == dataFormat);
|
|
|
|
const size_t bpp = dataFormat == Format::R8G8B8A8 ? 4 : 1;
|
2022-01-05 15:49:54 +01:00
|
|
|
ENSURE(dataSize == width * height * bpp);
|
|
|
|
ENSURE(xOffset + width <= texture->GetWidth());
|
|
|
|
ENSURE(yOffset + height <= texture->GetHeight());
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->GetHandle());
|
2022-01-07 21:00:41 +01:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, level,
|
2022-01-05 15:49:54 +01:00
|
|
|
xOffset, yOffset, width, height,
|
2022-01-08 14:44:40 +01:00
|
|
|
dataFormat == Format::R8G8B8A8 ? GL_RGBA : GL_ALPHA, GL_UNSIGNED_BYTE, data);
|
2022-01-05 15:49:54 +01:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
ogl_WarnIfError();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
debug_warn("Unsupported format");
|
|
|
|
}
|
2022-01-07 21:00:41 +01:00
|
|
|
else if (texture->GetType() == CTexture::Type::TEXTURE_CUBE)
|
|
|
|
{
|
|
|
|
if (texture->GetFormat() == Format::R8G8B8A8)
|
|
|
|
{
|
2022-01-08 14:44:40 +01:00
|
|
|
ENSURE(texture->GetFormat() == dataFormat);
|
|
|
|
ENSURE(level == 0 && layer < 6);
|
2022-01-07 21:00:41 +01:00
|
|
|
ENSURE(xOffset == 0 && yOffset == 0 && texture->GetWidth() == width && texture->GetHeight() == height);
|
|
|
|
const size_t bpp = 4;
|
|
|
|
ENSURE(dataSize == width * height * bpp);
|
|
|
|
|
|
|
|
// The order of layers should be the following:
|
|
|
|
// front, back, top, bottom, right, left
|
|
|
|
static const GLenum targets[6] =
|
|
|
|
{
|
|
|
|
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
|
|
|
|
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
|
|
|
|
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
|
|
|
|
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
|
|
|
|
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
|
|
|
|
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
|
|
|
|
};
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, texture->GetHandle());
|
|
|
|
glTexImage2D(targets[layer], level, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
|
|
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
|
|
|
|
|
|
|
|
ogl_WarnIfError();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
debug_warn("Unsupported format");
|
|
|
|
}
|
2022-01-05 15:49:54 +01:00
|
|
|
else
|
|
|
|
debug_warn("Unsupported type");
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:28:47 +01:00
|
|
|
void CDeviceCommandContext::Flush()
|
|
|
|
{
|
|
|
|
ResetStates();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDeviceCommandContext::ResetStates()
|
|
|
|
{
|
|
|
|
SetGraphicsPipelineStateImpl(MakeDefaultGraphicsPipelineStateDesc(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CDeviceCommandContext::SetGraphicsPipelineStateImpl(
|
|
|
|
const GraphicsPipelineStateDesc& pipelineStateDesc, const bool force)
|
|
|
|
{
|
2022-01-31 21:10:06 +01:00
|
|
|
const DepthStencilStateDesc& currentDepthStencilStateDesc = m_GraphicsPipelineStateDesc.depthStencilState;
|
|
|
|
const DepthStencilStateDesc& nextDepthStencilStateDesc = pipelineStateDesc.depthStencilState;
|
|
|
|
if (force || currentDepthStencilStateDesc.depthCompareOp != nextDepthStencilStateDesc.depthCompareOp)
|
|
|
|
{
|
|
|
|
glDepthFunc(Mapping::DepthFuncFromCompareOp(nextDepthStencilStateDesc.depthCompareOp));
|
|
|
|
}
|
|
|
|
if (force || currentDepthStencilStateDesc.depthWriteEnabled != nextDepthStencilStateDesc.depthWriteEnabled)
|
|
|
|
{
|
|
|
|
glDepthMask(nextDepthStencilStateDesc.depthWriteEnabled ? GL_TRUE : GL_FALSE);
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:28:47 +01:00
|
|
|
const BlendStateDesc& currentBlendStateDesc = m_GraphicsPipelineStateDesc.blendState;
|
|
|
|
const BlendStateDesc& nextBlendStateDesc = pipelineStateDesc.blendState;
|
|
|
|
if (force || currentBlendStateDesc.enabled != nextBlendStateDesc.enabled)
|
|
|
|
{
|
|
|
|
if (nextBlendStateDesc.enabled)
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
else
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|
|
|
|
if (force ||
|
|
|
|
currentBlendStateDesc.srcColorBlendFactor != nextBlendStateDesc.srcColorBlendFactor ||
|
|
|
|
currentBlendStateDesc.srcAlphaBlendFactor != nextBlendStateDesc.srcAlphaBlendFactor ||
|
|
|
|
currentBlendStateDesc.dstColorBlendFactor != nextBlendStateDesc.dstColorBlendFactor ||
|
|
|
|
currentBlendStateDesc.dstAlphaBlendFactor != nextBlendStateDesc.dstAlphaBlendFactor)
|
|
|
|
{
|
|
|
|
if (nextBlendStateDesc.srcColorBlendFactor == nextBlendStateDesc.srcAlphaBlendFactor &&
|
|
|
|
nextBlendStateDesc.dstColorBlendFactor == nextBlendStateDesc.dstAlphaBlendFactor)
|
|
|
|
{
|
|
|
|
glBlendFunc(
|
|
|
|
Mapping::FromBlendFactor(nextBlendStateDesc.srcColorBlendFactor),
|
|
|
|
Mapping::FromBlendFactor(nextBlendStateDesc.dstColorBlendFactor));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBlendFuncSeparate(
|
|
|
|
Mapping::FromBlendFactor(nextBlendStateDesc.srcColorBlendFactor),
|
|
|
|
Mapping::FromBlendFactor(nextBlendStateDesc.dstColorBlendFactor),
|
|
|
|
Mapping::FromBlendFactor(nextBlendStateDesc.srcAlphaBlendFactor),
|
|
|
|
Mapping::FromBlendFactor(nextBlendStateDesc.dstAlphaBlendFactor));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (force ||
|
|
|
|
currentBlendStateDesc.colorBlendOp != nextBlendStateDesc.colorBlendOp ||
|
|
|
|
currentBlendStateDesc.alphaBlendOp != nextBlendStateDesc.alphaBlendOp)
|
|
|
|
{
|
|
|
|
if (nextBlendStateDesc.colorBlendOp == nextBlendStateDesc.alphaBlendOp)
|
|
|
|
{
|
|
|
|
glBlendEquation(Mapping::FromBlendOp(nextBlendStateDesc.colorBlendOp));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBlendEquationSeparate(
|
|
|
|
Mapping::FromBlendOp(nextBlendStateDesc.colorBlendOp),
|
|
|
|
Mapping::FromBlendOp(nextBlendStateDesc.alphaBlendOp));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (force ||
|
|
|
|
currentBlendStateDesc.constant != nextBlendStateDesc.constant)
|
|
|
|
{
|
|
|
|
glBlendColor(
|
|
|
|
nextBlendStateDesc.constant.r,
|
|
|
|
nextBlendStateDesc.constant.g,
|
|
|
|
nextBlendStateDesc.constant.b,
|
|
|
|
nextBlendStateDesc.constant.a);
|
|
|
|
}
|
|
|
|
|
2022-01-31 21:10:06 +01:00
|
|
|
if (force ||
|
|
|
|
currentBlendStateDesc.colorWriteMask != nextBlendStateDesc.colorWriteMask)
|
|
|
|
{
|
|
|
|
glColorMask(
|
|
|
|
(nextBlendStateDesc.colorWriteMask & ColorWriteMask::RED) != 0 ? GL_TRUE : GL_FALSE,
|
|
|
|
(nextBlendStateDesc.colorWriteMask & ColorWriteMask::GREEN) != 0 ? GL_TRUE : GL_FALSE,
|
|
|
|
(nextBlendStateDesc.colorWriteMask & ColorWriteMask::BLUE) != 0 ? GL_TRUE : GL_FALSE,
|
|
|
|
(nextBlendStateDesc.colorWriteMask & ColorWriteMask::ALPHA) != 0 ? GL_TRUE : GL_FALSE);
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:25:37 +01:00
|
|
|
const RasterizationStateDesc& currentRasterizationStateDesc = m_GraphicsPipelineStateDesc.rasterizationState;
|
|
|
|
const RasterizationStateDesc& nextRasterizationStateDesc = pipelineStateDesc.rasterizationState;
|
|
|
|
if (force ||
|
|
|
|
currentRasterizationStateDesc.cullMode != nextRasterizationStateDesc.cullMode)
|
|
|
|
{
|
|
|
|
if (nextRasterizationStateDesc.cullMode == CullMode::NONE)
|
|
|
|
{
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (force || currentRasterizationStateDesc.cullMode == CullMode::NONE)
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glCullFace(nextRasterizationStateDesc.cullMode == CullMode::FRONT ? GL_FRONT : GL_BACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (force ||
|
|
|
|
currentRasterizationStateDesc.frontFace != nextRasterizationStateDesc.frontFace)
|
|
|
|
{
|
|
|
|
if (nextRasterizationStateDesc.frontFace == FrontFace::CLOCKWISE)
|
|
|
|
glFrontFace(GL_CW);
|
|
|
|
else
|
|
|
|
glFrontFace(GL_CCW);
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:28:47 +01:00
|
|
|
m_GraphicsPipelineStateDesc = pipelineStateDesc;
|
|
|
|
}
|
|
|
|
|
2022-01-05 15:49:54 +01:00
|
|
|
} // namespace GL
|
|
|
|
|
|
|
|
} // namespace Backend
|
|
|
|
|
|
|
|
} // namespace Renderer
|