Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gl: Avoid UBO/SSBO binding index collisions #12676

Merged
merged 2 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions rpcs3/Emu/RSX/GL/GLCompute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@

namespace gl
{
struct bind_image_view_safe
{
GLuint m_layer;
GLenum m_target;
GLuint m_value;
gl::command_context& m_commands;

bind_image_view_safe(gl::command_context& cmd, GLuint layer, gl::texture_view* value)
: m_layer(layer), m_target(value->target()), m_commands(cmd)
{
m_value = cmd->get_bound_texture(layer, m_target);
value->bind(cmd, layer);
}

~bind_image_view_safe()
{
m_commands->bind_texture(m_layer, m_target, m_value);
}
};

void compute_task::initialize()
{
// Set up optimal kernel size
Expand Down Expand Up @@ -311,11 +331,13 @@ namespace gl
m_sampler.apply_defaults();
}

// This method is callable in sensitive code and must restore the GL state on exit
gl::saved_sampler_state save_0(GL_COMPUTE_BUFFER_SLOT(0), m_sampler);
gl::saved_sampler_state save_1(GL_COMPUTE_BUFFER_SLOT(1), m_sampler);

depth_view->bind(cmd, GL_COMPUTE_BUFFER_SLOT(0));
stencil_view->bind(cmd, GL_COMPUTE_BUFFER_SLOT(1));
gl::bind_image_view_safe(cmd, GL_COMPUTE_BUFFER_SLOT(0), depth_view);
gl::bind_image_view_safe(cmd, GL_COMPUTE_BUFFER_SLOT(1), stencil_view);

dst->bind_range(gl::buffer::target::ssbo, GL_COMPUTE_BUFFER_SLOT(2), out_offset, row_pitch * 4 * region.height);

const int num_invocations = utils::aligned_div(region.width * region.height, optimal_kernel_size * optimal_group_size);
Expand Down Expand Up @@ -360,9 +382,10 @@ namespace gl
m_sampler.apply_defaults();
}

// This method is callable in sensitive code and must restore the GL state on exit
gl::saved_sampler_state save(GL_COMPUTE_BUFFER_SLOT(0), m_sampler);
gl::bind_image_view_safe(cmd, GL_COMPUTE_BUFFER_SLOT(0), data_view);

data_view->bind(cmd, GL_COMPUTE_BUFFER_SLOT(0));
dst->bind_range(gl::buffer::target::ssbo, GL_COMPUTE_BUFFER_SLOT(1), out_offset, row_pitch * 4 * region.height);

