summaryrefslogtreecommitdiffstats
path: root/src/video_core/renderer_opengl
diff options
context:
space:
mode:
Diffstat (limited to 'src/video_core/renderer_opengl')
-rw-r--r--src/video_core/renderer_opengl/gl_buffer_cache.cpp2
-rw-r--r--src/video_core/renderer_opengl/gl_global_cache.cpp94
-rw-r--r--src/video_core/renderer_opengl/gl_global_cache.h78
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp219
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.h61
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.cpp116
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer_cache.h36
-rw-r--r--src/video_core/renderer_opengl/gl_resource_manager.cpp5
-rw-r--r--src/video_core/renderer_opengl/gl_shader_cache.cpp199
-rw-r--r--src/video_core/renderer_opengl/gl_shader_cache.h96
-rw-r--r--src/video_core/renderer_opengl/gl_shader_decompiler.cpp4710
-rw-r--r--src/video_core/renderer_opengl/gl_shader_decompiler.h103
-rw-r--r--src/video_core/renderer_opengl/gl_shader_gen.cpp118
-rw-r--r--src/video_core/renderer_opengl/gl_shader_gen.h161
-rw-r--r--src/video_core/renderer_opengl/gl_state.cpp27
-rw-r--r--src/video_core/renderer_opengl/gl_state.h9
-rw-r--r--src/video_core/renderer_opengl/gl_stream_buffer.cpp26
-rw-r--r--src/video_core/renderer_opengl/gl_stream_buffer.h3
-rw-r--r--src/video_core/renderer_opengl/renderer_opengl.cpp89
-rw-r--r--src/video_core/renderer_opengl/renderer_opengl.h19
20 files changed, 2054 insertions, 4117 deletions
diff --git a/src/video_core/renderer_opengl/gl_buffer_cache.cpp b/src/video_core/renderer_opengl/gl_buffer_cache.cpp
index 46a6c0308..bd2b30e77 100644
--- a/src/video_core/renderer_opengl/gl_buffer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_buffer_cache.cpp
@@ -14,7 +14,7 @@
namespace OpenGL {
OGLBufferCache::OGLBufferCache(RasterizerOpenGL& rasterizer, std::size_t size)
- : RasterizerCache{rasterizer}, stream_buffer(GL_ARRAY_BUFFER, size) {}
+ : RasterizerCache{rasterizer}, stream_buffer(size, true) {}
GLintptr OGLBufferCache::UploadMemory(Tegra::GPUVAddr gpu_addr, std::size_t size,
std::size_t alignment, bool cache) {
diff --git a/src/video_core/renderer_opengl/gl_global_cache.cpp b/src/video_core/renderer_opengl/gl_global_cache.cpp
new file mode 100644
index 000000000..c7f32feaa
--- /dev/null
+++ b/src/video_core/renderer_opengl/gl_global_cache.cpp
@@ -0,0 +1,94 @@
+// Copyright 2018 yuzu Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#include <glad/glad.h>
+
+#include "common/assert.h"
+#include "common/logging/log.h"
+#include "core/core.h"
+#include "core/memory.h"
+#include "video_core/renderer_opengl/gl_global_cache.h"
+#include "video_core/renderer_opengl/gl_rasterizer.h"
+#include "video_core/renderer_opengl/gl_shader_decompiler.h"
+#include "video_core/renderer_opengl/utils.h"
+
+namespace OpenGL {
+
+CachedGlobalRegion::CachedGlobalRegion(VAddr addr, u32 size) : addr{addr}, size{size} {
+ buffer.Create();
+ // Bind and unbind the buffer so it gets allocated by the driver
+ glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer.handle);
+ glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
+ LabelGLObject(GL_BUFFER, buffer.handle, addr, "GlobalMemory");
+}
+
+void CachedGlobalRegion::Reload(u32 size_) {
+ constexpr auto max_size = static_cast<u32>(RasterizerOpenGL::MaxGlobalMemorySize);
+
+ size = size_;
+ if (size > max_size) {
+ size = max_size;
+ LOG_CRITICAL(HW_GPU, "Global region size {} exceeded the expected size {}!", size_,
+ max_size);
+ }
+
+ // TODO(Rodrigo): Get rid of Memory::GetPointer with a staging buffer
+ glBindBuffer(GL_SHADER_STORAGE_BUFFER, buffer.handle);
+ glBufferData(GL_SHADER_STORAGE_BUFFER, size, Memory::GetPointer(addr), GL_DYNAMIC_DRAW);
+}
+
+GlobalRegion GlobalRegionCacheOpenGL::TryGetReservedGlobalRegion(VAddr addr, u32 size) const {
+ const auto search{reserve.find(addr)};
+ if (search == reserve.end()) {
+ return {};
+ }
+ return search->second;
+}
+
+GlobalRegion GlobalRegionCacheOpenGL::GetUncachedGlobalRegion(VAddr addr, u32 size) {
+ GlobalRegion region{TryGetReservedGlobalRegion(addr, size)};
+ if (!region) {
+ // No reserved surface available, create a new one and reserve it
+ region = std::make_shared<CachedGlobalRegion>(addr, size);
+ ReserveGlobalRegion(region);
+ }
+ region->Reload(size);
+ return region;
+}
+
+void GlobalRegionCacheOpenGL::ReserveGlobalRegion(const GlobalRegion& region) {
+ reserve[region->GetAddr()] = region;
+}
+
+GlobalRegionCacheOpenGL::GlobalRegionCacheOpenGL(RasterizerOpenGL& rasterizer)
+ : RasterizerCache{rasterizer} {}
+
+GlobalRegion GlobalRegionCacheOpenGL::GetGlobalRegion(
+ const GLShader::GlobalMemoryEntry& global_region,
+ Tegra::Engines::Maxwell3D::Regs::ShaderStage stage) {
+
+ auto& gpu{Core::System::GetInstance().GPU()};
+ const auto cbufs = gpu.Maxwell3D().state.shader_stages[static_cast<u64>(stage)];
+ const auto cbuf_addr = gpu.MemoryManager().GpuToCpuAddress(
+ cbufs.const_buffers[global_region.GetCbufIndex()].address + global_region.GetCbufOffset());
+ ASSERT(cbuf_addr);
+
+ const auto actual_addr_gpu = Memory::Read64(*cbuf_addr);
+ const auto size = Memory::Read32(*cbuf_addr + 8);
+ const auto actual_addr = gpu.MemoryManager().GpuToCpuAddress(actual_addr_gpu);
+ ASSERT(actual_addr);
+
+ // Look up global region in the cache based on address
+ GlobalRegion region = TryGet(*actual_addr);
+
+ if (!region) {
+ // No global region found - create a new one
+ region = GetUncachedGlobalRegion(*actual_addr, size);
+ Register(region);
+ }
+
+ return region;
+}
+
+} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_global_cache.h b/src/video_core/renderer_opengl/gl_global_cache.h
new file mode 100644
index 000000000..37830bb7c
--- /dev/null
+++ b/src/video_core/renderer_opengl/gl_global_cache.h
@@ -0,0 +1,78 @@
+// Copyright 2018 yuzu Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <memory>
+#include <unordered_map>
+
+#include <glad/glad.h>
+
+#include "common/assert.h"
+#include "common/common_types.h"
+#include "video_core/engines/maxwell_3d.h"
+#include "video_core/rasterizer_cache.h"
+#include "video_core/renderer_opengl/gl_resource_manager.h"
+
+namespace OpenGL {
+
+namespace GLShader {
+class GlobalMemoryEntry;
+} // namespace GLShader
+
+class RasterizerOpenGL;
+class CachedGlobalRegion;
+using GlobalRegion = std::shared_ptr<CachedGlobalRegion>;
+
+class CachedGlobalRegion final : public RasterizerCacheObject {
+public:
+ explicit CachedGlobalRegion(VAddr addr, u32 size);
+
+ /// Gets the address of the shader in guest memory, required for cache management
+ VAddr GetAddr() const {
+ return addr;
+ }
+
+ /// Gets the size of the shader in guest memory, required for cache management
+ std::size_t GetSizeInBytes() const {
+ return size;
+ }
+
+ /// Gets the GL program handle for the buffer
+ GLuint GetBufferHandle() const {
+ return buffer.handle;
+ }
+
+ /// Reloads the global region from guest memory
+ void Reload(u32 size_);
+
+ // TODO(Rodrigo): When global memory is written (STG), implement flushing
+ void Flush() override {
+ UNIMPLEMENTED();
+ }
+
+private:
+ VAddr addr{};
+ u32 size{};
+
+ OGLBuffer buffer;
+};
+
+class GlobalRegionCacheOpenGL final : public RasterizerCache<GlobalRegion> {
+public:
+ explicit GlobalRegionCacheOpenGL(RasterizerOpenGL& rasterizer);
+
+ /// Gets the current specified shader stage program
+ GlobalRegion GetGlobalRegion(const GLShader::GlobalMemoryEntry& descriptor,
+ Tegra::Engines::Maxwell3D::Regs::ShaderStage stage);
+
+private:
+ GlobalRegion TryGetReservedGlobalRegion(VAddr addr, u32 size) const;
+ GlobalRegion GetUncachedGlobalRegion(VAddr addr, u32 size);
+ void ReserveGlobalRegion(const GlobalRegion& region);
+
+ std::unordered_map<VAddr, GlobalRegion> reserve;
+};
+
+} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index 2b29fc45f..ee313cb2f 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -101,7 +101,7 @@ struct FramebufferCacheKey {
RasterizerOpenGL::RasterizerOpenGL(Core::Frontend::EmuWindow& window, ScreenInfo& info)
: res_cache{*this}, shader_cache{*this}, emu_window{window}, screen_info{info},
- buffer_cache(*this, STREAM_BUFFER_SIZE) {
+ buffer_cache(*this, STREAM_BUFFER_SIZE), global_cache{*this} {
// Create sampler objects
for (std::size_t i = 0; i < texture_samplers.size(); ++i) {
texture_samplers[i].Create();
@@ -135,27 +135,31 @@ void RasterizerOpenGL::CheckExtensions() {
}
}
-void RasterizerOpenGL::SetupVertexFormat() {
+GLuint RasterizerOpenGL::SetupVertexFormat() {
auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
const auto& regs = gpu.regs;
- if (!gpu.dirty_flags.vertex_attrib_format)
- return;
+ if (!gpu.dirty_flags.vertex_attrib_format) {
+ return state.draw.vertex_array;
+ }
gpu.dirty_flags.vertex_attrib_format = false;
MICROPROFILE_SCOPE(OpenGL_VAO);
auto [iter, is_cache_miss] = vertex_array_cache.try_emplace(regs.vertex_attrib_format);
- auto& VAO = iter->second;
+ auto& vao_entry = iter->second;
if (is_cache_miss) {
- VAO.Create();
- state.draw.vertex_array = VAO.handle;
- state.ApplyVertexBufferState();
+ vao_entry.Create();
+ const GLuint vao = vao_entry.handle;
+
+ // Eventhough we are using DSA to create this vertex array, there is a bug on Intel's blob
+ // that fails to properly create the vertex array if it's not bound even after creating it
+ // with glCreateVertexArrays
+ state.draw.vertex_array = vao;
+ state.ApplyVertexArrayState();
- // The index buffer binding is stored within the VAO. Stupid OpenGL, but easy to work
- // around.
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer_cache.GetHandle());
+ glVertexArrayElementBuffer(vao, buffer_cache.GetHandle());
// Use the vertex array as-is, assumes that the data is formatted correctly for OpenGL.
// Enables the first 16 vertex attributes always, as we don't know which ones are actually
@@ -163,7 +167,7 @@ void RasterizerOpenGL::SetupVertexFormat() {
// for now to avoid OpenGL errors.
// TODO(Subv): Analyze the shader to identify which attributes are actually used and don't
// assume every shader uses them all.
- for (unsigned index = 0; index < 16; ++index) {
+ for (u32 index = 0; index < 16; ++index) {
const auto& attrib = regs.vertex_attrib_format[index];
// Ignore invalid attributes.
@@ -178,28 +182,29 @@ void RasterizerOpenGL::SetupVertexFormat() {
ASSERT(buffer.IsEnabled());
- glEnableVertexAttribArray(index);
+ glEnableVertexArrayAttrib(vao, index);
if (attrib.type == Tegra::Engines::Maxwell3D::Regs::VertexAttribute::Type::SignedInt ||
attrib.type ==
Tegra::Engines::Maxwell3D::Regs::VertexAttribute::Type::UnsignedInt) {
- glVertexAttribIFormat(index, attrib.ComponentCount(),
- MaxwellToGL::VertexType(attrib), attrib.offset);
+ glVertexArrayAttribIFormat(vao, index, attrib.ComponentCount(),
+ MaxwellToGL::VertexType(attrib), attrib.offset);
} else {
- glVertexAttribFormat(index, attrib.ComponentCount(),
- MaxwellToGL::VertexType(attrib),
- attrib.IsNormalized() ? GL_TRUE : GL_FALSE, attrib.offset);
+ glVertexArrayAttribFormat(
+ vao, index, attrib.ComponentCount(), MaxwellToGL::VertexType(attrib),
+ attrib.IsNormalized() ? GL_TRUE : GL_FALSE, attrib.offset);
}
- glVertexAttribBinding(index, attrib.buffer);
+ glVertexArrayAttribBinding(vao, index, attrib.buffer);
}
}
- state.draw.vertex_array = VAO.handle;
- state.ApplyVertexBufferState();
// Rebinding the VAO invalidates the vertex buffer bindings.
gpu.dirty_flags.vertex_array = 0xFFFFFFFF;
+
+ state.draw.vertex_array = vao_entry.handle;
+ return vao_entry.handle;
}
-void RasterizerOpenGL::SetupVertexBuffer() {
+void RasterizerOpenGL::SetupVertexBuffer(GLuint vao) {
auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
const auto& regs = gpu.regs;
@@ -217,7 +222,7 @@ void RasterizerOpenGL::SetupVertexBuffer() {
if (!vertex_array.IsEnabled())
continue;
- Tegra::GPUVAddr start = vertex_array.StartAddress();
+ const Tegra::GPUVAddr start = vertex_array.StartAddress();
const Tegra::GPUVAddr end = regs.vertex_array_limit[index].LimitAddress();
ASSERT(end > start);
@@ -225,21 +230,18 @@ void RasterizerOpenGL::SetupVertexBuffer() {
const GLintptr vertex_buffer_offset = buffer_cache.UploadMemory(start, size);
// Bind the vertex array to the buffer at the current offset.
- glBindVertexBuffer(index, buffer_cache.GetHandle(), vertex_buffer_offset,
- vertex_array.stride);
+ glVertexArrayVertexBuffer(vao, index, buffer_cache.GetHandle(), vertex_buffer_offset,
+ vertex_array.stride);
if (regs.instanced_arrays.IsInstancingEnabled(index) && vertex_array.divisor != 0) {
// Enable vertex buffer instancing with the specified divisor.
- glVertexBindingDivisor(index, vertex_array.divisor);
+ glVertexArrayBindingDivisor(vao, index, vertex_array.divisor);
} else {
// Disable the vertex buffer instancing.
- glVertexBindingDivisor(index, 0);
+ glVertexArrayBindingDivisor(vao, index, 0);
}
}
- // Implicit set by glBindVertexBuffer. Stupid glstate handling...
- state.draw.vertex_buffer = buffer_cache.GetHandle();
-
gpu.dirty_flags.vertex_array = 0;
}
@@ -293,12 +295,9 @@ DrawParameters RasterizerOpenGL::SetupDraw() {
void RasterizerOpenGL::SetupShaders(GLenum primitive_mode) {
MICROPROFILE_SCOPE(OpenGL_Shader);
- const auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
+ auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
- // Next available bindpoints to use when uploading the const buffers and textures to the GLSL
- // shaders. The constbuffer bindpoint starts after the shader stage configuration bind points.
- u32 current_constbuffer_bindpoint = Tegra::Engines::Maxwell3D::Regs::MaxShaderStage;
- u32 current_texture_bindpoint = 0;
+ BaseBindings base_bindings;
std::array<bool, Maxwell::NumClipDistances> clip_distances{};
for (std::size_t index = 0; index < Maxwell::MaxShaderProgram; ++index) {
@@ -322,50 +321,42 @@ void RasterizerOpenGL::SetupShaders(GLenum primitive_mode) {
const GLintptr offset = buffer_cache.UploadHostMemory(
&ubo, sizeof(ubo), static_cast<std::size_t>(uniform_buffer_alignment));
- // Bind the buffer
- glBindBufferRange(GL_UNIFORM_BUFFER, static_cast<GLuint>(stage), buffer_cache.GetHandle(),
- offset, static_cast<GLsizeiptr>(sizeof(ubo)));
+ // Bind the emulation info buffer
+ glBindBufferRange(GL_UNIFORM_BUFFER, base_bindings.cbuf, buffer_cache.GetHandle(), offset,
+ static_cast<GLsizeiptr>(sizeof(ubo)));
Shader shader{shader_cache.GetStageProgram(program)};
+ const auto [program_handle, next_bindings] =
+ shader->GetProgramHandle(primitive_mode, base_bindings);
switch (program) {
case Maxwell::ShaderProgram::VertexA:
- case Maxwell::ShaderProgram::VertexB: {
- shader_program_manager->UseProgrammableVertexShader(
- shader->GetProgramHandle(primitive_mode));
+ case Maxwell::ShaderProgram::VertexB:
+ shader_program_manager->UseProgrammableVertexShader(program_handle);
break;
- }
- case Maxwell::ShaderProgram::Geometry: {
- shader_program_manager->UseProgrammableGeometryShader(
- shader->GetProgramHandle(primitive_mode));
+ case Maxwell::ShaderProgram::Geometry:
+ shader_program_manager->UseProgrammableGeometryShader(program_handle);
break;
- }
- case Maxwell::ShaderProgram::Fragment: {
- shader_program_manager->UseProgrammableFragmentShader(
- shader->GetProgramHandle(primitive_mode));
+ case Maxwell::ShaderProgram::Fragment:
+ shader_program_manager->UseProgrammableFragmentShader(program_handle);
break;
- }
default:
LOG_CRITICAL(HW_GPU, "Unimplemented shader index={}, enable={}, offset=0x{:08X}", index,
shader_config.enable.Value(), shader_config.offset);
UNREACHABLE();
}
- // Configure the const buffers for this shader stage.
- current_constbuffer_bindpoint =
- SetupConstBuffers(static_cast<Maxwell::ShaderStage>(stage), shader, primitive_mode,
- current_constbuffer_bindpoint);
-
- // Configure the textures for this shader stage.
- current_texture_bindpoint = SetupTextures(static_cast<Maxwell::ShaderStage>(stage), shader,
- primitive_mode, current_texture_bindpoint);
+ const auto stage_enum = static_cast<Maxwell::ShaderStage>(stage);
+ SetupConstBuffers(stage_enum, shader, program_handle, base_bindings);
+ SetupGlobalRegions(stage_enum, shader, program_handle, base_bindings);
+ SetupTextures(stage_enum, shader, program_handle, base_bindings);
// Workaround for Intel drivers.
// When a clip distance is enabled but not set in the shader it crops parts of the screen
// (sometimes it's half the screen, sometimes three quarters). To avoid this, enable the
// clip distances only when it's written by a shader stage.
for (std::size_t i = 0; i < Maxwell::NumClipDistances; ++i) {
- clip_distances[i] |= shader->GetShaderEntries().clip_distances[i];
+ clip_distances[i] = clip_distances[i] || shader->GetShaderEntries().clip_distances[i];
}
// When VertexA is enabled, we have dual vertex shaders
@@ -373,9 +364,13 @@ void RasterizerOpenGL::SetupShaders(GLenum primitive_mode) {
// VertexB was combined with VertexA, so we skip the VertexB iteration
index++;
}
+
+ base_bindings = next_bindings;
}
SyncClipEnabled(clip_distances);
+
+ gpu.dirty_flags.shaders = false;
}
void RasterizerOpenGL::SetupCachedFramebuffer(const FramebufferCacheKey& fbkey,
@@ -486,17 +481,26 @@ void RasterizerOpenGL::ConfigureFramebuffers(OpenGLState& current_state, bool us
bool using_depth_fb, bool preserve_contents,
std::optional<std::size_t> single_color_target) {
MICROPROFILE_SCOPE(OpenGL_Framebuffer);
- const auto& regs = Core::System::GetInstance().GPU().Maxwell3D().regs;
+ const auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
+ const auto& regs = gpu.regs;
+
+ const FramebufferConfigState fb_config_state{using_color_fb, using_depth_fb, preserve_contents,
+ single_color_target};
+ if (fb_config_state == current_framebuffer_config_state && gpu.dirty_flags.color_buffer == 0 &&
+ !gpu.dirty_flags.zeta_buffer) {
+ // Only skip if the previous ConfigureFramebuffers call was from the same kind (multiple or
+ // single color targets). This is done because the guest registers may not change but the
+ // host framebuffer may contain different attachments
+ return;
+ }
+ current_framebuffer_config_state = fb_config_state;
Surface depth_surface;
if (using_depth_fb) {
depth_surface = res_cache.GetDepthBufferSurface(preserve_contents);
}
- // TODO(bunnei): Figure out how the below register works. According to envytools, this should be
- // used to enable multiple render targets. However, it is left unset on all games that I have
- // tested.
- UNIMPLEMENTED_IF(regs.rt_separate_frag_data != 0);
+ UNIMPLEMENTED_IF(regs.rt_separate_frag_data == 0);
// Bind the framebuffer surfaces
current_state.framebuffer_srgb.enabled = regs.framebuffer_srgb != 0;
@@ -630,8 +634,6 @@ void RasterizerOpenGL::Clear() {
return;
}
- ScopeAcquireGLContext acquire_context{emu_window};
-
ConfigureFramebuffers(clear_state, use_color, use_depth || use_stencil, false,
regs.clear_buffers.RT.Value());
if (regs.clear_flags.scissor) {
@@ -665,8 +667,6 @@ void RasterizerOpenGL::DrawArrays() {
auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
const auto& regs = gpu.regs;
- ScopeAcquireGLContext acquire_context{emu_window};
-
ConfigureFramebuffers(state);
SyncColorMask();
SyncFragmentColorClampState();
@@ -689,9 +689,6 @@ void RasterizerOpenGL::DrawArrays() {
// Draw the vertex batch
const bool is_indexed = accelerate_draw == AccelDraw::Indexed;
- state.draw.vertex_buffer = buffer_cache.GetHandle();
- state.ApplyVertexBufferState();
-
std::size_t buffer_size = CalculateVertexArraysSize();
// Add space for index buffer (keeping in mind non-core primitives)
@@ -721,8 +718,9 @@ void RasterizerOpenGL::DrawArrays() {
gpu.dirty_flags.vertex_array = 0xFFFFFFFF;
}
- SetupVertexFormat();
- SetupVertexBuffer();
+ const GLuint vao = SetupVertexFormat();
+ SetupVertexBuffer(vao);
+
DrawParameters params = SetupDraw();
SetupShaders(params.primitive_mode);
@@ -761,6 +759,7 @@ void RasterizerOpenGL::InvalidateRegion(VAddr addr, u64 size) {
MICROPROFILE_SCOPE(OpenGL_CacheManagement);
res_cache.InvalidateRegion(addr, size);
shader_cache.InvalidateRegion(addr, size);
+ global_cache.InvalidateRegion(addr, size);
buffer_cache.InvalidateRegion(addr, size);
}
@@ -916,13 +915,14 @@ void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Tegra::Texture::TSCEntr
}
}
-u32 RasterizerOpenGL::SetupConstBuffers(Maxwell::ShaderStage stage, Shader& shader,
- GLenum primitive_mode, u32 current_bindpoint) {
+void RasterizerOpenGL::SetupConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
+ const Shader& shader, GLuint program_handle,
+ BaseBindings base_bindings) {
MICROPROFILE_SCOPE(OpenGL_UBO);
const auto& gpu = Core::System::GetInstance().GPU();
const auto& maxwell3d = gpu.Maxwell3D();
const auto& shader_stage = maxwell3d.state.shader_stages[static_cast<std::size_t>(stage)];
- const auto& entries = shader->GetShaderEntries().const_buffer_entries;
+ const auto& entries = shader->GetShaderEntries().const_buffers;
constexpr u64 max_binds = Tegra::Engines::Maxwell3D::Regs::MaxConstBuffers;
std::array<GLuint, max_binds> bind_buffers;
@@ -965,72 +965,73 @@ u32 RasterizerOpenGL::SetupConstBuffers(Maxwell::ShaderStage stage, Shader& shad
size = Common::AlignUp(size, sizeof(GLvec4));
ASSERT_MSG(size <= MaxConstbufferSize, "Constbuffer too big");
- GLintptr const_buffer_offset = buffer_cache.UploadMemory(
+ const GLintptr const_buffer_offset = buffer_cache.UploadMemory(
buffer.address, size, static_cast<std::size_t>(uniform_buffer_alignment));
- // Now configure the bindpoint of the buffer inside the shader
- glUniformBlockBinding(shader->GetProgramHandle(primitive_mode),
- shader->GetProgramResourceIndex(used_buffer),
- current_bindpoint + bindpoint);
-
// Prepare values for multibind
bind_buffers[bindpoint] = buffer_cache.GetHandle();
bind_offsets[bindpoint] = const_buffer_offset;
bind_sizes[bindpoint] = size;
}
- glBindBuffersRange(GL_UNIFORM_BUFFER, current_bindpoint, static_cast<GLsizei>(entries.size()),
+ // The first binding is reserved for emulation values
+ const GLuint ubo_base_binding = base_bindings.cbuf + 1;
+ glBindBuffersRange(GL_UNIFORM_BUFFER, ubo_base_binding, static_cast<GLsizei>(entries.size()),
bind_buffers.data(), bind_offsets.data(), bind_sizes.data());
+}
+
+void RasterizerOpenGL::SetupGlobalRegions(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
+ const Shader& shader, GLenum primitive_mode,
+ BaseBindings base_bindings) {
+ // TODO(Rodrigo): Use ARB_multi_bind here
+ const auto& entries = shader->GetShaderEntries().global_memory_entries;
- return current_bindpoint + static_cast<u32>(entries.size());
+ for (u32 bindpoint = 0; bindpoint < static_cast<u32>(entries.size()); ++bindpoint) {
+ const auto& entry = entries[bindpoint];
+ const u32 current_bindpoint = base_bindings.gmem + bindpoint;
+ const auto& region = global_cache.GetGlobalRegion(entry, stage);
+
+ glBindBufferBase(GL_SHADER_STORAGE_BUFFER, current_bindpoint, region->GetBufferHandle());
+ }
}
-u32 RasterizerOpenGL::SetupTextures(Maxwell::ShaderStage stage, Shader& shader,
- GLenum primitive_mode, u32 current_unit) {
+void RasterizerOpenGL::SetupTextures(Maxwell::ShaderStage stage, const Shader& shader,
+ GLuint program_handle, BaseBindings base_bindings) {
MICROPROFILE_SCOPE(OpenGL_Texture);
const auto& gpu = Core::System::GetInstance().GPU();
const auto& maxwell3d = gpu.Maxwell3D();
- const auto& entries = shader->GetShaderEntries().texture_samplers;
+ const auto& entries = shader->GetShaderEntries().samplers;
- ASSERT_MSG(current_unit + entries.size() <= std::size(state.texture_units),
+ ASSERT_MSG(base_bindings.sampler + entries.size() <= std::size(state.texture_units),
"Exceeded the number of active textures.");
for (u32 bindpoint = 0; bindpoint < entries.size(); ++bindpoint) {
const auto& entry = entries[bindpoint];
- const u32 current_bindpoint = current_unit + bindpoint;
-
- // Bind the uniform to the sampler.
-
- glProgramUniform1i(shader->GetProgramHandle(primitive_mode),
- shader->GetUniformLocation(entry), current_bindpoint);
+ const u32 current_bindpoint = base_bindings.sampler + bindpoint;
+ auto& unit = state.texture_units[current_bindpoint];
const auto texture = maxwell3d.GetStageTexture(entry.GetStage(), entry.GetOffset());
-
if (!texture.enabled) {
- state.texture_units[current_bindpoint].texture = 0;
+ unit.texture = 0;
continue;
}
texture_samplers[current_bindpoint].SyncWithConfig(texture.tsc);
+
Surface surface = res_cache.GetTextureSurface(texture, entry);
if (surface != nullptr) {
- state.texture_units[current_bindpoint].texture = surface->Texture().handle;
- state.texture_units[current_bindpoint].target = surface->Target();
- state.texture_units[current_bindpoint].swizzle.r =
- MaxwellToGL::SwizzleSource(texture.tic.x_source);
- state.texture_units[current_bindpoint].swizzle.g =
- MaxwellToGL::SwizzleSource(texture.tic.y_source);
- state.texture_units[current_bindpoint].swizzle.b =
- MaxwellToGL::SwizzleSource(texture.tic.z_source);
- state.texture_units[current_bindpoint].swizzle.a =
- MaxwellToGL::SwizzleSource(texture.tic.w_source);
+ unit.texture =
+ entry.IsArray() ? surface->TextureLayer().handle : surface->Texture().handle;
+ unit.target = entry.IsArray() ? surface->TargetLayer() : surface->Target();
+ unit.swizzle.r = MaxwellToGL::SwizzleSource(texture.tic.x_source);
+ unit.swizzle.g = MaxwellToGL::SwizzleSource(texture.tic.y_source);
+ unit.swizzle.b = MaxwellToGL::SwizzleSource(texture.tic.z_source);
+ unit.swizzle.a = MaxwellToGL::SwizzleSource(texture.tic.w_source);
} else {
// Can occur when texture addr is null or its memory is unmapped/invalid
- state.texture_units[current_bindpoint].texture = 0;
+ unit.texture = 0;
}
}
-
- return current_unit + static_cast<u32>(entries.size());
}
void RasterizerOpenGL::SyncViewport(OpenGLState& current_state) {
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index 8a891ffc7..a103692f9 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -23,6 +23,7 @@
#include "video_core/rasterizer_cache.h"
#include "video_core/rasterizer_interface.h"
#include "video_core/renderer_opengl/gl_buffer_cache.h"
+#include "video_core/renderer_opengl/gl_global_cache.h"
#include "video_core/renderer_opengl/gl_primitive_assembler.h"
#include "video_core/renderer_opengl/gl_rasterizer_cache.h"
#include "video_core/renderer_opengl/gl_resource_manager.h"
@@ -66,6 +67,10 @@ public:
static_assert(MaxConstbufferSize % sizeof(GLvec4) == 0,
"The maximum size of a constbuffer must be a multiple of the size of GLvec4");
+ static constexpr std::size_t MaxGlobalMemorySize = 0x10000;
+ static_assert(MaxGlobalMemorySize % sizeof(float) == 0,
+ "The maximum size of a global memory must be a multiple of the size of float");
+
private:
class SamplerInfo {
public:
@@ -94,6 +99,23 @@ private:
float max_anisotropic = 1.0f;
};
+ struct FramebufferConfigState {
+ bool using_color_fb{};
+ bool using_depth_fb{};
+ bool preserve_contents{};
+ std::optional<std::size_t> single_color_target;
+
+ bool operator==(const FramebufferConfigState& rhs) const {
+ return std::tie(using_color_fb, using_depth_fb, preserve_contents,
+ single_color_target) == std::tie(rhs.using_color_fb, rhs.using_depth_fb,
+ rhs.preserve_contents,
+ rhs.single_color_target);
+ }
+ bool operator!=(const FramebufferConfigState& rhs) const {
+ return !operator==(rhs);
+ }
+ };
+
/**
* Configures the color and depth framebuffer states.
* @param use_color_fb If true, configure color framebuffers.
@@ -105,25 +127,18 @@ private:
bool using_depth_fb = true, bool preserve_contents = true,
std::optional<std::size_t> single_color_target = {});
- /*
- * Configures the current constbuffers to use for the draw command.
- * @param stage The shader stage to configure buffers for.
- * @param shader The shader object that contains the specified stage.
- * @param current_bindpoint The offset at which to start counting new buffer bindpoints.
- * @returns The next available bindpoint for use in the next shader stage.
- */
- u32 SetupConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, Shader& shader,
- GLenum primitive_mode, u32 current_bindpoint);
-
- /*
- * Configures the current textures to use for the draw command.
- * @param stage The shader stage to configure textures for.
- * @param shader The shader object that contains the specified stage.
- * @param current_unit The offset at which to start counting unused texture units.
- * @returns The next available bindpoint for use in the next shader stage.
- */
- u32 SetupTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, Shader& shader,
- GLenum primitive_mode, u32 current_unit);
+ /// Configures the current constbuffers to use for the draw command.
+ void SetupConstBuffers(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, const Shader& shader,
+ GLuint program_handle, BaseBindings base_bindings);
+
+ /// Configures the current global memory entries to use for the draw command.
+ void SetupGlobalRegions(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage,
+ const Shader& shader, GLenum primitive_mode,
+ BaseBindings base_bindings);
+
+ /// Configures the current textures to use for the draw command.
+ void SetupTextures(Tegra::Engines::Maxwell3D::Regs::ShaderStage stage, const Shader& shader,
+ GLuint program_handle, BaseBindings base_bindings);
/// Syncs the viewport and depth range to match the guest state
void SyncViewport(OpenGLState& current_state);
@@ -185,6 +200,7 @@ private:
RasterizerCacheOpenGL res_cache;
ShaderCacheOpenGL shader_cache;
+ GlobalRegionCacheOpenGL global_cache;
Core::Frontend::EmuWindow& emu_window;
@@ -197,6 +213,7 @@ private:
vertex_array_cache;
std::map<FramebufferCacheKey, OGLFramebuffer> framebuffer_cache;
+ FramebufferConfigState current_framebuffer_config_state;
std::array<SamplerInfo, Tegra::Engines::Maxwell3D::Regs::NumTextureSamplers> texture_samplers;
@@ -209,8 +226,10 @@ private:
std::size_t CalculateIndexBufferSize() const;
- void SetupVertexFormat();
- void SetupVertexBuffer();
+ /// Updates and returns a vertex array object representing current vertex format
+ GLuint SetupVertexFormat();
+
+ void SetupVertexBuffer(GLuint vao);
DrawParameters SetupDraw();
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
index cfc0dae29..2b9c4628f 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.cpp
@@ -44,6 +44,17 @@ struct FormatTuple {
bool compressed;
};
+static void ApplyTextureDefaults(GLenum target, u32 max_mip_level) {
+ glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+ glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+ glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, max_mip_level - 1);
+ if (max_mip_level == 1) {
+ glTexParameterf(target, GL_TEXTURE_LOD_BIAS, 1000.0);
+ }
+}
+
void SurfaceParams::InitCacheParameters(Tegra::GPUVAddr gpu_addr_) {
auto& memory_manager{Core::System::GetInstance().GPU().MemoryManager()};
const auto cpu_addr{memory_manager.GpuToCpuAddress(gpu_addr_)};
@@ -101,8 +112,18 @@ std::size_t SurfaceParams::InnerMemorySize(bool force_gl, bool layer_only,
params.srgb_conversion = config.tic.IsSrgbConversionEnabled();
params.pixel_format = PixelFormatFromTextureFormat(config.tic.format, config.tic.r_type.Value(),
params.srgb_conversion);
+
+ if (params.pixel_format == PixelFormat::R16U && config.tsc.depth_compare_enabled) {
+ // Some titles create a 'R16U' (normalized 16-bit) texture with depth_compare enabled,
+ // then attempt to sample from it via a shadow sampler. Convert format to Z16 (which also
+ // causes GetFormatType to properly return 'Depth' below).
+ params.pixel_format = PixelFormat::Z16;
+ }
+
params.component_type = ComponentTypeFromTexture(config.tic.r_type.Value());
params.type = GetFormatType(params.pixel_format);
+ UNIMPLEMENTED_IF(params.type == SurfaceType::ColorTexture && config.tsc.depth_compare_enabled);
+
params.width = Common::AlignUp(config.tic.Width(), GetCompressionFactor(params.pixel_format));
params.height = Common::AlignUp(config.tic.Height(), GetCompressionFactor(params.pixel_format));
params.unaligned_height = config.tic.Height();
@@ -147,6 +168,7 @@ std::size_t SurfaceParams::InnerMemorySize(bool force_gl, bool layer_only,
}
params.is_layered = SurfaceTargetIsLayered(params.target);
+ params.is_array = SurfaceTargetIsArray(params.target);
params.max_mip_level = config.tic.max_mip_level + 1;
params.rt = {};
@@ -261,7 +283,7 @@ static constexpr std::array<FormatTuple, VideoCore::Surface::MaxPixelFormat> tex
{GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, ComponentType::UInt, false}, // R8UI
{GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT, ComponentType::Float, false}, // RGBA16F
{GL_RGBA16, GL_RGBA, GL_UNSIGNED_SHORT, ComponentType::UNorm, false}, // RGBA16U
- {GL_RGBA16UI, GL_RGBA, GL_UNSIGNED_SHORT, ComponentType::UInt, false}, // RGBA16UI
+ {GL_RGBA16UI, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT, ComponentType::UInt, false}, // RGBA16UI
{GL_R11F_G11F_B10F, GL_RGB, GL_UNSIGNED_INT_10F_11F_11F_REV, ComponentType::Float,
false}, // R11FG11FB10F
{GL_RGBA32UI, GL_RGBA_INTEGER, GL_UNSIGNED_INT, ComponentType::UInt, false}, // RGBA32UI
@@ -282,8 +304,6 @@ static constexpr std::array<FormatTuple, VideoCore::Surface::MaxPixelFormat> tex
{GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT, GL_RGB, GL_UNSIGNED_INT_8_8_8_8, ComponentType::Float,
true}, // BC6H_SF16
{GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // ASTC_2D_4X4
- {GL_RG8, GL_RG, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // G8R8U
- {GL_RG8, GL_RG, GL_BYTE, ComponentType::SNorm, false}, // G8R8S
{GL_RGBA8, GL_BGRA, GL_UNSIGNED_BYTE, ComponentType::UNorm, false}, // BGRA8
{GL_RGBA32F, GL_RGBA, GL_FLOAT, ComponentType::Float, false}, // RGBA32F
{GL_RG32F, GL_RG, GL_FLOAT, ComponentType::Float, false}, // RG32F
@@ -437,7 +457,7 @@ static void CopySurface(const Surface& src_surface, const Surface& dst_surface,
const std::size_t buffer_size = std::max(src_params.size_in_bytes, dst_params.size_in_bytes);
glBindBuffer(GL_PIXEL_PACK_BUFFER, copy_pbo_handle);
- glBufferData(GL_PIXEL_PACK_BUFFER, buffer_size, nullptr, GL_STREAM_DRAW);
+ glBufferData(GL_PIXEL_PACK_BUFFER, buffer_size, nullptr, GL_STREAM_COPY);
if (source_format.compressed) {
glGetCompressedTextureImage(src_surface->Texture().handle, src_attachment,
static_cast<GLsizei>(src_params.size_in_bytes), nullptr);
@@ -526,6 +546,9 @@ CachedSurface::CachedSurface(const SurfaceParams& params)
glActiveTexture(GL_TEXTURE0);
const auto& format_tuple = GetFormatTuple(params.pixel_format, params.component_type);
+ gl_internal_format = format_tuple.internal_format;
+ gl_is_compressed = format_tuple.compressed;
+
if (!format_tuple.compressed) {
// Only pre-create the texture for non-compressed textures.
switch (params.target) {
@@ -554,15 +577,7 @@ CachedSurface::CachedSurface(const SurfaceParams& params)
}
}
- glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
- glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
- glTexParameteri(SurfaceTargetToGL(params.target), GL_TEXTURE_MAX_LEVEL,
- params.max_mip_level - 1);
- if (params.max_mip_level == 1) {
- glTexParameterf(SurfaceTargetToGL(params.target), GL_TEXTURE_LOD_BIAS, 1000.0);
- }
+ ApplyTextureDefaults(SurfaceTargetToGL(params.target), params.max_mip_level);
OpenGL::LabelGLObject(GL_TEXTURE, texture.handle, params.addr, params.IdentityString());
@@ -613,18 +628,6 @@ static void ConvertS8Z24ToZ24S8(std::vector<u8>& data, u32 width, u32 height, bo
}
}
-static void ConvertG8R8ToR8G8(std::vector<u8>& data, u32 width, u32 height) {
- constexpr auto bpp{GetBytesPerPixel(PixelFormat::G8R8U)};
- for (std::size_t y = 0; y < height; ++y) {
- for (std::size_t x = 0; x < width; ++x) {
- const std::size_t offset{bpp * (y * width + x)};
- const u8 temp{data[offset]};
- data[offset] = data[offset + 1];
- data[offset + 1] = temp;
- }
- }
-}
-
/**
* Helper function to perform software conversion (as needed) when loading a buffer from Switch
* memory. This is for Maxwell pixel formats that cannot be represented as-is in OpenGL or with
@@ -657,12 +660,6 @@ static void ConvertFormatAsNeeded_LoadGLBuffer(std::vector<u8>& data, PixelForma
// Convert the S8Z24 depth format to Z24S8, as OpenGL does not support S8Z24.
ConvertS8Z24ToZ24S8(data, width, height, false);
break;
-
- case PixelFormat::G8R8U:
- case PixelFormat::G8R8S:
- // Convert the G8R8 color format to R8G8, as OpenGL does not support G8R8.
- ConvertG8R8ToR8G8(data, width, height);
- break;
}
}
@@ -674,8 +671,6 @@ static void ConvertFormatAsNeeded_LoadGLBuffer(std::vector<u8>& data, PixelForma
static void ConvertFormatAsNeeded_FlushGLBuffer(std::vector<u8>& data, PixelFormat pixel_format,
u32 width, u32 height) {
switch (pixel_format) {
- case PixelFormat::G8R8U:
- case PixelFormat::G8R8S:
case PixelFormat::ASTC_2D_4X4:
case PixelFormat::ASTC_2D_8X8:
case PixelFormat::ASTC_2D_4X4_SRGB:
@@ -879,6 +874,34 @@ void CachedSurface::UploadGLMipmapTexture(u32 mip_map, GLuint read_fb_handle,
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
}
+void CachedSurface::EnsureTextureView() {
+ if (texture_view.handle != 0)
+ return;
+ // Compressed texture are not being created with immutable storage
+ UNIMPLEMENTED_IF(gl_is_compressed);
+
+ const GLenum target{TargetLayer()};
+ const GLuint num_layers{target == GL_TEXTURE_CUBE_MAP_ARRAY ? 6u : 1u};
+ constexpr GLuint min_layer = 0;
+ constexpr GLuint min_level = 0;
+
+ texture_view.Create();
+ glTextureView(texture_view.handle, target, texture.handle, gl_internal_format, min_level,
+ params.max_mip_level, min_layer, num_layers);
+
+ OpenGLState cur_state = OpenGLState::GetCurState();
+ const auto& old_tex = cur_state.texture_units[0];
+ SCOPE_EXIT({
+ cur_state.texture_units[0] = old_tex;
+ cur_state.Apply();
+ });
+ cur_state.texture_units[0].texture = texture_view.handle;
+ cur_state.texture_units[0].target = target;
+ cur_state.Apply();
+
+ ApplyTextureDefaults(target, params.max_mip_level);
+}
+
MICROPROFILE_DEFINE(OpenGL_TextureUL, "OpenGL", "Texture Upload", MP_RGB(128, 192, 64));
void CachedSurface::UploadGLTexture(GLuint read_fb_handle, GLuint draw_fb_handle) {
if (params.type == SurfaceType::Fill)
@@ -903,9 +926,16 @@ Surface RasterizerCacheOpenGL::GetTextureSurface(const Tegra::Texture::FullTextu
}
Surface RasterizerCacheOpenGL::GetDepthBufferSurface(bool preserve_contents) {
- const auto& regs{Core::System::GetInstance().GPU().Maxwell3D().regs};
+ auto& gpu{Core::System::GetInstance().GPU().Maxwell3D()};
+ const auto& regs{gpu.regs};
+
+ if (!gpu.dirty_flags.zeta_buffer) {
+ return last_depth_buffer;
+ }
+ gpu.dirty_flags.zeta_buffer = false;
+
if (!regs.zeta.Address() || !regs.zeta_enable) {
- return {};
+ return last_depth_buffer = {};
}
SurfaceParams depth_params{SurfaceParams::CreateForDepthBuffer(
@@ -913,25 +943,31 @@ Surface RasterizerCacheOpenGL::GetDepthBufferSurface(bool preserve_contents) {
regs.zeta.memory_layout.block_width, regs.zeta.memory_layout.block_height,
regs.zeta.memory_layout.block_depth, regs.zeta.memory_layout.type)};
- return GetSurface(depth_params, preserve_contents);
+ return last_depth_buffer = GetSurface(depth_params, preserve_contents);
}
Surface RasterizerCacheOpenGL::GetColorBufferSurface(std::size_t index, bool preserve_contents) {
- const auto& regs{Core::System::GetInstance().GPU().Maxwell3D().regs};
+ auto& gpu{Core::System::GetInstance().GPU().Maxwell3D()};
+ const auto& regs{gpu.regs};
+
+ if ((gpu.dirty_flags.color_buffer & (1u << static_cast<u32>(index))) == 0) {
+ return last_color_buffers[index];
+ }
+ gpu.dirty_flags.color_buffer &= ~(1u << static_cast<u32>(index));
ASSERT(index < Tegra::Engines::Maxwell3D::Regs::NumRenderTargets);
if (index >= regs.rt_control.count) {
- return {};
+ return last_color_buffers[index] = {};
}
if (regs.rt[index].Address() == 0 || regs.rt[index].format == Tegra::RenderTargetFormat::NONE) {
- return {};
+ return last_color_buffers[index] = {};
}
const SurfaceParams color_params{SurfaceParams::CreateForFramebuffer(index)};
- return GetSurface(color_params, preserve_contents);
+ return last_color_buffers[index] = GetSurface(color_params, preserve_contents);
}
void RasterizerCacheOpenGL::LoadSurface(const Surface& surface) {
diff --git a/src/video_core/renderer_opengl/gl_rasterizer_cache.h b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
index ee158bd91..8d7d6722c 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer_cache.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer_cache.h
@@ -276,6 +276,7 @@ struct SurfaceParams {
SurfaceClass identity;
u32 max_mip_level;
bool is_layered;
+ bool is_array;
bool srgb_conversion;
// Parameters used for caching
VAddr addr;
@@ -345,10 +346,31 @@ public:
return texture;
}
+ const OGLTexture& TextureLayer() {
+ if (params.is_array) {
+ return Texture();
+ }
+ EnsureTextureView();
+ return texture_view;
+ }
+
GLenum Target() const {
return gl_target;
}
+ GLenum TargetLayer() const {
+ using VideoCore::Surface::SurfaceTarget;
+ switch (params.target) {
+ case SurfaceTarget::Texture1D:
+ return GL_TEXTURE_1D_ARRAY;
+ case SurfaceTarget::Texture2D:
+ return GL_TEXTURE_2D_ARRAY;
+ case SurfaceTarget::TextureCubemap:
+ return GL_TEXTURE_CUBE_MAP_ARRAY;
+ }
+ return Target();
+ }
+
const SurfaceParams& GetSurfaceParams() const {
return params;
}
@@ -363,11 +385,16 @@ public:
private:
void UploadGLMipmapTexture(u32 mip_map, GLuint read_fb_handle, GLuint draw_fb_handle);
+ void EnsureTextureView();
+
OGLTexture texture;
+ OGLTexture texture_view;
std::vector<std::vector<u8>> gl_buffer;
- SurfaceParams params;
- GLenum gl_target;
- std::size_t cached_size_in_bytes;
+ SurfaceParams params{};
+ GLenum gl_target{};
+ GLenum gl_internal_format{};
+ bool gl_is_compressed{};
+ std::size_t cached_size_in_bytes{};
};
class RasterizerCacheOpenGL final : public RasterizerCache<Surface> {
@@ -422,6 +449,9 @@ private:
/// Use a Pixel Buffer Object to download the previous texture and then upload it to the new one
/// using the new format.
OGLBuffer copy_pbo;
+
+ std::array<Surface, Tegra::Engines::Maxwell3D::Regs::NumRenderTargets> last_color_buffers;
+ Surface last_depth_buffer;
};
} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_resource_manager.cpp b/src/video_core/renderer_opengl/gl_resource_manager.cpp
index c17d5ac00..1da744158 100644
--- a/src/video_core/renderer_opengl/gl_resource_manager.cpp
+++ b/src/video_core/renderer_opengl/gl_resource_manager.cpp
@@ -117,7 +117,7 @@ void OGLBuffer::Create() {
return;
MICROPROFILE_SCOPE(OpenGL_ResourceCreation);
- glGenBuffers(1, &handle);
+ glCreateBuffers(1, &handle);
}
void OGLBuffer::Release() {
@@ -126,7 +126,6 @@ void OGLBuffer::Release() {
MICROPROFILE_SCOPE(OpenGL_ResourceDeletion);
glDeleteBuffers(1, &handle);
- OpenGLState::GetCurState().ResetBuffer(handle).Apply();
handle = 0;
}
@@ -152,7 +151,7 @@ void OGLVertexArray::Create() {
return;
MICROPROFILE_SCOPE(OpenGL_ResourceCreation);
- glGenVertexArrays(1, &handle);
+ glCreateVertexArrays(1, &handle);
}
void OGLVertexArray::Release() {
diff --git a/src/video_core/renderer_opengl/gl_shader_cache.cpp b/src/video_core/renderer_opengl/gl_shader_cache.cpp
index 038b25c75..90eda7814 100644
--- a/src/video_core/renderer_opengl/gl_shader_cache.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_cache.cpp
@@ -2,17 +2,23 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include <boost/functional/hash.hpp>
#include "common/assert.h"
+#include "common/hash.h"
#include "core/core.h"
#include "core/memory.h"
#include "video_core/engines/maxwell_3d.h"
#include "video_core/renderer_opengl/gl_rasterizer.h"
#include "video_core/renderer_opengl/gl_shader_cache.h"
+#include "video_core/renderer_opengl/gl_shader_decompiler.h"
#include "video_core/renderer_opengl/gl_shader_manager.h"
#include "video_core/renderer_opengl/utils.h"
+#include "video_core/shader/shader_ir.h"
namespace OpenGL {
+using VideoCommon::Shader::ProgramCode;
+
/// Gets the address for the specified shader stage program
static VAddr GetShaderAddress(Maxwell::ShaderProgram program) {
const auto& gpu = Core::System::GetInstance().GPU().Maxwell3D();
@@ -22,42 +28,31 @@ static VAddr GetShaderAddress(Maxwell::ShaderProgram program) {
}
/// Gets the shader program code from memory for the specified address
-static GLShader::ProgramCode GetShaderCode(VAddr addr) {
- GLShader::ProgramCode program_code(GLShader::MAX_PROGRAM_CODE_LENGTH);
+static ProgramCode GetShaderCode(VAddr addr) {
+ ProgramCode program_code(VideoCommon::Shader::MAX_PROGRAM_LENGTH);
Memory::ReadBlock(addr, program_code.data(), program_code.size() * sizeof(u64));
return program_code;
}
-/// Helper function to set shader uniform block bindings for a single shader stage
-static void SetShaderUniformBlockBinding(GLuint shader, const char* name,
- Maxwell::ShaderStage binding, std::size_t expected_size) {
- const GLuint ub_index = glGetUniformBlockIndex(shader, name);
- if (ub_index == GL_INVALID_INDEX) {
- return;
+/// Gets the shader type from a Maxwell program type
+constexpr GLenum GetShaderType(Maxwell::ShaderProgram program_type) {
+ switch (program_type) {
+ case Maxwell::ShaderProgram::VertexA:
+ case Maxwell::ShaderProgram::VertexB:
+ return GL_VERTEX_SHADER;
+ case Maxwell::ShaderProgram::Geometry:
+ return GL_GEOMETRY_SHADER;
+ case Maxwell::ShaderProgram::Fragment:
+ return GL_FRAGMENT_SHADER;
+ default:
+ return GL_NONE;
}
-
- GLint ub_size = 0;
- glGetActiveUniformBlockiv(shader, ub_index, GL_UNIFORM_BLOCK_DATA_SIZE, &ub_size);
- ASSERT_MSG(static_cast<std::size_t>(ub_size) == expected_size,
- "Uniform block size did not match! Got {}, expected {}", ub_size, expected_size);
- glUniformBlockBinding(shader, ub_index, static_cast<GLuint>(binding));
-}
-
-/// Sets shader uniform block bindings for an entire shader program
-static void SetShaderUniformBlockBindings(GLuint shader) {
- SetShaderUniformBlockBinding(shader, "vs_config", Maxwell::ShaderStage::Vertex,
- sizeof(GLShader::MaxwellUniformData));
- SetShaderUniformBlockBinding(shader, "gs_config", Maxwell::ShaderStage::Geometry,
- sizeof(GLShader::MaxwellUniformData));
- SetShaderUniformBlockBinding(shader, "fs_config", Maxwell::ShaderStage::Fragment,
- sizeof(GLShader::MaxwellUniformData));
}
CachedShader::CachedShader(VAddr addr, Maxwell::ShaderProgram program_type)
: addr{addr}, program_type{program_type}, setup{GetShaderCode(addr)} {
GLShader::ProgramResult program_result;
- GLenum gl_type{};
switch (program_type) {
case Maxwell::ShaderProgram::VertexA:
@@ -66,16 +61,16 @@ CachedShader::CachedShader(VAddr addr, Maxwell::ShaderProgram program_type)
// stage here.
setup.SetProgramB(GetShaderCode(GetShaderAddress(Maxwell::ShaderProgram::VertexB)));
case Maxwell::ShaderProgram::VertexB:
+ CalculateProperties();
program_result = GLShader::GenerateVertexShader(setup);
- gl_type = GL_VERTEX_SHADER;
break;
case Maxwell::ShaderProgram::Geometry:
+ CalculateProperties();
program_result = GLShader::GenerateGeometryShader(setup);
- gl_type = GL_GEOMETRY_SHADER;
break;
case Maxwell::ShaderProgram::Fragment:
+ CalculateProperties();
program_result = GLShader::GenerateFragmentShader(setup);
- gl_type = GL_FRAGMENT_SHADER;
break;
default:
LOG_CRITICAL(HW_GPU, "Unimplemented program_type={}", static_cast<u32>(program_type));
@@ -83,66 +78,156 @@ CachedShader::CachedShader(VAddr addr, Maxwell::ShaderProgram program_type)
return;
}
+ code = program_result.first;
entries = program_result.second;
shader_length = entries.shader_length;
+}
- if (program_type != Maxwell::ShaderProgram::Geometry) {
- OGLShader shader;
- shader.Create(program_result.first.c_str(), gl_type);
- program.Create(true, shader.handle);
- SetShaderUniformBlockBindings(program.handle);
- LabelGLObject(GL_PROGRAM, program.handle, addr);
+std::tuple<GLuint, BaseBindings> CachedShader::GetProgramHandle(GLenum primitive_mode,
+ BaseBindings base_bindings) {
+ GLuint handle{};
+ if (program_type == Maxwell::ShaderProgram::Geometry) {
+ handle = GetGeometryShader(primitive_mode, base_bindings);
} else {
- // Store shader's code to lazily build it on draw
- geometry_programs.code = program_result.first;
+ const auto [entry, is_cache_miss] = programs.try_emplace(base_bindings);
+ auto& program = entry->second;
+ if (is_cache_miss) {
+ std::string source = AllocateBindings(base_bindings);
+ source += code;
+
+ OGLShader shader;
+ shader.Create(source.c_str(), GetShaderType(program_type));
+ program.Create(true, shader.handle);
+ LabelGLObject(GL_PROGRAM, program.handle, addr);
+ }
+
+ handle = program.handle;
}
+
+ // Add const buffer and samplers offset reserved by this shader. One UBO binding is reserved for
+ // emulation values
+ base_bindings.cbuf += static_cast<u32>(entries.const_buffers.size()) + 1;
+ base_bindings.gmem += static_cast<u32>(entries.global_memory_entries.size());
+ base_bindings.sampler += static_cast<u32>(entries.samplers.size());
+
+ return {handle, base_bindings};
}
-GLuint CachedShader::GetProgramResourceIndex(const GLShader::ConstBufferEntry& buffer) {
- const auto search{resource_cache.find(buffer.GetHash())};
- if (search == resource_cache.end()) {
- const GLuint index{
- glGetProgramResourceIndex(program.handle, GL_UNIFORM_BLOCK, buffer.GetName().c_str())};
- resource_cache[buffer.GetHash()] = index;
- return index;
+std::string CachedShader::AllocateBindings(BaseBindings base_bindings) {
+ std::string code = "#version 430 core\n";
+ code += fmt::format("#define EMULATION_UBO_BINDING {}\n", base_bindings.cbuf++);
+
+ for (const auto& cbuf : entries.const_buffers) {
+ code += fmt::format("#define CBUF_BINDING_{} {}\n", cbuf.GetIndex(), base_bindings.cbuf++);
}
- return search->second;
-}
+ for (const auto& gmem : entries.global_memory_entries) {
+ code += fmt::format("#define GMEM_BINDING_{}_{} {}\n", gmem.GetCbufIndex(),
+ gmem.GetCbufOffset(), base_bindings.gmem++);
+ }
-GLint CachedShader::GetUniformLocation(const GLShader::SamplerEntry& sampler) {
- const auto search{uniform_cache.find(sampler.GetHash())};
- if (search == uniform_cache.end()) {
- const GLint index{glGetUniformLocation(program.handle, sampler.GetName().c_str())};
- uniform_cache[sampler.GetHash()] = index;
- return index;
+ for (const auto& sampler : entries.samplers) {
+ code += fmt::format("#define SAMPLER_BINDING_{} {}\n", sampler.GetIndex(),
+ base_bindings.sampler++);
}
- return search->second;
+ return code;
+}
+
+GLuint CachedShader::GetGeometryShader(GLenum primitive_mode, BaseBindings base_bindings) {
+ const auto [entry, is_cache_miss] = geometry_programs.try_emplace(base_bindings);
+ auto& programs = entry->second;
+
+ switch (primitive_mode) {
+ case GL_POINTS:
+ return LazyGeometryProgram(programs.points, base_bindings, "points", 1, "ShaderPoints");
+ case GL_LINES:
+ case GL_LINE_STRIP:
+ return LazyGeometryProgram(programs.lines, base_bindings, "lines", 2, "ShaderLines");
+ case GL_LINES_ADJACENCY:
+ case GL_LINE_STRIP_ADJACENCY:
+ return LazyGeometryProgram(programs.lines_adjacency, base_bindings, "lines_adjacency", 4,
+ "ShaderLinesAdjacency");
+ case GL_TRIANGLES:
+ case GL_TRIANGLE_STRIP:
+ case GL_TRIANGLE_FAN:
+ return LazyGeometryProgram(programs.triangles, base_bindings, "triangles", 3,
+ "ShaderTriangles");
+ case GL_TRIANGLES_ADJACENCY:
+ case GL_TRIANGLE_STRIP_ADJACENCY:
+ return LazyGeometryProgram(programs.triangles_adjacency, base_bindings,
+ "triangles_adjacency", 6, "ShaderTrianglesAdjacency");
+ default:
+ UNREACHABLE_MSG("Unknown primitive mode.");
+ return LazyGeometryProgram(programs.points, base_bindings, "points", 1, "ShaderPoints");
+ }
}
-GLuint CachedShader::LazyGeometryProgram(OGLProgram& target_program,
+GLuint CachedShader::LazyGeometryProgram(OGLProgram& target_program, BaseBindings base_bindings,
const std::string& glsl_topology, u32 max_vertices,
const std::string& debug_name) {
if (target_program.handle != 0) {
return target_program.handle;
}
- std::string source = "#version 430 core\n";
+ std::string source = AllocateBindings(base_bindings);
source += "layout (" + glsl_topology + ") in;\n";
source += "#define MAX_VERTEX_INPUT " + std::to_string(max_vertices) + '\n';
- source += geometry_programs.code;
+ source += code;
OGLShader shader;
shader.Create(source.c_str(), GL_GEOMETRY_SHADER);
target_program.Create(true, shader.handle);
- SetShaderUniformBlockBindings(target_program.handle);
LabelGLObject(GL_PROGRAM, target_program.handle, addr, debug_name);
return target_program.handle;
};
+static bool IsSchedInstruction(std::size_t offset, std::size_t main_offset) {
+ // sched instructions appear once every 4 instructions.
+ static constexpr std::size_t SchedPeriod = 4;
+ const std::size_t absolute_offset = offset - main_offset;
+ return (absolute_offset % SchedPeriod) == 0;
+}
+
+static std::size_t CalculateProgramSize(const GLShader::ProgramCode& program) {
+ constexpr std::size_t start_offset = 10;
+ std::size_t offset = start_offset;
+ std::size_t size = start_offset * sizeof(u64);
+ while (offset < program.size()) {
+ const u64 inst = program[offset];
+ if (!IsSchedInstruction(offset, start_offset)) {
+ if (inst == 0 || (inst >> 52) == 0x50b) {
+ break;
+ }
+ }
+ size += sizeof(inst);
+ offset++;
+ }
+ return size;
+}
+
+void CachedShader::CalculateProperties() {
+ setup.program.real_size = CalculateProgramSize(setup.program.code);
+ setup.program.real_size_b = 0;
+ setup.program.unique_identifier = Common::CityHash64(
+ reinterpret_cast<const char*>(setup.program.code.data()), setup.program.real_size);
+ if (program_type == Maxwell::ShaderProgram::VertexA) {
+ std::size_t seed = 0;
+ boost::hash_combine(seed, setup.program.unique_identifier);
+ setup.program.real_size_b = CalculateProgramSize(setup.program.code_b);
+ const u64 identifier_b = Common::CityHash64(
+ reinterpret_cast<const char*>(setup.program.code_b.data()), setup.program.real_size_b);
+ boost::hash_combine(seed, identifier_b);
+ setup.program.unique_identifier = static_cast<u64>(seed);
+ }
+}
+
ShaderCacheOpenGL::ShaderCacheOpenGL(RasterizerOpenGL& rasterizer) : RasterizerCache{rasterizer} {}
Shader ShaderCacheOpenGL::GetStageProgram(Maxwell::ShaderProgram program) {
+ if (!Core::System::GetInstance().GPU().Maxwell3D().dirty_flags.shaders) {
+ return last_shaders[static_cast<u32>(program)];
+ }
+
const VAddr program_addr{GetShaderAddress(program)};
// Look up shader in the cache based on address
@@ -154,7 +239,7 @@ Shader ShaderCacheOpenGL::GetStageProgram(Maxwell::ShaderProgram program) {
Register(shader);
}
- return shader;
+ return last_shaders[static_cast<u32>(program)] = shader;
}
} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_shader_cache.h b/src/video_core/renderer_opengl/gl_shader_cache.h
index 08f470de3..904d15dd0 100644
--- a/src/video_core/renderer_opengl/gl_shader_cache.h
+++ b/src/video_core/renderer_opengl/gl_shader_cache.h
@@ -4,13 +4,18 @@
#pragma once
+#include <array>
#include <map>
#include <memory>
+#include <tuple>
+
+#include <glad/glad.h>
#include "common/assert.h"
#include "common/common_types.h"
#include "video_core/rasterizer_cache.h"
#include "video_core/renderer_opengl/gl_resource_manager.h"
+#include "video_core/renderer_opengl/gl_shader_decompiler.h"
#include "video_core/renderer_opengl/gl_shader_gen.h"
namespace OpenGL {
@@ -21,6 +26,16 @@ class RasterizerOpenGL;
using Shader = std::shared_ptr<CachedShader>;
using Maxwell = Tegra::Engines::Maxwell3D::Regs;
+struct BaseBindings {
+ u32 cbuf{};
+ u32 gmem{};
+ u32 sampler{};
+
+ bool operator<(const BaseBindings& rhs) const {
+ return std::tie(cbuf, gmem, sampler) < std::tie(rhs.cbuf, rhs.gmem, rhs.sampler);
+ }
+};
+
class CachedShader final : public RasterizerCacheObject {
public:
CachedShader(VAddr addr, Maxwell::ShaderProgram program_type);
@@ -42,67 +57,45 @@ public:
}
/// Gets the GL program handle for the shader
- GLuint GetProgramHandle(GLenum primitive_mode) {
- if (program_type != Maxwell::ShaderProgram::Geometry) {
- return program.handle;
- }
- switch (primitive_mode) {
- case GL_POINTS:
- return LazyGeometryProgram(geometry_programs.points, "points", 1, "ShaderPoints");
- case GL_LINES:
- case GL_LINE_STRIP:
- return LazyGeometryProgram(geometry_programs.lines, "lines", 2, "ShaderLines");
- case GL_LINES_ADJACENCY:
- case GL_LINE_STRIP_ADJACENCY:
- return LazyGeometryProgram(geometry_programs.lines_adjacency, "lines_adjacency", 4,
- "ShaderLinesAdjacency");
- case GL_TRIANGLES:
- case GL_TRIANGLE_STRIP:
- case GL_TRIANGLE_FAN:
- return LazyGeometryProgram(geometry_programs.triangles, "triangles", 3,
- "ShaderTriangles");
- case GL_TRIANGLES_ADJACENCY:
- case GL_TRIANGLE_STRIP_ADJACENCY:
- return LazyGeometryProgram(geometry_programs.triangles_adjacency, "triangles_adjacency",
- 6, "ShaderTrianglesAdjacency");
- default:
- UNREACHABLE_MSG("Unknown primitive mode.");
- }
- }
-
- /// Gets the GL program resource location for the specified resource, caching as needed
- GLuint GetProgramResourceIndex(const GLShader::ConstBufferEntry& buffer);
-
- /// Gets the GL uniform location for the specified resource, caching as needed
- GLint GetUniformLocation(const GLShader::SamplerEntry& sampler);
+ std::tuple<GLuint, BaseBindings> GetProgramHandle(GLenum primitive_mode,
+ BaseBindings base_bindings);
private:
- /// Generates a geometry shader or returns one that already exists.
- GLuint LazyGeometryProgram(OGLProgram& target_program, const std::string& glsl_topology,
- u32 max_vertices, const std::string& debug_name);
-
- VAddr addr;
- std::size_t shader_length;
- Maxwell::ShaderProgram program_type;
- GLShader::ShaderSetup setup;
- GLShader::ShaderEntries entries;
-
- // Non-geometry program.
- OGLProgram program;
-
// Geometry programs. These are needed because GLSL needs an input topology but it's not
// declared by the hardware. Workaround this issue by generating a different shader per input
// topology class.
- struct {
- std::string code;
+ struct GeometryPrograms {
OGLProgram points;
OGLProgram lines;
OGLProgram lines_adjacency;
OGLProgram triangles;
OGLProgram triangles_adjacency;
- } geometry_programs;
+ };
+
+ std::string AllocateBindings(BaseBindings base_bindings);
- std::map<u32, GLuint> resource_cache;
+ GLuint GetGeometryShader(GLenum primitive_mode, BaseBindings base_bindings);
+
+ /// Generates a geometry shader or returns one that already exists.
+ GLuint LazyGeometryProgram(OGLProgram& target_program, BaseBindings base_bindings,
+ const std::string& glsl_topology, u32 max_vertices,
+ const std::string& debug_name);
+
+ void CalculateProperties();
+
+ VAddr addr{};
+ std::size_t shader_length{};
+ Maxwell::ShaderProgram program_type{};
+ GLShader::ShaderSetup setup;
+ GLShader::ShaderEntries entries;
+
+ std::string code;
+
+ std::map<BaseBindings, OGLProgram> programs;
+ std::map<BaseBindings, GeometryPrograms> geometry_programs;
+
+ std::map<u32, GLuint> cbuf_resource_cache;
+ std::map<u32, GLuint> gmem_resource_cache;
std::map<u32, GLint> uniform_cache;
};
@@ -112,6 +105,9 @@ public:
/// Gets the current specified shader stage program
Shader GetStageProgram(Maxwell::ShaderProgram program);
+
+private:
+ std::array<Shader, Maxwell::MaxShaderProgram> last_shaders;
};
} // namespace OpenGL
diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp
index 4fc09cac6..004245431 100644
--- a/src/video_core/renderer_opengl/gl_shader_decompiler.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_decompiler.cpp
@@ -2,247 +2,42 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
-#include <map>
-#include <optional>
-#include <set>
+#include <array>
#include <string>
#include <string_view>
-#include <unordered_set>
+#include <variant>
#include <fmt/format.h>
+#include "common/alignment.h"
#include "common/assert.h"
#include "common/common_types.h"
-#include "video_core/engines/shader_bytecode.h"
-#include "video_core/engines/shader_header.h"
+#include "video_core/engines/maxwell_3d.h"
#include "video_core/renderer_opengl/gl_rasterizer.h"
#include "video_core/renderer_opengl/gl_shader_decompiler.h"
+#include "video_core/shader/shader_ir.h"
-namespace OpenGL::GLShader::Decompiler {
+namespace OpenGL::GLShader {
using Tegra::Shader::Attribute;
-using Tegra::Shader::Instruction;
-using Tegra::Shader::LogicOperation;
-using Tegra::Shader::OpCode;
+using Tegra::Shader::Header;
+using Tegra::Shader::IpaInterpMode;
+using Tegra::Shader::IpaMode;
+using Tegra::Shader::IpaSampleMode;
using Tegra::Shader::Register;
-using Tegra::Shader::Sampler;
-using Tegra::Shader::SubOp;
+using namespace VideoCommon::Shader;
-constexpr u32 PROGRAM_END = MAX_PROGRAM_CODE_LENGTH;
-constexpr u32 PROGRAM_HEADER_SIZE = sizeof(Tegra::Shader::Header);
+using Maxwell = Tegra::Engines::Maxwell3D::Regs;
+using ShaderStage = Tegra::Engines::Maxwell3D::Regs::ShaderStage;
+using Operation = const OperationNode&;
-constexpr u32 MAX_GEOMETRY_BUFFERS = 6;
-constexpr u32 MAX_ATTRIBUTES = 0x100; // Size in vec4s, this value is untested
+enum : u32 { POSITION_VARYING_LOCATION = 0, GENERIC_VARYING_START_LOCATION = 1 };
+constexpr u32 MAX_CONSTBUFFER_ELEMENTS =
+ static_cast<u32>(RasterizerOpenGL::MaxConstbufferSize) / (4 * sizeof(float));
+constexpr u32 MAX_GLOBALMEMORY_ELEMENTS =
+ static_cast<u32>(RasterizerOpenGL::MaxGlobalMemorySize) / sizeof(float);
-static const char* INTERNAL_FLAG_NAMES[] = {"zero_flag", "sign_flag", "carry_flag",
- "overflow_flag"};
-
-enum class InternalFlag : u64 {
- ZeroFlag = 0,
- SignFlag = 1,
- CarryFlag = 2,
- OverflowFlag = 3,
- Amount
-};
-
-class DecompileFail : public std::runtime_error {
-public:
- using std::runtime_error::runtime_error;
-};
-
-/// Generates code to use for a swizzle operation.
-static std::string GetSwizzle(u64 elem) {
- ASSERT(elem <= 3);
- std::string swizzle = ".";
- swizzle += "xyzw"[elem];
- return swizzle;
-}
-
-/// Translate topology
-static std::string GetTopologyName(Tegra::Shader::OutputTopology topology) {
- switch (topology) {
- case Tegra::Shader::OutputTopology::PointList:
- return "points";
- case Tegra::Shader::OutputTopology::LineStrip:
- return "line_strip";
- case Tegra::Shader::OutputTopology::TriangleStrip:
- return "triangle_strip";
- default:
- UNIMPLEMENTED_MSG("Unknown output topology: {}", static_cast<u32>(topology));
- return "points";
- }
-}
-
-/// Describes the behaviour of code path of a given entry point and a return point.
-enum class ExitMethod {
- Undetermined, ///< Internal value. Only occur when analyzing JMP loop.
- AlwaysReturn, ///< All code paths reach the return point.
- Conditional, ///< Code path reaches the return point or an END instruction conditionally.
- AlwaysEnd, ///< All code paths reach a END instruction.
-};
-
-/// A subroutine is a range of code refereced by a CALL, IF or LOOP instruction.
-struct Subroutine {
- /// Generates a name suitable for GLSL source code.
- std::string GetName() const {
- return "sub_" + std::to_string(begin) + '_' + std::to_string(end) + '_' + suffix;
- }
-
- u32 begin; ///< Entry point of the subroutine.
- u32 end; ///< Return point of the subroutine.
- const std::string& suffix; ///< Suffix of the shader, used to make a unique subroutine name
- ExitMethod exit_method; ///< Exit method of the subroutine.
- std::set<u32> labels; ///< Addresses refereced by JMP instructions.
-
- bool operator<(const Subroutine& rhs) const {
- return std::tie(begin, end) < std::tie(rhs.begin, rhs.end);
- }
-};
-
-/// Analyzes shader code and produces a set of subroutines.
-class ControlFlowAnalyzer {
-public:
- ControlFlowAnalyzer(const ProgramCode& program_code, u32 main_offset, const std::string& suffix)
- : program_code(program_code), shader_coverage_begin(main_offset),
- shader_coverage_end(main_offset + 1) {
-
- // Recursively finds all subroutines.
- const Subroutine& program_main = AddSubroutine(main_offset, PROGRAM_END, suffix);
- if (program_main.exit_method != ExitMethod::AlwaysEnd)
- throw DecompileFail("Program does not always end");
- }
-
- std::set<Subroutine> GetSubroutines() {
- return std::move(subroutines);
- }
-
- std::size_t GetShaderLength() const {
- return shader_coverage_end * sizeof(u64);
- }
-
-private:
- const ProgramCode& program_code;
- std::set<Subroutine> subroutines;
- std::map<std::pair<u32, u32>, ExitMethod> exit_method_map;
- u32 shader_coverage_begin;
- u32 shader_coverage_end;
-
- /// Adds and analyzes a new subroutine if it is not added yet.
- const Subroutine& AddSubroutine(u32 begin, u32 end, const std::string& suffix) {
- Subroutine subroutine{begin, end, suffix, ExitMethod::Undetermined, {}};
-
- const auto iter = subroutines.find(subroutine);
- if (iter != subroutines.end()) {
- return *iter;
- }
-
- subroutine.exit_method = Scan(begin, end, subroutine.labels);
- if (subroutine.exit_method == ExitMethod::Undetermined) {
- throw DecompileFail("Recursive function detected");
- }
-
- return *subroutines.insert(std::move(subroutine)).first;
- }
-
- /// Merges exit method of two parallel branches.
- static ExitMethod ParallelExit(ExitMethod a, ExitMethod b) {
- if (a == ExitMethod::Undetermined) {
- return b;
- }
- if (b == ExitMethod::Undetermined) {
- return a;
- }
- if (a == b) {
- return a;
- }
- return ExitMethod::Conditional;
- }
-
- /// Scans a range of code for labels and determines the exit method.
- ExitMethod Scan(u32 begin, u32 end, std::set<u32>& labels) {
- const auto [iter, inserted] =
- exit_method_map.emplace(std::make_pair(begin, end), ExitMethod::Undetermined);
- ExitMethod& exit_method = iter->second;
- if (!inserted)
- return exit_method;
-
- for (u32 offset = begin; offset != end && offset != PROGRAM_END; ++offset) {
- shader_coverage_begin = std::min(shader_coverage_begin, offset);
- shader_coverage_end = std::max(shader_coverage_end, offset + 1);
-
- const Instruction instr = {program_code[offset]};
- if (const auto opcode = OpCode::Decode(instr)) {
- switch (opcode->get().GetId()) {
- case OpCode::Id::EXIT: {
- // The EXIT instruction can be predicated, which means that the shader can
- // conditionally end on this instruction. We have to consider the case where the
- // condition is not met and check the exit method of that other basic block.
- using Tegra::Shader::Pred;
- if (instr.pred.pred_index == static_cast<u64>(Pred::UnusedIndex)) {
- return exit_method = ExitMethod::AlwaysEnd;
- } else {
- const ExitMethod not_met = Scan(offset + 1, end, labels);
- return exit_method = ParallelExit(ExitMethod::AlwaysEnd, not_met);
- }
- }
- case OpCode::Id::BRA: {
- const u32 target = offset + instr.bra.GetBranchTarget();
- labels.insert(target);
- const ExitMethod no_jmp = Scan(offset + 1, end, labels);
- const ExitMethod jmp = Scan(target, end, labels);
- return exit_method = ParallelExit(no_jmp, jmp);
- }
- case OpCode::Id::SSY:
- case OpCode::Id::PBK: {
- // The SSY and PBK use a similar encoding as the BRA instruction.
- UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
- "Constant buffer branching is not supported");
- const u32 target = offset + instr.bra.GetBranchTarget();
- labels.insert(target);
- // Continue scanning for an exit method.
- break;
- }
- }
- }
- }
- return exit_method = ExitMethod::AlwaysReturn;
- }
-};
-
-template <typename T>
-class ShaderScopedScope {
-public:
- explicit ShaderScopedScope(T& writer, std::string_view begin_expr, std::string end_expr)
- : writer(writer), end_expr(std::move(end_expr)) {
-
- if (begin_expr.empty()) {
- writer.AddLine('{');
- } else {
- writer.AddExpression(begin_expr);
- writer.AddLine(" {");
- }
- ++writer.scope;
- }
-
- ShaderScopedScope(const ShaderScopedScope&) = delete;
-
- ~ShaderScopedScope() {
- --writer.scope;
- if (end_expr.empty()) {
- writer.AddLine('}');
- } else {
- writer.AddExpression("} ");
- writer.AddExpression(end_expr);
- writer.AddLine(';');
- }
- }
-
- ShaderScopedScope& operator=(const ShaderScopedScope&) = delete;
-
-private:
- T& writer;
- std::string end_expr;
-};
+enum class Type { Bool, Bool2, Float, Int, Uint, HalfFloat };
class ShaderWriter {
public:
@@ -271,16 +66,17 @@ public:
shader_source += '\n';
}
- std::string GetResult() {
- return std::move(shader_source);
+ std::string GenerateTemporal() {
+ std::string temporal = "tmp";
+ temporal += std::to_string(temporal_index++);
+ return temporal;
}
- ShaderScopedScope<ShaderWriter> Scope(std::string_view begin_expr = {},
- std::string end_expr = {}) {
- return ShaderScopedScope(*this, begin_expr, end_expr);
+ std::string GetResult() {
+ return std::move(shader_source);
}
- int scope = 0;
+ s32 scope = 0;
private:
void AppendIndentation() {
@@ -288,3604 +84,1482 @@ private:
}
std::string shader_source;
+ u32 temporal_index = 1;
};
-/**
- * Represents an emulated shader register, used to track the state of that register for emulation
- * with GLSL. At this time, a register can be used as a float or an integer. This class is used for
- * bookkeeping within the GLSL program.
- */
-class GLSLRegister {
-public:
- enum class Type {
- Float,
- Integer,
- UnsignedInteger,
- };
-
- GLSLRegister(std::size_t index, const std::string& suffix) : index{index}, suffix{suffix} {}
-
- /// Gets the GLSL type string for a register
- static std::string GetTypeString() {
- return "float";
- }
-
- /// Gets the GLSL register prefix string, used for declarations and referencing
- static std::string GetPrefixString() {
- return "reg_";
- }
-
- /// Returns a GLSL string representing the current state of the register
- std::string GetString() const {
- return GetPrefixString() + std::to_string(index) + '_' + suffix;
- }
-
- /// Returns the index of the register
- std::size_t GetIndex() const {
- return index;
- }
-
-private:
- const std::size_t index;
- const std::string& suffix;
-};
-
-/**
- * Used to manage shader registers that are emulated with GLSL. This class keeps track of the state
- * of all registers (e.g. whether they are currently being used as Floats or Integers), and
- * generates the necessary GLSL code to perform conversions as needed. This class is used for
- * bookkeeping within the GLSL program.
- */
-class GLSLRegisterManager {
-public:
- GLSLRegisterManager(ShaderWriter& shader, ShaderWriter& declarations,
- const Maxwell3D::Regs::ShaderStage& stage, const std::string& suffix,
- const Tegra::Shader::Header& header)
- : shader{shader}, declarations{declarations}, stage{stage}, suffix{suffix}, header{header},
- fixed_pipeline_output_attributes_used{}, local_memory_size{0} {
- BuildRegisterList();
- BuildInputList();
- }
-
- /**
- * Returns code that does an integer size conversion for the specified size.
- * @param value Value to perform integer size conversion on.
- * @param size Register size to use for conversion instructions.
- * @returns GLSL string corresponding to the value converted to the specified size.
- */
- static std::string ConvertIntegerSize(const std::string& value, Register::Size size) {
- switch (size) {
- case Register::Size::Byte:
- return "((" + value + " << 24) >> 24)";
- case Register::Size::Short:
- return "((" + value + " << 16) >> 16)";
- case Register::Size::Word:
- // Default - do nothing
- return value;
- default:
- UNREACHABLE_MSG("Unimplemented conversion size: {}", static_cast<u32>(size));
- }
- }
-
- /**
- * Gets a register as an float.
- * @param reg The register to get.
- * @param elem The element to use for the operation.
- * @returns GLSL string corresponding to the register as a float.
- */
- std::string GetRegisterAsFloat(const Register& reg, unsigned elem = 0) {
- return GetRegister(reg, elem);
- }
-
- /**
- * Gets a register as an integer.
- * @param reg The register to get.
- * @param elem The element to use for the operation.
- * @param is_signed Whether to get the register as a signed (or unsigned) integer.
- * @param size Register size to use for conversion instructions.
- * @returns GLSL string corresponding to the register as an integer.
- */
- std::string GetRegisterAsInteger(const Register& reg, unsigned elem = 0, bool is_signed = true,
- Register::Size size = Register::Size::Word) {
- const std::string func{is_signed ? "floatBitsToInt" : "floatBitsToUint"};
- const std::string value{func + '(' + GetRegister(reg, elem) + ')'};
- return ConvertIntegerSize(value, size);
- }
-
- /**
- * Writes code that does a register assignment to float value operation.
- * @param reg The destination register to use.
- * @param elem The element to use for the operation.
- * @param value The code representing the value to assign.
- * @param dest_num_components Number of components in the destination.
- * @param value_num_components Number of components in the value.
- * @param is_saturated Optional, when True, saturates the provided value.
- * @param dest_elem Optional, the destination element to use for the operation.
- */
- void SetRegisterToFloat(const Register& reg, u64 elem, const std::string& value,
- u64 dest_num_components, u64 value_num_components,
- bool is_saturated = false, u64 dest_elem = 0, bool precise = false) {
-
- SetRegister(reg, elem, is_saturated ? "clamp(" + value + ", 0.0, 1.0)" : value,
- dest_num_components, value_num_components, dest_elem, precise);
- }
-
- /**
- * Writes code that does a register assignment to integer value operation.
- * @param reg The destination register to use.
- * @param elem The element to use for the operation.
- * @param value The code representing the value to assign.
- * @param dest_num_components Number of components in the destination.
- * @param value_num_components Number of components in the value.
- * @param is_saturated Optional, when True, saturates the provided value.
- * @param dest_elem Optional, the destination element to use for the operation.
- * @param size Register size to use for conversion instructions.
- */
- void SetRegisterToInteger(const Register& reg, bool is_signed, u64 elem,
- const std::string& value, u64 dest_num_components,
- u64 value_num_components, bool is_saturated = false,
- u64 dest_elem = 0, Register::Size size = Register::Size::Word,
- bool sets_cc = false) {
- UNIMPLEMENTED_IF(is_saturated);
-
- const std::string func{is_signed ? "intBitsToFloat" : "uintBitsToFloat"};
-
- SetRegister(reg, elem, func + '(' + ConvertIntegerSize(value, size) + ')',
- dest_num_components, value_num_components, dest_elem, false);
-
- if (sets_cc) {
- const std::string zero_condition = "( " + ConvertIntegerSize(value, size) + " == 0 )";
- SetInternalFlag(InternalFlag::ZeroFlag, zero_condition);
- LOG_WARNING(HW_GPU, "Condition codes implementation is incomplete.");
- }
- }
-
- /**
- * Writes code that does a register assignment to a half float value operation.
- * @param reg The destination register to use.
- * @param elem The element to use for the operation.
- * @param value The code representing the value to assign. Type has to be half float.
- * @param merge Half float kind of assignment.
- * @param dest_num_components Number of components in the destination.
- * @param value_num_components Number of components in the value.
- * @param is_saturated Optional, when True, saturates the provided value.
- * @param dest_elem Optional, the destination element to use for the operation.
- */
- void SetRegisterToHalfFloat(const Register& reg, u64 elem, const std::string& value,
- Tegra::Shader::HalfMerge merge, u64 dest_num_components,
- u64 value_num_components, bool is_saturated = false,
- u64 dest_elem = 0) {
- UNIMPLEMENTED_IF(is_saturated);
-
- const std::string result = [&]() {
- switch (merge) {
- case Tegra::Shader::HalfMerge::H0_H1:
- return "uintBitsToFloat(packHalf2x16(" + value + "))";
- case Tegra::Shader::HalfMerge::F32:
- // Half float instructions take the first component when doing a float cast.
- return "float(" + value + ".x)";
- case Tegra::Shader::HalfMerge::Mrg_H0:
- // TODO(Rodrigo): I guess Mrg_H0 and Mrg_H1 take their respective component from the
- // pack. I couldn't test this on hardware but it shouldn't really matter since most
- // of the time when a Mrg_* flag is used both components will be mirrored. That
- // being said, it deserves a test.
- return "((" + GetRegisterAsInteger(reg, 0, false) +
- " & 0xffff0000) | (packHalf2x16(" + value + ") & 0x0000ffff))";
- case Tegra::Shader::HalfMerge::Mrg_H1:
- return "((" + GetRegisterAsInteger(reg, 0, false) +
- " & 0x0000ffff) | (packHalf2x16(" + value + ") & 0xffff0000))";
- default:
- UNREACHABLE();
- return std::string("0");
- }
- }();
-
- SetRegister(reg, elem, result, dest_num_components, value_num_components, dest_elem, false);
- }
-
- /**
- * Writes code that does a register assignment to input attribute operation. Input attributes
- * are stored as floats, so this may require conversion.
- * @param reg The destination register to use.
- * @param elem The element to use for the operation.
- * @param attribute The input attribute to use as the source value.
- * @param input_mode The input mode.
- * @param vertex The register that decides which vertex to read from (used in GS).
- */
- void SetRegisterToInputAttibute(const Register& reg, u64 elem, Attribute::Index attribute,
- const Tegra::Shader::IpaMode& input_mode,
- std::optional<Register> vertex = {}) {
- const std::string dest = GetRegisterAsFloat(reg);
- const std::string src = GetInputAttribute(attribute, input_mode, vertex) + GetSwizzle(elem);
- shader.AddLine(dest + " = " + src + ';');
- }
-
- std::string GetLocalMemoryAsFloat(const std::string& index) {
- return "lmem[" + index + ']';
- }
+/// Generates code to use for a swizzle operation.
+static std::string GetSwizzle(u32 elem) {
+ ASSERT(elem <= 3);
+ std::string swizzle = ".";
+ swizzle += "xyzw"[elem];
+ return swizzle;
+}
- std::string GetLocalMemoryAsInteger(const std::string& index, bool is_signed = false) {
- const std::string func{is_signed ? "floatToIntBits" : "floatBitsToUint"};
- return func + "(lmem[" + index + "])";
+/// Translate topology
+static std::string GetTopologyName(Tegra::Shader::OutputTopology topology) {
+ switch (topology) {
+ case Tegra::Shader::OutputTopology::PointList:
+ return "points";
+ case Tegra::Shader::OutputTopology::LineStrip:
+ return "line_strip";
+ case Tegra::Shader::OutputTopology::TriangleStrip:
+ return "triangle_strip";
+ default:
+ UNIMPLEMENTED_MSG("Unknown output topology: {}", static_cast<u32>(topology));
+ return "points";
}
+}
- void SetLocalMemoryAsFloat(const std::string& index, const std::string& value) {
- shader.AddLine("lmem[" + index + "] = " + value + ';');
- }
+/// Returns true if an object has to be treated as precise
+static bool IsPrecise(Operation operand) {
+ const auto& meta = operand.GetMeta();
- void SetLocalMemoryAsInteger(const std::string& index, const std::string& value,
- bool is_signed = false) {
- const std::string func{is_signed ? "intBitsToFloat" : "uintBitsToFloat"};
- shader.AddLine("lmem[" + index + "] = " + func + '(' + value + ");");
+ if (const auto arithmetic = std::get_if<MetaArithmetic>(&meta)) {
+ return arithmetic->precise;
}
-
- std::string GetConditionCode(const Tegra::Shader::ConditionCode cc) const {
- switch (cc) {
- case Tegra::Shader::ConditionCode::NEU:
- return "!(" + GetInternalFlag(InternalFlag::ZeroFlag) + ')';
- default:
- UNIMPLEMENTED_MSG("Unimplemented condition code: {}", static_cast<u32>(cc));
- return "false";
- }
+ if (const auto half_arithmetic = std::get_if<MetaHalfArithmetic>(&meta)) {
+ return half_arithmetic->precise;
}
+ return false;
+}
- std::string GetInternalFlag(const InternalFlag flag) const {
- const auto index = static_cast<u32>(flag);
- ASSERT(index < static_cast<u32>(InternalFlag::Amount));
-
- return std::string(INTERNAL_FLAG_NAMES[index]) + '_' + suffix;
+static bool IsPrecise(Node node) {
+ if (const auto operation = std::get_if<OperationNode>(node)) {
+ return IsPrecise(*operation);
}
+ return false;
+}
- void SetInternalFlag(const InternalFlag flag, const std::string& value) const {
- shader.AddLine(GetInternalFlag(flag) + " = " + value + ';');
- }
+class GLSLDecompiler final {
+public:
+ explicit GLSLDecompiler(const ShaderIR& ir, ShaderStage stage, std::string suffix)
+ : ir{ir}, stage{stage}, suffix{suffix}, header{ir.GetHeader()} {}
- /**
- * Writes code that does a output attribute assignment to register operation. Output attributes
- * are stored as floats, so this may require conversion.
- * @param attribute The destination output attribute.
- * @param elem The element to use for the operation.
- * @param val_reg The register to use as the source value.
- * @param buf_reg The register that tells which buffer to write to (used in geometry shaders).
- */
- void SetOutputAttributeToRegister(Attribute::Index attribute, u64 elem, const Register& val_reg,
- const Register& buf_reg) {
- const std::string dest = GetOutputAttribute(attribute);
- const std::string src = GetRegisterAsFloat(val_reg);
- if (dest.empty())
- return;
+ void Decompile() {
+ DeclareVertex();
+ DeclareGeometry();
+ DeclareRegisters();
+ DeclarePredicates();
+ DeclareLocalMemory();
+ DeclareInternalFlags();
+ DeclareInputAttributes();
+ DeclareOutputAttributes();
+ DeclareConstantBuffers();
+ DeclareGlobalMemory();
+ DeclareSamplers();
- // Can happen with unknown/unimplemented output attributes, in which case we ignore the
- // instruction for now.
- if (stage == Maxwell3D::Regs::ShaderStage::Geometry) {
- // TODO(Rodrigo): nouveau sets some attributes after setting emitting a geometry
- // shader. These instructions use a dirty register as buffer index, to avoid some
- // drivers from complaining about out of boundary writes, guard them.
- const std::string buf_index{"((" + GetRegisterAsInteger(buf_reg) + ") % " +
- std::to_string(MAX_GEOMETRY_BUFFERS) + ')'};
- shader.AddLine("amem[" + buf_index + "][" +
- std::to_string(static_cast<u32>(attribute)) + ']' + GetSwizzle(elem) +
- " = " + src + ';');
- return;
- }
+ code.AddLine("void execute_" + suffix + "() {");
+ ++code.scope;
- switch (attribute) {
- case Attribute::Index::ClipDistances0123:
- case Attribute::Index::ClipDistances4567: {
- const u64 index = (attribute == Attribute::Index::ClipDistances4567 ? 4 : 0) + elem;
- UNIMPLEMENTED_IF_MSG(
- ((header.vtg.clip_distances >> index) & 1) == 0,
- "Shader is setting gl_ClipDistance{} without enabling it in the header", index);
-
- clip_distances[index] = true;
- fixed_pipeline_output_attributes_used.insert(attribute);
- shader.AddLine(dest + '[' + std::to_string(index) + "] = " + src + ';');
- break;
- }
- case Attribute::Index::PointSize:
- fixed_pipeline_output_attributes_used.insert(attribute);
- shader.AddLine(dest + " = " + src + ';');
- break;
- default:
- shader.AddLine(dest + GetSwizzle(elem) + " = " + src + ';');
- break;
- }
- }
+ // VM's program counter
+ const auto first_address = ir.GetBasicBlocks().begin()->first;
+ code.AddLine("uint jmp_to = " + std::to_string(first_address) + "u;");
- /// Generates code representing a uniform (C buffer) register, interpreted as the input type.
- std::string GetUniform(u64 index, u64 offset, GLSLRegister::Type type,
- Register::Size size = Register::Size::Word) {
- declr_const_buffers[index].MarkAsUsed(index, offset, stage);
- std::string value = 'c' + std::to_string(index) + '[' + std::to_string(offset / 4) + "][" +
- std::to_string(offset % 4) + ']';
+ // TODO(Subv): Figure out the actual depth of the flow stack, for now it seems
+ // unlikely that shaders will use 20 nested SSYs and PBKs.
+ constexpr u32 FLOW_STACK_SIZE = 20;
+ code.AddLine(fmt::format("uint flow_stack[{}];", FLOW_STACK_SIZE));
+ code.AddLine("uint flow_stack_top = 0u;");
- if (type == GLSLRegister::Type::Float) {
- // Do nothing, default
- } else if (type == GLSLRegister::Type::Integer) {
- value = "floatBitsToInt(" + value + ')';
- } else if (type == GLSLRegister::Type::UnsignedInteger) {
- value = "floatBitsToUint(" + value + ')';
- } else {
- UNREACHABLE();
- }
+ code.AddLine("while (true) {");
+ ++code.scope;
- return ConvertIntegerSize(value, size);
- }
+ code.AddLine("switch (jmp_to) {");
- std::string GetUniformIndirect(u64 cbuf_index, s64 offset, const std::string& index_str,
- GLSLRegister::Type type) {
- declr_const_buffers[cbuf_index].MarkAsUsedIndirect(cbuf_index, stage);
+ for (const auto& pair : ir.GetBasicBlocks()) {
+ const auto [address, bb] = pair;
+ code.AddLine(fmt::format("case 0x{:x}u: {{", address));
+ ++code.scope;
- const std::string final_offset = fmt::format("({} + {})", index_str, offset / 4);
- const std::string value = 'c' + std::to_string(cbuf_index) + '[' + final_offset + " / 4][" +
- final_offset + " % 4]";
+ VisitBasicBlock(bb);
- if (type == GLSLRegister::Type::Float) {
- return value;
- } else if (type == GLSLRegister::Type::Integer) {
- return "floatBitsToInt(" + value + ')';
- } else {
- UNREACHABLE();
+ --code.scope;
+ code.AddLine('}');
}
- }
-
- /// Add declarations.
- void GenerateDeclarations(const std::string& suffix) {
- GenerateVertex();
- GenerateRegisters(suffix);
- GenerateLocalMemory();
- GenerateInternalFlags();
- GenerateInputAttrs();
- GenerateOutputAttrs();
- GenerateConstBuffers();
- GenerateSamplers();
- GenerateGeometry();
- }
-
- /// Returns a list of constant buffer declarations.
- std::vector<ConstBufferEntry> GetConstBuffersDeclarations() const {
- std::vector<ConstBufferEntry> result;
- std::copy_if(declr_const_buffers.begin(), declr_const_buffers.end(),
- std::back_inserter(result), [](const auto& entry) { return entry.IsUsed(); });
- return result;
- }
-
- /// Returns a list of samplers used in the shader.
- const std::vector<SamplerEntry>& GetSamplers() const {
- return used_samplers;
- }
- /// Returns an array of the used clip distances.
- const std::array<bool, Maxwell::NumClipDistances>& GetClipDistances() const {
- return clip_distances;
- }
-
- /// Returns the GLSL sampler used for the input shader sampler, and creates a new one if
- /// necessary.
- std::string AccessSampler(const Sampler& sampler, Tegra::Shader::TextureType type,
- bool is_array, bool is_shadow) {
- const auto offset = static_cast<std::size_t>(sampler.index.Value());
+ code.AddLine("default: return;");
+ code.AddLine('}');
- // If this sampler has already been used, return the existing mapping.
- const auto itr =
- std::find_if(used_samplers.begin(), used_samplers.end(),
- [&](const SamplerEntry& entry) { return entry.GetOffset() == offset; });
-
- if (itr != used_samplers.end()) {
- ASSERT(itr->GetType() == type && itr->IsArray() == is_array &&
- itr->IsShadow() == is_shadow);
- return itr->GetName();
+ for (std::size_t i = 0; i < 2; ++i) {
+ --code.scope;
+ code.AddLine('}');
}
-
- // Otherwise create a new mapping for this sampler
- const std::size_t next_index = used_samplers.size();
- const SamplerEntry entry{stage, offset, next_index, type, is_array, is_shadow};
- used_samplers.emplace_back(entry);
- return entry.GetName();
}
- void SetLocalMemory(u64 lmem) {
- local_memory_size = lmem;
+ std::string GetResult() {
+ return code.GetResult();
}
-private:
- /// Generates declarations for registers.
- void GenerateRegisters(const std::string& suffix) {
- for (const auto& reg : regs) {
- declarations.AddLine(GLSLRegister::GetTypeString() + ' ' + reg.GetPrefixString() +
- std::to_string(reg.GetIndex()) + '_' + suffix + " = 0;");
+ ShaderEntries GetShaderEntries() const {
+ ShaderEntries entries;
+ for (const auto& cbuf : ir.GetConstantBuffers()) {
+ entries.const_buffers.emplace_back(cbuf.second, stage, GetConstBufferBlock(cbuf.first),
+ cbuf.first);
}
- declarations.AddNewLine();
- }
-
- /// Generates declarations for local memory.
- void GenerateLocalMemory() {
- if (local_memory_size > 0) {
- declarations.AddLine("float lmem[" + std::to_string((local_memory_size - 1 + 4) / 4) +
- "];");
- declarations.AddNewLine();
+ for (const auto& sampler : ir.GetSamplers()) {
+ entries.samplers.emplace_back(sampler, stage, GetSampler(sampler));
}
- }
-
- /// Generates declarations for internal flags.
- void GenerateInternalFlags() {
- for (u32 flag = 0; flag < static_cast<u32>(InternalFlag::Amount); flag++) {
- const InternalFlag code = static_cast<InternalFlag>(flag);
- declarations.AddLine("bool " + GetInternalFlag(code) + " = false;");
+ for (const auto& gmem : ir.GetGlobalMemoryBases()) {
+ entries.global_memory_entries.emplace_back(gmem.cbuf_index, gmem.cbuf_offset, stage,
+ GetGlobalMemoryBlock(gmem));
}
- declarations.AddNewLine();
+ entries.clip_distances = ir.GetClipDistances();
+ entries.shader_length = ir.GetLength();
+ return entries;
}
- /// Generates declarations for input attributes.
- void GenerateInputAttrs() {
- for (const auto element : declr_input_attribute) {
- // TODO(bunnei): Use proper number of elements for these
- u32 idx =
- static_cast<u32>(element.first) - static_cast<u32>(Attribute::Index::Attribute_0);
- if (stage != Maxwell3D::Regs::ShaderStage::Vertex) {
- // If inputs are varyings, add an offset
- idx += GENERIC_VARYING_START_LOCATION;
- }
-
- std::string attr{GetInputAttribute(element.first, element.second)};
- if (stage == Maxwell3D::Regs::ShaderStage::Geometry) {
- attr = "gs_" + attr + "[]";
- }
- declarations.AddLine("layout (location = " + std::to_string(idx) + ") " +
- GetInputFlags(element.first) + "in vec4 " + attr + ';');
- }
-
- declarations.AddNewLine();
- }
+private:
+ using OperationDecompilerFn = std::string (GLSLDecompiler::*)(Operation);
+ using OperationDecompilersArray =
+ std::array<OperationDecompilerFn, static_cast<std::size_t>(OperationCode::Amount)>;
- /// Generates declarations for output attributes.
- void GenerateOutputAttrs() {
- for (const auto& index : declr_output_attribute) {
- // TODO(bunnei): Use proper number of elements for these
- const u32 idx = static_cast<u32>(index) -
- static_cast<u32>(Attribute::Index::Attribute_0) +
- GENERIC_VARYING_START_LOCATION;
- declarations.AddLine("layout (location = " + std::to_string(idx) + ") out vec4 " +
- GetOutputAttribute(index) + ';');
- }
- declarations.AddNewLine();
- }
-
- /// Generates declarations for constant buffers.
- void GenerateConstBuffers() {
- for (const auto& entry : GetConstBuffersDeclarations()) {
- declarations.AddLine("layout (std140) uniform " + entry.GetName());
- declarations.AddLine('{');
- declarations.AddLine(" vec4 c" + std::to_string(entry.GetIndex()) +
- "[MAX_CONSTBUFFER_ELEMENTS];");
- declarations.AddLine("};");
- declarations.AddNewLine();
- }
- declarations.AddNewLine();
- }
+ void DeclareVertex() {
+ if (stage != ShaderStage::Vertex)
+ return;
- /// Generates declarations for samplers.
- void GenerateSamplers() {
- const auto& samplers = GetSamplers();
- for (const auto& sampler : samplers) {
- declarations.AddLine("uniform " + sampler.GetTypeString() + ' ' + sampler.GetName() +
- ';');
- }
- declarations.AddNewLine();
+ DeclareVertexRedeclarations();
}
- /// Generates declarations used for geometry shaders.
- void GenerateGeometry() {
- if (stage != Maxwell3D::Regs::ShaderStage::Geometry)
+ void DeclareGeometry() {
+ if (stage != ShaderStage::Geometry)
return;
- declarations.AddLine(
- "layout (" + GetTopologyName(header.common3.output_topology) +
- ", max_vertices = " + std::to_string(header.common4.max_output_vertices) + ") out;");
- declarations.AddNewLine();
-
- declarations.AddLine("vec4 amem[" + std::to_string(MAX_GEOMETRY_BUFFERS) + "][" +
- std::to_string(MAX_ATTRIBUTES) + "];");
- declarations.AddNewLine();
-
- constexpr char buffer[] = "amem[output_buffer]";
- declarations.AddLine("void emit_vertex(uint output_buffer) {");
- ++declarations.scope;
- for (const auto element : declr_output_attribute) {
- declarations.AddLine(GetOutputAttribute(element) + " = " + buffer + '[' +
- std::to_string(static_cast<u32>(element)) + "];");
- }
-
- declarations.AddLine("position = " + std::string(buffer) + '[' +
- std::to_string(static_cast<u32>(Attribute::Index::Position)) + "];");
+ const auto topology = GetTopologyName(header.common3.output_topology);
+ const auto max_vertices = std::to_string(header.common4.max_output_vertices);
+ code.AddLine("layout (" + topology + ", max_vertices = " + max_vertices + ") out;");
+ code.AddNewLine();
- // If a geometry shader is attached, it will always flip (it's the last stage before
- // fragment). For more info about flipping, refer to gl_shader_gen.cpp.
- declarations.AddLine("position.xy *= viewport_flip.xy;");
- declarations.AddLine("gl_Position = position;");
- declarations.AddLine("position.w = 1.0;");
- declarations.AddLine("EmitVertex();");
- --declarations.scope;
- declarations.AddLine('}');
- declarations.AddNewLine();
+ DeclareVertexRedeclarations();
}
- void GenerateVertex() {
- if (stage != Maxwell3D::Regs::ShaderStage::Vertex)
- return;
+ void DeclareVertexRedeclarations() {
bool clip_distances_declared = false;
- declarations.AddLine("out gl_PerVertex {");
- ++declarations.scope;
- declarations.AddLine("vec4 gl_Position;");
- for (auto& o : fixed_pipeline_output_attributes_used) {
+ code.AddLine("out gl_PerVertex {");
+ ++code.scope;
+
+ code.AddLine("vec4 gl_Position;");
+
+ for (const auto o : ir.GetOutputAttributes()) {
if (o == Attribute::Index::PointSize)
- declarations.AddLine("float gl_PointSize;");
+ code.AddLine("float gl_PointSize;");
if (!clip_distances_declared && (o == Attribute::Index::ClipDistances0123 ||
o == Attribute::Index::ClipDistances4567)) {
- declarations.AddLine("float gl_ClipDistance[];");
+ code.AddLine("float gl_ClipDistance[];");
clip_distances_declared = true;
}
}
- --declarations.scope;
- declarations.AddLine("};");
- }
- /// Generates code representing a temporary (GPR) register.
- std::string GetRegister(const Register& reg, unsigned elem) {
- if (reg == Register::ZeroIndex) {
- return "0";
- }
-
- return regs[reg.GetSwizzledIndex(elem)].GetString();
- }
-
- /**
- * Writes code that does a register assignment to value operation.
- * @param reg The destination register to use.
- * @param elem The element to use for the operation.
- * @param value The code representing the value to assign.
- * @param dest_num_components Number of components in the destination.
- * @param value_num_components Number of components in the value.
- * @param dest_elem Optional, the destination element to use for the operation.
- */
- void SetRegister(const Register& reg, u64 elem, const std::string& value,
- u64 dest_num_components, u64 value_num_components, u64 dest_elem,
- bool precise) {
- if (reg == Register::ZeroIndex) {
- // Setting RZ is a nop in hardware.
- return;
- }
-
- std::string dest = GetRegister(reg, static_cast<u32>(dest_elem));
- if (dest_num_components > 1) {
- dest += GetSwizzle(elem);
- }
-
- std::string src = '(' + value + ')';
- if (value_num_components > 1) {
- src += GetSwizzle(elem);
- }
-
- if (precise && stage != Maxwell3D::Regs::ShaderStage::Fragment) {
- const auto scope = shader.Scope();
+ --code.scope;
+ code.AddLine("};");
+ code.AddNewLine();
+ }
- // This avoids optimizations of constant propagation and keeps the code as the original
- // Sadly using the precise keyword causes "linking" errors on fragment shaders.
- shader.AddLine("precise float tmp = " + src + ';');
- shader.AddLine(dest + " = tmp;");
- } else {
- shader.AddLine(dest + " = " + src + ';');
+ void DeclareRegisters() {
+ const auto& registers = ir.GetRegisters();
+ for (const u32 gpr : registers) {
+ code.AddLine("float " + GetRegister(gpr) + " = 0;");
}
+ if (!registers.empty())
+ code.AddNewLine();
}
- /// Build the GLSL register list.
- void BuildRegisterList() {
- regs.reserve(Register::NumRegisters);
-
- for (std::size_t index = 0; index < Register::NumRegisters; ++index) {
- regs.emplace_back(index, suffix);
+ void DeclarePredicates() {
+ const auto& predicates = ir.GetPredicates();
+ for (const auto pred : predicates) {
+ code.AddLine("bool " + GetPredicate(pred) + " = false;");
}
+ if (!predicates.empty())
+ code.AddNewLine();
}
- void BuildInputList() {
- const u32 size = static_cast<u32>(Attribute::Index::Attribute_31) -
- static_cast<u32>(Attribute::Index::Attribute_0) + 1;
- declr_input_attribute.reserve(size);
+ void DeclareLocalMemory() {
+ if (const u64 local_memory_size = header.GetLocalMemorySize(); local_memory_size > 0) {
+ const auto element_count = Common::AlignUp(local_memory_size, 4) / 4;
+ code.AddLine("float " + GetLocalMemory() + '[' + std::to_string(element_count) + "];");
+ code.AddNewLine();
+ }
}
- /// Generates code representing an input attribute register.
- std::string GetInputAttribute(Attribute::Index attribute,
- const Tegra::Shader::IpaMode& input_mode,
- std::optional<Register> vertex = {}) {
- auto GeometryPass = [&](const std::string& name) {
- if (stage == Maxwell3D::Regs::ShaderStage::Geometry && vertex) {
- // TODO(Rodrigo): Guard geometry inputs against out of bound reads. Some games set
- // an 0x80000000 index for those and the shader fails to build. Find out why this
- // happens and what's its intent.
- return "gs_" + name + '[' + GetRegisterAsInteger(*vertex, 0, false) +
- " % MAX_VERTEX_INPUT]";
- }
- return name;
- };
-
- switch (attribute) {
- case Attribute::Index::Position:
- if (stage != Maxwell3D::Regs::ShaderStage::Fragment) {
- return GeometryPass("position");
- } else {
- return "vec4(gl_FragCoord.x, gl_FragCoord.y, gl_FragCoord.z, 1.0)";
- }
- case Attribute::Index::PointCoord:
- return "vec4(gl_PointCoord.x, gl_PointCoord.y, 0, 0)";
- case Attribute::Index::TessCoordInstanceIDVertexID:
- // TODO(Subv): Find out what the values are for the first two elements when inside a
- // vertex shader, and what's the value of the fourth element when inside a Tess Eval
- // shader.
- ASSERT(stage == Maxwell3D::Regs::ShaderStage::Vertex);
- // Config pack's first value is instance_id.
- return "vec4(0, 0, uintBitsToFloat(config_pack[0]), uintBitsToFloat(gl_VertexID))";
- case Attribute::Index::FrontFacing:
- // TODO(Subv): Find out what the values are for the other elements.
- ASSERT(stage == Maxwell3D::Regs::ShaderStage::Fragment);
- return "vec4(0, 0, 0, uintBitsToFloat(gl_FrontFacing ? 1 : 0))";
- default:
- const u32 index{static_cast<u32>(attribute) -
- static_cast<u32>(Attribute::Index::Attribute_0)};
- if (attribute >= Attribute::Index::Attribute_0 &&
- attribute <= Attribute::Index::Attribute_31) {
- if (declr_input_attribute.count(attribute) == 0) {
- declr_input_attribute[attribute] = input_mode;
- } else {
- UNIMPLEMENTED_IF_MSG(declr_input_attribute[attribute] != input_mode,
- "Multiple input modes for the same attribute");
- }
- return GeometryPass("input_attribute_" + std::to_string(index));
- }
-
- UNIMPLEMENTED_MSG("Unhandled input attribute: {}", static_cast<u32>(attribute));
+ void DeclareInternalFlags() {
+ for (u32 flag = 0; flag < static_cast<u32>(InternalFlag::Amount); flag++) {
+ const InternalFlag flag_code = static_cast<InternalFlag>(flag);
+ code.AddLine("bool " + GetInternalFlag(flag_code) + " = false;");
}
-
- return "vec4(0, 0, 0, 0)";
+ code.AddNewLine();
}
- std::string GetInputFlags(const Attribute::Index attribute) {
- const Tegra::Shader::IpaSampleMode sample_mode =
- declr_input_attribute[attribute].sampling_mode;
- const Tegra::Shader::IpaInterpMode interp_mode =
- declr_input_attribute[attribute].interpolation_mode;
+ std::string GetInputFlags(const IpaMode& input_mode) {
+ const IpaSampleMode sample_mode = input_mode.sampling_mode;
+ const IpaInterpMode interp_mode = input_mode.interpolation_mode;
std::string out;
+
switch (interp_mode) {
- case Tegra::Shader::IpaInterpMode::Flat: {
+ case IpaInterpMode::Flat:
out += "flat ";
break;
- }
- case Tegra::Shader::IpaInterpMode::Linear: {
+ case IpaInterpMode::Linear:
out += "noperspective ";
break;
- }
- case Tegra::Shader::IpaInterpMode::Perspective: {
+ case IpaInterpMode::Perspective:
// Default, Smooth
break;
- }
- default: {
+ default:
UNIMPLEMENTED_MSG("Unhandled IPA interp mode: {}", static_cast<u32>(interp_mode));
}
- }
switch (sample_mode) {
- case Tegra::Shader::IpaSampleMode::Centroid:
- // It can be implemented with the "centroid " keyword in glsl
+ case IpaSampleMode::Centroid:
+ // It can be implemented with the "centroid " keyword in GLSL
UNIMPLEMENTED_MSG("Unimplemented IPA sampler mode centroid");
break;
- case Tegra::Shader::IpaSampleMode::Default:
+ case IpaSampleMode::Default:
// Default, n/a
break;
- default: {
+ default:
UNIMPLEMENTED_MSG("Unimplemented IPA sampler mode: {}", static_cast<u32>(sample_mode));
- break;
- }
}
return out;
}
- /// Generates code representing the declaration name of an output attribute register.
- std::string GetOutputAttribute(Attribute::Index attribute) {
- switch (attribute) {
- case Attribute::Index::PointSize:
- return "gl_PointSize";
- case Attribute::Index::Position:
- return "position";
- case Attribute::Index::ClipDistances0123:
- case Attribute::Index::ClipDistances4567: {
- return "gl_ClipDistance";
- }
- default:
- const u32 index{static_cast<u32>(attribute) -
- static_cast<u32>(Attribute::Index::Attribute_0)};
- if (attribute >= Attribute::Index::Attribute_0) {
- declr_output_attribute.insert(attribute);
- return "output_attribute_" + std::to_string(index);
+ void DeclareInputAttributes() {
+ const auto& attributes = ir.GetInputAttributes();
+ for (const auto element : attributes) {
+ const Attribute::Index index = element.first;
+ const IpaMode& input_mode = *element.second.begin();
+ if (index < Attribute::Index::Attribute_0 || index > Attribute::Index::Attribute_31) {
+ // Skip when it's not a generic attribute
+ continue;
}
- UNIMPLEMENTED_MSG("Unhandled output attribute={}", index);
- return {};
- }
- }
-
- ShaderWriter& shader;
- ShaderWriter& declarations;
- std::vector<GLSLRegister> regs;
- std::unordered_map<Attribute::Index, Tegra::Shader::IpaMode> declr_input_attribute;
- std::set<Attribute::Index> declr_output_attribute;
- std::array<ConstBufferEntry, Maxwell3D::Regs::MaxConstBuffers> declr_const_buffers;
- std::vector<SamplerEntry> used_samplers;
- const Maxwell3D::Regs::ShaderStage& stage;
- const std::string& suffix;
- const Tegra::Shader::Header& header;
- std::unordered_set<Attribute::Index> fixed_pipeline_output_attributes_used;
- std::array<bool, Maxwell::NumClipDistances> clip_distances{};
- u64 local_memory_size;
-};
-
-class GLSLGenerator {
-public:
- GLSLGenerator(const std::set<Subroutine>& subroutines, const ProgramCode& program_code,
- u32 main_offset, Maxwell3D::Regs::ShaderStage stage, const std::string& suffix,
- std::size_t shader_length)
- : subroutines(subroutines), program_code(program_code), main_offset(main_offset),
- stage(stage), suffix(suffix), shader_length(shader_length) {
- std::memcpy(&header, program_code.data(), sizeof(Tegra::Shader::Header));
- local_memory_size = header.GetLocalMemorySize();
- regs.SetLocalMemory(local_memory_size);
- Generate(suffix);
- }
+ ASSERT(element.second.size() > 0);
+ UNIMPLEMENTED_IF_MSG(element.second.size() > 1,
+ "Multiple input flag modes are not supported in GLSL");
- std::string GetShaderCode() {
- return declarations.GetResult() + shader.GetResult();
- }
-
- /// Returns entries in the shader that are useful for external functions
- ShaderEntries GetEntries() const {
- return {regs.GetConstBuffersDeclarations(), regs.GetSamplers(), regs.GetClipDistances(),
- shader_length};
- }
-
-private:
- /// Gets the Subroutine object corresponding to the specified address.
- const Subroutine& GetSubroutine(u32 begin, u32 end) const {
- const auto iter = subroutines.find(Subroutine{begin, end, suffix});
- ASSERT(iter != subroutines.end());
- return *iter;
- }
-
- /// Generates code representing a 19-bit immediate value
- static std::string GetImmediate19(const Instruction& instr) {
- return fmt::format("uintBitsToFloat({})", instr.alu.GetImm20_19());
- }
-
- /// Generates code representing a 32-bit immediate value
- static std::string GetImmediate32(const Instruction& instr) {
- return fmt::format("uintBitsToFloat({})", instr.alu.GetImm20_32());
- }
-
- /// Generates code representing a vec2 pair unpacked from a half float immediate
- static std::string UnpackHalfImmediate(const Instruction& instr, bool negate) {
- const std::string immediate = GetHalfFloat(std::to_string(instr.half_imm.PackImmediates()));
- if (!negate) {
- return immediate;
- }
- const std::string negate_first = instr.half_imm.first_negate != 0 ? "-" : "";
- const std::string negate_second = instr.half_imm.second_negate != 0 ? "-" : "";
- const std::string negate_vec = "vec2(" + negate_first + "1, " + negate_second + "1)";
-
- return '(' + immediate + " * " + negate_vec + ')';
- }
-
- /// Generates code representing a texture sampler.
- std::string GetSampler(const Sampler& sampler, Tegra::Shader::TextureType type, bool is_array,
- bool is_shadow) {
- return regs.AccessSampler(sampler, type, is_array, is_shadow);
- }
-
- /**
- * Adds code that calls a subroutine.
- * @param subroutine the subroutine to call.
- */
- void CallSubroutine(const Subroutine& subroutine) {
- if (subroutine.exit_method == ExitMethod::AlwaysEnd) {
- shader.AddLine(subroutine.GetName() + "();");
- shader.AddLine("return true;");
- } else if (subroutine.exit_method == ExitMethod::Conditional) {
- shader.AddLine("if (" + subroutine.GetName() + "()) { return true; }");
- } else {
- shader.AddLine(subroutine.GetName() + "();");
- }
- }
-
- /*
- * Writes code that assigns a predicate boolean variable.
- * @param pred The id of the predicate to write to.
- * @param value The expression value to assign to the predicate.
- */
- void SetPredicate(u64 pred, const std::string& value) {
- using Tegra::Shader::Pred;
- // Can't assign to the constant predicate.
- ASSERT(pred != static_cast<u64>(Pred::UnusedIndex));
-
- std::string variable = 'p' + std::to_string(pred) + '_' + suffix;
- shader.AddLine(variable + " = " + value + ';');
- declr_predicates.insert(std::move(variable));
- }
-
- /*
- * Returns the condition to use in the 'if' for a predicated instruction.
- * @param instr Instruction to generate the if condition for.
- * @returns string containing the predicate condition.
- */
- std::string GetPredicateCondition(u64 index, bool negate) {
- using Tegra::Shader::Pred;
- std::string variable;
-
- // Index 7 is used as an 'Always True' condition.
- if (index == static_cast<u64>(Pred::UnusedIndex)) {
- variable = "true";
- } else {
- variable = 'p' + std::to_string(index) + '_' + suffix;
- declr_predicates.insert(variable);
- }
- if (negate) {
- return "!(" + variable + ')';
- }
-
- return variable;
- }
-
- /**
- * Returns the comparison string to use to compare two values in the 'set' family of
- * instructions.
- * @param condition The condition used in the 'set'-family instruction.
- * @param op_a First operand to use for the comparison.
- * @param op_b Second operand to use for the comparison.
- * @returns String corresponding to the GLSL operator that matches the desired comparison.
- */
- std::string GetPredicateComparison(Tegra::Shader::PredCondition condition,
- const std::string& op_a, const std::string& op_b) const {
- using Tegra::Shader::PredCondition;
- static const std::unordered_map<PredCondition, const char*> PredicateComparisonStrings = {
- {PredCondition::LessThan, "<"},
- {PredCondition::Equal, "=="},
- {PredCondition::LessEqual, "<="},
- {PredCondition::GreaterThan, ">"},
- {PredCondition::NotEqual, "!="},
- {PredCondition::GreaterEqual, ">="},
- {PredCondition::LessThanWithNan, "<"},
- {PredCondition::NotEqualWithNan, "!="},
- {PredCondition::LessEqualWithNan, "<="},
- {PredCondition::GreaterThanWithNan, ">"},
- {PredCondition::GreaterEqualWithNan, ">="}};
-
- const auto& comparison{PredicateComparisonStrings.find(condition)};
- UNIMPLEMENTED_IF_MSG(comparison == PredicateComparisonStrings.end(),
- "Unknown predicate comparison operation");
-
- std::string predicate{'(' + op_a + ") " + comparison->second + " (" + op_b + ')'};
- if (condition == PredCondition::LessThanWithNan ||
- condition == PredCondition::NotEqualWithNan ||
- condition == PredCondition::LessEqualWithNan ||
- condition == PredCondition::GreaterThanWithNan ||
- condition == PredCondition::GreaterEqualWithNan) {
- predicate += " || isnan(" + op_a + ") || isnan(" + op_b + ')';
- }
-
- return predicate;
- }
-
- /**
- * Returns the operator string to use to combine two predicates in the 'setp' family of
- * instructions.
- * @params operation The operator used in the 'setp'-family instruction.
- * @returns String corresponding to the GLSL operator that matches the desired operator.
- */
- std::string GetPredicateCombiner(Tegra::Shader::PredOperation operation) const {
- using Tegra::Shader::PredOperation;
- static const std::unordered_map<PredOperation, const char*> PredicateOperationStrings = {
- {PredOperation::And, "&&"},
- {PredOperation::Or, "||"},
- {PredOperation::Xor, "^^"},
- };
-
- auto op = PredicateOperationStrings.find(operation);
- UNIMPLEMENTED_IF_MSG(op == PredicateOperationStrings.end(), "Unknown predicate operation");
- return op->second;
- }
-
- /**
- * Transforms the input string GLSL operand into one that applies the abs() function and negates
- * the output if necessary. When both abs and neg are true, the negation will be applied after
- * taking the absolute value.
- * @param operand The input operand to take the abs() of, negate, or both.
- * @param abs Whether to apply the abs() function to the input operand.
- * @param neg Whether to negate the input operand.
- * @returns String corresponding to the operand after being transformed by the abs() and
- * negation operations.
- */
- static std::string GetOperandAbsNeg(const std::string& operand, bool abs, bool neg) {
- std::string result = operand;
-
- if (abs) {
- result = "abs(" + result + ')';
- }
-
- if (neg) {
- result = "-(" + result + ')';
- }
-
- return result;
- }
-
- /*
- * Transforms the input string GLSL operand into an unpacked half float pair.
- * @note This function returns a float type pair instead of a half float pair. This is because
- * real half floats are not standardized in GLSL but unpackHalf2x16 (which returns a vec2) is.
- * @param operand Input operand. It has to be an unsigned integer.
- * @param type How to unpack the unsigned integer to a half float pair.
- * @param abs Get the absolute value of unpacked half floats.
- * @param neg Get the negative value of unpacked half floats.
- * @returns String corresponding to a half float pair.
- */
- static std::string GetHalfFloat(const std::string& operand,
- Tegra::Shader::HalfType type = Tegra::Shader::HalfType::H0_H1,
- bool abs = false, bool neg = false) {
- // "vec2" calls emitted in this function are intended to alias components.
- const std::string value = [&]() {
- switch (type) {
- case Tegra::Shader::HalfType::H0_H1:
- return "unpackHalf2x16(" + operand + ')';
- case Tegra::Shader::HalfType::F32:
- return "vec2(uintBitsToFloat(" + operand + "))";
- case Tegra::Shader::HalfType::H0_H0:
- case Tegra::Shader::HalfType::H1_H1: {
- const bool high = type == Tegra::Shader::HalfType::H1_H1;
- const char unpack_index = "xy"[high ? 1 : 0];
- return "vec2(unpackHalf2x16(" + operand + ")." + unpack_index + ')';
- }
- default:
- UNREACHABLE();
- return std::string("vec2(0)");
+ // TODO(bunnei): Use proper number of elements for these
+ u32 idx = static_cast<u32>(index) - static_cast<u32>(Attribute::Index::Attribute_0);
+ if (stage != ShaderStage::Vertex) {
+ // If inputs are varyings, add an offset
+ idx += GENERIC_VARYING_START_LOCATION;
}
- }();
-
- return GetOperandAbsNeg(value, abs, neg);
- }
-
- /*
- * Returns whether the instruction at the specified offset is a 'sched' instruction.
- * Sched instructions always appear before a sequence of 3 instructions.
- */
- bool IsSchedInstruction(u32 offset) const {
- // sched instructions appear once every 4 instructions.
- static constexpr std::size_t SchedPeriod = 4;
- u32 absolute_offset = offset - main_offset;
-
- return (absolute_offset % SchedPeriod) == 0;
- }
-
- void WriteLogicOperation(Register dest, LogicOperation logic_op, const std::string& op_a,
- const std::string& op_b,
- Tegra::Shader::PredicateResultMode predicate_mode,
- Tegra::Shader::Pred predicate) {
- std::string result{};
- switch (logic_op) {
- case LogicOperation::And: {
- result = '(' + op_a + " & " + op_b + ')';
- break;
- }
- case LogicOperation::Or: {
- result = '(' + op_a + " | " + op_b + ')';
- break;
- }
- case LogicOperation::Xor: {
- result = '(' + op_a + " ^ " + op_b + ')';
- break;
- }
- case LogicOperation::PassB: {
- result = op_b;
- break;
- }
- default:
- UNIMPLEMENTED_MSG("Unimplemented logic operation={}", static_cast<u32>(logic_op));
- }
-
- if (dest != Tegra::Shader::Register::ZeroIndex) {
- regs.SetRegisterToInteger(dest, true, 0, result, 1, 1);
- }
-
- using Tegra::Shader::PredicateResultMode;
- // Write the predicate value depending on the predicate mode.
- switch (predicate_mode) {
- case PredicateResultMode::None:
- // Do nothing.
- return;
- case PredicateResultMode::NotZero:
- // Set the predicate to true if the result is not zero.
- SetPredicate(static_cast<u64>(predicate), '(' + result + ") != 0");
- break;
- default:
- UNIMPLEMENTED_MSG("Unimplemented predicate result mode: {}",
- static_cast<u32>(predicate_mode));
- }
- }
-
- void WriteLop3Instruction(Register dest, const std::string& op_a, const std::string& op_b,
- const std::string& op_c, const std::string& imm_lut) {
- if (dest == Tegra::Shader::Register::ZeroIndex) {
- return;
- }
- static constexpr std::array<const char*, 32> shift_amounts = {
- "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
- "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21",
- "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};
-
- std::string result;
- result += '(';
-
- for (std::size_t i = 0; i < shift_amounts.size(); ++i) {
- if (i)
- result += '|';
- result += "(((" + imm_lut + " >> (((" + op_c + " >> " + shift_amounts[i] +
- ") & 1) | ((" + op_b + " >> " + shift_amounts[i] + ") & 1) << 1 | ((" + op_a +
- " >> " + shift_amounts[i] + ") & 1) << 2)) & 1) << " + shift_amounts[i] + ")";
+ std::string attr = GetInputAttribute(index);
+ if (stage == ShaderStage::Geometry) {
+ attr = "gs_" + attr + "[]";
+ }
+ code.AddLine("layout (location = " + std::to_string(idx) + ") " +
+ GetInputFlags(input_mode) + "in vec4 " + attr + ';');
}
-
- result += ')';
-
- regs.SetRegisterToInteger(dest, true, 0, result, 1, 1);
+ if (!attributes.empty())
+ code.AddNewLine();
}
- void WriteTexsInstructionFloat(const Instruction& instr, const std::string& texture) {
- // TEXS has two destination registers and a swizzle. The first two elements in the swizzle
- // go into gpr0+0 and gpr0+1, and the rest goes into gpr28+0 and gpr28+1
-
- std::size_t written_components = 0;
- for (u32 component = 0; component < 4; ++component) {
- if (!instr.texs.IsComponentEnabled(component)) {
+ void DeclareOutputAttributes() {
+ const auto& attributes = ir.GetOutputAttributes();
+ for (const auto index : attributes) {
+ if (index < Attribute::Index::Attribute_0 || index > Attribute::Index::Attribute_31) {
+ // Skip when it's not a generic attribute
continue;
}
-
- if (written_components < 2) {
- // Write the first two swizzle components to gpr0 and gpr0+1
- regs.SetRegisterToFloat(instr.gpr0, component, texture, 1, 4, false,
- written_components % 2);
- } else {
- ASSERT(instr.texs.HasTwoDestinations());
- // Write the rest of the swizzle components to gpr28 and gpr28+1
- regs.SetRegisterToFloat(instr.gpr28, component, texture, 1, 4, false,
- written_components % 2);
- }
-
- ++written_components;
+ // TODO(bunnei): Use proper number of elements for these
+ const auto idx = static_cast<u32>(index) -
+ static_cast<u32>(Attribute::Index::Attribute_0) +
+ GENERIC_VARYING_START_LOCATION;
+ code.AddLine("layout (location = " + std::to_string(idx) + ") out vec4 " +
+ GetOutputAttribute(index) + ';');
}
+ if (!attributes.empty())
+ code.AddNewLine();
}
- void WriteTexsInstructionHalfFloat(const Instruction& instr, const std::string& texture) {
- // TEXS.F16 destionation registers are packed in two registers in pairs (just like any half
- // float instruction).
-
- std::array<std::string, 4> components;
- u32 written_components = 0;
-
- for (u32 component = 0; component < 4; ++component) {
- if (!instr.texs.IsComponentEnabled(component))
- continue;
- components[written_components++] = texture + GetSwizzle(component);
- }
- if (written_components == 0)
- return;
-
- const auto BuildComponent = [&](std::string low, std::string high, bool high_enabled) {
- return "vec2(" + low + ", " + (high_enabled ? high : "0") + ')';
- };
-
- regs.SetRegisterToHalfFloat(
- instr.gpr0, 0, BuildComponent(components[0], components[1], written_components > 1),
- Tegra::Shader::HalfMerge::H0_H1, 1, 1);
-
- if (written_components > 2) {
- ASSERT(instr.texs.HasTwoDestinations());
- regs.SetRegisterToHalfFloat(
- instr.gpr28, 0,
- BuildComponent(components[2], components[3], written_components > 3),
- Tegra::Shader::HalfMerge::H0_H1, 1, 1);
+ void DeclareConstantBuffers() {
+ for (const auto& entry : ir.GetConstantBuffers()) {
+ const auto [index, size] = entry;
+ code.AddLine("layout (std140, binding = CBUF_BINDING_" + std::to_string(index) +
+ ") uniform " + GetConstBufferBlock(index) + " {");
+ code.AddLine(" vec4 " + GetConstBuffer(index) + "[MAX_CONSTBUFFER_ELEMENTS];");
+ code.AddLine("};");
+ code.AddNewLine();
}
}
- static u32 TextureCoordinates(Tegra::Shader::TextureType texture_type) {
- switch (texture_type) {
- case Tegra::Shader::TextureType::Texture1D:
- return 1;
- case Tegra::Shader::TextureType::Texture2D:
- return 2;
- case Tegra::Shader::TextureType::Texture3D:
- case Tegra::Shader::TextureType::TextureCube:
- return 3;
- default:
- UNIMPLEMENTED_MSG("Unhandled texture type: {}", static_cast<u32>(texture_type));
- return 0;
+ void DeclareGlobalMemory() {
+ for (const auto& entry : ir.GetGlobalMemoryBases()) {
+ const std::string binding =
+ fmt::format("GMEM_BINDING_{}_{}", entry.cbuf_index, entry.cbuf_offset);
+ code.AddLine("layout (std430, binding = " + binding + ") buffer " +
+ GetGlobalMemoryBlock(entry) + " {");
+ code.AddLine(" float " + GetGlobalMemory(entry) + "[MAX_GLOBALMEMORY_ELEMENTS];");
+ code.AddLine("};");
+ code.AddNewLine();
}
}
- /*
- * Emits code to push the input target address to the flow address stack, incrementing the stack
- * top.
- */
- void EmitPushToFlowStack(u32 target) {
- const auto scope = shader.Scope();
-
- shader.AddLine("flow_stack[flow_stack_top] = " + std::to_string(target) + "u;");
- shader.AddLine("flow_stack_top++;");
- }
-
- /*
- * Emits code to pop an address from the flow address stack, setting the jump address to the
- * popped address and decrementing the stack top.
- */
- void EmitPopFromFlowStack() {
- const auto scope = shader.Scope();
-
- shader.AddLine("flow_stack_top--;");
- shader.AddLine("jmp_to = flow_stack[flow_stack_top];");
- shader.AddLine("break;");
- }
-
- /// Writes the output values from a fragment shader to the corresponding GLSL output variables.
- void EmitFragmentOutputsWrite() {
- ASSERT(stage == Maxwell3D::Regs::ShaderStage::Fragment);
-
- UNIMPLEMENTED_IF_MSG(header.ps.omap.sample_mask != 0, "Samplemask write is unimplemented");
-
- shader.AddLine("if (alpha_test[0] != 0) {");
- ++shader.scope;
- // We start on the register containing the alpha value in the first RT.
- u32 current_reg = 3;
- for (u32 render_target = 0; render_target < Maxwell3D::Regs::NumRenderTargets;
- ++render_target) {
- // TODO(Blinkhawk): verify the behavior of alpha testing on hardware when
- // multiple render targets are used.
- if (header.ps.IsColorComponentOutputEnabled(render_target, 0) ||
- header.ps.IsColorComponentOutputEnabled(render_target, 1) ||
- header.ps.IsColorComponentOutputEnabled(render_target, 2) ||
- header.ps.IsColorComponentOutputEnabled(render_target, 3)) {
- shader.AddLine(fmt::format("if (!AlphaFunc({})) discard;",
- regs.GetRegisterAsFloat(current_reg)));
- current_reg += 4;
- }
- }
- --shader.scope;
- shader.AddLine('}');
-
- // Write the color outputs using the data in the shader registers, disabled
- // rendertargets/components are skipped in the register assignment.
- current_reg = 0;
- for (u32 render_target = 0; render_target < Maxwell3D::Regs::NumRenderTargets;
- ++render_target) {
- // TODO(Subv): Figure out how dual-source blending is configured in the Switch.
- for (u32 component = 0; component < 4; ++component) {
- if (header.ps.IsColorComponentOutputEnabled(render_target, component)) {
- shader.AddLine(fmt::format("FragColor{}[{}] = {};", render_target, component,
- regs.GetRegisterAsFloat(current_reg)));
- ++current_reg;
+ void DeclareSamplers() {
+ const auto& samplers = ir.GetSamplers();
+ for (const auto& sampler : samplers) {
+ std::string sampler_type = [&]() {
+ switch (sampler.GetType()) {
+ case Tegra::Shader::TextureType::Texture1D:
+ return "sampler1D";
+ case Tegra::Shader::TextureType::Texture2D:
+ return "sampler2D";
+ case Tegra::Shader::TextureType::Texture3D:
+ return "sampler3D";
+ case Tegra::Shader::TextureType::TextureCube:
+ return "samplerCube";
+ default:
+ UNREACHABLE();
+ return "sampler2D";
}
- }
- }
-
- if (header.ps.omap.depth) {
- // The depth output is always 2 registers after the last color output, and current_reg
- // already contains one past the last color register.
+ }();
+ if (sampler.IsArray())
+ sampler_type += "Array";
+ if (sampler.IsShadow())
+ sampler_type += "Shadow";
- shader.AddLine(
- "gl_FragDepth = " +
- regs.GetRegisterAsFloat(static_cast<Tegra::Shader::Register>(current_reg) + 1) +
- ';');
+ code.AddLine("layout (binding = SAMPLER_BINDING_" + std::to_string(sampler.GetIndex()) +
+ ") uniform " + sampler_type + ' ' + GetSampler(sampler) + ';');
}
+ if (!samplers.empty())
+ code.AddNewLine();
}
- /// Unpacks a video instruction operand (e.g. VMAD).
- std::string GetVideoOperand(const std::string& op, bool is_chunk, bool is_signed,
- Tegra::Shader::VideoType type, u64 byte_height) {
- const std::string value = [&]() {
- if (!is_chunk) {
- const auto offset = static_cast<u32>(byte_height * 8);
- return "((" + op + " >> " + std::to_string(offset) + ") & 0xff)";
+ void VisitBasicBlock(const BasicBlock& bb) {
+ for (const Node node : bb) {
+ if (const std::string expr = Visit(node); !expr.empty()) {
+ code.AddLine(expr);
}
- const std::string zero = "0";
-
- switch (type) {
- case Tegra::Shader::VideoType::Size16_Low:
- return '(' + op + " & 0xffff)";
- case Tegra::Shader::VideoType::Size16_High:
- return '(' + op + " >> 16)";
- case Tegra::Shader::VideoType::Size32:
- // TODO(Rodrigo): From my hardware tests it becomes a bit "mad" when
- // this type is used (1 * 1 + 0 == 0x5b800000). Until a better
- // explanation is found: abort.
- UNIMPLEMENTED();
- return zero;
- case Tegra::Shader::VideoType::Invalid:
- UNREACHABLE_MSG("Invalid instruction encoding");
- return zero;
- default:
- UNREACHABLE();
- return zero;
- }
- }();
-
- if (is_signed) {
- return "int(" + value + ')';
}
- return value;
- };
-
- /// Gets the A operand for a video instruction.
- std::string GetVideoOperandA(Instruction instr) {
- return GetVideoOperand(regs.GetRegisterAsInteger(instr.gpr8, 0, false),
- instr.video.is_byte_chunk_a != 0, instr.video.signed_a,
- instr.video.type_a, instr.video.byte_height_a);
}
- /// Gets the B operand for a video instruction.
- std::string GetVideoOperandB(Instruction instr) {
- if (instr.video.use_register_b) {
- return GetVideoOperand(regs.GetRegisterAsInteger(instr.gpr20, 0, false),
- instr.video.is_byte_chunk_b != 0, instr.video.signed_b,
- instr.video.type_b, instr.video.byte_height_b);
- } else {
- return '(' +
- std::to_string(instr.video.signed_b ? static_cast<s16>(instr.alu.GetImm20_16())
- : instr.alu.GetImm20_16()) +
- ')';
- }
- }
-
- std::pair<size_t, std::string> ValidateAndGetCoordinateElement(
- const Tegra::Shader::TextureType texture_type, const bool depth_compare,
- const bool is_array, const bool lod_bias_enabled, size_t max_coords, size_t max_inputs) {
- const size_t coord_count = TextureCoordinates(texture_type);
-
- size_t total_coord_count = coord_count + (is_array ? 1 : 0) + (depth_compare ? 1 : 0);
- const size_t total_reg_count = total_coord_count + (lod_bias_enabled ? 1 : 0);
- if (total_coord_count > max_coords || total_reg_count > max_inputs) {
- UNIMPLEMENTED_MSG("Unsupported Texture operation");
- total_coord_count = std::min(total_coord_count, max_coords);
- }
- // 1D.DC opengl is using a vec3 but 2nd component is ignored later.
- total_coord_count +=
- (depth_compare && !is_array && texture_type == Tegra::Shader::TextureType::Texture1D)
- ? 1
- : 0;
-
- constexpr std::array<const char*, 5> coord_container{
- {"", "float coord = (", "vec2 coord = vec2(", "vec3 coord = vec3(",
- "vec4 coord = vec4("}};
-
- return std::pair<size_t, std::string>(coord_count, coord_container[total_coord_count]);
- }
-
- std::string GetTextureCode(const Tegra::Shader::Instruction& instr,
- const Tegra::Shader::TextureType texture_type,
- const Tegra::Shader::TextureProcessMode process_mode,
- const bool depth_compare, const bool is_array,
- const size_t bias_offset) {
-
- if ((texture_type == Tegra::Shader::TextureType::Texture3D &&
- (is_array || depth_compare)) ||
- (texture_type == Tegra::Shader::TextureType::TextureCube && is_array &&
- depth_compare)) {
- UNIMPLEMENTED_MSG("This method is not supported.");
- }
-
- const std::string sampler =
- GetSampler(instr.sampler, texture_type, is_array, depth_compare);
-
- const bool lod_needed = process_mode == Tegra::Shader::TextureProcessMode::LZ ||
- process_mode == Tegra::Shader::TextureProcessMode::LL ||
- process_mode == Tegra::Shader::TextureProcessMode::LLA;
-
- const bool gl_lod_supported = !(
- (texture_type == Tegra::Shader::TextureType::Texture2D && is_array && depth_compare) ||
- (texture_type == Tegra::Shader::TextureType::TextureCube && !is_array &&
- depth_compare));
-
- const std::string read_method = lod_needed && gl_lod_supported ? "textureLod(" : "texture(";
- std::string texture = read_method + sampler + ", coord";
-
- if (process_mode != Tegra::Shader::TextureProcessMode::None) {
- if (process_mode == Tegra::Shader::TextureProcessMode::LZ) {
- if (gl_lod_supported) {
- texture += ", 0";
- } else {
- // Lod 0 is emulated by a big negative bias
- // in scenarios that are not supported by glsl
- texture += ", -1000";
- }
- } else {
- // If present, lod or bias are always stored in the register indexed by the
- // gpr20
- // field with an offset depending on the usage of the other registers
- texture += ',' + regs.GetRegisterAsFloat(instr.gpr20.Value() + bias_offset);
- }
- }
- texture += ")";
- return texture;
- }
-
- std::pair<std::string, std::string> GetTEXCode(
- const Instruction& instr, const Tegra::Shader::TextureType texture_type,
- const Tegra::Shader::TextureProcessMode process_mode, const bool depth_compare,
- const bool is_array) {
- const bool lod_bias_enabled = (process_mode != Tegra::Shader::TextureProcessMode::None &&
- process_mode != Tegra::Shader::TextureProcessMode::LZ);
-
- const auto [coord_count, coord_dcl] = ValidateAndGetCoordinateElement(
- texture_type, depth_compare, is_array, lod_bias_enabled, 4, 5);
- // If enabled arrays index is always stored in the gpr8 field
- const u64 array_register = instr.gpr8.Value();
- // First coordinate index is the gpr8 or gpr8 + 1 when arrays are used
- const u64 coord_register = array_register + (is_array ? 1 : 0);
-
- std::string coord = coord_dcl;
- for (size_t i = 0; i < coord_count;) {
- coord += regs.GetRegisterAsFloat(coord_register + i);
- ++i;
- if (i != coord_count) {
- coord += ',';
- }
- }
- // 1D.DC in opengl the 2nd component is ignored.
- if (depth_compare && !is_array && texture_type == Tegra::Shader::TextureType::Texture1D) {
- coord += ",0.0";
- }
- if (depth_compare) {
- // Depth is always stored in the register signaled by gpr20
- // or in the next register if lod or bias are used
- const u64 depth_register = instr.gpr20.Value() + (lod_bias_enabled ? 1 : 0);
- coord += ',' + regs.GetRegisterAsFloat(depth_register);
- }
- if (is_array) {
- coord += ',' + regs.GetRegisterAsInteger(array_register);
- }
- coord += ");";
- return std::make_pair(
- coord, GetTextureCode(instr, texture_type, process_mode, depth_compare, is_array, 0));
- }
-
- std::pair<std::string, std::string> GetTEXSCode(
- const Instruction& instr, const Tegra::Shader::TextureType texture_type,
- const Tegra::Shader::TextureProcessMode process_mode, const bool depth_compare,
- const bool is_array) {
- const bool lod_bias_enabled = (process_mode != Tegra::Shader::TextureProcessMode::None &&
- process_mode != Tegra::Shader::TextureProcessMode::LZ);
-
- const auto [coord_count, coord_dcl] = ValidateAndGetCoordinateElement(
- texture_type, depth_compare, is_array, lod_bias_enabled, 4, 4);
- // If enabled arrays index is always stored in the gpr8 field
- const u64 array_register = instr.gpr8.Value();
- // First coordinate index is stored in gpr8 field or (gpr8 + 1) when arrays are used
- const u64 coord_register = array_register + (is_array ? 1 : 0);
- const u64 last_coord_register =
- (is_array || !(lod_bias_enabled || depth_compare) || (coord_count > 2))
- ? static_cast<u64>(instr.gpr20.Value())
- : coord_register + 1;
-
- std::string coord = coord_dcl;
- for (size_t i = 0; i < coord_count; ++i) {
- const bool last = (i == (coord_count - 1)) && (coord_count > 1);
- coord += regs.GetRegisterAsFloat(last ? last_coord_register : coord_register + i);
- if (!last) {
- coord += ',';
- }
- }
-
- if (depth_compare) {
- // Depth is always stored in the register signaled by gpr20
- // or in the next register if lod or bias are used
- const u64 depth_register = instr.gpr20.Value() + (lod_bias_enabled ? 1 : 0);
- coord += ',' + regs.GetRegisterAsFloat(depth_register);
- }
- if (is_array) {
- coord += ',' + regs.GetRegisterAsInteger(array_register);
- }
- coord += ");";
-
- return std::make_pair(coord,
- GetTextureCode(instr, texture_type, process_mode, depth_compare,
- is_array, (coord_count > 2 ? 1 : 0)));
- }
-
- /**
- * Compiles a single instruction from Tegra to GLSL.
- * @param offset the offset of the Tegra shader instruction.
- * @return the offset of the next instruction to execute. Usually it is the current offset
- * + 1. If the current instruction always terminates the program, returns PROGRAM_END.
- */
- u32 CompileInstr(u32 offset) {
- // Ignore sched instructions when generating code.
- if (IsSchedInstruction(offset)) {
- return offset + 1;
- }
-
- const Instruction instr = {program_code[offset]};
- const auto opcode = OpCode::Decode(instr);
-
- // Decoding failure
- if (!opcode) {
- UNIMPLEMENTED_MSG("Unhandled instruction: {0:x}", instr.value);
- return offset + 1;
- }
-
- shader.AddLine(
- fmt::format("// {}: {} (0x{:016x})", offset, opcode->get().GetName(), instr.value));
-
- using Tegra::Shader::Pred;
- UNIMPLEMENTED_IF_MSG(instr.pred.full_pred == Pred::NeverExecute,
- "NeverExecute predicate not implemented");
-
- // Some instructions (like SSY) don't have a predicate field, they are always
- // unconditionally executed.
- bool can_be_predicated = OpCode::IsPredicatedInstruction(opcode->get().GetId());
-
- if (can_be_predicated && instr.pred.pred_index != static_cast<u64>(Pred::UnusedIndex)) {
- shader.AddLine("if (" +
- GetPredicateCondition(instr.pred.pred_index, instr.negate_pred != 0) +
- ')');
- shader.AddLine('{');
- ++shader.scope;
- }
-
- switch (opcode->get().GetType()) {
- case OpCode::Type::Arithmetic: {
- std::string op_a = regs.GetRegisterAsFloat(instr.gpr8);
-
- std::string op_b;
-
- if (instr.is_b_imm) {
- op_b = GetImmediate19(instr);
- } else {
- if (instr.is_b_gpr) {
- op_b = regs.GetRegisterAsFloat(instr.gpr20);
- } else {
- op_b = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::Float);
- }
+ std::string Visit(Node node) {
+ if (const auto operation = std::get_if<OperationNode>(node)) {
+ const auto operation_index = static_cast<std::size_t>(operation->GetCode());
+ const auto decompiler = operation_decompilers[operation_index];
+ if (decompiler == nullptr) {
+ UNREACHABLE_MSG("Operation decompiler {} not defined", operation_index);
}
+ return (this->*decompiler)(*operation);
- switch (opcode->get().GetId()) {
- case OpCode::Id::MOV_C:
- case OpCode::Id::MOV_R: {
- // MOV does not have neither 'abs' nor 'neg' bits.
- regs.SetRegisterToFloat(instr.gpr0, 0, op_b, 1, 1);
- break;
+ } else if (const auto gpr = std::get_if<GprNode>(node)) {
+ const u32 index = gpr->GetIndex();
+ if (index == Register::ZeroIndex) {
+ return "0";
}
+ return GetRegister(index);
- case OpCode::Id::FMUL_C:
- case OpCode::Id::FMUL_R:
- case OpCode::Id::FMUL_IMM: {
- // FMUL does not have 'abs' bits and only the second operand has a 'neg' bit.
- UNIMPLEMENTED_IF_MSG(instr.fmul.tab5cb8_2 != 0,
- "FMUL tab5cb8_2({}) is not implemented",
- instr.fmul.tab5cb8_2.Value());
- UNIMPLEMENTED_IF_MSG(instr.fmul.tab5c68_1 != 0,
- "FMUL tab5cb8_1({}) is not implemented",
- instr.fmul.tab5c68_1.Value());
- UNIMPLEMENTED_IF_MSG(
- instr.fmul.tab5c68_0 != 1, "FMUL tab5cb8_0({}) is not implemented",
- instr.fmul.tab5c68_0
- .Value()); // SMO typical sends 1 here which seems to be the default
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in FMUL is not implemented");
-
- op_b = GetOperandAbsNeg(op_b, false, instr.fmul.negate_b);
-
- regs.SetRegisterToFloat(instr.gpr0, 0, op_a + " * " + op_b, 1, 1,
- instr.alu.saturate_d, 0, true);
- break;
+ } else if (const auto immediate = std::get_if<ImmediateNode>(node)) {
+ const u32 value = immediate->GetValue();
+ if (value < 10) {
+ // For eyecandy avoid using hex numbers on single digits
+ return fmt::format("utof({}u)", immediate->GetValue());
}
- case OpCode::Id::FADD_C:
- case OpCode::Id::FADD_R:
- case OpCode::Id::FADD_IMM: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in FADD is not implemented");
+ return fmt::format("utof(0x{:x}u)", immediate->GetValue());
- op_a = GetOperandAbsNeg(op_a, instr.alu.abs_a, instr.alu.negate_a);
- op_b = GetOperandAbsNeg(op_b, instr.alu.abs_b, instr.alu.negate_b);
-
- regs.SetRegisterToFloat(instr.gpr0, 0, op_a + " + " + op_b, 1, 1,
- instr.alu.saturate_d, 0, true);
- break;
- }
- case OpCode::Id::MUFU: {
- op_a = GetOperandAbsNeg(op_a, instr.alu.abs_a, instr.alu.negate_a);
- switch (instr.sub_op) {
- case SubOp::Cos:
- regs.SetRegisterToFloat(instr.gpr0, 0, "cos(" + op_a + ')', 1, 1,
- instr.alu.saturate_d, 0, true);
- break;
- case SubOp::Sin:
- regs.SetRegisterToFloat(instr.gpr0, 0, "sin(" + op_a + ')', 1, 1,
- instr.alu.saturate_d, 0, true);
- break;
- case SubOp::Ex2:
- regs.SetRegisterToFloat(instr.gpr0, 0, "exp2(" + op_a + ')', 1, 1,
- instr.alu.saturate_d, 0, true);
- break;
- case SubOp::Lg2:
- regs.SetRegisterToFloat(instr.gpr0, 0, "log2(" + op_a + ')', 1, 1,
- instr.alu.saturate_d, 0, true);
- break;
- case SubOp::Rcp:
- regs.SetRegisterToFloat(instr.gpr0, 0, "1.0 / " + op_a, 1, 1,
- instr.alu.saturate_d, 0, true);
- break;
- case SubOp::Rsq:
- regs.SetRegisterToFloat(instr.gpr0, 0, "inversesqrt(" + op_a + ')', 1, 1,
- instr.alu.saturate_d, 0, true);
- break;
- case SubOp::Sqrt:
- regs.SetRegisterToFloat(instr.gpr0, 0, "sqrt(" + op_a + ')', 1, 1,
- instr.alu.saturate_d, 0, true);
- break;
+ } else if (const auto predicate = std::get_if<PredicateNode>(node)) {
+ const auto value = [&]() -> std::string {
+ switch (const auto index = predicate->GetIndex(); index) {
+ case Tegra::Shader::Pred::UnusedIndex:
+ return "true";
+ case Tegra::Shader::Pred::NeverExecute:
+ return "false";
default:
- UNIMPLEMENTED_MSG("Unhandled MUFU sub op={0:x}",
- static_cast<unsigned>(instr.sub_op.Value()));
+ return GetPredicate(index);
}
- break;
- }
- case OpCode::Id::FMNMX_C:
- case OpCode::Id::FMNMX_R:
- case OpCode::Id::FMNMX_IMM: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in FMNMX is not implemented");
-
- op_a = GetOperandAbsNeg(op_a, instr.alu.abs_a, instr.alu.negate_a);
- op_b = GetOperandAbsNeg(op_b, instr.alu.abs_b, instr.alu.negate_b);
-
- std::string condition =
- GetPredicateCondition(instr.alu.fmnmx.pred, instr.alu.fmnmx.negate_pred != 0);
- std::string parameters = op_a + ',' + op_b;
- regs.SetRegisterToFloat(instr.gpr0, 0,
- '(' + condition + ") ? min(" + parameters + ") : max(" +
- parameters + ')',
- 1, 1, false, 0, true);
- break;
- }
- case OpCode::Id::RRO_C:
- case OpCode::Id::RRO_R:
- case OpCode::Id::RRO_IMM: {
- // Currently RRO is only implemented as a register move.
- op_b = GetOperandAbsNeg(op_b, instr.alu.abs_b, instr.alu.negate_b);
- regs.SetRegisterToFloat(instr.gpr0, 0, op_b, 1, 1);
- LOG_WARNING(HW_GPU, "RRO instruction is incomplete");
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled arithmetic instruction: {}", opcode->get().GetName());
- }
- }
- break;
- }
- case OpCode::Type::ArithmeticImmediate: {
- switch (opcode->get().GetId()) {
- case OpCode::Id::MOV32_IMM: {
- regs.SetRegisterToFloat(instr.gpr0, 0, GetImmediate32(instr), 1, 1);
- break;
- }
- case OpCode::Id::FMUL32_IMM: {
- UNIMPLEMENTED_IF_MSG(instr.op_32.generates_cc,
- "Condition codes generation in FMUL32 is not implemented");
-
- regs.SetRegisterToFloat(instr.gpr0, 0,
- regs.GetRegisterAsFloat(instr.gpr8) + " * " +
- GetImmediate32(instr),
- 1, 1, instr.fmul32.saturate, 0, true);
- break;
+ }();
+ if (predicate->IsNegated()) {
+ return "!(" + value + ')';
}
- case OpCode::Id::FADD32I: {
- UNIMPLEMENTED_IF_MSG(instr.op_32.generates_cc,
- "Condition codes generation in FADD32I is not implemented");
-
- std::string op_a = regs.GetRegisterAsFloat(instr.gpr8);
- std::string op_b = GetImmediate32(instr);
-
- if (instr.fadd32i.abs_a) {
- op_a = "abs(" + op_a + ')';
- }
-
- if (instr.fadd32i.negate_a) {
- op_a = "-(" + op_a + ')';
- }
+ return value;
- if (instr.fadd32i.abs_b) {
- op_b = "abs(" + op_b + ')';
+ } else if (const auto abuf = std::get_if<AbufNode>(node)) {
+ const auto attribute = abuf->GetIndex();
+ const auto element = abuf->GetElement();
+
+ const auto GeometryPass = [&](const std::string& name) {
+ if (stage == ShaderStage::Geometry && abuf->GetBuffer()) {
+ // TODO(Rodrigo): Guard geometry inputs against out of bound reads. Some games
+ // set an 0x80000000 index for those and the shader fails to build. Find out why
+ // this happens and what's its intent.
+ return "gs_" + name + "[ftou(" + Visit(abuf->GetBuffer()) +
+ ") % MAX_VERTEX_INPUT]";
+ }
+ return name;
+ };
+
+ switch (attribute) {
+ case Attribute::Index::Position:
+ if (stage != ShaderStage::Fragment) {
+ return GeometryPass("position") + GetSwizzle(element);
+ } else {
+ return element == 3 ? "1.0f" : "gl_FragCoord" + GetSwizzle(element);
+ }
+ case Attribute::Index::PointCoord:
+ switch (element) {
+ case 0:
+ return "gl_PointCoord.x";
+ case 1:
+ return "gl_PointCoord.y";
+ case 2:
+ case 3:
+ return "0";
}
-
- if (instr.fadd32i.negate_b) {
- op_b = "-(" + op_b + ')';
+ UNREACHABLE();
+ return "0";
+ case Attribute::Index::TessCoordInstanceIDVertexID:
+ // TODO(Subv): Find out what the values are for the first two elements when inside a
+ // vertex shader, and what's the value of the fourth element when inside a Tess Eval
+ // shader.
+ ASSERT(stage == ShaderStage::Vertex);
+ switch (element) {
+ case 2:
+ // Config pack's first value is instance_id.
+ return "uintBitsToFloat(config_pack[0])";
+ case 3:
+ return "uintBitsToFloat(gl_VertexID)";
+ }
+ UNIMPLEMENTED_MSG("Unmanaged TessCoordInstanceIDVertexID element={}", element);
+ return "0";
+ case Attribute::Index::FrontFacing:
+ // TODO(Subv): Find out what the values are for the other elements.
+ ASSERT(stage == ShaderStage::Fragment);
+ switch (element) {
+ case 3:
+ return "itof(gl_FrontFacing ? -1 : 0)";
+ }
+ UNIMPLEMENTED_MSG("Unmanaged FrontFacing element={}", element);
+ return "0";
+ default:
+ if (attribute >= Attribute::Index::Attribute_0 &&
+ attribute <= Attribute::Index::Attribute_31) {
+ return GeometryPass(GetInputAttribute(attribute)) + GetSwizzle(element);
}
-
- regs.SetRegisterToFloat(instr.gpr0, 0, op_a + " + " + op_b, 1, 1, false, 0, true);
break;
}
- }
- break;
- }
- case OpCode::Type::Bfe: {
- UNIMPLEMENTED_IF(instr.bfe.negate_b);
+ UNIMPLEMENTED_MSG("Unhandled input attribute: {}", static_cast<u32>(attribute));
- std::string op_a = instr.bfe.negate_a ? "-" : "";
- op_a += regs.GetRegisterAsInteger(instr.gpr8);
+ } else if (const auto cbuf = std::get_if<CbufNode>(node)) {
+ const Node offset = cbuf->GetOffset();
+ if (const auto immediate = std::get_if<ImmediateNode>(offset)) {
+ // Direct access
+ const u32 offset_imm = immediate->GetValue();
+ return fmt::format("{}[{}][{}]", GetConstBuffer(cbuf->GetIndex()), offset_imm / 4,
+ offset_imm % 4);
+
+ } else if (std::holds_alternative<OperationNode>(*offset)) {
+ // Indirect access
+ const std::string final_offset = code.GenerateTemporal();
+ code.AddLine("uint " + final_offset + " = (ftou(" + Visit(offset) + ") / 4) & " +
+ std::to_string(MAX_CONSTBUFFER_ELEMENTS - 1) + ';');
+ return fmt::format("{}[{} / 4][{} % 4]", GetConstBuffer(cbuf->GetIndex()),
+ final_offset, final_offset);
- switch (opcode->get().GetId()) {
- case OpCode::Id::BFE_IMM: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in BFE is not implemented");
+ } else {
+ UNREACHABLE_MSG("Unmanaged offset node type");
+ }
- std::string inner_shift =
- '(' + op_a + " << " + std::to_string(instr.bfe.GetLeftShiftValue()) + ')';
- std::string outer_shift =
- '(' + inner_shift + " >> " +
- std::to_string(instr.bfe.GetLeftShiftValue() + instr.bfe.shift_position) + ')';
+ } else if (const auto gmem = std::get_if<GmemNode>(node)) {
+ const std::string real = Visit(gmem->GetRealAddress());
+ const std::string base = Visit(gmem->GetBaseAddress());
+ const std::string final_offset = "(ftou(" + real + ") - ftou(" + base + ")) / 4";
+ return fmt::format("{}[{}]", GetGlobalMemory(gmem->GetDescriptor()), final_offset);
- regs.SetRegisterToInteger(instr.gpr0, true, 0, outer_shift, 1, 1);
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled BFE instruction: {}", opcode->get().GetName());
- }
- }
+ } else if (const auto lmem = std::get_if<LmemNode>(node)) {
+ return fmt::format("{}[ftou({}) / 4]", GetLocalMemory(), Visit(lmem->GetAddress()));
- break;
- }
- case OpCode::Type::Bfi: {
- UNIMPLEMENTED_IF(instr.generates_cc);
-
- const auto [base, packed_shift] = [&]() -> std::tuple<std::string, std::string> {
- switch (opcode->get().GetId()) {
- case OpCode::Id::BFI_IMM_R:
- return {regs.GetRegisterAsInteger(instr.gpr39, 0, false),
- std::to_string(instr.alu.GetSignedImm20_20())};
- default:
- UNREACHABLE();
- }
- }();
- const std::string offset = '(' + packed_shift + " & 0xff)";
- const std::string bits = "((" + packed_shift + " >> 8) & 0xff)";
- const std::string insert = regs.GetRegisterAsInteger(instr.gpr8, 0, false);
- regs.SetRegisterToInteger(
- instr.gpr0, false, 0,
- "bitfieldInsert(" + base + ", " + insert + ", " + offset + ", " + bits + ')', 1, 1);
- break;
- }
- case OpCode::Type::Shift: {
- std::string op_a = regs.GetRegisterAsInteger(instr.gpr8, 0, true);
- std::string op_b;
+ } else if (const auto internal_flag = std::get_if<InternalFlagNode>(node)) {
+ return GetInternalFlag(internal_flag->GetFlag());
- if (instr.is_b_imm) {
- op_b += '(' + std::to_string(instr.alu.GetSignedImm20_20()) + ')';
- } else {
- if (instr.is_b_gpr) {
- op_b += regs.GetRegisterAsInteger(instr.gpr20);
- } else {
- op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::Integer);
- }
- }
+ } else if (const auto conditional = std::get_if<ConditionalNode>(node)) {
+ // It's invalid to call conditional on nested nodes, use an operation instead
+ code.AddLine("if (" + Visit(conditional->GetCondition()) + ") {");
+ ++code.scope;
- switch (opcode->get().GetId()) {
- case OpCode::Id::SHR_C:
- case OpCode::Id::SHR_R:
- case OpCode::Id::SHR_IMM: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in SHR is not implemented");
+ VisitBasicBlock(conditional->GetCode());
- if (!instr.shift.is_signed) {
- // Logical shift right
- op_a = "uint(" + op_a + ')';
- }
+ --code.scope;
+ code.AddLine('}');
+ return {};
- // Cast to int is superfluous for arithmetic shift, it's only for a logical shift
- regs.SetRegisterToInteger(instr.gpr0, true, 0, "int(" + op_a + " >> " + op_b + ')',
- 1, 1);
- break;
- }
- case OpCode::Id::SHL_C:
- case OpCode::Id::SHL_R:
- case OpCode::Id::SHL_IMM:
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in SHL is not implemented");
- regs.SetRegisterToInteger(instr.gpr0, true, 0, op_a + " << " + op_b, 1, 1);
- break;
- default: {
- UNIMPLEMENTED_MSG("Unhandled shift instruction: {}", opcode->get().GetName());
- }
- }
- break;
+ } else if (const auto comment = std::get_if<CommentNode>(node)) {
+ return "// " + comment->GetText();
}
- case OpCode::Type::ArithmeticIntegerImmediate: {
- std::string op_a = regs.GetRegisterAsInteger(instr.gpr8);
- std::string op_b = std::to_string(instr.alu.imm20_32.Value());
+ UNREACHABLE();
+ return {};
+ }
- switch (opcode->get().GetId()) {
- case OpCode::Id::IADD32I:
- UNIMPLEMENTED_IF_MSG(instr.op_32.generates_cc,
- "Condition codes generation in IADD32I is not implemented");
+ std::string ApplyPrecise(Operation operation, const std::string& value) {
+ if (!IsPrecise(operation)) {
+ return value;
+ }
+ // There's a bug in NVidia's proprietary drivers that makes precise fail on fragment shaders
+ const std::string precise = stage != ShaderStage::Fragment ? "precise " : "";
- if (instr.iadd32i.negate_a)
- op_a = "-(" + op_a + ')';
+ const std::string temporal = code.GenerateTemporal();
+ code.AddLine(precise + "float " + temporal + " = " + value + ';');
+ return temporal;
+ }
- regs.SetRegisterToInteger(instr.gpr0, true, 0, op_a + " + " + op_b, 1, 1,
- instr.iadd32i.saturate != 0);
- break;
- case OpCode::Id::LOP32I: {
- UNIMPLEMENTED_IF_MSG(instr.op_32.generates_cc,
- "Condition codes generation in LOP32I is not implemented");
+ std::string VisitOperand(Operation operation, std::size_t operand_index) {
+ const auto& operand = operation[operand_index];
+ const bool parent_precise = IsPrecise(operation);
+ const bool child_precise = IsPrecise(operand);
+ const bool child_trivial = !std::holds_alternative<OperationNode>(*operand);
+ if (!parent_precise || child_precise || child_trivial) {
+ return Visit(operand);
+ }
- if (instr.alu.lop32i.invert_a)
- op_a = "~(" + op_a + ')';
+ const std::string temporal = code.GenerateTemporal();
+ code.AddLine("float " + temporal + " = " + Visit(operand) + ';');
+ return temporal;
+ }
- if (instr.alu.lop32i.invert_b)
- op_b = "~(" + op_b + ')';
+ std::string VisitOperand(Operation operation, std::size_t operand_index, Type type) {
+ std::string value = VisitOperand(operation, operand_index);
- WriteLogicOperation(instr.gpr0, instr.alu.lop32i.operation, op_a, op_b,
- Tegra::Shader::PredicateResultMode::None,
- Tegra::Shader::Pred::UnusedIndex);
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled ArithmeticIntegerImmediate instruction: {}",
- opcode->get().GetName());
+ switch (type) {
+ case Type::Bool:
+ case Type::Bool2:
+ case Type::Float:
+ return value;
+ case Type::Int:
+ return "ftoi(" + value + ')';
+ case Type::Uint:
+ return "ftou(" + value + ')';
+ case Type::HalfFloat:
+ const auto half_meta = std::get_if<MetaHalfArithmetic>(&operation.GetMeta());
+ if (!half_meta) {
+ value = "toHalf2(" + value + ')';
}
+
+ switch (half_meta->types.at(operand_index)) {
+ case Tegra::Shader::HalfType::H0_H1:
+ return "toHalf2(" + value + ')';
+ case Tegra::Shader::HalfType::F32:
+ return "vec2(" + value + ')';
+ case Tegra::Shader::HalfType::H0_H0:
+ return "vec2(toHalf2(" + value + ")[0])";
+ case Tegra::Shader::HalfType::H1_H1:
+ return "vec2(toHalf2(" + value + ")[1])";
}
- break;
}
- case OpCode::Type::ArithmeticInteger: {
- std::string op_a = regs.GetRegisterAsInteger(instr.gpr8);
- std::string op_b;
- if (instr.is_b_imm) {
- op_b += '(' + std::to_string(instr.alu.GetSignedImm20_20()) + ')';
- } else {
- if (instr.is_b_gpr) {
- op_b += regs.GetRegisterAsInteger(instr.gpr20);
- } else {
- op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::Integer);
- }
- }
-
- switch (opcode->get().GetId()) {
- case OpCode::Id::IADD_C:
- case OpCode::Id::IADD_R:
- case OpCode::Id::IADD_IMM: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in IADD is not implemented");
+ UNREACHABLE();
+ return value;
+ }
- if (instr.alu_integer.negate_a)
- op_a = "-(" + op_a + ')';
+ std::string BitwiseCastResult(std::string value, Type type, bool needs_parenthesis = false) {
+ switch (type) {
+ case Type::Bool:
+ case Type::Float:
+ if (needs_parenthesis) {
+ return '(' + value + ')';
+ }
+ return value;
+ case Type::Int:
+ return "itof(" + value + ')';
+ case Type::Uint:
+ return "utof(" + value + ')';
+ case Type::HalfFloat:
+ return "fromHalf2(" + value + ')';
+ }
+ UNREACHABLE();
+ return value;
+ }
- if (instr.alu_integer.negate_b)
- op_b = "-(" + op_b + ')';
+ std::string GenerateUnary(Operation operation, const std::string& func, Type result_type,
+ Type type_a, bool needs_parenthesis = true) {
+ return ApplyPrecise(operation,
+ BitwiseCastResult(func + '(' + VisitOperand(operation, 0, type_a) + ')',
+ result_type, needs_parenthesis));
+ }
- regs.SetRegisterToInteger(instr.gpr0, true, 0, op_a + " + " + op_b, 1, 1,
- instr.alu.saturate_d);
- break;
- }
- case OpCode::Id::IADD3_C:
- case OpCode::Id::IADD3_R:
- case OpCode::Id::IADD3_IMM: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in IADD3 is not implemented");
-
- std::string op_c = regs.GetRegisterAsInteger(instr.gpr39);
-
- auto apply_height = [](auto height, auto& oprand) {
- switch (height) {
- case Tegra::Shader::IAdd3Height::None:
- break;
- case Tegra::Shader::IAdd3Height::LowerHalfWord:
- oprand = "((" + oprand + ") & 0xFFFF)";
- break;
- case Tegra::Shader::IAdd3Height::UpperHalfWord:
- oprand = "((" + oprand + ") >> 16)";
- break;
- default:
- UNIMPLEMENTED_MSG("Unhandled IADD3 height: {}",
- static_cast<u32>(height.Value()));
- }
- };
+ std::string GenerateBinaryInfix(Operation operation, const std::string& func, Type result_type,
+ Type type_a, Type type_b) {
+ const std::string op_a = VisitOperand(operation, 0, type_a);
+ const std::string op_b = VisitOperand(operation, 1, type_b);
- if (opcode->get().GetId() == OpCode::Id::IADD3_R) {
- apply_height(instr.iadd3.height_a, op_a);
- apply_height(instr.iadd3.height_b, op_b);
- apply_height(instr.iadd3.height_c, op_c);
- }
+ return ApplyPrecise(
+ operation, BitwiseCastResult('(' + op_a + ' ' + func + ' ' + op_b + ')', result_type));
+ }
- if (instr.iadd3.neg_a)
- op_a = "-(" + op_a + ')';
-
- if (instr.iadd3.neg_b)
- op_b = "-(" + op_b + ')';
-
- if (instr.iadd3.neg_c)
- op_c = "-(" + op_c + ')';
-
- std::string result;
- if (opcode->get().GetId() == OpCode::Id::IADD3_R) {
- switch (instr.iadd3.mode) {
- case Tegra::Shader::IAdd3Mode::RightShift:
- // TODO(tech4me): According to
- // https://envytools.readthedocs.io/en/latest/hw/graph/maxwell/cuda/int.html?highlight=iadd3
- // The addition between op_a and op_b should be done in uint33, more
- // investigation required
- result = "(((" + op_a + " + " + op_b + ") >> 16) + " + op_c + ')';
- break;
- case Tegra::Shader::IAdd3Mode::LeftShift:
- result = "(((" + op_a + " + " + op_b + ") << 16) + " + op_c + ')';
- break;
- default:
- result = '(' + op_a + " + " + op_b + " + " + op_c + ')';
- break;
- }
- } else {
- result = '(' + op_a + " + " + op_b + " + " + op_c + ')';
- }
+ std::string GenerateBinaryCall(Operation operation, const std::string& func, Type result_type,
+ Type type_a, Type type_b) {
+ const std::string op_a = VisitOperand(operation, 0, type_a);
+ const std::string op_b = VisitOperand(operation, 1, type_b);
- regs.SetRegisterToInteger(instr.gpr0, true, 0, result, 1, 1);
- break;
- }
- case OpCode::Id::ISCADD_C:
- case OpCode::Id::ISCADD_R:
- case OpCode::Id::ISCADD_IMM: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in ISCADD is not implemented");
+ return ApplyPrecise(operation,
+ BitwiseCastResult(func + '(' + op_a + ", " + op_b + ')', result_type));
+ }
- if (instr.alu_integer.negate_a)
- op_a = "-(" + op_a + ')';
+ std::string GenerateTernary(Operation operation, const std::string& func, Type result_type,
+ Type type_a, Type type_b, Type type_c) {
+ const std::string op_a = VisitOperand(operation, 0, type_a);
+ const std::string op_b = VisitOperand(operation, 1, type_b);
+ const std::string op_c = VisitOperand(operation, 2, type_c);
- if (instr.alu_integer.negate_b)
- op_b = "-(" + op_b + ')';
+ return ApplyPrecise(
+ operation,
+ BitwiseCastResult(func + '(' + op_a + ", " + op_b + ", " + op_c + ')', result_type));
+ }
- const std::string shift = std::to_string(instr.alu_integer.shift_amount.Value());
+ std::string GenerateQuaternary(Operation operation, const std::string& func, Type result_type,
+ Type type_a, Type type_b, Type type_c, Type type_d) {
+ const std::string op_a = VisitOperand(operation, 0, type_a);
+ const std::string op_b = VisitOperand(operation, 1, type_b);
+ const std::string op_c = VisitOperand(operation, 2, type_c);
+ const std::string op_d = VisitOperand(operation, 3, type_d);
- regs.SetRegisterToInteger(instr.gpr0, true, 0,
- "((" + op_a + " << " + shift + ") + " + op_b + ')', 1, 1);
- break;
- }
- case OpCode::Id::POPC_C:
- case OpCode::Id::POPC_R:
- case OpCode::Id::POPC_IMM: {
- if (instr.popc.invert) {
- op_b = "~(" + op_b + ')';
- }
- regs.SetRegisterToInteger(instr.gpr0, true, 0, "bitCount(" + op_b + ')', 1, 1);
- break;
- }
- case OpCode::Id::SEL_C:
- case OpCode::Id::SEL_R:
- case OpCode::Id::SEL_IMM: {
- const std::string condition =
- GetPredicateCondition(instr.sel.pred, instr.sel.neg_pred != 0);
- regs.SetRegisterToInteger(instr.gpr0, true, 0,
- '(' + condition + ") ? " + op_a + " : " + op_b, 1, 1);
- break;
- }
- case OpCode::Id::LOP_C:
- case OpCode::Id::LOP_R:
- case OpCode::Id::LOP_IMM: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in LOP is not implemented");
+ return ApplyPrecise(operation, BitwiseCastResult(func + '(' + op_a + ", " + op_b + ", " +
+ op_c + ", " + op_d + ')',
+ result_type));
+ }
- if (instr.alu.lop.invert_a)
- op_a = "~(" + op_a + ')';
+ std::string GenerateTexture(Operation operation, const std::string& func,
+ bool is_extra_int = false) {
+ constexpr std::array<const char*, 4> coord_constructors = {"float", "vec2", "vec3", "vec4"};
- if (instr.alu.lop.invert_b)
- op_b = "~(" + op_b + ')';
+ const auto meta = std::get_if<MetaTexture>(&operation.GetMeta());
+ const auto count = static_cast<u32>(operation.GetOperandsCount());
+ ASSERT(meta);
- WriteLogicOperation(instr.gpr0, instr.alu.lop.operation, op_a, op_b,
- instr.alu.lop.pred_result_mode, instr.alu.lop.pred48);
- break;
- }
- case OpCode::Id::LOP3_C:
- case OpCode::Id::LOP3_R:
- case OpCode::Id::LOP3_IMM: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in LOP3 is not implemented");
+ std::string expr = func;
+ expr += '(';
+ expr += GetSampler(meta->sampler);
+ expr += ", ";
- const std::string op_c = regs.GetRegisterAsInteger(instr.gpr39);
- std::string lut;
+ expr += coord_constructors[meta->coords_count - 1];
+ expr += '(';
+ for (u32 i = 0; i < count; ++i) {
+ const bool is_extra = i >= meta->coords_count;
+ const bool is_array = i == meta->array_index;
- if (opcode->get().GetId() == OpCode::Id::LOP3_R) {
- lut = '(' + std::to_string(instr.alu.lop3.GetImmLut28()) + ')';
+ std::string operand = [&]() {
+ if (is_extra && is_extra_int) {
+ if (const auto immediate = std::get_if<ImmediateNode>(operation[i])) {
+ return std::to_string(static_cast<s32>(immediate->GetValue()));
+ } else {
+ return "ftoi(" + Visit(operation[i]) + ')';
+ }
} else {
- lut = '(' + std::to_string(instr.alu.lop3.GetImmLut48()) + ')';
+ return Visit(operation[i]);
}
-
- WriteLop3Instruction(instr.gpr0, op_a, op_b, op_c, lut);
- break;
- }
- case OpCode::Id::IMNMX_C:
- case OpCode::Id::IMNMX_R:
- case OpCode::Id::IMNMX_IMM: {
- UNIMPLEMENTED_IF(instr.imnmx.exchange != Tegra::Shader::IMinMaxExchange::None);
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in IMNMX is not implemented");
-
- const std::string condition =
- GetPredicateCondition(instr.imnmx.pred, instr.imnmx.negate_pred != 0);
- const std::string parameters = op_a + ',' + op_b;
- regs.SetRegisterToInteger(instr.gpr0, instr.imnmx.is_signed, 0,
- '(' + condition + ") ? min(" + parameters + ") : max(" +
- parameters + ')',
- 1, 1);
- break;
+ }();
+ if (is_array) {
+ ASSERT(!is_extra);
+ operand = "float(ftoi(" + operand + "))";
}
- case OpCode::Id::LEA_R2:
- case OpCode::Id::LEA_R1:
- case OpCode::Id::LEA_IMM:
- case OpCode::Id::LEA_RZ:
- case OpCode::Id::LEA_HI: {
- std::string op_c;
-
- switch (opcode->get().GetId()) {
- case OpCode::Id::LEA_R2: {
- op_a = regs.GetRegisterAsInteger(instr.gpr20);
- op_b = regs.GetRegisterAsInteger(instr.gpr39);
- op_c = std::to_string(instr.lea.r2.entry_a);
- break;
- }
-
- case OpCode::Id::LEA_R1: {
- const bool neg = instr.lea.r1.neg != 0;
- op_a = regs.GetRegisterAsInteger(instr.gpr8);
- if (neg)
- op_a = "-(" + op_a + ')';
- op_b = regs.GetRegisterAsInteger(instr.gpr20);
- op_c = std::to_string(instr.lea.r1.entry_a);
- break;
- }
-
- case OpCode::Id::LEA_IMM: {
- const bool neg = instr.lea.imm.neg != 0;
- op_b = regs.GetRegisterAsInteger(instr.gpr8);
- if (neg)
- op_b = "-(" + op_b + ')';
- op_a = std::to_string(instr.lea.imm.entry_a);
- op_c = std::to_string(instr.lea.imm.entry_b);
- break;
- }
-
- case OpCode::Id::LEA_RZ: {
- const bool neg = instr.lea.rz.neg != 0;
- op_b = regs.GetRegisterAsInteger(instr.gpr8);
- if (neg)
- op_b = "-(" + op_b + ')';
- op_a = regs.GetUniform(instr.lea.rz.cb_index, instr.lea.rz.cb_offset,
- GLSLRegister::Type::Integer);
- op_c = std::to_string(instr.lea.rz.entry_a);
-
- break;
- }
- case OpCode::Id::LEA_HI:
- default: {
- op_b = regs.GetRegisterAsInteger(instr.gpr8);
- op_a = std::to_string(instr.lea.imm.entry_a);
- op_c = std::to_string(instr.lea.imm.entry_b);
- UNIMPLEMENTED_MSG("Unhandled LEA subinstruction: {}", opcode->get().GetName());
- }
- }
- UNIMPLEMENTED_IF_MSG(instr.lea.pred48 != static_cast<u64>(Pred::UnusedIndex),
- "Unhandled LEA Predicate");
- const std::string value = '(' + op_a + " + (" + op_b + "*(1 << " + op_c + ")))";
- regs.SetRegisterToInteger(instr.gpr0, true, 0, value, 1, 1);
+ expr += operand;
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled ArithmeticInteger instruction: {}",
- opcode->get().GetName());
+ if (i + 1 == meta->coords_count) {
+ expr += ')';
}
+ if (i + 1 < count) {
+ expr += ", ";
}
-
- break;
}
- case OpCode::Type::ArithmeticHalf: {
- if (opcode->get().GetId() == OpCode::Id::HADD2_C ||
- opcode->get().GetId() == OpCode::Id::HADD2_R) {
- UNIMPLEMENTED_IF(instr.alu_half.ftz != 0);
- }
- const bool negate_a =
- opcode->get().GetId() != OpCode::Id::HMUL2_R && instr.alu_half.negate_a != 0;
- const bool negate_b =
- opcode->get().GetId() != OpCode::Id::HMUL2_C && instr.alu_half.negate_b != 0;
-
- const std::string op_a =
- GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr8, 0, false), instr.alu_half.type_a,
- instr.alu_half.abs_a != 0, negate_a);
-
- std::string op_b;
- switch (opcode->get().GetId()) {
- case OpCode::Id::HADD2_C:
- case OpCode::Id::HMUL2_C:
- op_b = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::UnsignedInteger);
- break;
- case OpCode::Id::HADD2_R:
- case OpCode::Id::HMUL2_R:
- op_b = regs.GetRegisterAsInteger(instr.gpr20, 0, false);
- break;
- default:
- UNREACHABLE();
- op_b = "0";
- break;
- }
- op_b = GetHalfFloat(op_b, instr.alu_half.type_b, instr.alu_half.abs_b != 0, negate_b);
-
- const std::string result = [&]() {
- switch (opcode->get().GetId()) {
- case OpCode::Id::HADD2_C:
- case OpCode::Id::HADD2_R:
- return '(' + op_a + " + " + op_b + ')';
- case OpCode::Id::HMUL2_C:
- case OpCode::Id::HMUL2_R:
- return '(' + op_a + " * " + op_b + ')';
- default:
- UNIMPLEMENTED_MSG("Unhandled half float instruction: {}",
- opcode->get().GetName());
- return std::string("0");
- }
- }();
-
- regs.SetRegisterToHalfFloat(instr.gpr0, 0, result, instr.alu_half.merge, 1, 1,
- instr.alu_half.saturate != 0);
- break;
- }
- case OpCode::Type::ArithmeticHalfImmediate: {
- if (opcode->get().GetId() == OpCode::Id::HADD2_IMM) {
- UNIMPLEMENTED_IF(instr.alu_half_imm.ftz != 0);
- } else {
- UNIMPLEMENTED_IF(instr.alu_half_imm.precision !=
- Tegra::Shader::HalfPrecision::None);
- }
+ expr += ')';
+ return expr;
+ }
- const std::string op_a = GetHalfFloat(
- regs.GetRegisterAsInteger(instr.gpr8, 0, false), instr.alu_half_imm.type_a,
- instr.alu_half_imm.abs_a != 0, instr.alu_half_imm.negate_a != 0);
+ std::string Assign(Operation operation) {
+ const Node dest = operation[0];
+ const Node src = operation[1];
- const std::string op_b = UnpackHalfImmediate(instr, true);
+ std::string target;
+ if (const auto gpr = std::get_if<GprNode>(dest)) {
+ if (gpr->GetIndex() == Register::ZeroIndex) {
+ // Writing to Register::ZeroIndex is a no op
+ return {};
+ }
+ target = GetRegister(gpr->GetIndex());
- const std::string result = [&]() {
- switch (opcode->get().GetId()) {
- case OpCode::Id::HADD2_IMM:
- return op_a + " + " + op_b;
- case OpCode::Id::HMUL2_IMM:
- return op_a + " * " + op_b;
+ } else if (const auto abuf = std::get_if<AbufNode>(dest)) {
+ target = [&]() -> std::string {
+ switch (const auto attribute = abuf->GetIndex(); abuf->GetIndex()) {
+ case Attribute::Index::Position:
+ return "position" + GetSwizzle(abuf->GetElement());
+ case Attribute::Index::PointSize:
+ return "gl_PointSize";
+ case Attribute::Index::ClipDistances0123:
+ return "gl_ClipDistance[" + std::to_string(abuf->GetElement()) + ']';
+ case Attribute::Index::ClipDistances4567:
+ return "gl_ClipDistance[" + std::to_string(abuf->GetElement() + 4) + ']';
default:
- UNREACHABLE();
- return std::string("0");
+ if (attribute >= Attribute::Index::Attribute_0 &&
+ attribute <= Attribute::Index::Attribute_31) {
+ return GetOutputAttribute(attribute) + GetSwizzle(abuf->GetElement());
+ }
+ UNIMPLEMENTED_MSG("Unhandled output attribute: {}",
+ static_cast<u32>(attribute));
+ return "0";
}
}();
- regs.SetRegisterToHalfFloat(instr.gpr0, 0, result, instr.alu_half_imm.merge, 1, 1,
- instr.alu_half_imm.saturate != 0);
- break;
- }
- case OpCode::Type::Ffma: {
- const std::string op_a = regs.GetRegisterAsFloat(instr.gpr8);
- std::string op_b = instr.ffma.negate_b ? "-" : "";
- std::string op_c = instr.ffma.negate_c ? "-" : "";
-
- UNIMPLEMENTED_IF_MSG(instr.ffma.cc != 0, "FFMA cc not implemented");
- UNIMPLEMENTED_IF_MSG(
- instr.ffma.tab5980_0 != 1, "FFMA tab5980_0({}) not implemented",
- instr.ffma.tab5980_0.Value()); // Seems to be 1 by default based on SMO
- UNIMPLEMENTED_IF_MSG(instr.ffma.tab5980_1 != 0, "FFMA tab5980_1({}) not implemented",
- instr.ffma.tab5980_1.Value());
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in FFMA is not implemented");
-
- switch (opcode->get().GetId()) {
- case OpCode::Id::FFMA_CR: {
- op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::Float);
- op_c += regs.GetRegisterAsFloat(instr.gpr39);
- break;
- }
- case OpCode::Id::FFMA_RR: {
- op_b += regs.GetRegisterAsFloat(instr.gpr20);
- op_c += regs.GetRegisterAsFloat(instr.gpr39);
- break;
- }
- case OpCode::Id::FFMA_RC: {
- op_b += regs.GetRegisterAsFloat(instr.gpr39);
- op_c += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::Float);
- break;
- }
- case OpCode::Id::FFMA_IMM: {
- op_b += GetImmediate19(instr);
- op_c += regs.GetRegisterAsFloat(instr.gpr39);
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled FFMA instruction: {}", opcode->get().GetName());
- }
- }
+ } else if (const auto lmem = std::get_if<LmemNode>(dest)) {
+ target = GetLocalMemory() + "[ftou(" + Visit(lmem->GetAddress()) + ") / 4]";
- regs.SetRegisterToFloat(instr.gpr0, 0, "fma(" + op_a + ", " + op_b + ", " + op_c + ')',
- 1, 1, instr.alu.saturate_d, 0, true);
- break;
+ } else {
+ UNREACHABLE_MSG("Assign called without a proper target");
}
- case OpCode::Type::Hfma2: {
- if (opcode->get().GetId() == OpCode::Id::HFMA2_RR) {
- UNIMPLEMENTED_IF(instr.hfma2.rr.precision != Tegra::Shader::HalfPrecision::None);
- } else {
- UNIMPLEMENTED_IF(instr.hfma2.precision != Tegra::Shader::HalfPrecision::None);
- }
- const bool saturate = opcode->get().GetId() == OpCode::Id::HFMA2_RR
- ? instr.hfma2.rr.saturate != 0
- : instr.hfma2.saturate != 0;
-
- const std::string op_a =
- GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr8, 0, false), instr.hfma2.type_a);
- std::string op_b, op_c;
-
- switch (opcode->get().GetId()) {
- case OpCode::Id::HFMA2_CR:
- op_b = GetHalfFloat(regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::UnsignedInteger),
- instr.hfma2.type_b, false, instr.hfma2.negate_b);
- op_c = GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr39, 0, false),
- instr.hfma2.type_reg39, false, instr.hfma2.negate_c);
- break;
- case OpCode::Id::HFMA2_RC:
- op_b = GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr39, 0, false),
- instr.hfma2.type_reg39, false, instr.hfma2.negate_b);
- op_c = GetHalfFloat(regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::UnsignedInteger),
- instr.hfma2.type_b, false, instr.hfma2.negate_c);
- break;
- case OpCode::Id::HFMA2_RR:
- op_b = GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr20, 0, false),
- instr.hfma2.type_b, false, instr.hfma2.negate_b);
- op_c = GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr39, 0, false),
- instr.hfma2.rr.type_c, false, instr.hfma2.rr.negate_c);
- break;
- case OpCode::Id::HFMA2_IMM_R:
- op_b = UnpackHalfImmediate(instr, true);
- op_c = GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr39, 0, false),
- instr.hfma2.type_reg39, false, instr.hfma2.negate_c);
- break;
- default:
- UNREACHABLE();
- op_c = op_b = "vec2(0)";
- break;
- }
- const std::string result = '(' + op_a + " * " + op_b + " + " + op_c + ')';
+ code.AddLine(target + " = " + Visit(src) + ';');
+ return {};
+ }
- regs.SetRegisterToHalfFloat(instr.gpr0, 0, result, instr.hfma2.merge, 1, 1, saturate);
- break;
+ std::string Composite(Operation operation) {
+ std::string value = "vec4(";
+ for (std::size_t i = 0; i < 4; ++i) {
+ value += Visit(operation[i]);
+ if (i < 3)
+ value += ", ";
}
- case OpCode::Type::Conversion: {
- switch (opcode->get().GetId()) {
- case OpCode::Id::I2I_R: {
- UNIMPLEMENTED_IF(instr.conversion.selector);
+ value += ')';
+ return value;
+ }
- std::string op_a = regs.GetRegisterAsInteger(
- instr.gpr20, 0, instr.conversion.is_input_signed, instr.conversion.src_size);
+ template <Type type>
+ std::string Add(Operation operation) {
+ return GenerateBinaryInfix(operation, "+", type, type, type);
+ }
- if (instr.conversion.abs_a) {
- op_a = "abs(" + op_a + ')';
- }
+ template <Type type>
+ std::string Mul(Operation operation) {
+ return GenerateBinaryInfix(operation, "*", type, type, type);
+ }
- if (instr.conversion.negate_a) {
- op_a = "-(" + op_a + ')';
- }
+ template <Type type>
+ std::string Div(Operation operation) {
+ return GenerateBinaryInfix(operation, "/", type, type, type);
+ }
- regs.SetRegisterToInteger(instr.gpr0, instr.conversion.is_output_signed, 0, op_a, 1,
- 1, instr.alu.saturate_d, 0, instr.conversion.dest_size,
- instr.generates_cc.Value() != 0);
- break;
- }
- case OpCode::Id::I2F_R:
- case OpCode::Id::I2F_C: {
- UNIMPLEMENTED_IF(instr.conversion.dest_size != Register::Size::Word);
- UNIMPLEMENTED_IF(instr.conversion.selector);
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in I2F is not implemented");
- std::string op_a;
-
- if (instr.is_b_gpr) {
- op_a =
- regs.GetRegisterAsInteger(instr.gpr20, 0, instr.conversion.is_input_signed,
- instr.conversion.src_size);
- } else {
- op_a = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- instr.conversion.is_input_signed
- ? GLSLRegister::Type::Integer
- : GLSLRegister::Type::UnsignedInteger,
- instr.conversion.src_size);
- }
+ template <Type type>
+ std::string Fma(Operation operation) {
+ return GenerateTernary(operation, "fma", type, type, type, type);
+ }
- if (instr.conversion.abs_a) {
- op_a = "abs(" + op_a + ')';
- }
+ template <Type type>
+ std::string Negate(Operation operation) {
+ return GenerateUnary(operation, "-", type, type, true);
+ }
- if (instr.conversion.negate_a) {
- op_a = "-(" + op_a + ')';
- }
+ template <Type type>
+ std::string Absolute(Operation operation) {
+ return GenerateUnary(operation, "abs", type, type, false);
+ }
- regs.SetRegisterToFloat(instr.gpr0, 0, op_a, 1, 1);
- break;
- }
- case OpCode::Id::F2F_R: {
- UNIMPLEMENTED_IF(instr.conversion.dest_size != Register::Size::Word);
- UNIMPLEMENTED_IF(instr.conversion.src_size != Register::Size::Word);
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in F2F is not implemented");
- std::string op_a = regs.GetRegisterAsFloat(instr.gpr20);
-
- if (instr.conversion.abs_a) {
- op_a = "abs(" + op_a + ')';
- }
+ std::string FClamp(Operation operation) {
+ return GenerateTernary(operation, "clamp", Type::Float, Type::Float, Type::Float,
+ Type::Float);
+ }
- if (instr.conversion.negate_a) {
- op_a = "-(" + op_a + ')';
- }
+ template <Type type>
+ std::string Min(Operation operation) {
+ return GenerateBinaryCall(operation, "min", type, type, type);
+ }
- switch (instr.conversion.f2f.rounding) {
- case Tegra::Shader::F2fRoundingOp::None:
- break;
- case Tegra::Shader::F2fRoundingOp::Round:
- op_a = "roundEven(" + op_a + ')';
- break;
- case Tegra::Shader::F2fRoundingOp::Floor:
- op_a = "floor(" + op_a + ')';
- break;
- case Tegra::Shader::F2fRoundingOp::Ceil:
- op_a = "ceil(" + op_a + ')';
- break;
- case Tegra::Shader::F2fRoundingOp::Trunc:
- op_a = "trunc(" + op_a + ')';
- break;
- default:
- UNIMPLEMENTED_MSG("Unimplemented F2F rounding mode {}",
- static_cast<u32>(instr.conversion.f2f.rounding.Value()));
- break;
- }
+ template <Type type>
+ std::string Max(Operation operation) {
+ return GenerateBinaryCall(operation, "max", type, type, type);
+ }
- regs.SetRegisterToFloat(instr.gpr0, 0, op_a, 1, 1, instr.alu.saturate_d);
- break;
- }
- case OpCode::Id::F2I_R:
- case OpCode::Id::F2I_C: {
- UNIMPLEMENTED_IF(instr.conversion.src_size != Register::Size::Word);
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in F2I is not implemented");
- std::string op_a{};
-
- if (instr.is_b_gpr) {
- op_a = regs.GetRegisterAsFloat(instr.gpr20);
- } else {
- op_a = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::Float);
- }
+ std::string Select(Operation operation) {
+ const std::string condition = Visit(operation[0]);
+ const std::string true_case = Visit(operation[1]);
+ const std::string false_case = Visit(operation[2]);
+ return ApplyPrecise(operation,
+ '(' + condition + " ? " + true_case + " : " + false_case + ')');
+ }
- if (instr.conversion.abs_a) {
- op_a = "abs(" + op_a + ')';
- }
+ std::string FCos(Operation operation) {
+ return GenerateUnary(operation, "cos", Type::Float, Type::Float, false);
+ }
- if (instr.conversion.negate_a) {
- op_a = "-(" + op_a + ')';
- }
+ std::string FSin(Operation operation) {
+ return GenerateUnary(operation, "sin", Type::Float, Type::Float, false);
+ }
- switch (instr.conversion.f2i.rounding) {
- case Tegra::Shader::F2iRoundingOp::None:
- break;
- case Tegra::Shader::F2iRoundingOp::Floor:
- op_a = "floor(" + op_a + ')';
- break;
- case Tegra::Shader::F2iRoundingOp::Ceil:
- op_a = "ceil(" + op_a + ')';
- break;
- case Tegra::Shader::F2iRoundingOp::Trunc:
- op_a = "trunc(" + op_a + ')';
- break;
- default:
- UNIMPLEMENTED_MSG("Unimplemented F2I rounding mode {}",
- static_cast<u32>(instr.conversion.f2i.rounding.Value()));
- break;
- }
+ std::string FExp2(Operation operation) {
+ return GenerateUnary(operation, "exp2", Type::Float, Type::Float, false);
+ }
- if (instr.conversion.is_output_signed) {
- op_a = "int(" + op_a + ')';
- } else {
- op_a = "uint(" + op_a + ')';
- }
+ std::string FLog2(Operation operation) {
+ return GenerateUnary(operation, "log2", Type::Float, Type::Float, false);
+ }
- regs.SetRegisterToInteger(instr.gpr0, instr.conversion.is_output_signed, 0, op_a, 1,
- 1, false, 0, instr.conversion.dest_size);
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled conversion instruction: {}", opcode->get().GetName());
- }
- }
- break;
- }
- case OpCode::Type::Memory: {
- switch (opcode->get().GetId()) {
- case OpCode::Id::LD_A: {
- // Note: Shouldn't this be interp mode flat? As in no interpolation made.
- UNIMPLEMENTED_IF_MSG(instr.gpr8.Value() != Register::ZeroIndex,
- "Indirect attribute loads are not supported");
- UNIMPLEMENTED_IF_MSG((instr.attribute.fmt20.immediate.Value() % sizeof(u32)) != 0,
- "Unaligned attribute loads are not supported");
-
- Tegra::Shader::IpaMode input_mode{Tegra::Shader::IpaInterpMode::Perspective,
- Tegra::Shader::IpaSampleMode::Default};
-
- u64 next_element = instr.attribute.fmt20.element;
- u64 next_index = static_cast<u64>(instr.attribute.fmt20.index.Value());
-
- const auto LoadNextElement = [&](u32 reg_offset) {
- regs.SetRegisterToInputAttibute(instr.gpr0.Value() + reg_offset, next_element,
- static_cast<Attribute::Index>(next_index),
- input_mode, instr.gpr39.Value());
-
- // Load the next attribute element into the following register. If the element
- // to load goes beyond the vec4 size, load the first element of the next
- // attribute.
- next_element = (next_element + 1) % 4;
- next_index = next_index + (next_element == 0 ? 1 : 0);
- };
-
- const u32 num_words = static_cast<u32>(instr.attribute.fmt20.size.Value()) + 1;
- for (u32 reg_offset = 0; reg_offset < num_words; ++reg_offset) {
- LoadNextElement(reg_offset);
- }
- break;
- }
- case OpCode::Id::LD_C: {
- UNIMPLEMENTED_IF(instr.ld_c.unknown != 0);
-
- const auto scope = shader.Scope();
-
- shader.AddLine("uint index = (" + regs.GetRegisterAsInteger(instr.gpr8, 0, false) +
- " / 4) & (MAX_CONSTBUFFER_ELEMENTS - 1);");
-
- const std::string op_a =
- regs.GetUniformIndirect(instr.cbuf36.index, instr.cbuf36.offset + 0, "index",
- GLSLRegister::Type::Float);
-
- switch (instr.ld_c.type.Value()) {
- case Tegra::Shader::UniformType::Single:
- regs.SetRegisterToFloat(instr.gpr0, 0, op_a, 1, 1);
- break;
-
- case Tegra::Shader::UniformType::Double: {
- const std::string op_b =
- regs.GetUniformIndirect(instr.cbuf36.index, instr.cbuf36.offset + 4,
- "index", GLSLRegister::Type::Float);
- regs.SetRegisterToFloat(instr.gpr0, 0, op_a, 1, 1);
- regs.SetRegisterToFloat(instr.gpr0.Value() + 1, 0, op_b, 1, 1);
- break;
- }
- default:
- UNIMPLEMENTED_MSG("Unhandled type: {}",
- static_cast<unsigned>(instr.ld_c.type.Value()));
- }
- break;
- }
- case OpCode::Id::LD_L: {
- UNIMPLEMENTED_IF_MSG(instr.ld_l.unknown == 1, "LD_L Unhandled mode: {}",
- static_cast<unsigned>(instr.ld_l.unknown.Value()));
+ std::string FInverseSqrt(Operation operation) {
+ return GenerateUnary(operation, "inversesqrt", Type::Float, Type::Float, false);
+ }
- const auto scope = shader.Scope();
+ std::string FSqrt(Operation operation) {
+ return GenerateUnary(operation, "sqrt", Type::Float, Type::Float, false);
+ }
- std::string op = '(' + regs.GetRegisterAsInteger(instr.gpr8, 0, false) + " + " +
- std::to_string(instr.smem_imm.Value()) + ')';
+ std::string FRoundEven(Operation operation) {
+ return GenerateUnary(operation, "roundEven", Type::Float, Type::Float, false);
+ }
- shader.AddLine("uint index = (" + op + " / 4);");
+ std::string FFloor(Operation operation) {
+ return GenerateUnary(operation, "floor", Type::Float, Type::Float, false);
+ }
- const std::string op_a = regs.GetLocalMemoryAsFloat("index");
+ std::string FCeil(Operation operation) {
+ return GenerateUnary(operation, "ceil", Type::Float, Type::Float, false);
+ }
- switch (instr.ldst_sl.type.Value()) {
- case Tegra::Shader::StoreType::Bytes32:
- regs.SetRegisterToFloat(instr.gpr0, 0, op_a, 1, 1);
- break;
- default:
- UNIMPLEMENTED_MSG("LD_L Unhandled type: {}",
- static_cast<unsigned>(instr.ldst_sl.type.Value()));
- }
- break;
- }
- case OpCode::Id::ST_A: {
- UNIMPLEMENTED_IF_MSG(instr.gpr8.Value() != Register::ZeroIndex,
- "Indirect attribute loads are not supported");
- UNIMPLEMENTED_IF_MSG((instr.attribute.fmt20.immediate.Value() % sizeof(u32)) != 0,
- "Unaligned attribute loads are not supported");
-
- u64 next_element = instr.attribute.fmt20.element;
- u64 next_index = static_cast<u64>(instr.attribute.fmt20.index.Value());
-
- const auto StoreNextElement = [&](u32 reg_offset) {
- regs.SetOutputAttributeToRegister(static_cast<Attribute::Index>(next_index),
- next_element, instr.gpr0.Value() + reg_offset,
- instr.gpr39.Value());
-
- // Load the next attribute element into the following register. If the element
- // to load goes beyond the vec4 size, load the first element of the next
- // attribute.
- next_element = (next_element + 1) % 4;
- next_index = next_index + (next_element == 0 ? 1 : 0);
- };
-
- const u32 num_words = static_cast<u32>(instr.attribute.fmt20.size.Value()) + 1;
- for (u32 reg_offset = 0; reg_offset < num_words; ++reg_offset) {
- StoreNextElement(reg_offset);
- }
+ std::string FTrunc(Operation operation) {
+ return GenerateUnary(operation, "trunc", Type::Float, Type::Float, false);
+ }
- break;
- }
- case OpCode::Id::ST_L: {
- UNIMPLEMENTED_IF_MSG(instr.st_l.unknown == 0, "ST_L Unhandled mode: {}",
- static_cast<unsigned>(instr.st_l.unknown.Value()));
+ template <Type type>
+ std::string FCastInteger(Operation operation) {
+ return GenerateUnary(operation, "float", Type::Float, type, false);
+ }
- const auto scope = shader.Scope();
+ std::string ICastFloat(Operation operation) {
+ return GenerateUnary(operation, "int", Type::Int, Type::Float, false);
+ }
- std::string op = '(' + regs.GetRegisterAsInteger(instr.gpr8, 0, false) + " + " +
- std::to_string(instr.smem_imm.Value()) + ')';
+ std::string ICastUnsigned(Operation operation) {
+ return GenerateUnary(operation, "int", Type::Int, Type::Uint, false);
+ }
- shader.AddLine("uint index = (" + op + " / 4);");
+ template <Type type>
+ std::string LogicalShiftLeft(Operation operation) {
+ return GenerateBinaryInfix(operation, "<<", type, type, Type::Uint);
+ }
- switch (instr.ldst_sl.type.Value()) {
- case Tegra::Shader::StoreType::Bytes32:
- regs.SetLocalMemoryAsFloat("index", regs.GetRegisterAsFloat(instr.gpr0));
- break;
- default:
- UNIMPLEMENTED_MSG("ST_L Unhandled type: {}",
- static_cast<unsigned>(instr.ldst_sl.type.Value()));
- }
- break;
- }
- case OpCode::Id::TEX: {
- Tegra::Shader::TextureType texture_type{instr.tex.texture_type};
- const bool is_array = instr.tex.array != 0;
- const bool depth_compare =
- instr.tex.UsesMiscMode(Tegra::Shader::TextureMiscMode::DC);
- const auto process_mode = instr.tex.GetTextureProcessMode();
- UNIMPLEMENTED_IF_MSG(instr.tex.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
- "NODEP is not implemented");
- UNIMPLEMENTED_IF_MSG(instr.tex.UsesMiscMode(Tegra::Shader::TextureMiscMode::AOFFI),
- "AOFFI is not implemented");
-
- const auto [coord, texture] =
- GetTEXCode(instr, texture_type, process_mode, depth_compare, is_array);
-
- const auto scope = shader.Scope();
- shader.AddLine(coord);
-
- if (depth_compare) {
- regs.SetRegisterToFloat(instr.gpr0, 0, texture, 1, 1, false);
- } else {
- shader.AddLine("vec4 texture_tmp = " + texture + ';');
- std::size_t dest_elem{};
- for (std::size_t elem = 0; elem < 4; ++elem) {
- if (!instr.tex.IsComponentEnabled(elem)) {
- // Skip disabled components
- continue;
- }
- regs.SetRegisterToFloat(instr.gpr0, elem, "texture_tmp", 1, 4, false,
- dest_elem);
- ++dest_elem;
- }
- }
- break;
- }
- case OpCode::Id::TEXS: {
- Tegra::Shader::TextureType texture_type{instr.texs.GetTextureType()};
- const bool is_array{instr.texs.IsArrayTexture()};
- const bool depth_compare =
- instr.texs.UsesMiscMode(Tegra::Shader::TextureMiscMode::DC);
- const auto process_mode = instr.texs.GetTextureProcessMode();
+ std::string ILogicalShiftRight(Operation operation) {
+ const std::string op_a = VisitOperand(operation, 0, Type::Uint);
+ const std::string op_b = VisitOperand(operation, 1, Type::Uint);
- UNIMPLEMENTED_IF_MSG(instr.texs.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
- "NODEP is not implemented");
+ return ApplyPrecise(operation,
+ BitwiseCastResult("int(" + op_a + " >> " + op_b + ')', Type::Int));
+ }
- const auto scope = shader.Scope();
+ std::string IArithmeticShiftRight(Operation operation) {
+ return GenerateBinaryInfix(operation, ">>", Type::Int, Type::Int, Type::Uint);
+ }
- auto [coord, texture] =
- GetTEXSCode(instr, texture_type, process_mode, depth_compare, is_array);
+ template <Type type>
+ std::string BitwiseAnd(Operation operation) {
+ return GenerateBinaryInfix(operation, "&", type, type, type);
+ }
- shader.AddLine(coord);
+ template <Type type>
+ std::string BitwiseOr(Operation operation) {
+ return GenerateBinaryInfix(operation, "|", type, type, type);
+ }
- if (depth_compare) {
- texture = "vec4(" + texture + ')';
- }
- shader.AddLine("vec4 texture_tmp = " + texture + ';');
+ template <Type type>
+ std::string BitwiseXor(Operation operation) {
+ return GenerateBinaryInfix(operation, "^", type, type, type);
+ }
- if (instr.texs.fp32_flag) {
- WriteTexsInstructionFloat(instr, "texture_tmp");
- } else {
- WriteTexsInstructionHalfFloat(instr, "texture_tmp");
- }
- break;
- }
- case OpCode::Id::TLDS: {
- const Tegra::Shader::TextureType texture_type{instr.tlds.GetTextureType()};
- const bool is_array{instr.tlds.IsArrayTexture()};
+ template <Type type>
+ std::string BitwiseNot(Operation operation) {
+ return GenerateUnary(operation, "~", type, type, false);
+ }
- ASSERT(texture_type == Tegra::Shader::TextureType::Texture2D);
- ASSERT(is_array == false);
+ std::string UCastFloat(Operation operation) {
+ return GenerateUnary(operation, "uint", Type::Uint, Type::Float, false);
+ }
- UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
- "NODEP is not implemented");
- UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(Tegra::Shader::TextureMiscMode::AOFFI),
- "AOFFI is not implemented");
- UNIMPLEMENTED_IF_MSG(instr.tlds.UsesMiscMode(Tegra::Shader::TextureMiscMode::MZ),
- "MZ is not implemented");
+ std::string UCastSigned(Operation operation) {
+ return GenerateUnary(operation, "uint", Type::Uint, Type::Int, false);
+ }
- u32 extra_op_offset = 0;
+ std::string UShiftRight(Operation operation) {
+ return GenerateBinaryInfix(operation, ">>", Type::Uint, Type::Uint, Type::Uint);
+ }
- ShaderScopedScope scope = shader.Scope();
+ template <Type type>
+ std::string BitfieldInsert(Operation operation) {
+ return GenerateQuaternary(operation, "bitfieldInsert", type, type, type, Type::Int,
+ Type::Int);
+ }
- switch (texture_type) {
- case Tegra::Shader::TextureType::Texture1D: {
- const std::string x = regs.GetRegisterAsInteger(instr.gpr8);
- shader.AddLine("float coords = " + x + ';');
- break;
- }
- case Tegra::Shader::TextureType::Texture2D: {
- UNIMPLEMENTED_IF_MSG(is_array, "Unhandled 2d array texture");
-
- const std::string x = regs.GetRegisterAsInteger(instr.gpr8);
- const std::string y = regs.GetRegisterAsInteger(instr.gpr20);
- // shader.AddLine("ivec2 coords = ivec2(" + x + ", " + y + ");");
- shader.AddLine("ivec2 coords = ivec2(" + x + ", " + y + ");");
- extra_op_offset = 1;
- break;
- }
- default:
- UNIMPLEMENTED_MSG("Unhandled texture type {}", static_cast<u32>(texture_type));
- }
- const std::string sampler =
- GetSampler(instr.sampler, texture_type, is_array, false);
-
- const std::string texture = [&]() {
- switch (instr.tlds.GetTextureProcessMode()) {
- case Tegra::Shader::TextureProcessMode::LZ:
- return "texelFetch(" + sampler + ", coords, 0)";
- case Tegra::Shader::TextureProcessMode::LL:
- shader.AddLine(
- "float lod = " +
- regs.GetRegisterAsInteger(instr.gpr20.Value() + extra_op_offset) + ';');
- return "texelFetch(" + sampler + ", coords, lod)";
- default:
- UNIMPLEMENTED_MSG("Unhandled texture process mode {}",
- static_cast<u32>(instr.tlds.GetTextureProcessMode()));
- return "texelFetch(" + sampler + ", coords, 0)";
- }
- }();
+ template <Type type>
+ std::string BitfieldExtract(Operation operation) {
+ return GenerateTernary(operation, "bitfieldExtract", type, type, Type::Int, Type::Int);
+ }
- WriteTexsInstructionFloat(instr, texture);
- break;
- }
- case OpCode::Id::TLD4: {
- ASSERT(instr.tld4.texture_type == Tegra::Shader::TextureType::Texture2D);
- ASSERT(instr.tld4.array == 0);
-
- UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
- "NODEP is not implemented");
- UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(Tegra::Shader::TextureMiscMode::AOFFI),
- "AOFFI is not implemented");
- UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(Tegra::Shader::TextureMiscMode::NDV),
- "NDV is not implemented");
- UNIMPLEMENTED_IF_MSG(instr.tld4.UsesMiscMode(Tegra::Shader::TextureMiscMode::PTP),
- "PTP is not implemented");
- const bool depth_compare =
- instr.tld4.UsesMiscMode(Tegra::Shader::TextureMiscMode::DC);
- auto texture_type = instr.tld4.texture_type.Value();
- u32 num_coordinates = TextureCoordinates(texture_type);
- if (depth_compare)
- num_coordinates += 1;
-
- const auto scope = shader.Scope();
-
- switch (num_coordinates) {
- case 2: {
- const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
- const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
- shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
- break;
- }
- case 3: {
- const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
- const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
- const std::string z = regs.GetRegisterAsFloat(instr.gpr8.Value() + 2);
- shader.AddLine("vec3 coords = vec3(" + x + ", " + y + ", " + z + ");");
- break;
- }
- default:
- UNIMPLEMENTED_MSG("Unhandled coordinates number {}",
- static_cast<u32>(num_coordinates));
- const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
- const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
- shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
- texture_type = Tegra::Shader::TextureType::Texture2D;
- }
+ template <Type type>
+ std::string BitCount(Operation operation) {
+ return GenerateUnary(operation, "bitCount", type, type, false);
+ }
- const std::string sampler =
- GetSampler(instr.sampler, texture_type, false, depth_compare);
+ std::string HNegate(Operation operation) {
+ const auto GetNegate = [&](std::size_t index) -> std::string {
+ return VisitOperand(operation, index, Type::Bool) + " ? -1 : 1";
+ };
+ const std::string value = '(' + VisitOperand(operation, 0, Type::HalfFloat) + " * vec2(" +
+ GetNegate(1) + ", " + GetNegate(2) + "))";
+ return BitwiseCastResult(value, Type::HalfFloat);
+ }
- const std::string texture = "textureGather(" + sampler + ", coords, " +
- std::to_string(instr.tld4.component) + ')';
+ std::string HMergeF32(Operation operation) {
+ return "float(toHalf2(" + Visit(operation[0]) + ")[0])";
+ }
- if (depth_compare) {
- regs.SetRegisterToFloat(instr.gpr0, 0, texture, 1, 1, false);
- } else {
- std::size_t dest_elem{};
- for (std::size_t elem = 0; elem < 4; ++elem) {
- if (!instr.tex.IsComponentEnabled(elem)) {
- // Skip disabled components
- continue;
- }
- regs.SetRegisterToFloat(instr.gpr0, elem, texture, 1, 4, false, dest_elem);
- ++dest_elem;
- }
- }
- break;
- }
- case OpCode::Id::TLD4S: {
- UNIMPLEMENTED_IF_MSG(
- instr.tld4s.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
- "NODEP is not implemented");
- UNIMPLEMENTED_IF_MSG(
- instr.tld4s.UsesMiscMode(Tegra::Shader::TextureMiscMode::AOFFI),
- "AOFFI is not implemented");
-
- const auto scope = shader.Scope();
-
- const bool depth_compare =
- instr.tld4s.UsesMiscMode(Tegra::Shader::TextureMiscMode::DC);
- const std::string op_a = regs.GetRegisterAsFloat(instr.gpr8);
- const std::string op_b = regs.GetRegisterAsFloat(instr.gpr20);
- // TODO(Subv): Figure out how the sampler type is encoded in the TLD4S instruction.
- const std::string sampler = GetSampler(
- instr.sampler, Tegra::Shader::TextureType::Texture2D, false, depth_compare);
- if (depth_compare) {
- // Note: TLD4S coordinate encoding works just like TEXS's
- const std::string op_y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
- shader.AddLine("vec3 coords = vec3(" + op_a + ", " + op_y + ", " + op_b + ");");
- } else {
- shader.AddLine("vec2 coords = vec2(" + op_a + ", " + op_b + ");");
- }
+ std::string HMergeH0(Operation operation) {
+ return "fromHalf2(vec2(toHalf2(" + Visit(operation[0]) + ")[1], toHalf2(" +
+ Visit(operation[1]) + ")[0]))";
+ }
- std::string texture = "textureGather(" + sampler + ", coords, " +
- std::to_string(instr.tld4s.component) + ')';
- if (depth_compare) {
- texture = "vec4(" + texture + ')';
- }
+ std::string HMergeH1(Operation operation) {
+ return "fromHalf2(vec2(toHalf2(" + Visit(operation[0]) + ")[0], toHalf2(" +
+ Visit(operation[1]) + ")[1]))";
+ }
- WriteTexsInstructionFloat(instr, texture);
- break;
- }
- case OpCode::Id::TXQ: {
- UNIMPLEMENTED_IF_MSG(instr.txq.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
- "NODEP is not implemented");
-
- const auto scope = shader.Scope();
-
- // TODO: The new commits on the texture refactor, change the way samplers work.
- // Sadly, not all texture instructions specify the type of texture their sampler
- // uses. This must be fixed at a later instance.
- const std::string sampler =
- GetSampler(instr.sampler, Tegra::Shader::TextureType::Texture2D, false, false);
- switch (instr.txq.query_type) {
- case Tegra::Shader::TextureQueryType::Dimension: {
- const std::string texture = "textureSize(" + sampler + ", " +
- regs.GetRegisterAsInteger(instr.gpr8) + ')';
- const std::string mip_level = "textureQueryLevels(" + sampler + ')';
- shader.AddLine("ivec2 sizes = " + texture + ';');
-
- regs.SetRegisterToInteger(instr.gpr0.Value() + 0, true, 0, "sizes.x", 1, 1);
- regs.SetRegisterToInteger(instr.gpr0.Value() + 1, true, 0, "sizes.y", 1, 1);
- regs.SetRegisterToInteger(instr.gpr0.Value() + 2, true, 0, "0", 1, 1);
- regs.SetRegisterToInteger(instr.gpr0.Value() + 3, true, 0, mip_level, 1, 1);
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled texture query type: {}",
- static_cast<u32>(instr.txq.query_type.Value()));
- }
- }
- break;
- }
- case OpCode::Id::TMML: {
- UNIMPLEMENTED_IF_MSG(instr.tmml.UsesMiscMode(Tegra::Shader::TextureMiscMode::NODEP),
- "NODEP is not implemented");
- UNIMPLEMENTED_IF_MSG(instr.tmml.UsesMiscMode(Tegra::Shader::TextureMiscMode::NDV),
- "NDV is not implemented");
-
- const std::string x = regs.GetRegisterAsFloat(instr.gpr8);
- const bool is_array = instr.tmml.array != 0;
- auto texture_type = instr.tmml.texture_type.Value();
- const std::string sampler =
- GetSampler(instr.sampler, texture_type, is_array, false);
-
- const auto scope = shader.Scope();
-
- // TODO: Add coordinates for different samplers once other texture types are
- // implemented.
- switch (texture_type) {
- case Tegra::Shader::TextureType::Texture1D: {
- shader.AddLine("float coords = " + x + ';');
- break;
- }
- case Tegra::Shader::TextureType::Texture2D: {
- const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
- shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
- break;
- }
- default:
- UNIMPLEMENTED_MSG("Unhandled texture type {}", static_cast<u32>(texture_type));
+ std::string HPack2(Operation operation) {
+ return "utof(packHalf2x16(vec2(" + Visit(operation[0]) + ", " + Visit(operation[1]) + ")))";
+ }
- // Fallback to interpreting as a 2D texture for now
- const std::string y = regs.GetRegisterAsFloat(instr.gpr8.Value() + 1);
- shader.AddLine("vec2 coords = vec2(" + x + ", " + y + ");");
- texture_type = Tegra::Shader::TextureType::Texture2D;
- }
+ template <Type type>
+ std::string LogicalLessThan(Operation operation) {
+ return GenerateBinaryInfix(operation, "<", Type::Bool, type, type);
+ }
- const std::string texture = "textureQueryLod(" + sampler + ", coords)";
- shader.AddLine("vec2 tmp = " + texture + " * vec2(256.0, 256.0);");
+ template <Type type>
+ std::string LogicalEqual(Operation operation) {
+ return GenerateBinaryInfix(operation, "==", Type::Bool, type, type);
+ }
- regs.SetRegisterToInteger(instr.gpr0, true, 0, "int(tmp.y)", 1, 1);
- regs.SetRegisterToInteger(instr.gpr0.Value() + 1, false, 0, "uint(tmp.x)", 1, 1);
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled memory instruction: {}", opcode->get().GetName());
- }
- }
- break;
- }
- case OpCode::Type::FloatSetPredicate: {
- const std::string op_a =
- GetOperandAbsNeg(regs.GetRegisterAsFloat(instr.gpr8), instr.fsetp.abs_a != 0,
- instr.fsetp.neg_a != 0);
+ template <Type type>
+ std::string LogicalLessEqual(Operation operation) {
+ return GenerateBinaryInfix(operation, "<=", Type::Bool, type, type);
+ }
- std::string op_b;
+ template <Type type>
+ std::string LogicalGreaterThan(Operation operation) {
+ return GenerateBinaryInfix(operation, ">", Type::Bool, type, type);
+ }
- if (instr.is_b_imm) {
- op_b += '(' + GetImmediate19(instr) + ')';
- } else {
- if (instr.is_b_gpr) {
- op_b += regs.GetRegisterAsFloat(instr.gpr20);
- } else {
- op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::Float);
- }
- }
+ template <Type type>
+ std::string LogicalNotEqual(Operation operation) {
+ return GenerateBinaryInfix(operation, "!=", Type::Bool, type, type);
+ }
- if (instr.fsetp.abs_b) {
- op_b = "abs(" + op_b + ')';
- }
+ template <Type type>
+ std::string LogicalGreaterEqual(Operation operation) {
+ return GenerateBinaryInfix(operation, ">=", Type::Bool, type, type);
+ }
- // We can't use the constant predicate as destination.
- ASSERT(instr.fsetp.pred3 != static_cast<u64>(Pred::UnusedIndex));
+ std::string LogicalFIsNan(Operation operation) {
+ return GenerateUnary(operation, "isnan", Type::Bool, Type::Float, false);
+ }
- const std::string second_pred =
- GetPredicateCondition(instr.fsetp.pred39, instr.fsetp.neg_pred != 0);
+ std::string LogicalAssign(Operation operation) {
+ const Node dest = operation[0];
+ const Node src = operation[1];
- const std::string combiner = GetPredicateCombiner(instr.fsetp.op);
+ std::string target;
- const std::string predicate = GetPredicateComparison(instr.fsetp.cond, op_a, op_b);
- // Set the primary predicate to the result of Predicate OP SecondPredicate
- SetPredicate(instr.fsetp.pred3,
- '(' + predicate + ") " + combiner + " (" + second_pred + ')');
+ if (const auto pred = std::get_if<PredicateNode>(dest)) {
+ ASSERT_MSG(!pred->IsNegated(), "Negating logical assignment");
- if (instr.fsetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
- // Set the secondary predicate to the result of !Predicate OP SecondPredicate,
- // if enabled
- SetPredicate(instr.fsetp.pred0,
- "!(" + predicate + ") " + combiner + " (" + second_pred + ')');
+ const auto index = pred->GetIndex();
+ switch (index) {
+ case Tegra::Shader::Pred::NeverExecute:
+ case Tegra::Shader::Pred::UnusedIndex:
+ // Writing to these predicates is a no-op
+ return {};
}
- break;
+ target = GetPredicate(index);
+ } else if (const auto flag = std::get_if<InternalFlagNode>(dest)) {
+ target = GetInternalFlag(flag->GetFlag());
}
- case OpCode::Type::IntegerSetPredicate: {
- const std::string op_a =
- regs.GetRegisterAsInteger(instr.gpr8, 0, instr.isetp.is_signed);
- std::string op_b;
- if (instr.is_b_imm) {
- op_b += '(' + std::to_string(instr.alu.GetSignedImm20_20()) + ')';
- } else {
- if (instr.is_b_gpr) {
- op_b += regs.GetRegisterAsInteger(instr.gpr20, 0, instr.isetp.is_signed);
- } else {
- op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::Integer);
- }
- }
-
- // We can't use the constant predicate as destination.
- ASSERT(instr.isetp.pred3 != static_cast<u64>(Pred::UnusedIndex));
+ code.AddLine(target + " = " + Visit(src) + ';');
+ return {};
+ }
- const std::string second_pred =
- GetPredicateCondition(instr.isetp.pred39, instr.isetp.neg_pred != 0);
+ std::string LogicalAnd(Operation operation) {
+ return GenerateBinaryInfix(operation, "&&", Type::Bool, Type::Bool, Type::Bool);
+ }
- const std::string combiner = GetPredicateCombiner(instr.isetp.op);
+ std::string LogicalOr(Operation operation) {
+ return GenerateBinaryInfix(operation, "||", Type::Bool, Type::Bool, Type::Bool);
+ }
- const std::string predicate = GetPredicateComparison(instr.isetp.cond, op_a, op_b);
- // Set the primary predicate to the result of Predicate OP SecondPredicate
- SetPredicate(instr.isetp.pred3,
- '(' + predicate + ") " + combiner + " (" + second_pred + ')');
+ std::string LogicalXor(Operation operation) {
+ return GenerateBinaryInfix(operation, "^^", Type::Bool, Type::Bool, Type::Bool);
+ }
- if (instr.isetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
- // Set the secondary predicate to the result of !Predicate OP SecondPredicate,
- // if enabled
- SetPredicate(instr.isetp.pred0,
- "!(" + predicate + ") " + combiner + " (" + second_pred + ')');
- }
- break;
- }
- case OpCode::Type::HalfSetPredicate: {
- UNIMPLEMENTED_IF(instr.hsetp2.ftz != 0);
-
- const std::string op_a =
- GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr8, 0, false), instr.hsetp2.type_a,
- instr.hsetp2.abs_a, instr.hsetp2.negate_a);
-
- const std::string op_b = [&]() {
- switch (opcode->get().GetId()) {
- case OpCode::Id::HSETP2_R:
- return GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr20, 0, false),
- instr.hsetp2.type_b, instr.hsetp2.abs_a,
- instr.hsetp2.negate_b);
- default:
- UNREACHABLE();
- return std::string("vec2(0)");
- }
- }();
+ std::string LogicalNegate(Operation operation) {
+ return GenerateUnary(operation, "!", Type::Bool, Type::Bool, false);
+ }
- // We can't use the constant predicate as destination.
- ASSERT(instr.hsetp2.pred3 != static_cast<u64>(Pred::UnusedIndex));
+ std::string LogicalPick2(Operation operation) {
+ const std::string pair = VisitOperand(operation, 0, Type::Bool2);
+ return pair + '[' + VisitOperand(operation, 1, Type::Uint) + ']';
+ }
- const std::string second_pred =
- GetPredicateCondition(instr.hsetp2.pred39, instr.hsetp2.neg_pred != 0);
+ std::string LogicalAll2(Operation operation) {
+ return GenerateUnary(operation, "all", Type::Bool, Type::Bool2);
+ }
- const std::string combiner = GetPredicateCombiner(instr.hsetp2.op);
+ std::string LogicalAny2(Operation operation) {
+ return GenerateUnary(operation, "any", Type::Bool, Type::Bool2);
+ }
- const std::string component_combiner = instr.hsetp2.h_and ? "&&" : "||";
- const std::string predicate =
- '(' + GetPredicateComparison(instr.hsetp2.cond, op_a + ".x", op_b + ".x") + ' ' +
- component_combiner + ' ' +
- GetPredicateComparison(instr.hsetp2.cond, op_a + ".y", op_b + ".y") + ')';
+ std::string Logical2HLessThan(Operation operation) {
+ return GenerateBinaryCall(operation, "lessThan", Type::Bool2, Type::HalfFloat,
+ Type::HalfFloat);
+ }
- // Set the primary predicate to the result of Predicate OP SecondPredicate
- SetPredicate(instr.hsetp2.pred3,
- '(' + predicate + ") " + combiner + " (" + second_pred + ')');
+ std::string Logical2HEqual(Operation operation) {
+ return GenerateBinaryCall(operation, "equal", Type::Bool2, Type::HalfFloat,
+ Type::HalfFloat);
+ }
- if (instr.hsetp2.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
- // Set the secondary predicate to the result of !Predicate OP SecondPredicate,
- // if enabled
- SetPredicate(instr.hsetp2.pred0,
- "!(" + predicate + ") " + combiner + " (" + second_pred + ')');
- }
- break;
- }
- case OpCode::Type::PredicateSetRegister: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in PSET is not implemented");
-
- const std::string op_a =
- GetPredicateCondition(instr.pset.pred12, instr.pset.neg_pred12 != 0);
- const std::string op_b =
- GetPredicateCondition(instr.pset.pred29, instr.pset.neg_pred29 != 0);
-
- const std::string second_pred =
- GetPredicateCondition(instr.pset.pred39, instr.pset.neg_pred39 != 0);
-
- const std::string combiner = GetPredicateCombiner(instr.pset.op);
-
- const std::string predicate =
- '(' + op_a + ") " + GetPredicateCombiner(instr.pset.cond) + " (" + op_b + ')';
- const std::string result = '(' + predicate + ") " + combiner + " (" + second_pred + ')';
- if (instr.pset.bf == 0) {
- const std::string value = '(' + result + ") ? 0xFFFFFFFF : 0";
- regs.SetRegisterToInteger(instr.gpr0, false, 0, value, 1, 1);
- } else {
- const std::string value = '(' + result + ") ? 1.0 : 0.0";
- regs.SetRegisterToFloat(instr.gpr0, 0, value, 1, 1);
- }
- break;
- }
- case OpCode::Type::PredicateSetPredicate: {
- switch (opcode->get().GetId()) {
- case OpCode::Id::PSETP: {
- const std::string op_a =
- GetPredicateCondition(instr.psetp.pred12, instr.psetp.neg_pred12 != 0);
- const std::string op_b =
- GetPredicateCondition(instr.psetp.pred29, instr.psetp.neg_pred29 != 0);
-
- // We can't use the constant predicate as destination.
- ASSERT(instr.psetp.pred3 != static_cast<u64>(Pred::UnusedIndex));
-
- const std::string second_pred =
- GetPredicateCondition(instr.psetp.pred39, instr.psetp.neg_pred39 != 0);
-
- const std::string combiner = GetPredicateCombiner(instr.psetp.op);
-
- const std::string predicate =
- '(' + op_a + ") " + GetPredicateCombiner(instr.psetp.cond) + " (" + op_b + ')';
-
- // Set the primary predicate to the result of Predicate OP SecondPredicate
- SetPredicate(instr.psetp.pred3,
- '(' + predicate + ") " + combiner + " (" + second_pred + ')');
-
- if (instr.psetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
- // Set the secondary predicate to the result of !Predicate OP SecondPredicate,
- // if enabled
- SetPredicate(instr.psetp.pred0,
- "!(" + predicate + ") " + combiner + " (" + second_pred + ')');
- }
- break;
- }
- case OpCode::Id::CSETP: {
- const std::string pred =
- GetPredicateCondition(instr.csetp.pred39, instr.csetp.neg_pred39 != 0);
- const std::string combiner = GetPredicateCombiner(instr.csetp.op);
- const std::string condition_code = regs.GetConditionCode(instr.csetp.cc);
- if (instr.csetp.pred3 != static_cast<u64>(Pred::UnusedIndex)) {
- SetPredicate(instr.csetp.pred3,
- '(' + condition_code + ") " + combiner + " (" + pred + ')');
- }
- if (instr.csetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
- SetPredicate(instr.csetp.pred0,
- "!(" + condition_code + ") " + combiner + " (" + pred + ')');
- }
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled predicate instruction: {}", opcode->get().GetName());
- }
- }
- break;
- }
- case OpCode::Type::RegisterSetPredicate: {
- UNIMPLEMENTED_IF(instr.r2p.mode != Tegra::Shader::R2pMode::Pr);
+ std::string Logical2HLessEqual(Operation operation) {
+ return GenerateBinaryCall(operation, "lessThanEqual", Type::Bool2, Type::HalfFloat,
+ Type::HalfFloat);
+ }
- const std::string apply_mask = [&]() {
- switch (opcode->get().GetId()) {
- case OpCode::Id::R2P_IMM:
- return std::to_string(instr.r2p.immediate_mask);
- default:
- UNREACHABLE();
- }
- }();
- const std::string mask = '(' + regs.GetRegisterAsInteger(instr.gpr8, 0, false) +
- " >> " + std::to_string(instr.r2p.byte) + ')';
+ std::string Logical2HGreaterThan(Operation operation) {
+ return GenerateBinaryCall(operation, "greaterThan", Type::Bool2, Type::HalfFloat,
+ Type::HalfFloat);
+ }
- constexpr u64 programmable_preds = 7;
- for (u64 pred = 0; pred < programmable_preds; ++pred) {
- const auto shift = std::to_string(1 << pred);
+ std::string Logical2HNotEqual(Operation operation) {
+ return GenerateBinaryCall(operation, "notEqual", Type::Bool2, Type::HalfFloat,
+ Type::HalfFloat);
+ }
- shader.AddLine("if ((" + apply_mask + " & " + shift + ") != 0) {");
- ++shader.scope;
+ std::string Logical2HGreaterEqual(Operation operation) {
+ return GenerateBinaryCall(operation, "greaterThanEqual", Type::Bool2, Type::HalfFloat,
+ Type::HalfFloat);
+ }
- SetPredicate(pred, '(' + mask + " & " + shift + ") != 0");
+ std::string F4Texture(Operation operation) {
+ const auto meta = std::get_if<MetaTexture>(&operation.GetMeta());
+ ASSERT(meta);
- --shader.scope;
- shader.AddLine('}');
- }
- break;
+ std::string expr = GenerateTexture(operation, "texture");
+ if (meta->sampler.IsShadow()) {
+ expr = "vec4(" + expr + ')';
}
- case OpCode::Type::FloatSet: {
- const std::string op_a = GetOperandAbsNeg(regs.GetRegisterAsFloat(instr.gpr8),
- instr.fset.abs_a != 0, instr.fset.neg_a != 0);
-
- std::string op_b;
-
- if (instr.is_b_imm) {
- const std::string imm = GetImmediate19(instr);
- op_b = imm;
- } else {
- if (instr.is_b_gpr) {
- op_b = regs.GetRegisterAsFloat(instr.gpr20);
- } else {
- op_b = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::Float);
- }
- }
-
- op_b = GetOperandAbsNeg(op_b, instr.fset.abs_b != 0, instr.fset.neg_b != 0);
-
- // The fset instruction sets a register to 1.0 or -1 (depending on the bf bit) if the
- // condition is true, and to 0 otherwise.
- const std::string second_pred =
- GetPredicateCondition(instr.fset.pred39, instr.fset.neg_pred != 0);
-
- const std::string combiner = GetPredicateCombiner(instr.fset.op);
+ return expr + GetSwizzle(meta->element);
+ }
- const std::string predicate = "((" +
- GetPredicateComparison(instr.fset.cond, op_a, op_b) +
- ") " + combiner + " (" + second_pred + "))";
+ std::string F4TextureLod(Operation operation) {
+ const auto meta = std::get_if<MetaTexture>(&operation.GetMeta());
+ ASSERT(meta);
- if (instr.fset.bf) {
- regs.SetRegisterToFloat(instr.gpr0, 0, predicate + " ? 1.0 : 0.0", 1, 1);
- } else {
- regs.SetRegisterToInteger(instr.gpr0, false, 0, predicate + " ? 0xFFFFFFFF : 0", 1,
- 1);
- }
- if (instr.generates_cc.Value() != 0) {
- regs.SetInternalFlag(InternalFlag::ZeroFlag, predicate);
- LOG_WARNING(HW_GPU, "FSET Condition Code is incomplete");
- }
- break;
+ std::string expr = GenerateTexture(operation, "textureLod");
+ if (meta->sampler.IsShadow()) {
+ expr = "vec4(" + expr + ')';
}
- case OpCode::Type::IntegerSet: {
- const std::string op_a = regs.GetRegisterAsInteger(instr.gpr8, 0, instr.iset.is_signed);
+ return expr + GetSwizzle(meta->element);
+ }
- std::string op_b;
+ std::string F4TextureGather(Operation operation) {
+ const auto meta = std::get_if<MetaTexture>(&operation.GetMeta());
+ ASSERT(meta);
- if (instr.is_b_imm) {
- op_b = std::to_string(instr.alu.GetSignedImm20_20());
- } else {
- if (instr.is_b_gpr) {
- op_b = regs.GetRegisterAsInteger(instr.gpr20, 0, instr.iset.is_signed);
- } else {
- op_b = regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- GLSLRegister::Type::Integer);
- }
- }
-
- // The iset instruction sets a register to 1.0 or -1 (depending on the bf bit) if the
- // condition is true, and to 0 otherwise.
- const std::string second_pred =
- GetPredicateCondition(instr.iset.pred39, instr.iset.neg_pred != 0);
+ return GenerateTexture(operation, "textureGather", !meta->sampler.IsShadow()) +
+ GetSwizzle(meta->element);
+ }
- const std::string combiner = GetPredicateCombiner(instr.iset.op);
+ std::string F4TextureQueryDimensions(Operation operation) {
+ const auto meta = std::get_if<MetaTexture>(&operation.GetMeta());
+ ASSERT(meta);
- const std::string predicate = "((" +
- GetPredicateComparison(instr.iset.cond, op_a, op_b) +
- ") " + combiner + " (" + second_pred + "))";
+ const std::string sampler = GetSampler(meta->sampler);
+ const std::string lod = VisitOperand(operation, 0, Type::Int);
- if (instr.iset.bf) {
- regs.SetRegisterToFloat(instr.gpr0, 0, predicate + " ? 1.0 : 0.0", 1, 1);
- } else {
- regs.SetRegisterToInteger(instr.gpr0, false, 0, predicate + " ? 0xFFFFFFFF : 0", 1,
- 1);
- }
- break;
+ switch (meta->element) {
+ case 0:
+ case 1:
+ return "textureSize(" + sampler + ", " + lod + ')' + GetSwizzle(meta->element);
+ case 2:
+ return "0";
+ case 3:
+ return "textureQueryLevels(" + sampler + ')';
}
- case OpCode::Type::HalfSet: {
- UNIMPLEMENTED_IF(instr.hset2.ftz != 0);
-
- const std::string op_a =
- GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr8, 0, false), instr.hset2.type_a,
- instr.hset2.abs_a != 0, instr.hset2.negate_a != 0);
-
- const std::string op_b = [&]() {
- switch (opcode->get().GetId()) {
- case OpCode::Id::HSET2_R:
- return GetHalfFloat(regs.GetRegisterAsInteger(instr.gpr20, 0, false),
- instr.hset2.type_b, instr.hset2.abs_b != 0,
- instr.hset2.negate_b != 0);
- default:
- UNREACHABLE();
- return std::string("vec2(0)");
- }
- }();
-
- const std::string second_pred =
- GetPredicateCondition(instr.hset2.pred39, instr.hset2.neg_pred != 0);
-
- const std::string combiner = GetPredicateCombiner(instr.hset2.op);
-
- // HSET2 operates on each half float in the pack.
- std::string result;
- for (int i = 0; i < 2; ++i) {
- const std::string float_value = i == 0 ? "0x00003c00" : "0x3c000000";
- const std::string integer_value = i == 0 ? "0x0000ffff" : "0xffff0000";
- const std::string value = instr.hset2.bf == 1 ? float_value : integer_value;
+ UNREACHABLE();
+ return "0";
+ }
- const std::string comp = std::string(".") + "xy"[i];
- const std::string predicate =
- "((" + GetPredicateComparison(instr.hset2.cond, op_a + comp, op_b + comp) +
- ") " + combiner + " (" + second_pred + "))";
+ std::string F4TextureQueryLod(Operation operation) {
+ const auto meta = std::get_if<MetaTexture>(&operation.GetMeta());
+ ASSERT(meta);
- result += '(' + predicate + " ? " + value + " : 0)";
- if (i == 0) {
- result += " | ";
- }
- }
- regs.SetRegisterToInteger(instr.gpr0, false, 0, '(' + result + ')', 1, 1);
- break;
+ if (meta->element < 2) {
+ return "itof(int((" + GenerateTexture(operation, "textureQueryLod") + " * vec2(256))" +
+ GetSwizzle(meta->element) + "))";
}
- case OpCode::Type::Xmad: {
- UNIMPLEMENTED_IF(instr.xmad.sign_a);
- UNIMPLEMENTED_IF(instr.xmad.sign_b);
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in XMAD is not implemented");
-
- std::string op_a{regs.GetRegisterAsInteger(instr.gpr8, 0, instr.xmad.sign_a)};
- std::string op_b;
- std::string op_c;
-
- // TODO(bunnei): Needs to be fixed once op_a or op_b is signed
- UNIMPLEMENTED_IF(instr.xmad.sign_a != instr.xmad.sign_b);
- const bool is_signed{instr.xmad.sign_a == 1};
-
- bool is_merge{};
- switch (opcode->get().GetId()) {
- case OpCode::Id::XMAD_CR: {
- is_merge = instr.xmad.merge_56;
- op_b += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- instr.xmad.sign_b ? GLSLRegister::Type::Integer
- : GLSLRegister::Type::UnsignedInteger);
- op_c += regs.GetRegisterAsInteger(instr.gpr39, 0, is_signed);
- break;
- }
- case OpCode::Id::XMAD_RR: {
- is_merge = instr.xmad.merge_37;
- op_b += regs.GetRegisterAsInteger(instr.gpr20, 0, instr.xmad.sign_b);
- op_c += regs.GetRegisterAsInteger(instr.gpr39, 0, is_signed);
- break;
- }
- case OpCode::Id::XMAD_RC: {
- op_b += regs.GetRegisterAsInteger(instr.gpr39, 0, instr.xmad.sign_b);
- op_c += regs.GetUniform(instr.cbuf34.index, instr.cbuf34.offset,
- is_signed ? GLSLRegister::Type::Integer
- : GLSLRegister::Type::UnsignedInteger);
- break;
- }
- case OpCode::Id::XMAD_IMM: {
- is_merge = instr.xmad.merge_37;
- op_b += std::to_string(instr.xmad.imm20_16);
- op_c += regs.GetRegisterAsInteger(instr.gpr39, 0, is_signed);
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled XMAD instruction: {}", opcode->get().GetName());
- }
- }
+ return "0";
+ }
- // TODO(bunnei): Ensure this is right with signed operands
- if (instr.xmad.high_a) {
- op_a = "((" + op_a + ") >> 16)";
- } else {
- op_a = "((" + op_a + ") & 0xFFFF)";
- }
+ std::string F4TexelFetch(Operation operation) {
+ constexpr std::array<const char*, 4> constructors = {"int", "ivec2", "ivec3", "ivec4"};
+ const auto meta = std::get_if<MetaTexture>(&operation.GetMeta());
+ const auto count = static_cast<u32>(operation.GetOperandsCount());
+ ASSERT(meta);
- std::string src2 = '(' + op_b + ')'; // Preserve original source 2
- if (instr.xmad.high_b) {
- op_b = '(' + src2 + " >> 16)";
- } else {
- op_b = '(' + src2 + " & 0xFFFF)";
- }
+ std::string expr = "texelFetch(";
+ expr += GetSampler(meta->sampler);
+ expr += ", ";
- std::string product = '(' + op_a + " * " + op_b + ')';
- if (instr.xmad.product_shift_left) {
- product = '(' + product + " << 16)";
- }
+ expr += constructors[meta->coords_count - 1];
+ expr += '(';
+ for (u32 i = 0; i < count; ++i) {
+ expr += VisitOperand(operation, i, Type::Int);
- switch (instr.xmad.mode) {
- case Tegra::Shader::XmadMode::None:
- break;
- case Tegra::Shader::XmadMode::CLo:
- op_c = "((" + op_c + ") & 0xFFFF)";
- break;
- case Tegra::Shader::XmadMode::CHi:
- op_c = "((" + op_c + ") >> 16)";
- break;
- case Tegra::Shader::XmadMode::CBcc:
- op_c = "((" + op_c + ") + (" + src2 + "<< 16))";
- break;
- default: {
- UNIMPLEMENTED_MSG("Unhandled XMAD mode: {}",
- static_cast<u32>(instr.xmad.mode.Value()));
+ if (i + 1 == meta->coords_count) {
+ expr += ')';
}
+ if (i + 1 < count) {
+ expr += ", ";
}
-
- std::string sum{'(' + product + " + " + op_c + ')'};
- if (is_merge) {
- sum = "((" + sum + " & 0xFFFF) | (" + src2 + "<< 16))";
- }
-
- regs.SetRegisterToInteger(instr.gpr0, is_signed, 0, sum, 1, 1);
- break;
}
- default: {
- switch (opcode->get().GetId()) {
- case OpCode::Id::EXIT: {
- const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
- UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T,
- "EXIT condition code used: {}", static_cast<u32>(cc));
-
- if (stage == Maxwell3D::Regs::ShaderStage::Fragment) {
- EmitFragmentOutputsWrite();
- }
-
- switch (instr.flow.cond) {
- case Tegra::Shader::FlowCondition::Always:
- shader.AddLine("return true;");
- if (instr.pred.pred_index == static_cast<u64>(Pred::UnusedIndex)) {
- // If this is an unconditional exit then just end processing here,
- // otherwise we have to account for the possibility of the condition
- // not being met, so continue processing the next instruction.
- offset = PROGRAM_END - 1;
- }
- break;
-
- case Tegra::Shader::FlowCondition::Fcsm_Tr:
- // TODO(bunnei): What is this used for? If we assume this conditon is not
- // satisifed, dual vertex shaders in Farming Simulator make more sense
- UNIMPLEMENTED_MSG("Skipping unknown FlowCondition::Fcsm_Tr");
- break;
+ expr += ')';
+ return expr + GetSwizzle(meta->element);
+ }
- default:
- UNIMPLEMENTED_MSG("Unhandled flow condition: {}",
- static_cast<u32>(instr.flow.cond.Value()));
- }
- break;
- }
- case OpCode::Id::KIL: {
- UNIMPLEMENTED_IF(instr.flow.cond != Tegra::Shader::FlowCondition::Always);
+ std::string Branch(Operation operation) {
+ const auto target = std::get_if<ImmediateNode>(operation[0]);
+ UNIMPLEMENTED_IF(!target);
- const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
- UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T,
- "KIL condition code used: {}", static_cast<u32>(cc));
+ code.AddLine(fmt::format("jmp_to = 0x{:x}u;", target->GetValue()));
+ code.AddLine("break;");
+ return {};
+ }
- // Enclose "discard" in a conditional, so that GLSL compilation does not complain
- // about unexecuted instructions that may follow this.
- shader.AddLine("if (true) {");
- ++shader.scope;
- shader.AddLine("discard;");
- --shader.scope;
- shader.AddLine("}");
+ std::string PushFlowStack(Operation operation) {
+ const auto target = std::get_if<ImmediateNode>(operation[0]);
+ UNIMPLEMENTED_IF(!target);
- break;
- }
- case OpCode::Id::OUT_R: {
- UNIMPLEMENTED_IF_MSG(instr.gpr20.Value() != Register::ZeroIndex,
- "Stream buffer is not supported");
- ASSERT_MSG(stage == Maxwell3D::Regs::ShaderStage::Geometry,
- "OUT is expected to be used in a geometry shader.");
-
- if (instr.out.emit) {
- // gpr0 is used to store the next address. Hardware returns a pointer but
- // we just return the next index with a cyclic cap.
- const std::string current{regs.GetRegisterAsInteger(instr.gpr8, 0, false)};
- const std::string next = "((" + current + " + 1" + ") % " +
- std::to_string(MAX_GEOMETRY_BUFFERS) + ')';
- shader.AddLine("emit_vertex(" + current + ");");
- regs.SetRegisterToInteger(instr.gpr0, false, 0, next, 1, 1);
- }
- if (instr.out.cut) {
- shader.AddLine("EndPrimitive();");
- }
-
- break;
- }
- case OpCode::Id::MOV_SYS: {
- switch (instr.sys20) {
- case Tegra::Shader::SystemVariable::InvocationInfo: {
- LOG_WARNING(HW_GPU, "MOV_SYS instruction with InvocationInfo is incomplete");
- regs.SetRegisterToInteger(instr.gpr0, false, 0, "0u", 1, 1);
- break;
- }
- case Tegra::Shader::SystemVariable::Ydirection: {
- // Config pack's third value is Y_NEGATE's state.
- regs.SetRegisterToFloat(instr.gpr0, 0, "uintBitsToFloat(config_pack[2])", 1, 1);
- break;
- }
- default: {
- UNIMPLEMENTED_MSG("Unhandled system move: {}",
- static_cast<u32>(instr.sys20.Value()));
- }
- }
- break;
- }
- case OpCode::Id::ISBERD: {
- UNIMPLEMENTED_IF(instr.isberd.o != 0);
- UNIMPLEMENTED_IF(instr.isberd.skew != 0);
- UNIMPLEMENTED_IF(instr.isberd.shift != Tegra::Shader::IsberdShift::None);
- UNIMPLEMENTED_IF(instr.isberd.mode != Tegra::Shader::IsberdMode::None);
- ASSERT_MSG(stage == Maxwell3D::Regs::ShaderStage::Geometry,
- "ISBERD is expected to be used in a geometry shader.");
- LOG_WARNING(HW_GPU, "ISBERD instruction is incomplete");
- regs.SetRegisterToFloat(instr.gpr0, 0, regs.GetRegisterAsFloat(instr.gpr8), 1, 1);
- break;
- }
- case OpCode::Id::BRA: {
- UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
- "BRA with constant buffers are not implemented");
-
- const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
- const u32 target = offset + instr.bra.GetBranchTarget();
- if (cc != Tegra::Shader::ConditionCode::T) {
- const std::string condition_code = regs.GetConditionCode(cc);
- shader.AddLine("if (" + condition_code + "){");
- shader.scope++;
- shader.AddLine("{ jmp_to = " + std::to_string(target) + "u; break; }");
- shader.scope--;
- shader.AddLine('}');
- } else {
- shader.AddLine("{ jmp_to = " + std::to_string(target) + "u; break; }");
- }
- break;
- }
- case OpCode::Id::IPA: {
- const auto& attribute = instr.attribute.fmt28;
- const auto& reg = instr.gpr0;
-
- Tegra::Shader::IpaMode input_mode{instr.ipa.interp_mode.Value(),
- instr.ipa.sample_mode.Value()};
- regs.SetRegisterToInputAttibute(reg, attribute.element, attribute.index,
- input_mode);
+ code.AddLine(fmt::format("flow_stack[flow_stack_top++] = 0x{:x}u;", target->GetValue()));
+ return {};
+ }
- if (instr.ipa.saturate) {
- regs.SetRegisterToFloat(reg, 0, regs.GetRegisterAsFloat(reg), 1, 1, true);
- }
- break;
- }
- case OpCode::Id::SSY: {
- // The SSY opcode tells the GPU where to re-converge divergent execution paths, it
- // sets the target of the jump that the SYNC instruction will make. The SSY opcode
- // has a similar structure to the BRA opcode.
- UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
- "Constant buffer flow is not supported");
-
- const u32 target = offset + instr.bra.GetBranchTarget();
- EmitPushToFlowStack(target);
- break;
- }
- case OpCode::Id::PBK: {
- // PBK pushes to a stack the address where BRK will jump to. This shares stack with
- // SSY but using SYNC on a PBK address will kill the shader execution. We don't
- // emulate this because it's very unlikely a driver will emit such invalid shader.
- UNIMPLEMENTED_IF_MSG(instr.bra.constant_buffer != 0,
- "Constant buffer PBK is not supported");
-
- const u32 target = offset + instr.bra.GetBranchTarget();
- EmitPushToFlowStack(target);
- break;
- }
- case OpCode::Id::SYNC: {
- const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
- UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T,
- "SYNC condition code used: {}", static_cast<u32>(cc));
+ std::string PopFlowStack(Operation operation) {
+ code.AddLine("jmp_to = flow_stack[--flow_stack_top];");
+ code.AddLine("break;");
+ return {};
+ }
- // The SYNC opcode jumps to the address previously set by the SSY opcode
- EmitPopFromFlowStack();
- break;
+ std::string Exit(Operation operation) {
+ if (stage != ShaderStage::Fragment) {
+ code.AddLine("return;");
+ return {};
+ }
+ const auto& used_registers = ir.GetRegisters();
+ const auto SafeGetRegister = [&](u32 reg) -> std::string {
+ // TODO(Rodrigo): Replace with contains once C++20 releases
+ if (used_registers.find(reg) != used_registers.end()) {
+ return GetRegister(reg);
}
- case OpCode::Id::BRK: {
- // The BRK opcode jumps to the address previously set by the PBK opcode
- const Tegra::Shader::ConditionCode cc = instr.flow_condition_code;
- UNIMPLEMENTED_IF_MSG(cc != Tegra::Shader::ConditionCode::T,
- "BRK condition code used: {}", static_cast<u32>(cc));
+ return "0.0f";
+ };
- EmitPopFromFlowStack();
- break;
- }
- case OpCode::Id::DEPBAR: {
- // TODO(Subv): Find out if we actually have to care about this instruction or if
- // the GLSL compiler takes care of that for us.
- LOG_WARNING(HW_GPU, "DEPBAR instruction is stubbed");
- break;
- }
- case OpCode::Id::VMAD: {
- UNIMPLEMENTED_IF_MSG(instr.generates_cc,
- "Condition codes generation in VMAD is not implemented");
-
- const bool result_signed = instr.video.signed_a == 1 || instr.video.signed_b == 1;
- const std::string op_a = GetVideoOperandA(instr);
- const std::string op_b = GetVideoOperandB(instr);
- const std::string op_c = regs.GetRegisterAsInteger(instr.gpr39, 0, result_signed);
-
- std::string result = '(' + op_a + " * " + op_b + " + " + op_c + ')';
-
- switch (instr.vmad.shr) {
- case Tegra::Shader::VmadShr::Shr7:
- result = '(' + result + " >> 7)";
- break;
- case Tegra::Shader::VmadShr::Shr15:
- result = '(' + result + " >> 15)";
- break;
- }
+ UNIMPLEMENTED_IF_MSG(header.ps.omap.sample_mask != 0, "Sample mask write is unimplemented");
- regs.SetRegisterToInteger(instr.gpr0, result_signed, 1, result, 1, 1,
- instr.vmad.saturate == 1, 0, Register::Size::Word,
- instr.vmad.cc);
- break;
+ code.AddLine("if (alpha_test[0] != 0) {");
+ ++code.scope;
+ // We start on the register containing the alpha value in the first RT.
+ u32 current_reg = 3;
+ for (u32 render_target = 0; render_target < Maxwell::NumRenderTargets; ++render_target) {
+ // TODO(Blinkhawk): verify the behavior of alpha testing on hardware when
+ // multiple render targets are used.
+ if (header.ps.IsColorComponentOutputEnabled(render_target, 0) ||
+ header.ps.IsColorComponentOutputEnabled(render_target, 1) ||
+ header.ps.IsColorComponentOutputEnabled(render_target, 2) ||
+ header.ps.IsColorComponentOutputEnabled(render_target, 3)) {
+ code.AddLine(
+ fmt::format("if (!AlphaFunc({})) discard;", SafeGetRegister(current_reg)));
+ current_reg += 4;
}
- case OpCode::Id::VSETP: {
- const std::string op_a = GetVideoOperandA(instr);
- const std::string op_b = GetVideoOperandB(instr);
-
- // We can't use the constant predicate as destination.
- ASSERT(instr.vsetp.pred3 != static_cast<u64>(Pred::UnusedIndex));
-
- const std::string second_pred = GetPredicateCondition(instr.vsetp.pred39, false);
-
- const std::string combiner = GetPredicateCombiner(instr.vsetp.op);
-
- const std::string predicate = GetPredicateComparison(instr.vsetp.cond, op_a, op_b);
- // Set the primary predicate to the result of Predicate OP SecondPredicate
- SetPredicate(instr.vsetp.pred3,
- '(' + predicate + ") " + combiner + " (" + second_pred + ')');
+ }
+ --code.scope;
+ code.AddLine('}');
- if (instr.vsetp.pred0 != static_cast<u64>(Pred::UnusedIndex)) {
- // Set the secondary predicate to the result of !Predicate OP SecondPredicate,
- // if enabled
- SetPredicate(instr.vsetp.pred0,
- "!(" + predicate + ") " + combiner + " (" + second_pred + ')');
+ // Write the color outputs using the data in the shader registers, disabled
+ // rendertargets/components are skipped in the register assignment.
+ current_reg = 0;
+ for (u32 render_target = 0; render_target < Maxwell::NumRenderTargets; ++render_target) {
+ // TODO(Subv): Figure out how dual-source blending is configured in the Switch.
+ for (u32 component = 0; component < 4; ++component) {
+ if (header.ps.IsColorComponentOutputEnabled(render_target, component)) {
+ code.AddLine(fmt::format("FragColor{}[{}] = {};", render_target, component,
+ SafeGetRegister(current_reg)));
+ ++current_reg;
}
- break;
}
- default: { UNIMPLEMENTED_MSG("Unhandled instruction: {}", opcode->get().GetName()); }
- }
-
- break;
- }
}
- // Close the predicate condition scope.
- if (can_be_predicated && instr.pred.pred_index != static_cast<u64>(Pred::UnusedIndex)) {
- --shader.scope;
- shader.AddLine('}');
+ if (header.ps.omap.depth) {
+ // The depth output is always 2 registers after the last color output, and current_reg
+ // already contains one past the last color register.
+ code.AddLine("gl_FragDepth = " + SafeGetRegister(current_reg + 1) + ';');
}
- return offset + 1;
+ code.AddLine("return;");
+ return {};
}
- /**
- * Compiles a range of instructions from Tegra to GLSL.
- * @param begin the offset of the starting instruction.
- * @param end the offset where the compilation should stop (exclusive).
- * @return the offset of the next instruction to compile. PROGRAM_END if the program
- * terminates.
- */
- u32 CompileRange(u32 begin, u32 end) {
- u32 program_counter;
- for (program_counter = begin; program_counter < (begin > end ? PROGRAM_END : end);) {
- program_counter = CompileInstr(program_counter);
- }
- return program_counter;
+ std::string Discard(Operation operation) {
+ // Enclose "discard" in a conditional, so that GLSL compilation does not complain
+ // about unexecuted instructions that may follow this.
+ code.AddLine("if (true) {");
+ ++code.scope;
+ code.AddLine("discard;");
+ --code.scope;
+ code.AddLine("}");
+ return {};
}
- void Generate(const std::string& suffix) {
- // Add declarations for all subroutines
- for (const auto& subroutine : subroutines) {
- shader.AddLine("bool " + subroutine.GetName() + "();");
- }
- shader.AddNewLine();
+ std::string EmitVertex(Operation operation) {
+ ASSERT_MSG(stage == ShaderStage::Geometry,
+ "EmitVertex is expected to be used in a geometry shader.");
- // Add the main entry point
- shader.AddLine("bool exec_" + suffix + "() {");
- ++shader.scope;
- CallSubroutine(GetSubroutine(main_offset, PROGRAM_END));
- --shader.scope;
- shader.AddLine("}\n");
-
- // Add definitions for all subroutines
- for (const auto& subroutine : subroutines) {
- std::set<u32> labels = subroutine.labels;
-
- shader.AddLine("bool " + subroutine.GetName() + "() {");
- ++shader.scope;
-
- if (labels.empty()) {
- if (CompileRange(subroutine.begin, subroutine.end) != PROGRAM_END) {
- shader.AddLine("return false;");
- }
- } else {
- labels.insert(subroutine.begin);
- shader.AddLine("uint jmp_to = " + std::to_string(subroutine.begin) + "u;");
-
- // TODO(Subv): Figure out the actual depth of the flow stack, for now it seems
- // unlikely that shaders will use 20 nested SSYs and PBKs.
- constexpr u32 FLOW_STACK_SIZE = 20;
- shader.AddLine("uint flow_stack[" + std::to_string(FLOW_STACK_SIZE) + "];");
- shader.AddLine("uint flow_stack_top = 0u;");
-
- shader.AddLine("while (true) {");
- ++shader.scope;
+ // If a geometry shader is attached, it will always flip (it's the last stage before
+ // fragment). For more info about flipping, refer to gl_shader_gen.cpp.
+ code.AddLine("position.xy *= viewport_flip.xy;");
+ code.AddLine("gl_Position = position;");
+ code.AddLine("position.w = 1.0;");
+ code.AddLine("EmitVertex();");
+ return {};
+ }
+
+ std::string EndPrimitive(Operation operation) {
+ ASSERT_MSG(stage == ShaderStage::Geometry,
+ "EndPrimitive is expected to be used in a geometry shader.");
+
+ code.AddLine("EndPrimitive();");
+ return {};
+ }
+
+ std::string YNegate(Operation operation) {
+ // Config pack's third value is Y_NEGATE's state.
+ return "uintBitsToFloat(config_pack[2])";
+ }
+
+ static constexpr OperationDecompilersArray operation_decompilers = {
+ &GLSLDecompiler::Assign,
+
+ &GLSLDecompiler::Select,
+
+ &GLSLDecompiler::Add<Type::Float>,
+ &GLSLDecompiler::Mul<Type::Float>,
+ &GLSLDecompiler::Div<Type::Float>,
+ &GLSLDecompiler::Fma<Type::Float>,
+ &GLSLDecompiler::Negate<Type::Float>,
+ &GLSLDecompiler::Absolute<Type::Float>,
+ &GLSLDecompiler::FClamp,
+ &GLSLDecompiler::Min<Type::Float>,
+ &GLSLDecompiler::Max<Type::Float>,
+ &GLSLDecompiler::FCos,
+ &GLSLDecompiler::FSin,
+ &GLSLDecompiler::FExp2,
+ &GLSLDecompiler::FLog2,
+ &GLSLDecompiler::FInverseSqrt,
+ &GLSLDecompiler::FSqrt,
+ &GLSLDecompiler::FRoundEven,
+ &GLSLDecompiler::FFloor,
+ &GLSLDecompiler::FCeil,
+ &GLSLDecompiler::FTrunc,
+ &GLSLDecompiler::FCastInteger<Type::Int>,
+ &GLSLDecompiler::FCastInteger<Type::Uint>,
+
+ &GLSLDecompiler::Add<Type::Int>,
+ &GLSLDecompiler::Mul<Type::Int>,
+ &GLSLDecompiler::Div<Type::Int>,
+ &GLSLDecompiler::Negate<Type::Int>,
+ &GLSLDecompiler::Absolute<Type::Int>,
+ &GLSLDecompiler::Min<Type::Int>,
+ &GLSLDecompiler::Max<Type::Int>,
+
+ &GLSLDecompiler::ICastFloat,
+ &GLSLDecompiler::ICastUnsigned,
+ &GLSLDecompiler::LogicalShiftLeft<Type::Int>,
+ &GLSLDecompiler::ILogicalShiftRight,
+ &GLSLDecompiler::IArithmeticShiftRight,
+ &GLSLDecompiler::BitwiseAnd<Type::Int>,
+ &GLSLDecompiler::BitwiseOr<Type::Int>,
+ &GLSLDecompiler::BitwiseXor<Type::Int>,
+ &GLSLDecompiler::BitwiseNot<Type::Int>,
+ &GLSLDecompiler::BitfieldInsert<Type::Int>,
+ &GLSLDecompiler::BitfieldExtract<Type::Int>,
+ &GLSLDecompiler::BitCount<Type::Int>,
+
+ &GLSLDecompiler::Add<Type::Uint>,
+ &GLSLDecompiler::Mul<Type::Uint>,
+ &GLSLDecompiler::Div<Type::Uint>,
+ &GLSLDecompiler::Min<Type::Uint>,
+ &GLSLDecompiler::Max<Type::Uint>,
+ &GLSLDecompiler::UCastFloat,
+ &GLSLDecompiler::UCastSigned,
+ &GLSLDecompiler::LogicalShiftLeft<Type::Uint>,
+ &GLSLDecompiler::UShiftRight,
+ &GLSLDecompiler::UShiftRight,
+ &GLSLDecompiler::BitwiseAnd<Type::Uint>,
+ &GLSLDecompiler::BitwiseOr<Type::Uint>,
+ &GLSLDecompiler::BitwiseXor<Type::Uint>,
+ &GLSLDecompiler::BitwiseNot<Type::Uint>,
+ &GLSLDecompiler::BitfieldInsert<Type::Uint>,
+ &GLSLDecompiler::BitfieldExtract<Type::Uint>,
+ &GLSLDecompiler::BitCount<Type::Uint>,
+
+ &GLSLDecompiler::Add<Type::HalfFloat>,
+ &GLSLDecompiler::Mul<Type::HalfFloat>,
+ &GLSLDecompiler::Fma<Type::HalfFloat>,
+ &GLSLDecompiler::Absolute<Type::HalfFloat>,
+ &GLSLDecompiler::HNegate,
+ &GLSLDecompiler::HMergeF32,
+ &GLSLDecompiler::HMergeH0,
+ &GLSLDecompiler::HMergeH1,
+ &GLSLDecompiler::HPack2,
+
+ &GLSLDecompiler::LogicalAssign,
+ &GLSLDecompiler::LogicalAnd,
+ &GLSLDecompiler::LogicalOr,
+ &GLSLDecompiler::LogicalXor,
+ &GLSLDecompiler::LogicalNegate,
+ &GLSLDecompiler::LogicalPick2,
+ &GLSLDecompiler::LogicalAll2,
+ &GLSLDecompiler::LogicalAny2,
+
+ &GLSLDecompiler::LogicalLessThan<Type::Float>,
+ &GLSLDecompiler::LogicalEqual<Type::Float>,
+ &GLSLDecompiler::LogicalLessEqual<Type::Float>,
+ &GLSLDecompiler::LogicalGreaterThan<Type::Float>,
+ &GLSLDecompiler::LogicalNotEqual<Type::Float>,
+ &GLSLDecompiler::LogicalGreaterEqual<Type::Float>,
+ &GLSLDecompiler::LogicalFIsNan,
+
+ &GLSLDecompiler::LogicalLessThan<Type::Int>,
+ &GLSLDecompiler::LogicalEqual<Type::Int>,
+ &GLSLDecompiler::LogicalLessEqual<Type::Int>,
+ &GLSLDecompiler::LogicalGreaterThan<Type::Int>,
+ &GLSLDecompiler::LogicalNotEqual<Type::Int>,
+ &GLSLDecompiler::LogicalGreaterEqual<Type::Int>,
+
+ &GLSLDecompiler::LogicalLessThan<Type::Uint>,
+ &GLSLDecompiler::LogicalEqual<Type::Uint>,
+ &GLSLDecompiler::LogicalLessEqual<Type::Uint>,
+ &GLSLDecompiler::LogicalGreaterThan<Type::Uint>,
+ &GLSLDecompiler::LogicalNotEqual<Type::Uint>,
+ &GLSLDecompiler::LogicalGreaterEqual<Type::Uint>,
+
+ &GLSLDecompiler::Logical2HLessThan,
+ &GLSLDecompiler::Logical2HEqual,
+ &GLSLDecompiler::Logical2HLessEqual,
+ &GLSLDecompiler::Logical2HGreaterThan,
+ &GLSLDecompiler::Logical2HNotEqual,
+ &GLSLDecompiler::Logical2HGreaterEqual,
+
+ &GLSLDecompiler::F4Texture,
+ &GLSLDecompiler::F4TextureLod,
+ &GLSLDecompiler::F4TextureGather,
+ &GLSLDecompiler::F4TextureQueryDimensions,
+ &GLSLDecompiler::F4TextureQueryLod,
+ &GLSLDecompiler::F4TexelFetch,
+
+ &GLSLDecompiler::Branch,
+ &GLSLDecompiler::PushFlowStack,
+ &GLSLDecompiler::PopFlowStack,
+ &GLSLDecompiler::Exit,
+ &GLSLDecompiler::Discard,
+
+ &GLSLDecompiler::EmitVertex,
+ &GLSLDecompiler::EndPrimitive,
+
+ &GLSLDecompiler::YNegate,
+ };
- shader.AddLine("switch (jmp_to) {");
+ std::string GetRegister(u32 index) const {
+ return GetDeclarationWithSuffix(index, "gpr");
+ }
- for (auto label : labels) {
- shader.AddLine("case " + std::to_string(label) + "u: {");
- ++shader.scope;
+ std::string GetPredicate(Tegra::Shader::Pred pred) const {
+ return GetDeclarationWithSuffix(static_cast<u32>(pred), "pred");
+ }
- const auto next_it = labels.lower_bound(label + 1);
- const u32 next_label = next_it == labels.end() ? subroutine.end : *next_it;
+ std::string GetInputAttribute(Attribute::Index attribute) const {
+ const auto index{static_cast<u32>(attribute) -
+ static_cast<u32>(Attribute::Index::Attribute_0)};
+ return GetDeclarationWithSuffix(index, "input_attr");
+ }
- const u32 compile_end = CompileRange(label, next_label);
- if (compile_end > next_label && compile_end != PROGRAM_END) {
- // This happens only when there is a label inside a IF/LOOP block
- shader.AddLine(" jmp_to = " + std::to_string(compile_end) + "u; break; }");
- labels.emplace(compile_end);
- }
+ std::string GetOutputAttribute(Attribute::Index attribute) const {
+ const auto index{static_cast<u32>(attribute) -
+ static_cast<u32>(Attribute::Index::Attribute_0)};
+ return GetDeclarationWithSuffix(index, "output_attr");
+ }
- --shader.scope;
- shader.AddLine('}');
- }
+ std::string GetConstBuffer(u32 index) const {
+ return GetDeclarationWithSuffix(index, "cbuf");
+ }
- shader.AddLine("default: return false;");
- shader.AddLine('}');
+ std::string GetGlobalMemory(const GlobalMemoryBase& descriptor) const {
+ return fmt::format("gmem_{}_{}_{}", descriptor.cbuf_index, descriptor.cbuf_offset, suffix);
+ }
- --shader.scope;
- shader.AddLine('}');
+ std::string GetGlobalMemoryBlock(const GlobalMemoryBase& descriptor) const {
+ return fmt::format("gmem_block_{}_{}_{}", descriptor.cbuf_index, descriptor.cbuf_offset,
+ suffix);
+ }
- shader.AddLine("return false;");
- }
+ std::string GetConstBufferBlock(u32 index) const {
+ return GetDeclarationWithSuffix(index, "cbuf_block");
+ }
- --shader.scope;
- shader.AddLine("}\n");
+ std::string GetLocalMemory() const {
+ return "lmem_" + suffix;
+ }
- DEBUG_ASSERT(shader.scope == 0);
- }
+ std::string GetInternalFlag(InternalFlag flag) const {
+ constexpr std::array<const char*, 4> InternalFlagNames = {"zero_flag", "sign_flag",
+ "carry_flag", "overflow_flag"};
+ const auto index = static_cast<u32>(flag);
+ ASSERT(index < static_cast<u32>(InternalFlag::Amount));
- GenerateDeclarations();
+ return std::string(InternalFlagNames[index]) + '_' + suffix;
}
- /// Add declarations for registers
- void GenerateDeclarations() {
- regs.GenerateDeclarations(suffix);
+ std::string GetSampler(const Sampler& sampler) const {
+ return GetDeclarationWithSuffix(static_cast<u32>(sampler.GetIndex()), "sampler");
+ }
- for (const auto& pred : declr_predicates) {
- declarations.AddLine("bool " + pred + " = false;");
- }
- declarations.AddNewLine();
+ std::string GetDeclarationWithSuffix(u32 index, const std::string& name) const {
+ return name + '_' + std::to_string(index) + '_' + suffix;
}
-private:
- const std::set<Subroutine>& subroutines;
- const ProgramCode& program_code;
- Tegra::Shader::Header header;
- const u32 main_offset;
- Maxwell3D::Regs::ShaderStage stage;
- const std::string& suffix;
- u64 local_memory_size;
- std::size_t shader_length;
-
- ShaderWriter shader;
- ShaderWriter declarations;
- GLSLRegisterManager regs{shader, declarations, stage, suffix, header};
-
- // Declarations
- std::set<std::string> declr_predicates;
-}; // namespace OpenGL::GLShader::Decompiler
+ const ShaderIR& ir;
+ const ShaderStage stage;
+ const std::string suffix;
+ const Header header;
+
+ ShaderWriter code;
+};
std::string GetCommonDeclarations() {
- return fmt::format("#define MAX_CONSTBUFFER_ELEMENTS {}\n",
- RasterizerOpenGL::MaxConstbufferSize / sizeof(GLvec4));
+ const auto cbuf = std::to_string(MAX_CONSTBUFFER_ELEMENTS);
+ const auto gmem = std::to_string(MAX_GLOBALMEMORY_ELEMENTS);
+ return "#define MAX_CONSTBUFFER_ELEMENTS " + cbuf + "\n" +
+ "#define MAX_GLOBALMEMORY_ELEMENTS " + gmem + "\n" +
+ "#define ftoi floatBitsToInt\n"
+ "#define ftou floatBitsToUint\n"
+ "#define itof intBitsToFloat\n"
+ "#define utof uintBitsToFloat\n\n"
+ "float fromHalf2(vec2 pair) {\n"
+ " return utof(packHalf2x16(pair));\n"
+ "}\n\n"
+ "vec2 toHalf2(float value) {\n"
+ " return unpackHalf2x16(ftou(value));\n"
+ "}\n";
}
-std::optional<ProgramResult> DecompileProgram(const ProgramCode& program_code, u32 main_offset,
- Maxwell3D::Regs::ShaderStage stage,
- const std::string& suffix) {
- try {
- ControlFlowAnalyzer analyzer(program_code, main_offset, suffix);
- const auto subroutines = analyzer.GetSubroutines();
- GLSLGenerator generator(subroutines, program_code, main_offset, stage, suffix,
- analyzer.GetShaderLength());
- return ProgramResult{generator.GetShaderCode(), generator.GetEntries()};
- } catch (const DecompileFail& exception) {
- LOG_ERROR(HW_GPU, "Shader decompilation failed: {}", exception.what());
- }
- return {};
+ProgramResult Decompile(const ShaderIR& ir, Maxwell::ShaderStage stage, const std::string& suffix) {
+ GLSLDecompiler decompiler(ir, stage, suffix);
+ decompiler.Decompile();
+ return {decompiler.GetResult(), decompiler.GetShaderEntries()};
}
-} // namespace OpenGL::GLShader::Decompiler \ No newline at end of file
+} // namespace OpenGL::GLShader \ No newline at end of file
diff --git a/src/video_core/renderer_opengl/gl_shader_decompiler.h b/src/video_core/renderer_opengl/gl_shader_decompiler.h
index d01a4a7ee..0856a1361 100644
--- a/src/video_core/renderer_opengl/gl_shader_decompiler.h
+++ b/src/video_core/renderer_opengl/gl_shader_decompiler.h
@@ -5,21 +5,106 @@
#pragma once
#include <array>
-#include <functional>
-#include <optional>
#include <string>
+#include <utility>
+#include <vector>
#include "common/common_types.h"
#include "video_core/engines/maxwell_3d.h"
-#include "video_core/renderer_opengl/gl_shader_gen.h"
+#include "video_core/shader/shader_ir.h"
-namespace OpenGL::GLShader::Decompiler {
+namespace VideoCommon::Shader {
+class ShaderIR;
+}
-using Tegra::Engines::Maxwell3D;
+namespace OpenGL::GLShader {
+
+using Maxwell = Tegra::Engines::Maxwell3D::Regs;
+
+class ConstBufferEntry : public VideoCommon::Shader::ConstBuffer {
+public:
+ explicit ConstBufferEntry(const VideoCommon::Shader::ConstBuffer& entry,
+ Maxwell::ShaderStage stage, const std::string& name, u32 index)
+ : VideoCommon::Shader::ConstBuffer{entry}, stage{stage}, name{name}, index{index} {}
+
+ const std::string& GetName() const {
+ return name;
+ }
+
+ Maxwell::ShaderStage GetStage() const {
+ return stage;
+ }
+
+ u32 GetIndex() const {
+ return index;
+ }
+
+private:
+ std::string name;
+ Maxwell::ShaderStage stage{};
+ u32 index{};
+};
+
+class SamplerEntry : public VideoCommon::Shader::Sampler {
+public:
+ explicit SamplerEntry(const VideoCommon::Shader::Sampler& entry, Maxwell::ShaderStage stage,
+ const std::string& name)
+ : VideoCommon::Shader::Sampler{entry}, stage{stage}, name{name} {}
+
+ const std::string& GetName() const {
+ return name;
+ }
+
+ Maxwell::ShaderStage GetStage() const {
+ return stage;
+ }
+
+private:
+ std::string name;
+ Maxwell::ShaderStage stage{};
+};
+
+class GlobalMemoryEntry {
+public:
+ explicit GlobalMemoryEntry(u32 cbuf_index, u32 cbuf_offset, Maxwell::ShaderStage stage,
+ std::string name)
+ : cbuf_index{cbuf_index}, cbuf_offset{cbuf_offset}, stage{stage}, name{std::move(name)} {}
+
+ u32 GetCbufIndex() const {
+ return cbuf_index;
+ }
+
+ u32 GetCbufOffset() const {
+ return cbuf_offset;
+ }
+
+ const std::string& GetName() const {
+ return name;
+ }
+
+ Maxwell::ShaderStage GetStage() const {
+ return stage;
+ }
+
+private:
+ u32 cbuf_index{};
+ u32 cbuf_offset{};
+ Maxwell::ShaderStage stage{};
+ std::string name;
+};
+
+struct ShaderEntries {
+ std::vector<ConstBufferEntry> const_buffers;
+ std::vector<SamplerEntry> samplers;
+ std::vector<GlobalMemoryEntry> global_memory_entries;
+ std::array<bool, Maxwell::NumClipDistances> clip_distances{};
+ std::size_t shader_length{};
+};
+
+using ProgramResult = std::pair<std::string, ShaderEntries>;
std::string GetCommonDeclarations();
-std::optional<ProgramResult> DecompileProgram(const ProgramCode& program_code, u32 main_offset,
- Maxwell3D::Regs::ShaderStage stage,
- const std::string& suffix);
+ProgramResult Decompile(const VideoCommon::Shader::ShaderIR& ir, Maxwell::ShaderStage stage,
+ const std::string& suffix);
-} // namespace OpenGL::GLShader::Decompiler
+} // namespace OpenGL::GLShader \ No newline at end of file
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp
index 23ed91e27..04e1db911 100644
--- a/src/video_core/renderer_opengl/gl_shader_gen.cpp
+++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp
@@ -2,65 +2,62 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
+#include <fmt/format.h>
#include "common/assert.h"
#include "video_core/engines/maxwell_3d.h"
#include "video_core/renderer_opengl/gl_shader_decompiler.h"
#include "video_core/renderer_opengl/gl_shader_gen.h"
+#include "video_core/shader/shader_ir.h"
namespace OpenGL::GLShader {
using Tegra::Engines::Maxwell3D;
+using VideoCommon::Shader::ProgramCode;
+using VideoCommon::Shader::ShaderIR;
static constexpr u32 PROGRAM_OFFSET{10};
ProgramResult GenerateVertexShader(const ShaderSetup& setup) {
- std::string out = "#version 430 core\n";
- out += "#extension GL_ARB_separate_shader_objects : enable\n\n";
- out += Decompiler::GetCommonDeclarations();
+ const std::string id = fmt::format("{:016x}", setup.program.unique_identifier);
- out += R"(
+ std::string out = "#extension GL_ARB_separate_shader_objects : enable\n\n";
+ out += "// Shader Unique Id: VS" + id + "\n\n";
+ out += GetCommonDeclarations();
+ out += R"(
layout (location = 0) out vec4 position;
-layout(std140) uniform vs_config {
+layout (std140, binding = EMULATION_UBO_BINDING) uniform vs_config {
vec4 viewport_flip;
uvec4 config_pack; // instance_id, flip_stage, y_direction, padding
uvec4 alpha_test;
};
-)";
-
- if (setup.IsDualProgram()) {
- out += "bool exec_vertex_b();\n";
- }
- ProgramResult program =
- Decompiler::DecompileProgram(setup.program.code, PROGRAM_OFFSET,
- Maxwell3D::Regs::ShaderStage::Vertex, "vertex")
- .value_or(ProgramResult());
+)";
+ ShaderIR program_ir(setup.program.code, PROGRAM_OFFSET);
+ ProgramResult program = Decompile(program_ir, Maxwell3D::Regs::ShaderStage::Vertex, "vertex");
out += program.first;
if (setup.IsDualProgram()) {
+ ShaderIR program_ir_b(setup.program.code_b, PROGRAM_OFFSET);
ProgramResult program_b =
- Decompiler::DecompileProgram(setup.program.code_b, PROGRAM_OFFSET,
- Maxwell3D::Regs::ShaderStage::Vertex, "vertex_b")
- .value_or(ProgramResult());
+ Decompile(program_ir_b, Maxwell3D::Regs::ShaderStage::Vertex, "vertex_b");
+
out += program_b.first;
}
out += R"(
-
void main() {
position = vec4(0.0, 0.0, 0.0, 0.0);
- exec_vertex();
+ execute_vertex();
)";
if (setup.IsDualProgram()) {
- out += " exec_vertex_b();";
+ out += " execute_vertex_b();";
}
out += R"(
-
// Check if the flip stage is VertexB
// Config pack's second value is flip_stage
if (config_pack[1] == 1) {
@@ -74,69 +71,62 @@ void main() {
if (config_pack[1] == 1) {
position.w = 1.0;
}
-}
-
-)";
+})";
return {out, program.second};
}
ProgramResult GenerateGeometryShader(const ShaderSetup& setup) {
- // Version is intentionally skipped in shader generation, it's added by the lazy compilation.
+ const std::string id = fmt::format("{:016x}", setup.program.unique_identifier);
+
std::string out = "#extension GL_ARB_separate_shader_objects : enable\n\n";
- out += Decompiler::GetCommonDeclarations();
- out += "bool exec_geometry();\n";
+ out += "// Shader Unique Id: GS" + id + "\n\n";
+ out += GetCommonDeclarations();
- ProgramResult program =
- Decompiler::DecompileProgram(setup.program.code, PROGRAM_OFFSET,
- Maxwell3D::Regs::ShaderStage::Geometry, "geometry")
- .value_or(ProgramResult());
out += R"(
-out gl_PerVertex {
- vec4 gl_Position;
-};
-
layout (location = 0) in vec4 gs_position[];
layout (location = 0) out vec4 position;
-layout (std140) uniform gs_config {
+layout (std140, binding = EMULATION_UBO_BINDING) uniform gs_config {
vec4 viewport_flip;
uvec4 config_pack; // instance_id, flip_stage, y_direction, padding
uvec4 alpha_test;
};
-void main() {
- exec_geometry();
-}
-
)";
+ ShaderIR program_ir(setup.program.code, PROGRAM_OFFSET);
+ ProgramResult program =
+ Decompile(program_ir, Maxwell3D::Regs::ShaderStage::Geometry, "geometry");
out += program.first;
+
+ out += R"(
+void main() {
+ execute_geometry();
+};)";
+
return {out, program.second};
}
ProgramResult GenerateFragmentShader(const ShaderSetup& setup) {
- std::string out = "#version 430 core\n";
- out += "#extension GL_ARB_separate_shader_objects : enable\n\n";
- out += Decompiler::GetCommonDeclarations();
- out += "bool exec_fragment();\n";
+ const std::string id = fmt::format("{:016x}", setup.program.unique_identifier);
+
+ std::string out = "#extension GL_ARB_separate_shader_objects : enable\n\n";
+ out += "// Shader Unique Id: FS" + id + "\n\n";
+ out += GetCommonDeclarations();
- ProgramResult program =
- Decompiler::DecompileProgram(setup.program.code, PROGRAM_OFFSET,
- Maxwell3D::Regs::ShaderStage::Fragment, "fragment")
- .value_or(ProgramResult());
out += R"(
-layout(location = 0) out vec4 FragColor0;
-layout(location = 1) out vec4 FragColor1;
-layout(location = 2) out vec4 FragColor2;
-layout(location = 3) out vec4 FragColor3;
-layout(location = 4) out vec4 FragColor4;
-layout(location = 5) out vec4 FragColor5;
-layout(location = 6) out vec4 FragColor6;
-layout(location = 7) out vec4 FragColor7;
+layout (location = 0) out vec4 FragColor0;
+layout (location = 1) out vec4 FragColor1;
+layout (location = 2) out vec4 FragColor2;
+layout (location = 3) out vec4 FragColor3;
+layout (location = 4) out vec4 FragColor4;
+layout (location = 5) out vec4 FragColor5;
+layout (location = 6) out vec4 FragColor6;
+layout (location = 7) out vec4 FragColor7;
layout (location = 0) in vec4 position;
-layout (std140) uniform fs_config {
+layout (std140, binding = EMULATION_UBO_BINDING) uniform fs_config {
vec4 viewport_flip;
uvec4 config_pack; // instance_id, flip_stage, y_direction, padding
uvec4 alpha_test;
@@ -166,12 +156,20 @@ bool AlphaFunc(in float value) {
}
}
+)";
+ ShaderIR program_ir(setup.program.code, PROGRAM_OFFSET);
+ ProgramResult program =
+ Decompile(program_ir, Maxwell3D::Regs::ShaderStage::Fragment, "fragment");
+
+ out += program.first;
+
+ out += R"(
void main() {
- exec_fragment();
+ execute_fragment();
}
)";
- out += program.first;
return {out, program.second};
}
-} // namespace OpenGL::GLShader
+
+} // namespace OpenGL::GLShader \ No newline at end of file
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.h b/src/video_core/renderer_opengl/gl_shader_gen.h
index 4fa6d7612..ac5e6917b 100644
--- a/src/video_core/renderer_opengl/gl_shader_gen.h
+++ b/src/video_core/renderer_opengl/gl_shader_gen.h
@@ -10,164 +10,12 @@
#include "common/common_types.h"
#include "video_core/engines/shader_bytecode.h"
+#include "video_core/renderer_opengl/gl_shader_decompiler.h"
+#include "video_core/shader/shader_ir.h"
namespace OpenGL::GLShader {
-constexpr std::size_t MAX_PROGRAM_CODE_LENGTH{0x1000};
-using ProgramCode = std::vector<u64>;
-
-enum : u32 { POSITION_VARYING_LOCATION = 0, GENERIC_VARYING_START_LOCATION = 1 };
-
-class ConstBufferEntry {
- using Maxwell = Tegra::Engines::Maxwell3D::Regs;
-
-public:
- void MarkAsUsed(u64 index, u64 offset, Maxwell::ShaderStage stage) {
- is_used = true;
- this->index = static_cast<unsigned>(index);
- this->stage = stage;
- max_offset = std::max(max_offset, static_cast<unsigned>(offset));
- }
-
- void MarkAsUsedIndirect(u64 index, Maxwell::ShaderStage stage) {
- is_used = true;
- is_indirect = true;
- this->index = static_cast<unsigned>(index);
- this->stage = stage;
- }
-
- bool IsUsed() const {
- return is_used;
- }
-
- bool IsIndirect() const {
- return is_indirect;
- }
-
- unsigned GetIndex() const {
- return index;
- }
-
- unsigned GetSize() const {
- return max_offset + 1;
- }
-
- std::string GetName() const {
- return BufferBaseNames[static_cast<std::size_t>(stage)] + std::to_string(index);
- }
-
- u32 GetHash() const {
- return (static_cast<u32>(stage) << 16) | index;
- }
-
-private:
- static constexpr std::array<const char*, Maxwell::MaxShaderStage> BufferBaseNames = {
- "buffer_vs_c", "buffer_tessc_c", "buffer_tesse_c", "buffer_gs_c", "buffer_fs_c",
- };
-
- bool is_used{};
- bool is_indirect{};
- unsigned index{};
- unsigned max_offset{};
- Maxwell::ShaderStage stage;
-};
-
-class SamplerEntry {
- using Maxwell = Tegra::Engines::Maxwell3D::Regs;
-
-public:
- SamplerEntry(Maxwell::ShaderStage stage, std::size_t offset, std::size_t index,
- Tegra::Shader::TextureType type, bool is_array, bool is_shadow)
- : offset(offset), stage(stage), sampler_index(index), type(type), is_array(is_array),
- is_shadow(is_shadow) {}
-
- std::size_t GetOffset() const {
- return offset;
- }
-
- std::size_t GetIndex() const {
- return sampler_index;
- }
-
- Maxwell::ShaderStage GetStage() const {
- return stage;
- }
-
- std::string GetName() const {
- return std::string(TextureSamplerNames[static_cast<std::size_t>(stage)]) + '_' +
- std::to_string(sampler_index);
- }
-
- std::string GetTypeString() const {
- using Tegra::Shader::TextureType;
- std::string glsl_type;
-
- switch (type) {
- case TextureType::Texture1D:
- glsl_type = "sampler1D";
- break;
- case TextureType::Texture2D:
- glsl_type = "sampler2D";
- break;
- case TextureType::Texture3D:
- glsl_type = "sampler3D";
- break;
- case TextureType::TextureCube:
- glsl_type = "samplerCube";
- break;
- default:
- UNIMPLEMENTED();
- }
- if (is_array)
- glsl_type += "Array";
- if (is_shadow)
- glsl_type += "Shadow";
- return glsl_type;
- }
-
- Tegra::Shader::TextureType GetType() const {
- return type;
- }
-
- bool IsArray() const {
- return is_array;
- }
-
- bool IsShadow() const {
- return is_shadow;
- }
-
- u32 GetHash() const {
- return (static_cast<u32>(stage) << 16) | static_cast<u32>(sampler_index);
- }
-
- static std::string GetArrayName(Maxwell::ShaderStage stage) {
- return TextureSamplerNames[static_cast<std::size_t>(stage)];
- }
-
-private:
- static constexpr std::array<const char*, Maxwell::MaxShaderStage> TextureSamplerNames = {
- "tex_vs", "tex_tessc", "tex_tesse", "tex_gs", "tex_fs",
- };
-
- /// Offset in TSC memory from which to read the sampler object, as specified by the sampling
- /// instruction.
- std::size_t offset;
- Maxwell::ShaderStage stage; ///< Shader stage where this sampler was used.
- std::size_t sampler_index; ///< Value used to index into the generated GLSL sampler array.
- Tegra::Shader::TextureType type; ///< The type used to sample this texture (Texture2D, etc)
- bool is_array; ///< Whether the texture is being sampled as an array texture or not.
- bool is_shadow; ///< Whether the texture is being sampled as a depth texture or not.
-};
-
-struct ShaderEntries {
- std::vector<ConstBufferEntry> const_buffer_entries;
- std::vector<SamplerEntry> texture_samplers;
- std::array<bool, Tegra::Engines::Maxwell3D::Regs::NumClipDistances> clip_distances;
- std::size_t shader_length;
-};
-
-using ProgramResult = std::pair<std::string, ShaderEntries>;
+using VideoCommon::Shader::ProgramCode;
struct ShaderSetup {
explicit ShaderSetup(ProgramCode program_code) {
@@ -177,6 +25,9 @@ struct ShaderSetup {
struct {
ProgramCode code;
ProgramCode code_b; // Used for dual vertex shaders
+ u64 unique_identifier;
+ std::size_t real_size;
+ std::size_t real_size_b;
} program;
/// Used in scenarios where we have a dual vertex shaders
diff --git a/src/video_core/renderer_opengl/gl_state.cpp b/src/video_core/renderer_opengl/gl_state.cpp
index dc0a5ed5e..b7ba59350 100644
--- a/src/video_core/renderer_opengl/gl_state.cpp
+++ b/src/video_core/renderer_opengl/gl_state.cpp
@@ -83,8 +83,6 @@ OpenGLState::OpenGLState() {
draw.read_framebuffer = 0;
draw.draw_framebuffer = 0;
draw.vertex_array = 0;
- draw.vertex_buffer = 0;
- draw.uniform_buffer = 0;
draw.shader_program = 0;
draw.program_pipeline = 0;
@@ -505,7 +503,6 @@ void OpenGLState::ApplySamplers() const {
}
void OpenGLState::ApplyFramebufferState() const {
- // Framebuffer
if (draw.read_framebuffer != cur_state.draw.read_framebuffer) {
glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);
}
@@ -514,16 +511,10 @@ void OpenGLState::ApplyFramebufferState() const {
}
}
-void OpenGLState::ApplyVertexBufferState() const {
- // Vertex array
+void OpenGLState::ApplyVertexArrayState() const {
if (draw.vertex_array != cur_state.draw.vertex_array) {
glBindVertexArray(draw.vertex_array);
}
-
- // Vertex buffer
- if (draw.vertex_buffer != cur_state.draw.vertex_buffer) {
- glBindBuffer(GL_ARRAY_BUFFER, draw.vertex_buffer);
- }
}
void OpenGLState::ApplyDepthClamp() const {
@@ -543,11 +534,7 @@ void OpenGLState::ApplyDepthClamp() const {
void OpenGLState::Apply() const {
ApplyFramebufferState();
- ApplyVertexBufferState();
- // Uniform buffer
- if (draw.uniform_buffer != cur_state.draw.uniform_buffer) {
- glBindBuffer(GL_UNIFORM_BUFFER, draw.uniform_buffer);
- }
+ ApplyVertexArrayState();
// Shader program
if (draw.shader_program != cur_state.draw.shader_program) {
@@ -638,16 +625,6 @@ OpenGLState& OpenGLState::ResetPipeline(GLuint handle) {
return *this;
}
-OpenGLState& OpenGLState::ResetBuffer(GLuint handle) {
- if (draw.vertex_buffer == handle) {
- draw.vertex_buffer = 0;
- }
- if (draw.uniform_buffer == handle) {
- draw.uniform_buffer = 0;
- }
- return *this;
-}
-
OpenGLState& OpenGLState::ResetVertexArray(GLuint handle) {
if (draw.vertex_array == handle) {
draw.vertex_array = 0;
diff --git a/src/video_core/renderer_opengl/gl_state.h b/src/video_core/renderer_opengl/gl_state.h
index 439bfbc98..a5a7c0920 100644
--- a/src/video_core/renderer_opengl/gl_state.h
+++ b/src/video_core/renderer_opengl/gl_state.h
@@ -154,8 +154,6 @@ public:
GLuint read_framebuffer; // GL_READ_FRAMEBUFFER_BINDING
GLuint draw_framebuffer; // GL_DRAW_FRAMEBUFFER_BINDING
GLuint vertex_array; // GL_VERTEX_ARRAY_BINDING
- GLuint vertex_buffer; // GL_ARRAY_BUFFER_BINDING
- GLuint uniform_buffer; // GL_UNIFORM_BUFFER_BINDING
GLuint shader_program; // GL_CURRENT_PROGRAM
GLuint program_pipeline; // GL_PROGRAM_PIPELINE_BINDING
} draw;
@@ -206,10 +204,10 @@ public:
}
/// Apply this state as the current OpenGL state
void Apply() const;
- /// Apply only the state afecting the framebuffer
+ /// Apply only the state affecting the framebuffer
void ApplyFramebufferState() const;
- /// Apply only the state afecting the vertex buffer
- void ApplyVertexBufferState() const;
+ /// Apply only the state affecting the vertex array
+ void ApplyVertexArrayState() const;
/// Set the initial OpenGL state
static void ApplyDefaultState();
/// Resets any references to the given resource
@@ -217,7 +215,6 @@ public:
OpenGLState& ResetSampler(GLuint handle);
OpenGLState& ResetProgram(GLuint handle);
OpenGLState& ResetPipeline(GLuint handle);
- OpenGLState& ResetBuffer(GLuint handle);
OpenGLState& ResetVertexArray(GLuint handle);
OpenGLState& ResetFramebuffer(GLuint handle);
void EmulateViewportWithScissor();
diff --git a/src/video_core/renderer_opengl/gl_stream_buffer.cpp b/src/video_core/renderer_opengl/gl_stream_buffer.cpp
index b97b895a4..d0b14b3f6 100644
--- a/src/video_core/renderer_opengl/gl_stream_buffer.cpp
+++ b/src/video_core/renderer_opengl/gl_stream_buffer.cpp
@@ -15,13 +15,12 @@ MICROPROFILE_DEFINE(OpenGL_StreamBuffer, "OpenGL", "Stream Buffer Orphaning",
namespace OpenGL {
-OGLStreamBuffer::OGLStreamBuffer(GLenum target, GLsizeiptr size, bool prefer_coherent)
- : gl_target(target), buffer_size(size) {
+OGLStreamBuffer::OGLStreamBuffer(GLsizeiptr size, bool vertex_data_usage, bool prefer_coherent)
+ : buffer_size(size) {
gl_buffer.Create();
- glBindBuffer(gl_target, gl_buffer.handle);
GLsizeiptr allocate_size = size;
- if (target == GL_ARRAY_BUFFER) {
+ if (vertex_data_usage) {
// On AMD GPU there is a strange crash in indexed drawing. The crash happens when the buffer
// read position is near the end and is an out-of-bound access to the vertex buffer. This is
// probably a bug in the driver and is related to the usage of vec3<byte> attributes in the
@@ -35,18 +34,17 @@ OGLStreamBuffer::OGLStreamBuffer(GLenum target, GLsizeiptr size, bool prefer_coh
coherent = prefer_coherent;
const GLbitfield flags =
GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | (coherent ? GL_MAP_COHERENT_BIT : 0);
- glBufferStorage(gl_target, allocate_size, nullptr, flags);
- mapped_ptr = static_cast<u8*>(glMapBufferRange(
- gl_target, 0, buffer_size, flags | (coherent ? 0 : GL_MAP_FLUSH_EXPLICIT_BIT)));
+ glNamedBufferStorage(gl_buffer.handle, allocate_size, nullptr, flags);
+ mapped_ptr = static_cast<u8*>(glMapNamedBufferRange(
+ gl_buffer.handle, 0, buffer_size, flags | (coherent ? 0 : GL_MAP_FLUSH_EXPLICIT_BIT)));
} else {
- glBufferData(gl_target, allocate_size, nullptr, GL_STREAM_DRAW);
+ glNamedBufferData(gl_buffer.handle, allocate_size, nullptr, GL_STREAM_DRAW);
}
}
OGLStreamBuffer::~OGLStreamBuffer() {
if (persistent) {
- glBindBuffer(gl_target, gl_buffer.handle);
- glUnmapBuffer(gl_target);
+ glUnmapNamedBuffer(gl_buffer.handle);
}
gl_buffer.Release();
}
@@ -74,7 +72,7 @@ std::tuple<u8*, GLintptr, bool> OGLStreamBuffer::Map(GLsizeiptr size, GLintptr a
invalidate = true;
if (persistent) {
- glUnmapBuffer(gl_target);
+ glUnmapNamedBuffer(gl_buffer.handle);
}
}
@@ -84,7 +82,7 @@ std::tuple<u8*, GLintptr, bool> OGLStreamBuffer::Map(GLsizeiptr size, GLintptr a
(coherent ? GL_MAP_COHERENT_BIT : GL_MAP_FLUSH_EXPLICIT_BIT) |
(invalidate ? GL_MAP_INVALIDATE_BUFFER_BIT : GL_MAP_UNSYNCHRONIZED_BIT);
mapped_ptr = static_cast<u8*>(
- glMapBufferRange(gl_target, buffer_pos, buffer_size - buffer_pos, flags));
+ glMapNamedBufferRange(gl_buffer.handle, buffer_pos, buffer_size - buffer_pos, flags));
mapped_offset = buffer_pos;
}
@@ -95,11 +93,11 @@ void OGLStreamBuffer::Unmap(GLsizeiptr size) {
ASSERT(size <= mapped_size);
if (!coherent && size > 0) {
- glFlushMappedBufferRange(gl_target, buffer_pos - mapped_offset, size);
+ glFlushMappedNamedBufferRange(gl_buffer.handle, buffer_pos - mapped_offset, size);
}
if (!persistent) {
- glUnmapBuffer(gl_target);
+ glUnmapNamedBuffer(gl_buffer.handle);
}
buffer_pos += size;
diff --git a/src/video_core/renderer_opengl/gl_stream_buffer.h b/src/video_core/renderer_opengl/gl_stream_buffer.h
index ae7961bd7..3d18ecb4d 100644
--- a/src/video_core/renderer_opengl/gl_stream_buffer.h
+++ b/src/video_core/renderer_opengl/gl_stream_buffer.h
@@ -13,7 +13,7 @@ namespace OpenGL {
class OGLStreamBuffer : private NonCopyable {
public:
- explicit OGLStreamBuffer(GLenum target, GLsizeiptr size, bool prefer_coherent = false);
+ explicit OGLStreamBuffer(GLsizeiptr size, bool vertex_data_usage, bool prefer_coherent = false);
~OGLStreamBuffer();
GLuint GetHandle() const;
@@ -33,7 +33,6 @@ public:
private:
OGLBuffer gl_buffer;
- GLenum gl_target;
bool coherent = false;
bool persistent = false;
diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp
index 4fd0d66c5..e37b65b38 100644
--- a/src/video_core/renderer_opengl/renderer_opengl.cpp
+++ b/src/video_core/renderer_opengl/renderer_opengl.cpp
@@ -14,6 +14,7 @@
#include "core/core.h"
#include "core/core_timing.h"
#include "core/frontend/emu_window.h"
+#include "core/frontend/scope_acquire_window_context.h"
#include "core/memory.h"
#include "core/perf_stats.h"
#include "core/settings.h"
@@ -97,18 +98,6 @@ static std::array<GLfloat, 3 * 2> MakeOrthographicMatrix(const float width, cons
return matrix;
}
-ScopeAcquireGLContext::ScopeAcquireGLContext(Core::Frontend::EmuWindow& emu_window_)
- : emu_window{emu_window_} {
- if (Settings::values.use_multi_core) {
- emu_window.MakeCurrent();
- }
-}
-ScopeAcquireGLContext::~ScopeAcquireGLContext() {
- if (Settings::values.use_multi_core) {
- emu_window.DoneCurrent();
- }
-}
-
RendererOpenGL::RendererOpenGL(Core::Frontend::EmuWindow& window)
: VideoCore::RendererBase{window} {}
@@ -117,7 +106,6 @@ RendererOpenGL::~RendererOpenGL() = default;
/// Swap buffers (render frame)
void RendererOpenGL::SwapBuffers(
std::optional<std::reference_wrapper<const Tegra::FramebufferConfig>> framebuffer) {
- ScopeAcquireGLContext acquire_context{render_window};
Core::System::GetInstance().GetPerfStats().EndSystemFrame();
@@ -138,7 +126,12 @@ void RendererOpenGL::SwapBuffers(
// Load the framebuffer from memory, draw it to the screen, and swap buffers
LoadFBToScreenInfo(*framebuffer);
- DrawScreen();
+
+ if (renderer_settings.screenshot_requested)
+ CaptureScreenshot();
+
+ DrawScreen(render_window.GetFramebufferLayout());
+
render_window.SwapBuffers();
}
@@ -240,20 +233,20 @@ void RendererOpenGL::InitOpenGLObjects() {
// Generate VAO
vertex_array.Create();
-
state.draw.vertex_array = vertex_array.handle;
- state.draw.vertex_buffer = vertex_buffer.handle;
- state.draw.uniform_buffer = 0;
- state.Apply();
// Attach vertex data to VAO
- glBufferData(GL_ARRAY_BUFFER, sizeof(ScreenRectVertex) * 4, nullptr, GL_STREAM_DRAW);
- glVertexAttribPointer(attrib_position, 2, GL_FLOAT, GL_FALSE, sizeof(ScreenRectVertex),
- (GLvoid*)offsetof(ScreenRectVertex, position));
- glVertexAttribPointer(attrib_tex_coord, 2, GL_FLOAT, GL_FALSE, sizeof(ScreenRectVertex),
- (GLvoid*)offsetof(ScreenRectVertex, tex_coord));
- glEnableVertexAttribArray(attrib_position);
- glEnableVertexAttribArray(attrib_tex_coord);
+ glNamedBufferData(vertex_buffer.handle, sizeof(ScreenRectVertex) * 4, nullptr, GL_STREAM_DRAW);
+ glVertexArrayAttribFormat(vertex_array.handle, attrib_position, 2, GL_FLOAT, GL_FALSE,
+ offsetof(ScreenRectVertex, position));
+ glVertexArrayAttribFormat(vertex_array.handle, attrib_tex_coord, 2, GL_FLOAT, GL_FALSE,
+ offsetof(ScreenRectVertex, tex_coord));
+ glVertexArrayAttribBinding(vertex_array.handle, attrib_position, 0);
+ glVertexArrayAttribBinding(vertex_array.handle, attrib_tex_coord, 0);
+ glEnableVertexArrayAttrib(vertex_array.handle, attrib_position);
+ glEnableVertexArrayAttrib(vertex_array.handle, attrib_tex_coord);
+ glVertexArrayVertexBuffer(vertex_array.handle, 0, vertex_buffer.handle, 0,
+ sizeof(ScreenRectVertex));
// Allocate textures for the screen
screen_info.texture.resource.Create();
@@ -365,14 +358,12 @@ void RendererOpenGL::DrawScreenTriangles(const ScreenInfo& screen_info, float x,
state.texture_units[0].texture = screen_info.display_texture;
state.texture_units[0].swizzle = {GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA};
// Workaround brigthness problems in SMO by enabling sRGB in the final output
- // if it has been used in the frame
- // Needed because of this bug in QT
- // QTBUG-50987
+ // if it has been used in the frame. Needed because of this bug in QT: QTBUG-50987
state.framebuffer_srgb.enabled = OpenGLState::GetsRGBUsed();
state.Apply();
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices.data());
+ glNamedBufferSubData(vertex_buffer.handle, 0, sizeof(vertices), vertices.data());
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- // restore default state
+ // Restore default state
state.framebuffer_srgb.enabled = false;
state.texture_units[0].texture = 0;
state.Apply();
@@ -383,14 +374,13 @@ void RendererOpenGL::DrawScreenTriangles(const ScreenInfo& screen_info, float x,
/**
* Draws the emulated screens to the emulator window.
*/
-void RendererOpenGL::DrawScreen() {
+void RendererOpenGL::DrawScreen(const Layout::FramebufferLayout& layout) {
if (renderer_settings.set_background_color) {
// Update background color before drawing
glClearColor(Settings::values.bg_red, Settings::values.bg_green, Settings::values.bg_blue,
0.0f);
}
- const auto& layout = render_window.GetFramebufferLayout();
const auto& screen = layout.screen;
glViewport(0, 0, layout.width, layout.height);
@@ -414,6 +404,37 @@ void RendererOpenGL::DrawScreen() {
/// Updates the framerate
void RendererOpenGL::UpdateFramerate() {}
+void RendererOpenGL::CaptureScreenshot() {
+ // Draw the current frame to the screenshot framebuffer
+ screenshot_framebuffer.Create();
+ GLuint old_read_fb = state.draw.read_framebuffer;
+ GLuint old_draw_fb = state.draw.draw_framebuffer;
+ state.draw.read_framebuffer = state.draw.draw_framebuffer = screenshot_framebuffer.handle;
+ state.Apply();
+
+ Layout::FramebufferLayout layout{renderer_settings.screenshot_framebuffer_layout};
+
+ GLuint renderbuffer;
+ glGenRenderbuffers(1, &renderbuffer);
+ glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
+ glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB8, layout.width, layout.height);
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);
+
+ DrawScreen(layout);
+
+ glReadPixels(0, 0, layout.width, layout.height, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
+ renderer_settings.screenshot_bits);
+
+ screenshot_framebuffer.Release();
+ state.draw.read_framebuffer = old_read_fb;
+ state.draw.draw_framebuffer = old_draw_fb;
+ state.Apply();
+ glDeleteRenderbuffers(1, &renderbuffer);
+
+ renderer_settings.screenshot_complete_callback();
+ renderer_settings.screenshot_requested = false;
+}
+
static const char* GetSource(GLenum source) {
#define RET(s) \
case GL_DEBUG_SOURCE_##s: \
@@ -427,6 +448,7 @@ static const char* GetSource(GLenum source) {
RET(OTHER);
default:
UNREACHABLE();
+ return "Unknown source";
}
#undef RET
}
@@ -445,6 +467,7 @@ static const char* GetType(GLenum type) {
RET(MARKER);
default:
UNREACHABLE();
+ return "Unknown type";
}
#undef RET
}
@@ -471,7 +494,7 @@ static void APIENTRY DebugHandler(GLenum source, GLenum type, GLuint id, GLenum
/// Initialize the renderer
bool RendererOpenGL::Init() {
- ScopeAcquireGLContext acquire_context{render_window};
+ Core::Frontend::ScopeAcquireWindowContext acquire_context{render_window};
if (GLAD_GL_KHR_debug) {
glEnable(GL_DEBUG_OUTPUT);
diff --git a/src/video_core/renderer_opengl/renderer_opengl.h b/src/video_core/renderer_opengl/renderer_opengl.h
index c0868c0e4..1665018db 100644
--- a/src/video_core/renderer_opengl/renderer_opengl.h
+++ b/src/video_core/renderer_opengl/renderer_opengl.h
@@ -16,6 +16,10 @@ namespace Core::Frontend {
class EmuWindow;
}
+namespace Layout {
+struct FramebufferLayout;
+}
+
namespace OpenGL {
/// Structure used for storing information about the textures for the Switch screen
@@ -35,16 +39,6 @@ struct ScreenInfo {
TextureInfo texture;
};
-/// Helper class to acquire/release OpenGL context within a given scope
-class ScopeAcquireGLContext : NonCopyable {
-public:
- explicit ScopeAcquireGLContext(Core::Frontend::EmuWindow& window);
- ~ScopeAcquireGLContext();
-
-private:
- Core::Frontend::EmuWindow& emu_window;
-};
-
class RendererOpenGL : public VideoCore::RendererBase {
public:
explicit RendererOpenGL(Core::Frontend::EmuWindow& window);
@@ -66,10 +60,12 @@ private:
void ConfigureFramebufferTexture(TextureInfo& texture,
const Tegra::FramebufferConfig& framebuffer);
- void DrawScreen();
+ void DrawScreen(const Layout::FramebufferLayout& layout);
void DrawScreenTriangles(const ScreenInfo& screen_info, float x, float y, float w, float h);
void UpdateFramerate();
+ void CaptureScreenshot();
+
// Loads framebuffer from emulated memory into the display information structure
void LoadFBToScreenInfo(const Tegra::FramebufferConfig& framebuffer);
// Fills active OpenGL texture with the given RGBA color.
@@ -82,6 +78,7 @@ private:
OGLVertexArray vertex_array;
OGLBuffer vertex_buffer;
OGLProgram shader;
+ OGLFramebuffer screenshot_framebuffer;
/// Display information for Switch screen
ScreenInfo screen_info;