Skip to content

Commit

Permalink
Add OriginalAspect and OriginalAspectRot uniforms (#17123)
Browse files Browse the repository at this point in the history
* Initial implementation of CoreAspect uniform

* float -> float_t

* Possibly fix wii_u building

* vulkan: use float instead of float_t;

* slangp: Advertise support of CoreAspect uniform
by defining _RARCH_HAS_COREASPECT_UNIFORM early in the shader source, just after "#extension GL_GOOGLE_cpp_style_line_directive : require"

* CoreAspect + glsl fix: use glUniform1f()

* Add CoreAspectRot uniform.
It reports CoreAspect value or 1/CoreAspect when the content is rotated by 90 or 270 deg.

* Fixed stupid typo

* Just use _HAS_COREASPECT_UNIFORMS to check for CoreAspect uniforms support (was _RARCH_HAS_COREASPECT_UNIFORMS)

* Rename CoreAspect, CoreAspectRot, _HAS_COREASPECT_UNIFORMS to OriginalAspect, OriginlAspectRot, _HAS_ORIGINALASPECT_UNIFORMS

* GLCore: void Pass::build_semantic_float needs glUniform1f.
...how on earth did it worked for UBO !?

* d3d10,11,12, wrong function called by overlook.

* Add test shader, will remove that before PR

* Fix metal rotated aspect reporting

* remove test shader

* Fix C89 Build

* Use OriginalAspectRotated instead of OriginalAspectRot
  • Loading branch information
kokoko3k authored Nov 2, 2024
1 parent 99610f0 commit da5ecaa
Show file tree
Hide file tree
Showing 21 changed files with 329 additions and 23 deletions.
3 changes: 3 additions & 0 deletions gfx/common/d3d10_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ typedef struct
uint32_t rotation;
uint32_t total_subframes;
uint32_t current_subframe;
float core_aspect;
float core_aspect_rot;

} pass[GFX_MAX_SHADERS];

struct video_shader* shader_preset;
Expand Down
2 changes: 2 additions & 0 deletions gfx/common/d3d11_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ typedef struct
uint32_t rotation;
uint32_t total_subframes;
uint32_t current_subframe;
float core_aspect;
float core_aspect_rot;
} pass[GFX_MAX_SHADERS];

