Skip to content

Commit

Permalink
rsx: Validate requested images before attempting to upload them
Browse files Browse the repository at this point in the history
- Do not allow dimensions of 0 to reach the backend APIs
  • Loading branch information
kd-11 committed Jan 30, 2022
1 parent 0e320d1 commit 86919ec
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
10 changes: 10 additions & 0 deletions rpcs3/Emu/RSX/Common/texture_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,7 @@ namespace rsx
{
case rsx::texture_dimension_extended::texture_dimension_1d:
attributes.depth = 1;
attributes.height = 1;
attributes.slice_h = 1;
scale.height = scale.depth = 0.f;
subsurface_count = 1;
Expand Down Expand Up @@ -2113,6 +2114,15 @@ namespace rsx
fmt::throw_exception("Unsupported texture dimension %d", static_cast<int>(extended_dimension));
}

// Validation
if (!attributes.width || !attributes.height || !attributes.depth)
{
rsx_log.warning("Image at address 0x%x has invalid dimensions. Type=%d, Dims=%dx%dx%d",
attributes.address, static_cast<s32>(extended_dimension),
attributes.width, attributes.height, attributes.depth);
return {};
}

if (options.is_compressed_format)
{
// Compressed textures cannot be 1D in some APIs
Expand Down
18 changes: 12 additions & 6 deletions rpcs3/Emu/RSX/GL/GLDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,17 @@ void GLGSRender::load_texture_env()
if (tex.enabled())
{
*sampler_state = m_gl_texture_cache.upload_texture(cmd, tex, m_rtts);

if (m_textures_dirty[i])
m_fs_sampler_states[i].apply(tex, fs_sampler_state[i].get());
}
else
{
*sampler_state = {};
}

if (m_textures_dirty[i] && sampler_state->validate())
{
m_fs_sampler_states[i].apply(tex, fs_sampler_state[i].get());
}

m_textures_dirty[i] = false;
}
}
Expand All @@ -327,12 +329,16 @@ void GLGSRender::load_texture_env()
if (rsx::method_registers.vertex_textures[i].enabled())
{
*sampler_state = m_gl_texture_cache.upload_texture(cmd, rsx::method_registers.vertex_textures[i], m_rtts);

if (m_vertex_textures_dirty[i])
m_vs_sampler_states[i].apply(tex, vs_sampler_state[i].get());
}
else
{
*sampler_state = {};
}

if (m_vertex_textures_dirty[i] && sampler_state->validate())
{
m_vs_sampler_states[i].apply(tex, vs_sampler_state[i].get());
}

m_vertex_textures_dirty[i] = false;
}
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/RSX/RSXThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,7 @@ namespace rsx
auto &tex = rsx::method_registers.fragment_textures[i];
current_fp_texture_state.clear(i);

if (tex.enabled())
if (tex.enabled() && sampler_descriptors[i]->format_class != RSX_FORMAT_CLASS_UNDEFINED)
{
current_fragment_program.texture_params[i].scale[0] = sampler_descriptors[i]->scale_x;
current_fragment_program.texture_params[i].scale[1] = sampler_descriptors[i]->scale_y;
Expand Down
20 changes: 14 additions & 6 deletions rpcs3/Emu/RSX/VK/VKDraw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,14 @@ void VKGSRender::load_texture_env()
{
check_heap_status(VK_HEAP_CHECK_TEXTURE_UPLOAD_STORAGE);
*sampler_state = m_texture_cache.upload_texture(*m_current_command_buffer, tex, m_rtts);
}
else
{
*sampler_state = {};
}

if (sampler_state->validate())
{
if (sampler_state->is_cyclic_reference)
{
check_for_cyclic_refs |= true;
Expand Down Expand Up @@ -295,10 +302,6 @@ void VKGSRender::load_texture_env()
border_color, compare_enabled, depth_compare_mode);
}
}
else
{
*sampler_state = {};
}

m_textures_dirty[i] = false;
}
Expand All @@ -321,7 +324,14 @@ void VKGSRender::load_texture_env()
{
check_heap_status(VK_HEAP_CHECK_TEXTURE_UPLOAD_STORAGE);
*sampler_state = m_texture_cache.upload_texture(*m_current_command_buffer, tex, m_rtts);
}
else
{
*sampler_state = {};
}

if (sampler_state->validate())
{
if (sampler_state->is_cyclic_reference || sampler_state->external_subresource_desc.do_not_cache)
{
check_for_cyclic_refs |= true;
Expand Down Expand Up @@ -353,8 +363,6 @@ void VKGSRender::load_texture_env()
VK_FILTER_NEAREST, VK_FILTER_NEAREST, VK_SAMPLER_MIPMAP_MODE_NEAREST, border_color);
}
}
else
*sampler_state = {};

m_vertex_textures_dirty[i] = false;
}
Expand Down

0 comments on commit 86919ec

Please sign in to comment.