2022-01-05 06:49:54 -08: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"
|
|
|
|
|
|
|
|
|
|
#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());
|
|
|
|
|
|
|
|
|
|
return deviceCommandContext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CDeviceCommandContext::CDeviceCommandContext() = default;
|
|
|
|
|
|
|
|
|
|
CDeviceCommandContext::~CDeviceCommandContext() = default;
|
|
|
|
|
|
|
|
|
|
void CDeviceCommandContext::UploadTexture(
|
2022-01-07 12:00:41 -08:00
|
|
|
CTexture* texture, const Format format,
|
|
|
|
|
const void* data, const size_t dataSize,
|
|
|
|
|
const uint32_t level, const uint32_t layer)
|
2022-01-05 06:49:54 -08:00
|
|
|
{
|
2022-01-07 12:00:41 -08:00
|
|
|
UploadTextureRegion(texture, format, data, dataSize,
|
|
|
|
|
0, 0, texture->GetWidth(), texture->GetHeight(), level, layer);
|
2022-01-05 06:49:54 -08:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 12:00:41 -08:00
|
|
|
void CDeviceCommandContext::UploadTextureRegion(
|
|
|
|
|
CTexture* texture, const Format format,
|
|
|
|
|
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 06:49:54 -08:00
|
|
|
{
|
|
|
|
|
if (texture->GetType() == CTexture::Type::TEXTURE_2D)
|
|
|
|
|
{
|
2022-01-07 12:00:41 -08:00
|
|
|
ENSURE(level == 0 && layer == 0);
|
2022-01-05 06:49:54 -08:00
|
|
|
if (texture->GetFormat() == Format::R8G8B8A8 || texture->GetFormat() == Format::A8)
|
|
|
|
|
{
|
|
|
|
|
ENSURE(width > 0 && height > 0);
|
|
|
|
|
ENSURE(texture->GetFormat() == format);
|
|
|
|
|
const size_t bpp = format == Format::R8G8B8A8 ? 4 : 1;
|
|
|
|
|
ENSURE(dataSize == width * height * bpp);
|
|
|
|
|
ENSURE(xOffset + width <= texture->GetWidth());
|
|
|
|
|
ENSURE(yOffset + height <= texture->GetHeight());
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture->GetHandle());
|
2022-01-07 12:00:41 -08:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, level,
|
2022-01-05 06:49:54 -08:00
|
|
|
xOffset, yOffset, width, height,
|
|
|
|
|
format == Format::R8G8B8A8 ? GL_RGBA : GL_ALPHA, GL_UNSIGNED_BYTE, data);
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
|
|
|
|
|
ogl_WarnIfError();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
debug_warn("Unsupported format");
|
|
|
|
|
}
|
2022-01-07 12:00:41 -08:00
|
|
|
else if (texture->GetType() == CTexture::Type::TEXTURE_CUBE)
|
|
|
|
|
{
|
|
|
|
|
if (texture->GetFormat() == Format::R8G8B8A8)
|
|
|
|
|
{
|
|
|
|
|
ENSURE(texture->GetFormat() == format);
|
|
|
|
|
ENSURE(level == 0 && 0 <= layer && layer < 6);
|
|
|
|
|
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 06:49:54 -08:00
|
|
|
else
|
|
|
|
|
debug_warn("Unsupported type");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace GL
|
|
|
|
|
|
|
|
|
|
} // namespace Backend
|
|
|
|
|
|
|
|
|
|
} // namespace Renderer
|