summaryrefslogtreecommitdiff
path: root/src/video_core/shader
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/shader')
-rw-r--r--src/video_core/shader/decode/image.cpp50
-rw-r--r--src/video_core/shader/decode/texture.cpp59
-rw-r--r--src/video_core/shader/node.h101
-rw-r--r--src/video_core/shader/shader_ir.h12
4 files changed, 100 insertions, 122 deletions
diff --git a/src/video_core/shader/decode/image.cpp b/src/video_core/shader/decode/image.cpp
index b02d2cb95..d2fe4ec5d 100644
--- a/src/video_core/shader/decode/image.cpp
+++ b/src/video_core/shader/decode/image.cpp
@@ -143,39 +143,37 @@ u32 ShaderIR::DecodeImage(NodeBlock& bb, u32 pc) {
143} 143}
144 144
145Image& ShaderIR::GetImage(Tegra::Shader::Image image, Tegra::Shader::ImageType type) { 145Image& ShaderIR::GetImage(Tegra::Shader::Image image, Tegra::Shader::ImageType type) {
146 const auto offset{static_cast<std::size_t>(image.index.Value())}; 146 const auto offset = static_cast<u32>(image.index.Value());
147 if (const auto existing_image = TryUseExistingImage(offset, type)) { 147
148 return *existing_image; 148 const auto it =
149 std::find_if(std::begin(used_images), std::end(used_images),
150 [offset](const Image& entry) { return entry.GetOffset() == offset; });
151 if (it != std::end(used_images)) {
152 ASSERT(!it->IsBindless() && it->GetType() == it->GetType());
153 return *it;
149 } 154 }
150 155
151 const std::size_t next_index{used_images.size()}; 156 const auto next_index = static_cast<u32>(used_images.size());
152 return used_images.emplace(offset, Image{offset, next_index, type}).first->second; 157 return used_images.emplace_back(next_index, offset, type);
153} 158}
154 159
155Image& ShaderIR::GetBindlessImage(Tegra::Shader::Register reg, Tegra::Shader::ImageType type) { 160Image& ShaderIR::GetBindlessImage(Tegra::Shader::Register reg, Tegra::Shader::ImageType type) {
156 const Node image_register{GetRegister(reg)}; 161 const Node image_register = GetRegister(reg);
157 const auto [base_image, cbuf_index, cbuf_offset]{ 162 const auto [base_image, buffer, offset] =
158 TrackCbuf(image_register, global_code, static_cast<s64>(global_code.size()))}; 163 TrackCbuf(image_register, global_code, static_cast<s64>(global_code.size()));
159 const auto cbuf_key{(static_cast<u64>(cbuf_index) << 32) | static_cast<u64>(cbuf_offset)}; 164
160 165 const auto it =
161 if (const auto image = TryUseExistingImage(cbuf_key, type)) { 166 std::find_if(std::begin(used_images), std::end(used_images),
162 return *image; 167 [buffer = buffer, offset = offset](const Image& entry) {
163 } 168 return entry.GetBuffer() == buffer && entry.GetOffset() == offset;
164 169 });
165 const std::size_t next_index{used_images.size()}; 170 if (it != std::end(used_images)) {
166 return used_images.emplace(cbuf_key, Image{cbuf_index, cbuf_offset, next_index, type}) 171 ASSERT(it->IsBindless() && it->GetType() == it->GetType());
167 .first->second; 172 return *it;
168}
169
170Image* ShaderIR::TryUseExistingImage(u64 offset, Tegra::Shader::ImageType type) {
171 auto it = used_images.find(offset);
172 if (it == used_images.end()) {
173 return nullptr;
174 } 173 }
175 auto& image = it->second;
176 ASSERT(image.GetType() == type);
177 174
178 return &image; 175 const auto next_index = static_cast<u32>(used_images.size());
176 return used_images.emplace_back(next_index, offset, buffer, type);
179} 177}
180 178
181} // namespace VideoCommon::Shader 179} // namespace VideoCommon::Shader
diff --git a/src/video_core/shader/decode/texture.cpp b/src/video_core/shader/decode/texture.cpp
index d61e656b7..8d3d962c7 100644
--- a/src/video_core/shader/decode/texture.cpp
+++ b/src/video_core/shader/decode/texture.cpp
@@ -255,7 +255,7 @@ u32 ShaderIR::DecodeTexture(NodeBlock& bb, u32 pc) {
255 break; 255 break;
256 } 256 }
257 case OpCode::Id::TLDS: { 257 case OpCode::Id::TLDS: {
258 const Tegra::Shader::TextureType texture_type{instr.tlds.GetTextureType()}; 258 const TextureType texture_type{instr.tlds.GetTextureType()};
259 const bool is_array{instr.tlds.IsArrayTexture()}; 259 const bool is_array{instr.tlds.IsArrayTexture()};
260 260
261 UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::AOFFI), 261 UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(TextureMiscMode::AOFFI),
@@ -286,77 +286,80 @@ const Sampler& ShaderIR::GetSampler(const Tegra::Shader::Sampler& sampler,
286 std::optional<SamplerInfo> sampler_info) { 286 std::optional<SamplerInfo> sampler_info) {
287 const auto offset = static_cast<u32>(sampler.index.Value()); 287 const auto offset = static_cast<u32>(sampler.index.Value());
288 288
289 Tegra::Shader::TextureType type; 289 TextureType type;
290 bool is_array; 290 bool is_array;
291 bool is_shadow; 291 bool is_shadow;
292 if (sampler_info) { 292 if (sampler_info) {
293 type = sampler_info->type; 293 type = sampler_info->type;
294 is_array = sampler_info->is_array; 294 is_array = sampler_info->is_array;
295 is_shadow = sampler_info->is_shadow; 295 is_shadow = sampler_info->is_shadow;
296 } else if (auto sampler = locker.ObtainBoundSampler(offset); sampler) { 296 } else if (const auto sampler = locker.ObtainBoundSampler(offset)) {
297 type = sampler->texture_type.Value(); 297 type = sampler->texture_type.Value();
298 is_array = sampler->is_array.Value() != 0; 298 is_array = sampler->is_array.Value() != 0;
299 is_shadow = sampler->is_shadow.Value() != 0; 299 is_shadow = sampler->is_shadow.Value() != 0;
300 } else { 300 } else {
301 type = Tegra::Shader::TextureType::Texture2D; 301 LOG_WARNING(HW_GPU, "Unknown sampler info");
302 type = TextureType::Texture2D;
302 is_array = false; 303 is_array = false;
303 is_shadow = false; 304 is_shadow = false;
304 } 305 }
305 306
306 // If this sampler has already been used, return the existing mapping. 307 // If this sampler has already been used, return the existing mapping.
307 const auto itr = 308 const auto it =
308 std::find_if(used_samplers.begin(), used_samplers.end(), 309 std::find_if(used_samplers.begin(), used_samplers.end(),
309 [&](const Sampler& entry) { return entry.GetOffset() == offset; }); 310 [offset](const Sampler& entry) { return entry.GetOffset() == offset; });
310 if (itr != used_samplers.end()) { 311 if (it != used_samplers.end()) {
311 ASSERT(itr->GetType() == type && itr->IsArray() == is_array && 312 ASSERT(!it->IsBindless() && it->GetType() == type && it->IsArray() == is_array &&
312 itr->IsShadow() == is_shadow); 313 it->IsShadow() == is_shadow);
313 return *itr; 314 return *it;
314 } 315 }
315 316
316 // Otherwise create a new mapping for this sampler 317 // Otherwise create a new mapping for this sampler
317 const std::size_t next_index = used_samplers.size(); 318 const auto next_index = static_cast<u32>(used_samplers.size());
318 const Sampler entry{offset, next_index, type, is_array, is_shadow}; 319 return used_samplers.emplace_back(Sampler(next_index, offset, type, is_array, is_shadow));
319 return *used_samplers.emplace(entry).first; 320}
320} // namespace VideoCommon::Shader
321 321
322const Sampler& ShaderIR::GetBindlessSampler(const Tegra::Shader::Register& reg, 322const Sampler& ShaderIR::GetBindlessSampler(const Tegra::Shader::Register& reg,
323 std::optional<SamplerInfo> sampler_info) { 323 std::optional<SamplerInfo> sampler_info) {
324 const Node sampler_register = GetRegister(reg); 324 const Node sampler_register = GetRegister(reg);
325 const auto [base_sampler, cbuf_index, cbuf_offset] = 325 const auto [base_sampler, buffer, offset] =
326 TrackCbuf(sampler_register, global_code, static_cast<s64>(global_code.size())); 326 TrackCbuf(sampler_register, global_code, static_cast<s64>(global_code.size()));
327 ASSERT(base_sampler != nullptr); 327 ASSERT(base_sampler != nullptr);
328 const auto cbuf_key = (static_cast<u64>(cbuf_index) << 32) | static_cast<u64>(cbuf_offset); 328
329 Tegra::Shader::TextureType type; 329 TextureType type;
330 bool is_array; 330 bool is_array;
331 bool is_shadow; 331 bool is_shadow;
332 if (sampler_info) { 332 if (sampler_info) {
333 type = sampler_info->type; 333 type = sampler_info->type;
334 is_array = sampler_info->is_array; 334 is_array = sampler_info->is_array;
335 is_shadow = sampler_info->is_shadow; 335 is_shadow = sampler_info->is_shadow;
336 } else if (auto sampler = locker.ObtainBindlessSampler(cbuf_index, cbuf_offset); sampler) { 336 } else if (const auto sampler = locker.ObtainBindlessSampler(buffer, offset)) {
337 type = sampler->texture_type.Value(); 337 type = sampler->texture_type.Value();
338 is_array = sampler->is_array.Value() != 0; 338 is_array = sampler->is_array.Value() != 0;
339 is_shadow = sampler->is_shadow.Value() != 0; 339 is_shadow = sampler->is_shadow.Value() != 0;
340 } else { 340 } else {
341 type = Tegra::Shader::TextureType::Texture2D; 341 LOG_WARNING(HW_GPU, "Unknown sampler info");
342 type = TextureType::Texture2D;
342 is_array = false; 343 is_array = false;
343 is_shadow = false; 344 is_shadow = false;
344 } 345 }
345 346
346 // If this sampler has already been used, return the existing mapping. 347 // If this sampler has already been used, return the existing mapping.
347 const auto itr = 348 const auto it =
348 std::find_if(used_samplers.begin(), used_samplers.end(), 349 std::find_if(used_samplers.begin(), used_samplers.end(),
349 [&](const Sampler& entry) { return entry.GetOffset() == cbuf_key; }); 350 [buffer = buffer, offset = offset](const Sampler& entry) {
350 if (itr != used_samplers.end()) { 351 return entry.GetBuffer() == buffer && entry.GetOffset() == offset;
351 ASSERT(itr->GetType() == type && itr->IsArray() == is_array && 352 });
352 itr->IsShadow() == is_shadow); 353 if (it != used_samplers.end()) {
353 return *itr; 354 ASSERT(it->IsBindless() && it->GetType() == type && it->IsArray() == is_array &&
355 it->IsShadow() == is_shadow);
356 return *it;
354 } 357 }
355 358
356 // Otherwise create a new mapping for this sampler 359 // Otherwise create a new mapping for this sampler
357 const std::size_t next_index = used_samplers.size(); 360 const auto next_index = static_cast<u32>(used_samplers.size());
358 const Sampler entry{cbuf_index, cbuf_offset, next_index, type, is_array, is_shadow}; 361 return used_samplers.emplace_back(
359 return *used_samplers.emplace(entry).first; 362 Sampler(next_index, offset, buffer, type, is_array, is_shadow));
360} 363}
361 364
362void ShaderIR::WriteTexInstructionFloat(NodeBlock& bb, Instruction instr, const Node4& components) { 365void ShaderIR::WriteTexInstructionFloat(NodeBlock& bb, Instruction instr, const Node4& components) {
diff --git a/src/video_core/shader/node.h b/src/video_core/shader/node.h
index 447fb5c1d..4300d9ff4 100644
--- a/src/video_core/shader/node.h
+++ b/src/video_core/shader/node.h
@@ -230,62 +230,49 @@ using NodeBlock = std::vector<Node>;
230class Sampler { 230class Sampler {
231public: 231public:
232 /// This constructor is for bound samplers 232 /// This constructor is for bound samplers
233 explicit Sampler(std::size_t offset, std::size_t index, Tegra::Shader::TextureType type, 233 constexpr explicit Sampler(u32 index, u32 offset, Tegra::Shader::TextureType type,
234 bool is_array, bool is_shadow) 234 bool is_array, bool is_shadow)
235 : offset{offset}, index{index}, type{type}, is_array{is_array}, is_shadow{is_shadow}, 235 : index{index}, offset{offset}, type{type}, is_array{is_array}, is_shadow{is_shadow} {}
236 is_bindless{false} {}
237 236
238 /// This constructor is for bindless samplers 237 /// This constructor is for bindless samplers
239 explicit Sampler(u32 cbuf_index, u32 cbuf_offset, std::size_t index, 238 constexpr explicit Sampler(u32 index, u32 offset, u32 buffer, Tegra::Shader::TextureType type,
240 Tegra::Shader::TextureType type, bool is_array, bool is_shadow) 239 bool is_array, bool is_shadow)
241 : offset{(static_cast<u64>(cbuf_index) << 32) | cbuf_offset}, index{index}, type{type}, 240 : index{index}, offset{offset}, buffer{buffer}, type{type}, is_array{is_array},
242 is_array{is_array}, is_shadow{is_shadow}, is_bindless{true} {} 241 is_shadow{is_shadow}, is_bindless{true} {}
243 242
244 /// This constructor is for serialization/deserialization 243 constexpr u32 GetIndex() const {
245 explicit Sampler(std::size_t offset, std::size_t index, Tegra::Shader::TextureType type, 244 return index;
246 bool is_array, bool is_shadow, bool is_bindless) 245 }
247 : offset{offset}, index{index}, type{type}, is_array{is_array}, is_shadow{is_shadow}, 246
248 is_bindless{is_bindless} {} 247 constexpr u32 GetOffset() const {
249
250 std::size_t GetOffset() const {
251 return offset; 248 return offset;
252 } 249 }
253 250
254 std::size_t GetIndex() const { 251 constexpr u32 GetBuffer() const {
255 return index; 252 return buffer;
256 } 253 }
257 254
258 Tegra::Shader::TextureType GetType() const { 255 constexpr Tegra::Shader::TextureType GetType() const {
259 return type; 256 return type;
260 } 257 }
261 258
262 bool IsArray() const { 259 constexpr bool IsArray() const {
263 return is_array; 260 return is_array;
264 } 261 }
265 262
266 bool IsShadow() const { 263 constexpr bool IsShadow() const {
267 return is_shadow; 264 return is_shadow;
268 } 265 }
269 266
270 bool IsBindless() const { 267 constexpr bool IsBindless() const {
271 return is_bindless; 268 return is_bindless;
272 } 269 }
273 270
274 std::pair<u32, u32> GetBindlessCBuf() const {
275 return {static_cast<u32>(offset >> 32), static_cast<u32>(offset)};
276 }
277
278 bool operator<(const Sampler& rhs) const {
279 return std::tie(index, offset, type, is_array, is_shadow, is_bindless) <
280 std::tie(rhs.index, rhs.offset, rhs.type, rhs.is_array, rhs.is_shadow,
281 rhs.is_bindless);
282 }
283
284private: 271private:
285 /// Offset in TSC memory from which to read the sampler object, as specified by the sampling 272 u32 index{}; ///< Emulated index given for the this sampler.
286 /// instruction. 273 u32 offset{}; ///< Offset in the const buffer from where the sampler is being read.
287 std::size_t offset{}; 274 u32 buffer{}; ///< Buffer where the bindless sampler is being read (unused on bound samplers).
288 std::size_t index{}; ///< Value used to index into the generated GLSL sampler array. 275
289 Tegra::Shader::TextureType type{}; ///< The type used to sample this texture (Texture2D, etc) 276 Tegra::Shader::TextureType type{}; ///< The type used to sample this texture (Texture2D, etc)
290 bool is_array{}; ///< Whether the texture is being sampled as an array texture or not. 277 bool is_array{}; ///< Whether the texture is being sampled as an array texture or not.
291 bool is_shadow{}; ///< Whether the texture is being sampled as a depth texture or not. 278 bool is_shadow{}; ///< Whether the texture is being sampled as a depth texture or not.
@@ -294,18 +281,13 @@ private:
294 281
295class Image final { 282class Image final {
296public: 283public:
297 constexpr explicit Image(std::size_t offset, std::size_t index, Tegra::Shader::ImageType type) 284 /// This constructor is for bound images
298 : offset{offset}, index{index}, type{type}, is_bindless{false} {} 285 constexpr explicit Image(u32 index, u32 offset, Tegra::Shader::ImageType type)
299 286 : index{index}, offset{offset}, type{type} {}
300 constexpr explicit Image(u32 cbuf_index, u32 cbuf_offset, std::size_t index,
301 Tegra::Shader::ImageType type)
302 : offset{(static_cast<u64>(cbuf_index) << 32) | cbuf_offset}, index{index}, type{type},
303 is_bindless{true} {}
304 287
305 constexpr explicit Image(std::size_t offset, std::size_t index, Tegra::Shader::ImageType type, 288 /// This constructor is for bindless samplers
306 bool is_bindless, bool is_written, bool is_read, bool is_atomic) 289 constexpr explicit Image(u32 index, u32 offset, u32 buffer, Tegra::Shader::ImageType type)
307 : offset{offset}, index{index}, type{type}, is_bindless{is_bindless}, 290 : index{index}, offset{offset}, buffer{buffer}, type{type}, is_bindless{true} {}
308 is_written{is_written}, is_read{is_read}, is_atomic{is_atomic} {}
309 291
310 void MarkWrite() { 292 void MarkWrite() {
311 is_written = true; 293 is_written = true;
@@ -321,12 +303,16 @@ public:
321 is_atomic = true; 303 is_atomic = true;
322 } 304 }
323 305
324 constexpr std::size_t GetOffset() const { 306 constexpr u32 GetIndex() const {
307 return index;
308 }
309
310 constexpr u32 GetOffset() const {
325 return offset; 311 return offset;
326 } 312 }
327 313
328 constexpr std::size_t GetIndex() const { 314 constexpr u32 GetBuffer() const {
329 return index; 315 return buffer;
330 } 316 }
331 317
332 constexpr Tegra::Shader::ImageType GetType() const { 318 constexpr Tegra::Shader::ImageType GetType() const {
@@ -349,18 +335,11 @@ public:
349 return is_atomic; 335 return is_atomic;
350 } 336 }
351 337
352 constexpr std::pair<u32, u32> GetBindlessCBuf() const {
353 return {static_cast<u32>(offset >> 32), static_cast<u32>(offset)};
354 }
355
356 constexpr bool operator<(const Image& rhs) const {
357 return std::tie(offset, index, type, is_bindless) <
358 std::tie(rhs.offset, rhs.index, rhs.type, rhs.is_bindless);
359 }
360
361private: 338private:
362 u64 offset{}; 339 u32 index{};
363 std::size_t index{}; 340 u32 offset{};
341 u32 buffer{};
342
364 Tegra::Shader::ImageType type{}; 343 Tegra::Shader::ImageType type{};
365 bool is_bindless{}; 344 bool is_bindless{};
366 bool is_written{}; 345 bool is_written{};
diff --git a/src/video_core/shader/shader_ir.h b/src/video_core/shader/shader_ir.h
index 1fd44bde1..5243644e8 100644
--- a/src/video_core/shader/shader_ir.h
+++ b/src/video_core/shader/shader_ir.h
@@ -5,6 +5,7 @@
5#pragma once 5#pragma once
6 6
7#include <array> 7#include <array>
8#include <list>
8#include <map> 9#include <map>
9#include <optional> 10#include <optional>
10#include <set> 11#include <set>
@@ -95,11 +96,11 @@ public:
95 return used_cbufs; 96 return used_cbufs;
96 } 97 }
97 98
98 const std::set<Sampler>& GetSamplers() const { 99 const std::list<Sampler>& GetSamplers() const {
99 return used_samplers; 100 return used_samplers;
100 } 101 }
101 102
102 const std::map<u64, Image>& GetImages() const { 103 const std::list<Image>& GetImages() const {
103 return used_images; 104 return used_images;
104 } 105 }
105 106
@@ -316,9 +317,6 @@ private:
316 /// Access a bindless image sampler. 317 /// Access a bindless image sampler.
317 Image& GetBindlessImage(Tegra::Shader::Register reg, Tegra::Shader::ImageType type); 318 Image& GetBindlessImage(Tegra::Shader::Register reg, Tegra::Shader::ImageType type);
318 319
319 /// Tries to access an existing image, updating it's state as needed
320 Image* TryUseExistingImage(u64 offset, Tegra::Shader::ImageType type);
321
322 /// Extracts a sequence of bits from a node 320 /// Extracts a sequence of bits from a node
323 Node BitfieldExtract(Node value, u32 offset, u32 bits); 321 Node BitfieldExtract(Node value, u32 offset, u32 bits);
324 322
@@ -402,8 +400,8 @@ private:
402 std::set<Tegra::Shader::Attribute::Index> used_input_attributes; 400 std::set<Tegra::Shader::Attribute::Index> used_input_attributes;
403 std::set<Tegra::Shader::Attribute::Index> used_output_attributes; 401 std::set<Tegra::Shader::Attribute::Index> used_output_attributes;
404 std::map<u32, ConstBuffer> used_cbufs; 402 std::map<u32, ConstBuffer> used_cbufs;
405 std::set<Sampler> used_samplers; 403 std::list<Sampler> used_samplers;
406 std::map<u64, Image> used_images; 404 std::list<Image> used_images;
407 std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances> used_clip_distances{}; 405 std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances> used_clip_distances{};
408 std::map<GlobalMemoryBase, GlobalMemoryUsage> used_global_memory; 406 std::map<GlobalMemoryBase, GlobalMemoryUsage> used_global_memory;
409 bool uses_layer{}; 407 bool uses_layer{};