Skip to content

Commit

Permalink
Swapped R and B channels in lut-default.png, and adjusted D3D9 LUT-ap…
Browse files Browse the repository at this point in the history
…plication behaviour. (mamedev#11004) [Ryan Holtz]

* lut-default.png: Swapped red and blue channels (fixes GitHub mamedev#11001).
* render/d3d/d3dlsl.cpp: Changed screen LUT application to be applied during the color convolution pass on raster systems.
  • Loading branch information
MooglyGuy authored Mar 19, 2023
1 parent 4ff301b commit ba5ec29
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
Binary file modified artwork/lut-default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 43 additions & 1 deletion hlsl/color.fx
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz
// copyright-holders:Ryan Holtz, W. M. Martinez
//-----------------------------------------------------------------------------
// Color-Convolution Effect
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Macros
//-----------------------------------------------------------------------------

#define LUT_TEXTURE_WIDTH 4096.0f
#define LUT_SIZE 64.0f
#define LUT_SCALE float2(1.0f / LUT_TEXTURE_WIDTH, 1.0f / LUT_SIZE)

//-----------------------------------------------------------------------------
// Sampler Definitions
//-----------------------------------------------------------------------------

texture Diffuse;
texture LutTexture;

sampler DiffuseSampler = sampler_state
{
Expand All @@ -21,6 +30,35 @@ sampler DiffuseSampler = sampler_state
AddressW = CLAMP;
};

sampler2D LutSampler = sampler_state
{
Texture = <LutTexture>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
AddressW = CLAMP;
};

//-----------------------------------------------------------------------------
// Utilities
//-----------------------------------------------------------------------------

float3 apply_lut(float3 color)
{
// NOTE: Do not change the order of parameters here.
float3 lutcoord = float3((color.rg * (LUT_SIZE - 1.0f) + 0.5f) *
LUT_SCALE, color.b * (LUT_SIZE - 1.0f));
float shift = floor(lutcoord.z);

lutcoord.x += shift * LUT_SCALE.y;
color.rgb = lerp(tex2D(LutSampler, lutcoord.xy).rgb, tex2D(LutSampler,
float2(lutcoord.x + LUT_SCALE.y, lutcoord.y)).rgb,
lutcoord.z - shift);
return color;
}

//-----------------------------------------------------------------------------
// Vertex Definitions
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -83,11 +121,15 @@ uniform float3 BluRatios = float3(0.0f, 0.0f, 1.0f);
uniform float3 Offset = float3(0.0f, 0.0f, 0.0f);
uniform float3 Scale = float3(1.0f, 1.0f, 1.0f);
uniform float Saturation = 1.0f;
uniform bool LutEnable;

float4 ps_main(PS_INPUT Input) : COLOR
{
float4 BaseTexel = tex2D(DiffuseSampler, Input.TexCoord);

if (LutEnable)
BaseTexel.rgb = apply_lut(BaseTexel.rgb);

float3 OutRGB = BaseTexel.rgb;

// RGB Tint & Shift
Expand Down
6 changes: 1 addition & 5 deletions hlsl/primary.fx
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,7 @@ uniform bool UiLutEnable;

float4 ps_screen_main(PS_INPUT Input) : COLOR
{
float4 BaseTexel = tex2D(DiffuseSampler, Input.TexCoord);

if (LutEnable)
BaseTexel.rgb = apply_lut(BaseTexel.rgb);
return BaseTexel;
return tex2D(DiffuseSampler, Input.TexCoord);
}

float4 ps_vector_buffer_main(PS_INPUT Input) : COLOR
Expand Down
12 changes: 7 additions & 5 deletions src/osd/modules/render/d3d/d3dhlsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,7 @@ int shaders::create_resources()
color_effect->add_uniform("Scale", uniform::UT_VEC3, uniform::CU_COLOR_SCALE);
color_effect->add_uniform("Saturation", uniform::UT_FLOAT, uniform::CU_COLOR_SATURATION);
color_effect->add_uniform("SourceDims", uniform::UT_VEC2, uniform::CU_SOURCE_DIMS);
color_effect->add_uniform("LutEnable", uniform::UT_BOOL, uniform::CU_LUT_ENABLE);

deconverge_effect->add_uniform("ConvergeX", uniform::UT_VEC3, uniform::CU_CONVERGE_LINEAR_X);
deconverge_effect->add_uniform("ConvergeY", uniform::UT_VEC3, uniform::CU_CONVERGE_LINEAR_Y);
Expand Down Expand Up @@ -932,17 +933,13 @@ int shaders::create_resources()

prescale_point_effect->add_uniform("SourceDims", uniform::UT_VEC2, uniform::CU_SOURCE_DIMS);

default_effect->add_uniform("LutEnable", uniform::UT_BOOL, uniform::CU_LUT_ENABLE);
default_effect->add_uniform("UiLutEnable", uniform::UT_BOOL, uniform::CU_UI_LUT_ENABLE);

ui_effect->add_uniform("LutEnable", uniform::UT_BOOL, uniform::CU_LUT_ENABLE);
ui_effect->add_uniform("UiLutEnable", uniform::UT_BOOL, uniform::CU_UI_LUT_ENABLE);

ui_wrap_effect->add_uniform("LutEnable", uniform::UT_BOOL, uniform::CU_LUT_ENABLE);
ui_wrap_effect->add_uniform("UiLutEnable", uniform::UT_BOOL, uniform::CU_UI_LUT_ENABLE);

vector_buffer_effect->add_uniform("LutEnable", uniform::UT_BOOL, uniform::CU_LUT_ENABLE);
vector_buffer_effect->add_uniform("UiLutEnable", uniform::UT_BOOL, uniform::CU_UI_LUT_ENABLE);

return 0;
}
Expand Down Expand Up @@ -1159,6 +1156,8 @@ int shaders::color_convolution_pass(d3d_render_target *rt, int source_index, pol
uint32_t tint = (uint32_t)poly->tint();
float prim_tint[3] = { ((tint >> 16) & 0xff) / 255.0f, ((tint >> 8) & 0xff) / 255.0f, (tint & 0xff) / 255.0f };
curr_effect->set_vector("PrimTint", 3, prim_tint);
curr_effect->set_texture("LutTexture", !lut_texture ? nullptr : lut_texture->get_finaltex());
curr_effect->set_bool("UiLutEnable", false);

next_index = rt->next_index(next_index);
blit(rt->source_surface[next_index].Get(), false, D3DPT_TRIANGLELIST, 0, 2);
Expand Down Expand Up @@ -1467,6 +1466,7 @@ int shaders::vector_buffer_pass(d3d_render_target *rt, int source_index, poly_in

curr_effect->set_texture("Diffuse", rt->target_texture[next_index].Get());
curr_effect->set_texture("LutTexture", !lut_texture ? nullptr : lut_texture->get_finaltex());
curr_effect->set_bool("UiLutEnable", false);

// we need to clear the vector render target here
next_index = rt->next_index(next_index);
Expand All @@ -1486,7 +1486,9 @@ int shaders::screen_pass(d3d_render_target *rt, int source_index, poly_info *pol
curr_effect->update_uniforms();

curr_effect->set_texture("Diffuse", rt->target_texture[next_index].Get());
curr_effect->set_texture("LutTexture", !lut_texture ? nullptr : lut_texture->get_finaltex());
curr_effect->set_texture("LutTexture", nullptr);
curr_effect->set_bool("LutEnable", false);
curr_effect->set_bool("UiLutEnable", false);

blit(backbuffer.Get(), false, poly->type(), vertnum, poly->count());

Expand Down

0 comments on commit ba5ec29

Please sign in to comment.