summaryrefslogtreecommitdiff
path: root/src/video_core/shader_environment.cpp
diff options
context:
space:
mode:
authorGravatar Feng Chen2022-11-04 14:39:42 +0800
committerGravatar GitHub2022-11-04 02:39:42 -0400
commit75596c07e0fc1462c2a19484e168f4944c33d3d3 (patch)
tree1aa919ea76f467c51b3fc591a72c57f5ade92560 /src/video_core/shader_environment.cpp
parentUI: Add options to hide extra columns (#9093) (diff)
downloadyuzu-75596c07e0fc1462c2a19484e168f4944c33d3d3.tar.gz
yuzu-75596c07e0fc1462c2a19484e168f4944c33d3d3.tar.xz
yuzu-75596c07e0fc1462c2a19484e168f4944c33d3d3.zip
video_core: Fix SNORM texture buffer emulating error (#9001)
Diffstat (limited to 'src/video_core/shader_environment.cpp')
-rw-r--r--src/video_core/shader_environment.cpp89
1 files changed, 79 insertions, 10 deletions
diff --git a/src/video_core/shader_environment.cpp b/src/video_core/shader_environment.cpp
index 63bcf9337..37bb76b72 100644
--- a/src/video_core/shader_environment.cpp
+++ b/src/video_core/shader_environment.cpp
@@ -19,6 +19,7 @@
19#include "video_core/engines/kepler_compute.h" 19#include "video_core/engines/kepler_compute.h"
20#include "video_core/memory_manager.h" 20#include "video_core/memory_manager.h"
21#include "video_core/shader_environment.h" 21#include "video_core/shader_environment.h"
22#include "video_core/texture_cache/format_lookup_table.h"
22#include "video_core/textures/texture.h" 23#include "video_core/textures/texture.h"
23 24
24namespace VideoCommon { 25namespace VideoCommon {
@@ -33,7 +34,7 @@ static u64 MakeCbufKey(u32 index, u32 offset) {
33 return (static_cast<u64>(index) << 32) | offset; 34 return (static_cast<u64>(index) << 32) | offset;
34} 35}
35 36
36static Shader::TextureType ConvertType(const Tegra::Texture::TICEntry& entry) { 37static Shader::TextureType ConvertTextureType(const Tegra::Texture::TICEntry& entry) {
37 switch (entry.texture_type) { 38 switch (entry.texture_type) {
38 case Tegra::Texture::TextureType::Texture1D: 39 case Tegra::Texture::TextureType::Texture1D:
39 return Shader::TextureType::Color1D; 40 return Shader::TextureType::Color1D;
@@ -59,6 +60,26 @@ static Shader::TextureType ConvertType(const Tegra::Texture::TICEntry& entry) {
59 } 60 }
60} 61}
61 62
63static Shader::TexturePixelFormat ConvertTexturePixelFormat(const Tegra::Texture::TICEntry& entry) {
64 switch (PixelFormatFromTextureInfo(entry.format, entry.r_type, entry.g_type, entry.b_type,
65 entry.a_type, entry.srgb_conversion)) {
66 case VideoCore::Surface::PixelFormat::A8B8G8R8_SNORM:
67 return Shader::TexturePixelFormat::A8B8G8R8_SNORM;
68 case VideoCore::Surface::PixelFormat::R8_SNORM:
69 return Shader::TexturePixelFormat::R8_SNORM;
70 case VideoCore::Surface::PixelFormat::R8G8_SNORM:
71 return Shader::TexturePixelFormat::R8G8_SNORM;
72 case VideoCore::Surface::PixelFormat::R16G16B16A16_SNORM:
73 return Shader::TexturePixelFormat::R16G16B16A16_SNORM;
74 case VideoCore::Surface::PixelFormat::R16G16_SNORM:
75 return Shader::TexturePixelFormat::R16G16_SNORM;
76 case VideoCore::Surface::PixelFormat::R16_SNORM:
77 return Shader::TexturePixelFormat::R16_SNORM;
78 default:
79 return Shader::TexturePixelFormat::OTHER;
80 }
81}
82
62static std::string_view StageToPrefix(Shader::Stage stage) { 83static std::string_view StageToPrefix(Shader::Stage stage) {
63 switch (stage) { 84 switch (stage) {
64 case Shader::Stage::VertexB: 85 case Shader::Stage::VertexB:
@@ -178,10 +199,13 @@ void GenericEnvironment::Dump(u64 hash) {
178void GenericEnvironment::Serialize(std::ofstream& file) const { 199void GenericEnvironment::Serialize(std::ofstream& file) const {
179 const u64 code_size{static_cast<u64>(CachedSize())}; 200 const u64 code_size{static_cast<u64>(CachedSize())};
180 const u64 num_texture_types{static_cast<u64>(texture_types.size())}; 201 const u64 num_texture_types{static_cast<u64>(texture_types.size())};
202 const u64 num_texture_pixel_formats{static_cast<u64>(texture_pixel_formats.size())};
181 const u64 num_cbuf_values{static_cast<u64>(cbuf_values.size())}; 203 const u64 num_cbuf_values{static_cast<u64>(cbuf_values.size())};
182 204
183 file.write(reinterpret_cast<const char*>(&code_size), sizeof(code_size)) 205 file.write(reinterpret_cast<const char*>(&code_size), sizeof(code_size))
184 .write(reinterpret_cast<const char*>(&num_texture_types), sizeof(num_texture_types)) 206 .write(reinterpret_cast<const char*>(&num_texture_types), sizeof(num_texture_types))
207 .write(reinterpret_cast<const char*>(&num_texture_pixel_formats),
208 sizeof(num_texture_pixel_formats))
185 .write(reinterpret_cast<const char*>(&num_cbuf_values), sizeof(num_cbuf_values)) 209 .write(reinterpret_cast<const char*>(&num_cbuf_values), sizeof(num_cbuf_values))
186 .write(reinterpret_cast<const char*>(&local_memory_size), sizeof(local_memory_size)) 210 .write(reinterpret_cast<const char*>(&local_memory_size), sizeof(local_memory_size))
187 .write(reinterpret_cast<const char*>(&texture_bound), sizeof(texture_bound)) 211 .write(reinterpret_cast<const char*>(&texture_bound), sizeof(texture_bound))
@@ -196,6 +220,10 @@ void GenericEnvironment::Serialize(std::ofstream& file) const {
196 file.write(reinterpret_cast<const char*>(&key), sizeof(key)) 220 file.write(reinterpret_cast<const char*>(&key), sizeof(key))
197 .write(reinterpret_cast<const char*>(&type), sizeof(type)); 221 .write(reinterpret_cast<const char*>(&type), sizeof(type));
198 } 222 }
223 for (const auto& [key, format] : texture_pixel_formats) {
224 file.write(reinterpret_cast<const char*>(&key), sizeof(key))
225 .write(reinterpret_cast<const char*>(&format), sizeof(format));
226 }
199 for (const auto& [key, type] : cbuf_values) { 227 for (const auto& [key, type] : cbuf_values) {
200 file.write(reinterpret_cast<const char*>(&key), sizeof(key)) 228 file.write(reinterpret_cast<const char*>(&key), sizeof(key))
201 .write(reinterpret_cast<const char*>(&type), sizeof(type)); 229 .write(reinterpret_cast<const char*>(&type), sizeof(type));
@@ -239,15 +267,13 @@ std::optional<u64> GenericEnvironment::TryFindSize() {
239 return std::nullopt; 267 return std::nullopt;
240} 268}
241 269
242Shader::TextureType GenericEnvironment::ReadTextureTypeImpl(GPUVAddr tic_addr, u32 tic_limit, 270Tegra::Texture::TICEntry GenericEnvironment::ReadTextureInfo(GPUVAddr tic_addr, u32 tic_limit,
243 bool via_header_index, u32 raw) { 271 bool via_header_index, u32 raw) {
244 const auto handle{Tegra::Texture::TexturePair(raw, via_header_index)}; 272 const auto handle{Tegra::Texture::TexturePair(raw, via_header_index)};
245 const GPUVAddr descriptor_addr{tic_addr + handle.first * sizeof(Tegra::Texture::TICEntry)}; 273 const GPUVAddr descriptor_addr{tic_addr + handle.first * sizeof(Tegra::Texture::TICEntry)};
246 Tegra::Texture::TICEntry entry; 274 Tegra::Texture::TICEntry entry;
247 gpu_memory->ReadBlock(descriptor_addr, &entry, sizeof(entry)); 275 gpu_memory->ReadBlock(descriptor_addr, &entry, sizeof(entry));
248 const Shader::TextureType result{ConvertType(entry)}; 276 return entry;
249 texture_types.emplace(raw, result);
250 return result;
251} 277}
252 278
253GraphicsEnvironment::GraphicsEnvironment(Tegra::Engines::Maxwell3D& maxwell3d_, 279GraphicsEnvironment::GraphicsEnvironment(Tegra::Engines::Maxwell3D& maxwell3d_,
@@ -307,13 +333,26 @@ u32 GraphicsEnvironment::ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) {
307Shader::TextureType GraphicsEnvironment::ReadTextureType(u32 handle) { 333Shader::TextureType GraphicsEnvironment::ReadTextureType(u32 handle) {
308 const auto& regs{maxwell3d->regs}; 334 const auto& regs{maxwell3d->regs};
309 const bool via_header_index{regs.sampler_binding == Maxwell::SamplerBinding::ViaHeaderBinding}; 335 const bool via_header_index{regs.sampler_binding == Maxwell::SamplerBinding::ViaHeaderBinding};
310 return ReadTextureTypeImpl(regs.tex_header.Address(), regs.tex_header.limit, via_header_index, 336 auto entry =
311 handle); 337 ReadTextureInfo(regs.tex_header.Address(), regs.tex_header.limit, via_header_index, handle);
338 const Shader::TextureType result{ConvertTextureType(entry)};
339 texture_types.emplace(handle, result);
340 return result;
341}
342
343Shader::TexturePixelFormat GraphicsEnvironment::ReadTexturePixelFormat(u32 handle) {
344 const auto& regs{maxwell3d->regs};
345 const bool via_header_index{regs.sampler_binding == Maxwell::SamplerBinding::ViaHeaderBinding};
346 auto entry =
347 ReadTextureInfo(regs.tex_header.Address(), regs.tex_header.limit, via_header_index, handle);
348 const Shader::TexturePixelFormat result(ConvertTexturePixelFormat(entry));
349 texture_pixel_formats.emplace(handle, result);
350 return result;
312} 351}
313 352
314u32 GraphicsEnvironment::ReadViewportTransformState() { 353u32 GraphicsEnvironment::ReadViewportTransformState() {
315 const auto& regs{maxwell3d->regs}; 354 const auto& regs{maxwell3d->regs};
316 viewport_transform_state = regs.viewport_transform_enabled; 355 viewport_transform_state = regs.viewport_scale_offset_enbled;
317 return viewport_transform_state; 356 return viewport_transform_state;
318} 357}
319 358
@@ -345,7 +384,19 @@ u32 ComputeEnvironment::ReadCbufValue(u32 cbuf_index, u32 cbuf_offset) {
345Shader::TextureType ComputeEnvironment::ReadTextureType(u32 handle) { 384Shader::TextureType ComputeEnvironment::ReadTextureType(u32 handle) {
346 const auto& regs{kepler_compute->regs}; 385 const auto& regs{kepler_compute->regs};
347 const auto& qmd{kepler_compute->launch_description}; 386 const auto& qmd{kepler_compute->launch_description};
348 return ReadTextureTypeImpl(regs.tic.Address(), regs.tic.limit, qmd.linked_tsc != 0, handle); 387 auto entry = ReadTextureInfo(regs.tic.Address(), regs.tic.limit, qmd.linked_tsc != 0, handle);
388 const Shader::TextureType result{ConvertTextureType(entry)};
389 texture_types.emplace(handle, result);
390 return result;
391}
392
393Shader::TexturePixelFormat ComputeEnvironment::ReadTexturePixelFormat(u32 handle) {
394 const auto& regs{kepler_compute->regs};
395 const auto& qmd{kepler_compute->launch_description};
396 auto entry = ReadTextureInfo(regs.tic.Address(), regs.tic.limit, qmd.linked_tsc != 0, handle);
397 const Shader::TexturePixelFormat result(ConvertTexturePixelFormat(entry));
398 texture_pixel_formats.emplace(handle, result);
399 return result;
349} 400}
350 401
351u32 ComputeEnvironment::ReadViewportTransformState() { 402u32 ComputeEnvironment::ReadViewportTransformState() {
@@ -355,9 +406,12 @@ u32 ComputeEnvironment::ReadViewportTransformState() {
355void FileEnvironment::Deserialize(std::ifstream& file) { 406void FileEnvironment::Deserialize(std::ifstream& file) {
356 u64 code_size{}; 407 u64 code_size{};
357 u64 num_texture_types{}; 408 u64 num_texture_types{};
409 u64 num_texture_pixel_formats{};
358 u64 num_cbuf_values{}; 410 u64 num_cbuf_values{};
359 file.read(reinterpret_cast<char*>(&code_size), sizeof(code_size)) 411 file.read(reinterpret_cast<char*>(&code_size), sizeof(code_size))
360 .read(reinterpret_cast<char*>(&num_texture_types), sizeof(num_texture_types)) 412 .read(reinterpret_cast<char*>(&num_texture_types), sizeof(num_texture_types))
413 .read(reinterpret_cast<char*>(&num_texture_pixel_formats),
414 sizeof(num_texture_pixel_formats))
361 .read(reinterpret_cast<char*>(&num_cbuf_values), sizeof(num_cbuf_values)) 415 .read(reinterpret_cast<char*>(&num_cbuf_values), sizeof(num_cbuf_values))
362 .read(reinterpret_cast<char*>(&local_memory_size), sizeof(local_memory_size)) 416 .read(reinterpret_cast<char*>(&local_memory_size), sizeof(local_memory_size))
363 .read(reinterpret_cast<char*>(&texture_bound), sizeof(texture_bound)) 417 .read(reinterpret_cast<char*>(&texture_bound), sizeof(texture_bound))
@@ -375,6 +429,13 @@ void FileEnvironment::Deserialize(std::ifstream& file) {
375 .read(reinterpret_cast<char*>(&type), sizeof(type)); 429 .read(reinterpret_cast<char*>(&type), sizeof(type));
376 texture_types.emplace(key, type); 430 texture_types.emplace(key, type);
377 } 431 }
432 for (size_t i = 0; i < num_texture_pixel_formats; ++i) {
433 u32 key;
434 Shader::TexturePixelFormat format;
435 file.read(reinterpret_cast<char*>(&key), sizeof(key))
436 .read(reinterpret_cast<char*>(&format), sizeof(format));
437 texture_pixel_formats.emplace(key, format);
438 }
378 for (size_t i = 0; i < num_cbuf_values; ++i) { 439 for (size_t i = 0; i < num_cbuf_values; ++i) {
379 u64 key; 440 u64 key;
380 u32 value; 441 u32 value;
@@ -422,6 +483,14 @@ Shader::TextureType FileEnvironment::ReadTextureType(u32 handle) {
422 return it->second; 483 return it->second;
423} 484}
424 485
486Shader::TexturePixelFormat FileEnvironment::ReadTexturePixelFormat(u32 handle) {
487 const auto it{texture_pixel_formats.find(handle)};
488 if (it == texture_pixel_formats.end()) {
489 throw Shader::LogicError("Uncached read texture pixel format");
490 }
491 return it->second;
492}
493
425u32 FileEnvironment::ReadViewportTransformState() { 494u32 FileEnvironment::ReadViewportTransformState() {
426 return viewport_transform_state; 495 return viewport_transform_state;
427} 496}