const int num_invocations = utils::aligned_div(region.width * region.height, optimal_kernel_size * optimal_group_size);
Expand All @@ -380,7 +403,8 @@ namespace gl
const std::pair<std::string_view, std::string> repl_list[] =
{
{ "%set, ", "" },
{ "%loc", std::to_string(GL_COMPUTE_BUFFER_SLOT(0)) },
{ "%image_slot", std::to_string(GL_COMPUTE_IMAGE_SLOT(0)) },
{ "%ssbo_slot", std::to_string(GL_COMPUTE_BUFFER_SLOT(0)) },
{ "%ws", std::to_string(optimal_group_size) },
{ "%wks", std::to_string(optimal_kernel_size) }
};
Expand Down
6 changes: 3 additions & 3 deletions rpcs3/Emu/RSX/GL/GLVertexProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::string GLVertexDecompilerThread::compareFunction(COMPARE f, const std::stri
void GLVertexDecompilerThread::insertHeader(std::stringstream &OS)
{
OS << "#version 430\n";
OS << "layout(std140, binding = 0) uniform VertexContextBuffer\n";
OS << "layout(std140, binding = " << GL_VERTEX_PARAMS_BIND_SLOT << ") uniform VertexContextBuffer\n";
OS << "{\n";
OS << " mat4 scale_offset_mat;\n";
OS << " ivec4 user_clip_enabled[2];\n";
Expand All @@ -42,7 +42,7 @@ void GLVertexDecompilerThread::insertHeader(std::stringstream &OS)
OS << " float z_far;\n";
OS << "};\n\n";

OS << "layout(std140, binding = 1) uniform VertexLayoutBuffer\n";
OS << "layout(std140, binding = " << GL_VERTEX_LAYOUT_BIND_SLOT << ") uniform VertexLayoutBuffer\n";
OS << "{\n";
OS << " uint vertex_base_index;\n";
OS << " uint vertex_index_offset;\n";
Expand All @@ -66,7 +66,7 @@ void GLVertexDecompilerThread::insertConstants(std::stringstream& OS, const std:
{
if (PI.name.starts_with("vc["))
{
OS << "layout(std140, binding = 2) uniform VertexConstantsBuffer\n";
OS << "layout(std140, binding = " << GL_VERTEX_CONSTANT_BUFFERS_BIND_SLOT << ") uniform VertexConstantsBuffer\n";
OS << "{\n";
OS << " vec4 " << PI.name << ";\n";
OS << "};\n\n";
Expand Down
4 changes: 2 additions & 2 deletions rpcs3/Emu/RSX/GL/glutils/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define GL_STREAM_BUFFER_START (GL_STENCIL_MIRRORS_START + 16)
#define GL_TEMP_IMAGE_SLOT 31

#define UBO_SLOT(x) (x)
#define UBO_SLOT(x) (x + 8)
#define SSBO_SLOT(x) (x)

#define GL_VERTEX_PARAMS_BIND_SLOT UBO_SLOT(0)
Expand All @@ -21,7 +21,7 @@
#define GL_INTERPRETER_VERTEX_BLOCK SSBO_SLOT(0)
#define GL_INTERPRETER_FRAGMENT_BLOCK SSBO_SLOT(1)
#define GL_COMPUTE_BUFFER_SLOT(index) SSBO_SLOT(2 + index)
#define GL_COMPUTE_IMAGE_SLOT(index) UBO_SLOT(index)
#define GL_COMPUTE_IMAGE_SLOT(index) SSBO_SLOT(index)

//Function call wrapped in ARB_DSA vs EXT_DSA compat check
#define DSA_CALL(func, object_name, target, ...)\
Expand Down
6 changes: 6 additions & 0 deletions rpcs3/Emu/RSX/GL/glutils/state_tracker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ namespace gl
glUseProgram(program);
}

GLuint get_bound_texture(GLuint layer, GLenum target)
{
ensure(layer < 48);
return bound_textures[layer][target];
}

void bind_texture(GLuint layer, GLenum target, GLuint name, GLboolean force = GL_FALSE)
{
ensure(layer < 48);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ R"(
#version 450
layout(local_size_x = %ws, local_size_y = 1, local_size_z = 1) in;

#define SSBO_LOCATION(x) (x + %loc)
#define IMAGE_LOCATION(x) (x)
#define IMAGE_LOCATION(x) (x + %image_slot)
#define SSBO_LOCATION(x) (x + %ssbo_slot)

layout(%set, binding=IMAGE_LOCATION(0)) uniform writeonly restrict image2D output2D;

Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/RSX/VK/vkutils/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ namespace vk
VkImageSubresourceRange range = { aspect(), 0, mipmaps(), 0, layers() };
const u32 src_queue_family = info.sharingMode == VK_SHARING_MODE_EXCLUSIVE ? current_queue_family : VK_QUEUE_FAMILY_IGNORED;
const u32 dst_queue_family2 = info.sharingMode == VK_SHARING_MODE_EXCLUSIVE ? dst_queue_family : VK_QUEUE_FAMILY_IGNORED;
change_image_layout(src_queue_cmd, value, current_layout, new_layout, range, current_queue_family, dst_queue_family2, ~0u, 0u);
change_image_layout(src_queue_cmd, value, current_layout, new_layout, range, src_queue_family, dst_queue_family2, ~0u, 0u);
}

current_layout = new_layout;
Expand Down