gl_shader_gen: Reorganize and cleanup lighting code.

- No functional difference.
pull/8/head
bunnei 9 years ago
parent 5f3bad8fb1
commit 76f303538b

@ -318,70 +318,30 @@ static void WriteTevStage(std::string& out, const PicaShaderConfig& config, unsi
out += "next_combiner_buffer.a = last_tex_env_out.a;\n";
}
std::string GenerateFragmentShader(const PicaShaderConfig& config) {
std::string out = R"(
#version 330 core
#define NUM_TEV_STAGES 6
#define NUM_LIGHTS 8
#define LIGHTING_LUT_SIZE 256
#define FLOAT_255 0.99609375
in vec4 primary_color;
in vec2 texcoord[3];
in vec4 normquat;
in vec3 view;
out vec4 color;
struct LightSrc {
vec3 specular_0;
vec3 specular_1;
vec3 diffuse;
vec3 ambient;
vec3 position;
};
layout (std140) uniform shader_data {
vec4 const_color[NUM_TEV_STAGES];
vec4 tev_combiner_buffer_color;
int alphatest_ref;
float depth_offset;
vec3 lighting_global_ambient;
LightSrc light_src[NUM_LIGHTS];
};
uniform sampler2D tex[3];
uniform sampler1D lut[6];
void main() {
vec4 primary_fragment_color = vec4(0.0);
vec4 secondary_fragment_color = vec4(0.0);
)";
/// Writes the code to emulate fragment lighting
static void WriteLighting(std::string& out, const PicaShaderConfig& config) {
// Define lighting globals
out += "vec3 diffuse_sum = vec3(0.0);\n";
out += "vec3 specular_sum = vec3(0.0);\n";
out += "vec3 light_vector = vec3(0.0);\n";
if (config.lighting_enabled) {
// Convert interpolated quaternion to a GL fragment normal
out += "vec3 normal = normalize(vec3(\n";
out += " 2.f*(normquat.x*normquat.z + normquat.y*normquat.w),\n";
out += " 2.f*(normquat.y*normquat.z + normquat.x*normquat.w),\n";
out += " 1.f - 2.f*(normquat.x*normquat.x + normquat.y*normquat.y)));\n";
out += "vec4 secondary_color = vec4(0.0);\n";
out += "vec3 diffuse_sum = vec3(0.0);\n";
out += "vec3 specular_sum = vec3(0.0);\n";
out += "vec3 fragment_position = -view;\n";
out += "vec3 light_vector = vec3(0.0);\n";
out += "vec3 half_angle_vector = vec3(0.0);\n";
out += "float clamp_highlights = 1.0;\n";
out += "float dist_atten = 1.0;\n";
// Gets the index into the specified lookup table for specular lighting
auto GetLutIndex = [&](unsigned light_num, Regs::LightingLutInput input, bool abs) {
auto GetLutIndex = [config](unsigned light_num, Regs::LightingLutInput input, bool abs) {
const std::string half_angle = "normalize(normalize(view) + light_vector)";
std::string index;
switch (input) {
case Regs::LightingLutInput::NH:
index = "dot(normal, half_angle_vector)";
index = "dot(normal, " + half_angle + ")";
break;
case Regs::LightingLutInput::VH:
index = std::string("dot(view, half_angle_vector)");
index = std::string("dot(view, " + half_angle + ")");
break;
case Regs::LightingLutInput::NV:
@ -399,11 +359,11 @@ vec4 secondary_fragment_color = vec4(0.0);
}
if (abs) {
// In the range of [ 0.f, 1.f]
// LUT index is in the range of (0.0, 1.0)
index = config.light_src[light_num].two_sided_diffuse ? "abs(" + index + ")" : "max(" + index + ", 0.f)";
return "clamp(" + index + ", 0.0, FLOAT_255)";
} else {
// In the range of [-1.f, 1.f]
// LUT index is in the range of (-1.0, 1.0)
index = "clamp(" + index + ", -1.0, 1.0)";
return "clamp(((" + index + " < 0) ? " + index + " + 2.0 : " + index + ") / 2.0, 0.0, FLOAT_255)";
}
@ -411,53 +371,100 @@ vec4 secondary_fragment_color = vec4(0.0);
return std::string();
};
// Gets the lighting lookup table value given the specified sampler and index
auto GetLutValue = [](Regs::LightingSampler sampler, std::string lut_index) {
return std::string("texture(lut[" + std::to_string((unsigned)sampler / 4) + "], " +
lut_index + ")[" + std::to_string((unsigned)sampler & 3) + "]");
};
// Write the code to emulate each enabled light
for (unsigned light_index = 0; light_index < config.num_lights; ++light_index) {
unsigned num = config.light_src[light_index].num;
const auto& light_config = config.light_src[light_index];
std::string light_src = "light_src[" + std::to_string(num) + "]";
if (config.light_src[light_index].directional)
// Compute light vector (directional or positional)
if (light_config.directional)
out += "light_vector = normalize(" + light_src + ".position);\n";
else
out += "light_vector = normalize(" + light_src + ".position - fragment_position);\n";
std::string dot_product;
if (config.light_src[light_index].two_sided_diffuse)
dot_product = "abs(dot(light_vector, normal))";
else
dot_product = "max(dot(light_vector, normal), 0.0)";
// Compute distance attenuation value
out += "dist_atten = 1.0;\n";
if (config.light_src[light_index].dist_atten_enabled) {
std::string scale = std::to_string(config.light_src[light_index].dist_atten_scale);
std::string bias = std::to_string(config.light_src[light_index].dist_atten_bias);
std::string lut_index = "(" + scale + " * length(fragment_position - " + light_src + ".position) + " + bias + ")";
std::string clamped_lut_index = "((clamp(" + lut_index + ", 0.0, FLOAT_255)))";
out += "light_vector = normalize(" + light_src + ".position + view);\n";
// Compute dot product of light_vector and normal, adjust if lighting is one-sided or two-sided
std::string dot_product = light_config.two_sided_diffuse ? "abs(dot(light_vector, normal))" : "max(dot(light_vector, normal), 0.0)";
// If enabled, compute distance attenuation value
std::string dist_atten = "1.0";
if (light_config.dist_atten_enabled) {
std::string scale = std::to_string(light_config.dist_atten_scale);
std::string bias = std::to_string(light_config.dist_atten_bias);
std::string lut_index = "(" + scale + " * length(-view - " + light_src + ".position) + " + bias + ")";
lut_index = "((clamp(" + lut_index + ", 0.0, FLOAT_255)))";
const unsigned lut_num = ((unsigned)Regs::LightingSampler::DistanceAttenuation + num);
out += "dist_atten = texture(lut[" + std::to_string(lut_num / 4) + "], " + clamped_lut_index + ")[" + std::to_string(lut_num & 3) + "];\n";
dist_atten = GetLutValue((Regs::LightingSampler)lut_num, lut_index);
}
// Compute primary fragment color (diffuse lighting) function
out += "diffuse_sum += ((light_src[" + std::to_string(num) + "].diffuse * " + dot_product + ") + light_src[" + std::to_string(num) + "].ambient) * dist_atten;\n";
out += "diffuse_sum += ((" + light_src + ".diffuse * " + dot_product + ") + " + light_src + ".ambient) * " + dist_atten + ";\n";
// Compute secondary fragment color (specular lighting) function
out += "half_angle_vector = normalize(normalize(view) + light_vector);\n";
std::string clamped_lut_index = GetLutIndex(num, config.lighting_lut.d0_type, config.lighting_lut.d0_abs);
const unsigned lut_num = (unsigned)Regs::LightingSampler::Distribution0;
std::string lut_lookup = "texture(lut[" + std::to_string(lut_num / 4) + "], " + clamped_lut_index + ")[" + std::to_string(lut_num & 3) + "]";
// If enabled, clamp specular component if lighting result is negative
std::string clamp_highlights = config.clamp_highlights ? "(dot(light_vector, normal) <= 0.0 ? 0.0 : 1.0)" : "1.0";
if (config.clamp_highlights) {
out += "clamp_highlights = (dot(light_vector, normal) <= 0.0) ? 0.0 : 1.0;\n";
}
// Lookup specular distribution 0 LUT value
std::string d0_lut_index = GetLutIndex(num, config.lighting_lut.d0_type, config.lighting_lut.d0_abs);
std::string d0_lut_value = GetLutValue(Regs::LightingSampler::Distribution0, d0_lut_index);
out += "specular_sum += clamp_highlights * " + lut_lookup + " * light_src[" + std::to_string(num) + "].specular_0 * dist_atten;\n";
// Compute secondary fragment color (specular lighting) function
out += "specular_sum += " + clamp_highlights + " * " + d0_lut_value + " * " + light_src + ".specular_0 * " + dist_atten + ";\n";
}
// Sum final lighting result
out += "diffuse_sum += lighting_global_ambient;\n";
out += "primary_fragment_color = vec4(clamp(diffuse_sum, vec3(0.0), vec3(1.0)), 1.0);\n";
out += "secondary_fragment_color = vec4(clamp(specular_sum, vec3(0.0), vec3(1.0)), 1.0);\n";
}
}
std::string GenerateFragmentShader(const PicaShaderConfig& config) {
std::string out = R"(
#version 330 core
#define NUM_TEV_STAGES 6
#define NUM_LIGHTS 8
#define LIGHTING_LUT_SIZE 256
#define FLOAT_255 0.99609375
in vec4 primary_color;
in vec2 texcoord[3];
in vec4 normquat;
in vec3 view;
out vec4 color;
struct LightSrc {
vec3 specular_0;
vec3 specular_1;
vec3 diffuse;
vec3 ambient;
vec3 position;
};
layout (std140) uniform shader_data {
vec4 const_color[NUM_TEV_STAGES];
vec4 tev_combiner_buffer_color;
int alphatest_ref;
float depth_offset;
vec3 lighting_global_ambient;
LightSrc light_src[NUM_LIGHTS];
};
uniform sampler2D tex[3];
uniform sampler1D lut[6];
void main() {
vec4 primary_fragment_color = vec4(0.0);
vec4 secondary_fragment_color = vec4(0.0);
)";
if (config.lighting_enabled)
WriteLighting(out, config);
// Do not do any sort of processing if it's obvious we're not going to pass the alpha test
if (config.alpha_test_func == Regs::CompareFunc::Never) {

Loading…
Cancel
Save