2017-04-23 04:44:34 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2017-04-23 04:44:34 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/Software/SWTexture.h"
|
|
|
|
|
2017-08-24 02:46:23 +00:00
|
|
|
#include <cstring>
|
2021-12-10 02:22:16 +00:00
|
|
|
|
2017-10-21 14:49:40 +00:00
|
|
|
#include "Common/Assert.h"
|
2017-08-24 02:46:23 +00:00
|
|
|
|
|
|
|
#include "VideoBackends/Software/CopyRegion.h"
|
2023-01-27 02:07:05 +00:00
|
|
|
#include "VideoBackends/Software/SWGfx.h"
|
2017-08-24 02:46:23 +00:00
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
namespace SW
|
|
|
|
{
|
2017-10-02 05:21:10 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct Pixel
|
|
|
|
{
|
|
|
|
u8 r;
|
|
|
|
u8 g;
|
|
|
|
u8 b;
|
|
|
|
u8 a;
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
2017-10-21 14:49:40 +00:00
|
|
|
|
|
|
|
void CopyTextureData(const TextureConfig& src_config, const u8* src_ptr, u32 src_x, u32 src_y,
|
2022-09-27 00:49:49 +00:00
|
|
|
u32 width, u32 height, u32 src_level, const TextureConfig& dst_config,
|
|
|
|
u8* dst_ptr, u32 dst_x, u32 dst_y, u32 dst_level)
|
2017-10-21 14:49:40 +00:00
|
|
|
{
|
2022-09-27 00:49:49 +00:00
|
|
|
const size_t texel_size = AbstractTexture::GetTexelSizeForFormat(src_config.format);
|
|
|
|
const size_t src_stride = src_config.GetMipStride(src_level);
|
|
|
|
const size_t src_offset =
|
2017-10-21 14:49:40 +00:00
|
|
|
static_cast<size_t>(src_y) * src_stride + static_cast<size_t>(src_x) * texel_size;
|
2022-09-27 00:49:49 +00:00
|
|
|
const size_t dst_stride = dst_config.GetMipStride(dst_level);
|
|
|
|
const size_t dst_offset =
|
2017-10-21 14:49:40 +00:00
|
|
|
static_cast<size_t>(dst_y) * dst_stride + static_cast<size_t>(dst_x) * texel_size;
|
2022-09-27 00:49:49 +00:00
|
|
|
const size_t copy_len = static_cast<size_t>(width) * texel_size;
|
2017-10-21 14:49:40 +00:00
|
|
|
|
|
|
|
src_ptr += src_offset;
|
|
|
|
dst_ptr += dst_offset;
|
|
|
|
for (u32 i = 0; i < height; i++)
|
|
|
|
{
|
|
|
|
std::memcpy(dst_ptr, src_ptr, copy_len);
|
|
|
|
src_ptr += src_stride;
|
|
|
|
dst_ptr += dst_stride;
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 01:59:50 +00:00
|
|
|
} // namespace
|
|
|
|
|
2023-01-27 02:07:05 +00:00
|
|
|
void SWGfx::ScaleTexture(AbstractFramebuffer* dst_framebuffer,
|
|
|
|
const MathUtil::Rectangle<int>& dst_rect,
|
|
|
|
const AbstractTexture* src_texture,
|
|
|
|
const MathUtil::Rectangle<int>& src_rect)
|
2019-02-15 01:59:50 +00:00
|
|
|
{
|
|
|
|
const SWTexture* software_source_texture = static_cast<const SWTexture*>(src_texture);
|
|
|
|
SWTexture* software_dest_texture = static_cast<SWTexture*>(dst_framebuffer->GetColorAttachment());
|
|
|
|
|
2022-09-27 00:49:49 +00:00
|
|
|
CopyRegion(reinterpret_cast<const Pixel*>(software_source_texture->GetData(0, 0)), src_rect,
|
2020-12-12 07:04:32 +00:00
|
|
|
src_texture->GetWidth(), src_texture->GetHeight(),
|
2022-09-27 00:49:49 +00:00
|
|
|
reinterpret_cast<Pixel*>(software_dest_texture->GetData(0, 0)), dst_rect,
|
2020-12-12 07:04:32 +00:00
|
|
|
dst_framebuffer->GetWidth(), dst_framebuffer->GetHeight());
|
2017-10-02 05:21:10 +00:00
|
|
|
}
|
2017-10-21 14:49:40 +00:00
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
SWTexture::SWTexture(const TextureConfig& tex_config) : AbstractTexture(tex_config)
|
|
|
|
{
|
2022-09-27 00:49:49 +00:00
|
|
|
m_data.resize(tex_config.layers);
|
|
|
|
for (u32 layer = 0; layer < tex_config.layers; layer++)
|
|
|
|
{
|
|
|
|
m_data[layer].resize(tex_config.levels);
|
|
|
|
for (u32 level = 0; level < tex_config.levels; level++)
|
|
|
|
{
|
|
|
|
m_data[layer][level].resize(std::max(tex_config.width >> level, 1u) *
|
|
|
|
std::max(tex_config.height >> level, 1u) * sizeof(Pixel));
|
|
|
|
}
|
|
|
|
}
|
2017-04-23 04:44:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 11:51:42 +00:00
|
|
|
void SWTexture::CopyRectangleFromTexture(const AbstractTexture* src,
|
|
|
|
const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
|
|
|
|
u32 src_level, const MathUtil::Rectangle<int>& dst_rect,
|
|
|
|
u32 dst_layer, u32 dst_level)
|
|
|
|
{
|
2022-09-27 00:49:49 +00:00
|
|
|
CopyTextureData(src->GetConfig(),
|
|
|
|
static_cast<const SWTexture*>(src)->GetData(src_layer, src_level), src_rect.left,
|
|
|
|
src_rect.top, src_rect.GetWidth(), src_rect.GetHeight(), src_level, m_config,
|
|
|
|
GetData(dst_layer, dst_level), dst_rect.left, dst_rect.top, dst_level);
|
2017-10-30 11:51:42 +00:00
|
|
|
}
|
2018-01-21 05:03:06 +00:00
|
|
|
void SWTexture::ResolveFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& rect,
|
|
|
|
u32 layer, u32 level)
|
|
|
|
{
|
|
|
|
}
|
2017-04-23 04:44:34 +00:00
|
|
|
|
|
|
|
void SWTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer,
|
2023-01-28 00:46:53 +00:00
|
|
|
size_t buffer_size, u32 layer)
|
2017-04-23 04:44:34 +00:00
|
|
|
{
|
2023-01-28 00:46:53 +00:00
|
|
|
u8* data = GetData(layer, level);
|
|
|
|
for (u32 y = 0; y < height; y++)
|
2022-09-27 00:49:49 +00:00
|
|
|
{
|
2023-01-28 00:46:53 +00:00
|
|
|
memcpy(&data[width * y * sizeof(Pixel)], &buffer[y * row_length * sizeof(Pixel)],
|
|
|
|
width * sizeof(Pixel));
|
2022-09-27 00:49:49 +00:00
|
|
|
}
|
2017-08-24 02:46:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 00:49:49 +00:00
|
|
|
const u8* SWTexture::GetData(u32 layer, u32 level) const
|
2017-08-24 02:46:23 +00:00
|
|
|
{
|
2022-09-27 00:49:49 +00:00
|
|
|
return m_data[layer][level].data();
|
2017-08-24 02:46:23 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 00:49:49 +00:00
|
|
|
u8* SWTexture::GetData(u32 layer, u32 level)
|
2017-08-24 02:46:23 +00:00
|
|
|
{
|
2022-09-27 00:49:49 +00:00
|
|
|
return m_data[layer][level].data();
|
2017-08-24 02:46:23 +00:00
|
|
|
}
|
|
|
|
|
2017-10-21 14:49:40 +00:00
|
|
|
SWStagingTexture::SWStagingTexture(StagingTextureType type, const TextureConfig& config)
|
|
|
|
: AbstractStagingTexture(type, config)
|
|
|
|
{
|
|
|
|
m_data.resize(m_texel_size * config.width * config.height);
|
|
|
|
m_map_pointer = reinterpret_cast<char*>(m_data.data());
|
|
|
|
m_map_stride = m_texel_size * config.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWStagingTexture::~SWStagingTexture() = default;
|
|
|
|
|
|
|
|
void SWStagingTexture::CopyFromTexture(const AbstractTexture* src,
|
|
|
|
const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
|
|
|
|
u32 src_level, const MathUtil::Rectangle<int>& dst_rect)
|
|
|
|
{
|
2022-09-27 00:49:49 +00:00
|
|
|
CopyTextureData(src->GetConfig(),
|
|
|
|
static_cast<const SWTexture*>(src)->GetData(src_layer, src_level), src_rect.left,
|
|
|
|
src_rect.top, src_rect.GetWidth(), src_rect.GetHeight(), src_level, m_config,
|
|
|
|
m_data.data(), dst_rect.left, dst_rect.top, 0);
|
2017-10-21 14:49:40 +00:00
|
|
|
m_needs_flush = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SWStagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, AbstractTexture* dst,
|
|
|
|
const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer,
|
|
|
|
u32 dst_level)
|
|
|
|
{
|
|
|
|
CopyTextureData(m_config, m_data.data(), src_rect.left, src_rect.top, src_rect.GetWidth(),
|
2022-09-27 00:49:49 +00:00
|
|
|
src_rect.GetHeight(), 0, dst->GetConfig(),
|
|
|
|
static_cast<SWTexture*>(dst)->GetData(dst_layer, dst_level), dst_rect.left,
|
|
|
|
dst_rect.top, dst_level);
|
2017-10-21 14:49:40 +00:00
|
|
|
m_needs_flush = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SWStagingTexture::Map()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SWStagingTexture::Unmap()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SWStagingTexture::Flush()
|
|
|
|
{
|
|
|
|
m_needs_flush = false;
|
|
|
|
}
|
2018-01-21 10:22:45 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
SWFramebuffer::SWFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
|
2023-05-29 01:59:02 +00:00
|
|
|
std::vector<AbstractTexture*> additional_color_attachments,
|
2019-02-15 01:59:50 +00:00
|
|
|
AbstractTextureFormat color_format, AbstractTextureFormat depth_format,
|
2018-01-21 10:22:45 +00:00
|
|
|
u32 width, u32 height, u32 layers, u32 samples)
|
2023-05-29 01:59:02 +00:00
|
|
|
: AbstractFramebuffer(color_attachment, depth_attachment,
|
|
|
|
std::move(additional_color_attachments), color_format, depth_format,
|
|
|
|
width, height, layers, samples)
|
2018-01-21 10:22:45 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-29 01:59:02 +00:00
|
|
|
std::unique_ptr<SWFramebuffer>
|
|
|
|
SWFramebuffer::Create(SWTexture* color_attachment, SWTexture* depth_attachment,
|
|
|
|
std::vector<AbstractTexture*> additional_color_attachments)
|
2018-01-21 10:22:45 +00:00
|
|
|
{
|
2023-05-29 01:59:02 +00:00
|
|
|
if (!ValidateConfig(color_attachment, depth_attachment, additional_color_attachments))
|
2018-01-21 10:22:45 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const AbstractTextureFormat color_format =
|
|
|
|
color_attachment ? color_attachment->GetFormat() : AbstractTextureFormat::Undefined;
|
|
|
|
const AbstractTextureFormat depth_format =
|
|
|
|
depth_attachment ? depth_attachment->GetFormat() : AbstractTextureFormat::Undefined;
|
|
|
|
const SWTexture* either_attachment = color_attachment ? color_attachment : depth_attachment;
|
|
|
|
const u32 width = either_attachment->GetWidth();
|
|
|
|
const u32 height = either_attachment->GetHeight();
|
|
|
|
const u32 layers = either_attachment->GetLayers();
|
|
|
|
const u32 samples = either_attachment->GetSamples();
|
|
|
|
|
2023-05-29 01:59:02 +00:00
|
|
|
return std::make_unique<SWFramebuffer>(color_attachment, depth_attachment,
|
|
|
|
std::move(additional_color_attachments), color_format,
|
2019-02-15 01:59:50 +00:00
|
|
|
depth_format, width, height, layers, samples);
|
2018-01-21 10:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 04:44:34 +00:00
|
|
|
} // namespace SW
|