struct video_shader* shader_preset;
Expand Down
2 changes: 2 additions & 0 deletions gfx/common/d3d12_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ typedef struct
uint32_t rotation;
uint32_t total_subframes;
uint32_t current_subframe;
float core_aspect;
float core_aspect_rot;
D3D12_GPU_DESCRIPTOR_HANDLE textures;
D3D12_GPU_DESCRIPTOR_HANDLE samplers;
} pass[GFX_MAX_SHADERS];
Expand Down
10 changes: 10 additions & 0 deletions gfx/drivers/d3d10.c
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,8 @@ static bool d3d10_gfx_set_shader(void* data,
&d3d10->pass[i].frame_count, /* FrameCount */
&d3d10->pass[i].frame_direction, /* FrameDirection */
&d3d10->pass[i].rotation, /* Rotation */
&d3d10->pass[i].core_aspect, /* OriginalAspect */
&d3d10->pass[i].core_aspect_rot, /* OriginalAspectRotated */
&d3d10->pass[i].total_subframes, /* TotalSubFrames */
&d3d10->pass[i].current_subframe,/* CurrentSubFrame */
}
Expand Down Expand Up @@ -2331,6 +2333,14 @@ static bool d3d10_gfx_frame(

d3d10->pass[i].rotation = retroarch_get_rotation();

d3d10->pass[i].core_aspect = video_driver_get_core_aspect();

/* OriginalAspectRotated: return 1/aspect for 90 and 270 rotated content */
d3d10->pass[i].core_aspect_rot = video_driver_get_core_aspect();
uint32_t rot = retroarch_get_rotation();
if (rot == 1 || rot == 3)
d3d10->pass[i].core_aspect_rot = 1/d3d10->pass[i].core_aspect_rot;

/* Sub-frame info for multiframe shaders (per real content frame).
Should always be 1 for non-use of subframes */
if (!(d3d10->flags & D3D10_ST_FLAG_FRAME_DUPE_LOCK))
Expand Down
10 changes: 10 additions & 0 deletions gfx/drivers/d3d11.c
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,8 @@ static bool d3d11_gfx_set_shader(void* data, enum rarch_shader_type type, const
&d3d11->pass[i].frame_count, /* FrameCount */
&d3d11->pass[i].frame_direction, /* FrameDirection */
&d3d11->pass[i].rotation, /* Rotation */
&d3d11->pass[i].core_aspect, /* OriginalAspect */
&d3d11->pass[i].core_aspect_rot, /* OriginalAspectRotated */
&d3d11->pass[i].total_subframes, /* TotalSubFrames */
&d3d11->pass[i].current_subframe,/* CurrentSubFrame */
}
Expand Down Expand Up @@ -3094,6 +3096,14 @@ static bool d3d11_gfx_frame(

d3d11->pass[i].rotation = retroarch_get_rotation();

d3d11->pass[i].core_aspect = video_driver_get_core_aspect();

/* OriginalAspectRotated: return 1/aspect for 90 and 270 rotated content */
d3d11->pass[i].core_aspect_rot = video_driver_get_core_aspect();
uint32_t rot = retroarch_get_rotation();
if (rot == 1 || rot == 3)
d3d11->pass[i].core_aspect_rot = 1/d3d11->pass[i].core_aspect_rot;

/* Sub-frame info for multiframe shaders (per real content frame).
Should always be 1 for non-use of subframes */
if (!(d3d11->flags & D3D11_ST_FLAG_FRAME_DUPE_LOCK))
Expand Down
10 changes: 10 additions & 0 deletions gfx/drivers/d3d12.c
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,8 @@ static bool d3d12_gfx_set_shader(void* data, enum rarch_shader_type type, const
&d3d12->pass[i].frame_count, /* FrameCount */
&d3d12->pass[i].frame_direction, /* FrameDirection */
&d3d12->pass[i].rotation, /* Rotation */
&d3d12->pass[i].core_aspect, /* OriginalAspect */
&d3d12->pass[i].core_aspect_rot, /* OriginalAspectRotated */
&d3d12->pass[i].total_subframes, /* TotalSubFrames */
&d3d12->pass[i].current_subframe,/* CurrentSubFrame */
}
Expand Down Expand Up @@ -3595,6 +3597,14 @@ static bool d3d12_gfx_frame(

d3d12->pass[i].rotation = retroarch_get_rotation();

d3d12->pass[i].core_aspect = video_driver_get_core_aspect();

/* OriginalAspectRotated: return 1/aspect for 90 and 270 rotated content */
d3d12->pass[i].core_aspect_rot = video_driver_get_core_aspect();
uint32_t rot = retroarch_get_rotation();
if (rot == 1 || rot == 3)
d3d12->pass[i].core_aspect_rot = 1/d3d12->pass[i].core_aspect_rot;

/* Sub-frame info for multiframe shaders (per real content frame).
Should always be 1 for non-use of subframes */
if (!(d3d12->flags & D3D12_ST_FLAG_FRAME_DUPE_LOCK))
Expand Down
9 changes: 9 additions & 0 deletions gfx/drivers/gl3.c
Original file line number Diff line number Diff line change
Expand Up @@ -2744,6 +2744,15 @@ static bool gl3_frame(void *data, const void *frame,
#endif
gl3_filter_chain_set_rotation(gl->filter_chain, retroarch_get_rotation());

gl3_filter_chain_set_core_aspect(gl->filter_chain, video_driver_get_core_aspect());

/* OriginalAspectRotated: return 1/aspect for 90 and 270 rotated content */
uint32_t rot = retroarch_get_rotation();
float core_aspect_rot = video_driver_get_core_aspect();
if (rot == 1 || rot == 3)
core_aspect_rot = 1/core_aspect_rot;
gl3_filter_chain_set_core_aspect_rot(gl->filter_chain, core_aspect_rot);

/* Sub-frame info for multiframe shaders (per real content frame).
Should always be 1 for non-use of subframes*/
if (!(gl->flags & GL3_FLAG_FRAME_DUPE_LOCK))
Expand Down
25 changes: 22 additions & 3 deletions gfx/drivers/gx2_gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ static bool wiiu_init_frame_textures(wiiu_video_t *wiiu, unsigned width, unsigne
static void gx2_update_uniform_block(wiiu_video_t *wiiu,
int pass, float *ubo, int id,
int size, int uniformVarCount, GX2UniformVar *uniformVars,
uint64_t frame_count, int32_t frame_direction, uint32_t rotation)
uint64_t frame_count, int32_t frame_direction, uint32_t rotation, float core_aspect, float core_aspect_rot)
{
unsigned i;
for (i = 0; i < uniformVarCount; i++)
Expand Down Expand Up @@ -1723,6 +1723,18 @@ static void gx2_update_uniform_block(wiiu_video_t *wiiu,
continue;
}

if (string_is_equal(id, "OriginalAspect"))
{
*dst = core_aspect;
continue;
}

if (string_is_equal(id, "OriginalAspectRotated"))
{
*dst = core_aspect_rot;
continue;
}

if (string_is_equal(id, "OriginalSize"))
{
((GX2_vec4 *)dst)->x = wiiu->texture.surface.width;
Expand Down Expand Up @@ -1940,6 +1952,13 @@ static bool gx2_frame(void *data, const void *frame,
#endif
uint32_t rotation = retroarch_get_rotation();

float core_aspect = video_driver_get_core_aspect();

/* OriginalAspectRotated: return 1/aspect for 90 and 270 rotated content */
float core_aspect_rot = core_aspect;
if (rotation == 1 || rotation == 3)
core_aspect_rot = 1/core_aspect;

for (i = 0; i < wiiu->shader_preset->passes; i++)
{
unsigned j;
Expand All @@ -1950,7 +1969,7 @@ static bool gx2_frame(void *data, const void *frame,
gx2_update_uniform_block(wiiu, i, wiiu->pass[i].vs_ubos[j], j,
wiiu->pass[i].gfd->vs->uniformBlocks[j].size,
wiiu->pass[i].gfd->vs->uniformVarCount, wiiu->pass[i].gfd->vs->uniformVars,
frame_count, frame_direction, rotation);
frame_count, frame_direction, rotation, core_aspect, core_aspect_rot);
GX2SetVertexUniformBlock(wiiu->pass[i].gfd->vs->uniformBlocks[j].offset,
wiiu->pass[i].gfd->vs->uniformBlocks[j].size, wiiu->pass[i].vs_ubos[j]);
}
Expand All @@ -1962,7 +1981,7 @@ static bool gx2_frame(void *data, const void *frame,
gx2_update_uniform_block(wiiu, i, wiiu->pass[i].ps_ubos[j], j,
wiiu->pass[i].gfd->ps->uniformBlocks[j].size,
wiiu->pass[i].gfd->ps->uniformVarCount, wiiu->pass[i].gfd->ps->uniformVars,
frame_count, frame_direction, rotation);
frame_count, frame_direction, rotation, core_aspect, core_aspect_rot);
GX2SetPixelUniformBlock(wiiu->pass[i].gfd->ps->uniformBlocks[j].offset,
wiiu->pass[i].gfd->ps->uniformBlocks[j].size, wiiu->pass[i].ps_ubos[j]);
}
Expand Down
13 changes: 13 additions & 0 deletions gfx/drivers/metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,8 @@ typedef struct MTLALIGN(16)
uint32_t frame_count;
int32_t frame_direction;
uint32_t rotation;
float_t core_aspect;
float_t core_aspect_rot;
pass_semantics_t semantics;
MTLViewport viewport;
__unsafe_unretained id<MTLRenderPipelineState> _state;
Expand Down Expand Up @@ -1579,6 +1581,15 @@ - (void)drawWithContext:(Context *)ctx

_engine.pass[i].rotation = retroarch_get_rotation();

_engine.pass[i].core_aspect = video_driver_get_core_aspect();

/* OriginalAspectRotated: return 1/aspect for 90 and 270 rotated content */
int rot = retroarch_get_rotation();
float core_aspect_rot = video_driver_get_core_aspect();
if (rot == 1 || rot == 3)
core_aspect_rot = 1/core_aspect_rot;
_engine.pass[i].core_aspect_rot = core_aspect_rot;

for (j = 0; j < SLANG_CBUFFER_MAX; j++)
{
id<MTLBuffer> buffer = _engine.pass[i].buffers[j];
Expand Down Expand Up @@ -1836,6 +1847,8 @@ - (BOOL)setShaderFromPath:(NSString *)path
&_engine.pass[i].frame_count, /* FrameCount */
&_engine.pass[i].frame_direction, /* FrameDirection */
&_engine.pass[i].rotation, /* Rotation */
&_engine.pass[i].core_aspect, /* OriginalAspect */
&_engine.pass[i].core_aspect_rot, /* OriginalAspectRotated */
}
};
/* clang-format on */
Expand Down
11 changes: 11 additions & 0 deletions gfx/drivers/vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -4574,6 +4574,17 @@ static bool vulkan_frame(void *data, const void *frame,
vulkan_filter_chain_set_rotation(
(vulkan_filter_chain_t*)vk->filter_chain, retroarch_get_rotation());

vulkan_filter_chain_set_core_aspect(
(vulkan_filter_chain_t*)vk->filter_chain, video_driver_get_core_aspect());

/* OriginalAspectRotated: return 1/aspect for 90 and 270 rotated content */
uint32_t rot = retroarch_get_rotation();
float core_aspect_rot = video_driver_get_core_aspect();
if (rot == 1 || rot == 3)
core_aspect_rot = 1/core_aspect_rot;
vulkan_filter_chain_set_core_aspect_rot(
(vulkan_filter_chain_t*)vk->filter_chain, core_aspect_rot);

/* Render offscreen filter chain passes. */
{
/* Set the source texture in the filter chain */
Expand Down
8 changes: 6 additions & 2 deletions gfx/drivers_shader/glslang_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,21 @@ bool glslang_read_shader_file(const char *path,
goto error;
}

/* Add defines about supported retroarch features */
if (!string_list_append(output, "#define _HAS_ORIGINALASPECT_UNIFORMS", attr))
goto error;

/* At least VIM treats the first line as line #1,
* so offset everything by one. */
snprintf(tmp, sizeof(tmp), "#line %u \"%s\"", root_file ? 2 : 1, basename);
if (!string_list_append(output, tmp, attr))
goto error;

/* Loop through lines of file */
for (i = root_file ? 1 : 0; i < lines.size; i++)
{
const char *line = lines.elems[i].data;

/* Check for 'include' statements */
bool include_optional = !strncmp("#pragma include_optional ", line, STRLEN_CONST("#pragma include_optional "));
if ( !strncmp("#include ", line, STRLEN_CONST("#include ")) || include_optional )
Expand Down
85 changes: 85 additions & 0 deletions gfx/drivers_shader/shader_gl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,16 @@ class Pass
rotation = rot;
}

void set_core_aspect(float coreaspect)
{
core_aspect = coreaspect;
}

void set_core_aspect_rot(float coreaspectrot)
{
core_aspect_rot = coreaspectrot;
}

void set_shader_subframes(uint32_t tot_subframes)
{
total_subframes = tot_subframes;
Expand Down Expand Up @@ -855,6 +865,8 @@ class Pass
slang_semantic semantic, uint32_t value);
void build_semantic_int(uint8_t *data,
slang_semantic semantic, int32_t value);
void build_semantic_float(uint8_t *data,
slang_semantic semantic, float value);
void build_semantic_parameter(uint8_t *data, unsigned index, float value);
void build_semantic_texture_vec4(uint8_t *data,
slang_texture_semantic semantic,
Expand All @@ -872,6 +884,8 @@ class Pass
unsigned frame_count_period = 0;
int32_t frame_direction = 1;
uint32_t rotation = 0;
float core_aspect = 0;
float core_aspect_rot = 0;
unsigned pass_number = 0;
uint32_t total_subframes = 1;
uint32_t current_subframe = 1;
Expand Down Expand Up @@ -1080,6 +1094,8 @@ bool Pass::init_pipeline()
reflect_parameter("FrameCount", reflection.semantics[SLANG_SEMANTIC_FRAME_COUNT]);
reflect_parameter("FrameDirection", reflection.semantics[SLANG_SEMANTIC_FRAME_DIRECTION]);
reflect_parameter("Rotation", reflection.semantics[SLANG_SEMANTIC_ROTATION]);
reflect_parameter("OriginalAspect", reflection.semantics[SLANG_SEMANTIC_CORE_ASPECT]);
reflect_parameter("OriginalAspectRotated", reflection.semantics[SLANG_SEMANTIC_CORE_ASPECT_ROT]);
reflect_parameter("TotalSubFrames", reflection.semantics[SLANG_SEMANTIC_TOTAL_SUBFRAMES]);
reflect_parameter("CurrentSubFrame", reflection.semantics[SLANG_SEMANTIC_CURRENT_SUBFRAME]);

Expand Down Expand Up @@ -1319,6 +1335,38 @@ void Pass::build_semantic_int(uint8_t *data, slang_semantic semantic,
}
}

void Pass::build_semantic_float(uint8_t *data, slang_semantic semantic,
float value)
{
auto &refl = reflection.semantics[semantic];

if (data && refl.uniform)
{
if (refl.location.ubo_vertex >= 0 || refl.location.ubo_fragment >= 0)
{
if (refl.location.ubo_vertex >= 0)
glUniform1f(refl.location.ubo_vertex, value);
if (refl.location.ubo_fragment >= 0)
glUniform1f(refl.location.ubo_fragment, value);
}
else
*reinterpret_cast<float *>(data + reflection.semantics[semantic].ubo_offset) = value;
}

if (refl.push_constant)
{
if (refl.location.push_vertex >= 0 || refl.location.push_fragment >= 0)
{
if (refl.location.push_vertex >= 0)
glUniform1f(refl.location.push_vertex, value);
if (refl.location.push_fragment >= 0)
glUniform1f(refl.location.push_fragment, value);
}
else
*reinterpret_cast<float *>(push_constant_buffer.data() + refl.push_constant_offset) = value;
}
}

void Pass::build_semantic_texture(uint8_t *buffer,
slang_texture_semantic semantic, const Texture &texture)
{
Expand Down Expand Up @@ -1515,6 +1563,12 @@ void Pass::build_semantics(uint8_t *buffer,
build_semantic_uint(buffer, SLANG_SEMANTIC_ROTATION,
rotation);

build_semantic_float(buffer, SLANG_SEMANTIC_CORE_ASPECT,
core_aspect);

build_semantic_float(buffer, SLANG_SEMANTIC_CORE_ASPECT_ROT,
core_aspect_rot);

build_semantic_uint(buffer, SLANG_SEMANTIC_TOTAL_SUBFRAMES,
total_subframes);
build_semantic_uint(buffer, SLANG_SEMANTIC_CURRENT_SUBFRAME,
Expand Down Expand Up @@ -1764,6 +1818,8 @@ struct gl3_filter_chain
void set_frame_count_period(unsigned pass, unsigned period);
void set_frame_direction(int32_t direction);
void set_rotation(uint32_t rot);
void set_core_aspect(float coreaspect);
void set_core_aspect_rot(float coreaspectrot);
void set_shader_subframes(uint32_t tot_subframes);
void set_current_shader_subframe(uint32_t cur_subframe);
#ifdef GL3_ROLLING_SCANLINE_SIMULATION
Expand Down Expand Up @@ -2259,6 +2315,21 @@ void gl3_filter_chain::set_rotation(uint32_t rot)
passes[i]->set_rotation(rot);
}

void gl3_filter_chain::set_core_aspect(float coreaspect)
{
unsigned i;
for (i = 0; i < passes.size(); i++)
passes[i]->set_core_aspect(coreaspect);
}

void gl3_filter_chain::set_core_aspect_rot(float coreaspectrot)
{
unsigned i;
for (i = 0; i < passes.size(); i++)
passes[i]->set_core_aspect_rot(coreaspectrot);
}


void gl3_filter_chain::set_shader_subframes(uint32_t tot_subframes)
{
unsigned i;
Expand Down Expand Up @@ -2702,6 +2773,20 @@ void gl3_filter_chain_set_rotation(
chain->set_rotation(rot);
}

void gl3_filter_chain_set_core_aspect(
gl3_filter_chain_t *chain,
float coreaspect)
{
chain->set_core_aspect(coreaspect);
}

void gl3_filter_chain_set_core_aspect_rot(
gl3_filter_chain_t *chain,
float coreaspectrot)
{
chain->set_core_aspect_rot(coreaspectrot);
}

void gl3_filter_chain_set_shader_subframes(
gl3_filter_chain_t *chain,
uint32_t tot_subframes)
Expand Down
Loading

0 comments on commit da5ecaa

Please sign in to comment.