Debian Codegraph @

Source/Core/VideoBackends/Software/SWTexture.cpp

raw symbols SCIP
1 // Copyright 2017 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/Software/SWTexture.h"
5
6 #include <cstring>
7
8 #include "VideoBackends/Software/CopyRegion.h"
9 #include "VideoBackends/Software/SWGfx.h"
10
11 namespace SW
12 {
13 namespace
14 {
15 #pragma pack(push, 1)
16 struct Pixel
17 {
18 u8 r;
19 u8 g;
20 u8 b;
21 u8 a;
22 };
23 #pragma pack(pop)
24
25 void CopyTextureData(const TextureConfig& src_config, const u8* src_ptr, u32 src_x, u32 src_y,
26 u32 width, u32 height, u32 src_level, const TextureConfig& dst_config,
27 u8* dst_ptr, u32 dst_x, u32 dst_y, u32 dst_level)
28 {
29 const size_t texel_size = AbstractTexture::GetTexelSizeForFormat(src_config.format);
30 const size_t src_stride = src_config.GetMipStride(src_level);
31 const size_t src_offset =
32 static_cast<size_t>(src_y) * src_stride + static_cast<size_t>(src_x) * texel_size;
33 const size_t dst_stride = dst_config.GetMipStride(dst_level);
34 const size_t dst_offset =
35 static_cast<size_t>(dst_y) * dst_stride + static_cast<size_t>(dst_x) * texel_size;
36 const size_t copy_len = static_cast<size_t>(width) * texel_size;
37
38 src_ptr += src_offset;
39 dst_ptr += dst_offset;
40 for (u32 i = 0; i < height; i++)
41 {
42 std::memcpy(dst_ptr, src_ptr, copy_len);
43 src_ptr += src_stride;
44 dst_ptr += dst_stride;
45 }
46 }
47 } // namespace
48
49 void SWGfx::ScaleTexture(AbstractFramebuffer* dst_framebuffer,
50 const MathUtil::Rectangle<int>& dst_rect,
51 const AbstractTexture* src_texture,
52 const MathUtil::Rectangle<int>& src_rect)
53 {
54 const SWTexture* software_source_texture = static_cast<const SWTexture*>(src_texture);
55 SWTexture* software_dest_texture = static_cast<SWTexture*>(dst_framebuffer->GetColorAttachment());
56
57 CopyRegion(reinterpret_cast<const Pixel*>(software_source_texture->GetData(0, 0)), src_rect,
58 src_texture->GetWidth(), src_texture->GetHeight(),
59 reinterpret_cast<Pixel*>(software_dest_texture->GetData(0, 0)), dst_rect,
60 dst_framebuffer->GetWidth(), dst_framebuffer->GetHeight());
61 }
62
63 SWTexture::SWTexture(const TextureConfig& tex_config) : AbstractTexture(tex_config)
64 {
65 m_data.resize(tex_config.layers);
66 for (u32 layer = 0; layer < tex_config.layers; layer++)
67 {
68 m_data[layer].resize(tex_config.levels);
69 for (u32 level = 0; level < tex_config.levels; level++)
70 {
71 m_data[layer][level].resize(std::max(tex_config.width >> level, 1u) *
72 std::max(tex_config.height >> level, 1u) * sizeof(Pixel));
73 }
74 }
75 }
76
77 void SWTexture::CopyRectangleFromTexture(const AbstractTexture* src,
78 const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
79 u32 src_level, const MathUtil::Rectangle<int>& dst_rect,
80 u32 dst_layer, u32 dst_level)
81 {
82 CopyTextureData(src->GetConfig(),
83 static_cast<const SWTexture*>(src)->GetData(src_layer, src_level), src_rect.left,
84 src_rect.top, src_rect.GetWidth(), src_rect.GetHeight(), src_level, m_config,
85 GetData(dst_layer, dst_level), dst_rect.left, dst_rect.top, dst_level);
86 }
87 void SWTexture::ResolveFromTexture(const AbstractTexture* src, const MathUtil::Rectangle<int>& rect,
88 u32 layer, u32 level)
89 {
90 }
91
92 void SWTexture::Load(u32 level, u32 width, u32 height, u32 row_length, const u8* buffer,
93 size_t buffer_size, u32 layer)
94 {
95 u8* data = GetData(layer, level);
96 for (u32 y = 0; y < height; y++)
97 {
98 memcpy(&data[width * y * sizeof(Pixel)], &buffer[y * row_length * sizeof(Pixel)],
99 width * sizeof(Pixel));
100 }
101 }
102
103 const u8* SWTexture::GetData(u32 layer, u32 level) const
104 {
105 return m_data[layer][level].data();
106 }
107
108 u8* SWTexture::GetData(u32 layer, u32 level)
109 {
110 return m_data[layer][level].data();
111 }
112
113 SWStagingTexture::SWStagingTexture(StagingTextureType type, const TextureConfig& config)
114 : AbstractStagingTexture(type, config)
115 {
116 m_data.resize(m_texel_size * config.width * config.height);
117 m_map_pointer = reinterpret_cast<char*>(m_data.data());
118 m_map_stride = m_texel_size * config.width;
119 }
120
121 SWStagingTexture::~SWStagingTexture() = default;
122
123 void SWStagingTexture::CopyFromTexture(const AbstractTexture* src,
124 const MathUtil::Rectangle<int>& src_rect, u32 src_layer,
125 u32 src_level, const MathUtil::Rectangle<int>& dst_rect)
126 {
127 CopyTextureData(src->GetConfig(),
128 static_cast<const SWTexture*>(src)->GetData(src_layer, src_level), src_rect.left,
129 src_rect.top, src_rect.GetWidth(), src_rect.GetHeight(), src_level, m_config,
130 m_data.data(), dst_rect.left, dst_rect.top, 0);
131 m_needs_flush = true;
132 }
133
134 void SWStagingTexture::CopyToTexture(const MathUtil::Rectangle<int>& src_rect, AbstractTexture* dst,
135 const MathUtil::Rectangle<int>& dst_rect, u32 dst_layer,
136 u32 dst_level)
137 {
138 CopyTextureData(m_config, m_data.data(), src_rect.left, src_rect.top, src_rect.GetWidth(),
139 src_rect.GetHeight(), 0, dst->GetConfig(),
140 static_cast<SWTexture*>(dst)->GetData(dst_layer, dst_level), dst_rect.left,
141 dst_rect.top, dst_level);
142 m_needs_flush = true;
143 }
144
145 bool SWStagingTexture::Map()
146 {
147 return true;
148 }
149
150 void SWStagingTexture::Unmap()
151 {
152 }
153
154 void SWStagingTexture::Flush()
155 {
156 m_needs_flush = false;
157 }
158
159 SWFramebuffer::SWFramebuffer(AbstractTexture* color_attachment, AbstractTexture* depth_attachment,
160 std::vector<AbstractTexture*> additional_color_attachments,
161 AbstractTextureFormat color_format, AbstractTextureFormat depth_format,
162 u32 width, u32 height, u32 layers, u32 samples)
163 : AbstractFramebuffer(color_attachment, depth_attachment,
164 std::move(additional_color_attachments), color_format, depth_format,
165 width, height, layers, samples)
166 {
167 }
168
169 std::unique_ptr<SWFramebuffer>
170 SWFramebuffer::Create(SWTexture* color_attachment, SWTexture* depth_attachment,
171 std::vector<AbstractTexture*> additional_color_attachments)
172 {
173 if (!ValidateConfig(color_attachment, depth_attachment, additional_color_attachments))
174 return nullptr;
175
176 const AbstractTextureFormat color_format =
177 color_attachment ? color_attachment->GetFormat() : AbstractTextureFormat::Undefined;
178 const AbstractTextureFormat depth_format =
179 depth_attachment ? depth_attachment->GetFormat() : AbstractTextureFormat::Undefined;
180 const SWTexture* either_attachment = color_attachment ? color_attachment : depth_attachment;
181 const u32 width = either_attachment->GetWidth();
182 const u32 height = either_attachment->GetHeight();
183 const u32 layers = either_attachment->GetLayers();
184 const u32 samples = either_attachment->GetSamples();
185
186 return std::make_unique<SWFramebuffer>(color_attachment, depth_attachment,
187 std::move(additional_color_attachments), color_format,
188 depth_format, width, height, layers, samples);
189 }
190
191 } // namespace SW