citra-emu
/
citra-canary
Archived
1
0
Fork 0

gl_rasterizer: implement procedural texture

This commit is contained in:
wwylele 2017-05-05 15:25:04 +03:00
parent ade45b5b99
commit 4d62e75fb2
6 changed files with 600 additions and 7 deletions

View File

@ -55,6 +55,12 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
uniform_block_data.fog_lut_dirty = true;
uniform_block_data.proctex_noise_lut_dirty = true;
uniform_block_data.proctex_color_map_dirty = true;
uniform_block_data.proctex_alpha_map_dirty = true;
uniform_block_data.proctex_lut_dirty = true;
uniform_block_data.proctex_diff_lut_dirty = true;
// Set vertex attributes
glVertexAttribPointer(GLShader::ATTRIBUTE_POSITION, 4, GL_FLOAT, GL_FALSE,
sizeof(HardwareVertex), (GLvoid*)offsetof(HardwareVertex, position));
@ -115,6 +121,51 @@ RasterizerOpenGL::RasterizerOpenGL() : shader_dirty(true) {
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Setup the noise LUT for proctex
proctex_noise_lut.Create();
state.proctex_noise_lut.texture_1d = proctex_noise_lut.handle;
state.Apply();
glActiveTexture(GL_TEXTURE10);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Setup the color map for proctex
proctex_color_map.Create();
state.proctex_color_map.texture_1d = proctex_color_map.handle;
state.Apply();
glActiveTexture(GL_TEXTURE11);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Setup the alpha map for proctex
proctex_alpha_map.Create();
state.proctex_alpha_map.texture_1d = proctex_alpha_map.handle;
state.Apply();
glActiveTexture(GL_TEXTURE12);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RG32F, 128, 0, GL_RG, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Setup the LUT for proctex
proctex_lut.Create();
state.proctex_lut.texture_1d = proctex_lut.handle;
state.Apply();
glActiveTexture(GL_TEXTURE13);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Setup the difference LUT for proctex
proctex_diff_lut.Create();
state.proctex_diff_lut.texture_1d = proctex_diff_lut.handle;
state.Apply();
glActiveTexture(GL_TEXTURE14);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA32F, 256, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Sync fixed function OpenGL state
SyncCullMode();
SyncBlendEnabled();
@ -272,6 +323,36 @@ void RasterizerOpenGL::DrawTriangles() {
uniform_block_data.fog_lut_dirty = false;
}
// Sync the proctex noise lut
if (uniform_block_data.proctex_noise_lut_dirty) {
SyncProcTexNoiseLUT();
uniform_block_data.proctex_noise_lut_dirty = false;
}
// Sync the proctex color map
if (uniform_block_data.proctex_color_map_dirty) {
SyncProcTexColorMap();
uniform_block_data.proctex_color_map_dirty = false;
}
// Sync the proctex alpha map
if (uniform_block_data.proctex_alpha_map_dirty) {
SyncProcTexAlphaMap();
uniform_block_data.proctex_alpha_map_dirty = false;
}
// Sync the proctex lut
if (uniform_block_data.proctex_lut_dirty) {
SyncProcTexLUT();
uniform_block_data.proctex_lut_dirty = false;
}
// Sync the proctex difference lut
if (uniform_block_data.proctex_diff_lut_dirty) {
SyncProcTexDiffLUT();
uniform_block_data.proctex_diff_lut_dirty = false;
}
// Sync the uniform data
if (uniform_block_data.dirty) {
glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformData), &uniform_block_data.data,
@ -354,6 +435,47 @@ void RasterizerOpenGL::NotifyPicaRegisterChanged(u32 id) {
uniform_block_data.fog_lut_dirty = true;
break;
// ProcTex state
case PICA_REG_INDEX(texturing.proctex):
case PICA_REG_INDEX(texturing.proctex_lut):
case PICA_REG_INDEX(texturing.proctex_lut_offset):
shader_dirty = true;
break;
case PICA_REG_INDEX(texturing.proctex_noise_u):
case PICA_REG_INDEX(texturing.proctex_noise_v):
case PICA_REG_INDEX(texturing.proctex_noise_frequency):
SyncProcTexNoise();
break;
case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[0], 0xb0):
case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[1], 0xb1):
case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[2], 0xb2):
case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[3], 0xb3):
case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[4], 0xb4):
case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[5], 0xb5):
case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[6], 0xb6):
case PICA_REG_INDEX_WORKAROUND(texturing.proctex_lut_data[7], 0xb7):
using Pica::TexturingRegs;
switch (regs.texturing.proctex_lut_config.ref_table.Value()) {
case TexturingRegs::ProcTexLutTable::Noise:
uniform_block_data.proctex_noise_lut_dirty = true;
break;
case TexturingRegs::ProcTexLutTable::ColorMap:
uniform_block_data.proctex_color_map_dirty = true;
break;
case TexturingRegs::ProcTexLutTable::AlphaMap:
uniform_block_data.proctex_alpha_map_dirty = true;
break;
case TexturingRegs::ProcTexLutTable::Color:
uniform_block_data.proctex_lut_dirty = true;
break;
case TexturingRegs::ProcTexLutTable::ColorDiff:
uniform_block_data.proctex_diff_lut_dirty = true;
break;
}
break;
// Alpha test
case PICA_REG_INDEX(framebuffer.output_merger.alpha_test):
SyncAlphaTest();
@ -1072,6 +1194,35 @@ void RasterizerOpenGL::SetShader() {
glUniform1i(uniform_fog_lut, 9);
}
GLuint uniform_proctex_noise_lut =
glGetUniformLocation(shader->shader.handle, "proctex_noise_lut");
if (uniform_proctex_noise_lut != -1) {
glUniform1i(uniform_proctex_noise_lut, 10);
}
GLuint uniform_proctex_color_map =
glGetUniformLocation(shader->shader.handle, "proctex_color_map");
if (uniform_proctex_color_map != -1) {
glUniform1i(uniform_proctex_color_map, 11);
}
GLuint uniform_proctex_alpha_map =
glGetUniformLocation(shader->shader.handle, "proctex_alpha_map");
if (uniform_proctex_alpha_map != -1) {
glUniform1i(uniform_proctex_alpha_map, 12);
}
GLuint uniform_proctex_lut = glGetUniformLocation(shader->shader.handle, "proctex_lut");
if (uniform_proctex_lut != -1) {
glUniform1i(uniform_proctex_lut, 13);
}
GLuint uniform_proctex_diff_lut =
glGetUniformLocation(shader->shader.handle, "proctex_diff_lut");
if (uniform_proctex_diff_lut != -1) {
glUniform1i(uniform_proctex_diff_lut, 14);
}
current_shader = shader_cache.emplace(config, std::move(shader)).first->second.get();
GLuint block_index = glGetUniformBlockIndex(current_shader->shader.handle, "shader_data");
@ -1105,6 +1256,7 @@ void RasterizerOpenGL::SetShader() {
}
SyncFogColor();
SyncProcTexNoise();
}
}
}
@ -1204,6 +1356,86 @@ void RasterizerOpenGL::SyncFogLUT() {
}
}
void RasterizerOpenGL::SyncProcTexNoise() {
const auto& regs = Pica::g_state.regs.texturing;
uniform_block_data.data.proctex_noise_f = {
Pica::float16::FromRaw(regs.proctex_noise_frequency.u).ToFloat32(),
Pica::float16::FromRaw(regs.proctex_noise_frequency.v).ToFloat32(),
};
uniform_block_data.data.proctex_noise_a = {
regs.proctex_noise_u.amplitude / 4095.0f, regs.proctex_noise_v.amplitude / 4095.0f,
};
uniform_block_data.data.proctex_noise_p = {
Pica::float16::FromRaw(regs.proctex_noise_u.phase).ToFloat32(),
Pica::float16::FromRaw(regs.proctex_noise_v.phase).ToFloat32(),
};
uniform_block_data.dirty = true;
}
// helper function for SyncProcTexNoiseLUT/ColorMap/AlphaMap
static void SyncProcTexValueLUT(const std::array<Pica::State::ProcTex::ValueEntry, 128>& lut,
std::array<GLvec2, 128>& lut_data, GLenum texture) {
std::array<GLvec2, 128> new_data;
std::transform(lut.begin(), lut.end(), new_data.begin(), [](const auto& entry) {
return GLvec2{entry.ToFloat(), entry.DiffToFloat()};
});
if (new_data != lut_data) {
lut_data = new_data;
glActiveTexture(texture);
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 128, GL_RG, GL_FLOAT, lut_data.data());
}
}
void RasterizerOpenGL::SyncProcTexNoiseLUT() {
SyncProcTexValueLUT(Pica::g_state.proctex.noise_table, proctex_noise_lut_data, GL_TEXTURE10);
}
void RasterizerOpenGL::SyncProcTexColorMap() {
SyncProcTexValueLUT(Pica::g_state.proctex.color_map_table, proctex_color_map_data,
GL_TEXTURE11);
}
void RasterizerOpenGL::SyncProcTexAlphaMap() {
SyncProcTexValueLUT(Pica::g_state.proctex.alpha_map_table, proctex_alpha_map_data,
GL_TEXTURE12);
}
void RasterizerOpenGL::SyncProcTexLUT() {
std::array<GLvec4, 256> new_data;
std::transform(Pica::g_state.proctex.color_table.begin(),
Pica::g_state.proctex.color_table.end(), new_data.begin(),
[](const auto& entry) {
auto rgba = entry.ToVector() / 255.0f;
return GLvec4{rgba.r(), rgba.g(), rgba.b(), rgba.a()};
});
if (new_data != proctex_lut_data) {
proctex_lut_data = new_data;
glActiveTexture(GL_TEXTURE13);
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, proctex_lut_data.data());
}
}
void RasterizerOpenGL::SyncProcTexDiffLUT() {
std::array<GLvec4, 256> new_data;
std::transform(Pica::g_state.proctex.color_diff_table.begin(),
Pica::g_state.proctex.color_diff_table.end(), new_data.begin(),
[](const auto& entry) {
auto rgba = entry.ToVector() / 255.0f;
return GLvec4{rgba.r(), rgba.g(), rgba.b(), rgba.a()};
});
if (new_data != proctex_diff_lut_data) {
proctex_diff_lut_data = new_data;
glActiveTexture(GL_TEXTURE14);
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, 256, GL_RGBA, GL_FLOAT, proctex_diff_lut_data.data());
}
}
void RasterizerOpenGL::SyncAlphaTest() {
const auto& regs = Pica::g_state.regs;
if (regs.framebuffer.output_merger.alpha_test.ref != uniform_block_data.data.alphatest_ref) {

View File

@ -143,6 +143,9 @@ private:
GLint scissor_x2;
GLint scissor_y2;
alignas(16) GLvec3 fog_color;
alignas(8) GLvec2 proctex_noise_f;
alignas(8) GLvec2 proctex_noise_a;
alignas(8) GLvec2 proctex_noise_p;
alignas(16) GLvec3 lighting_global_ambient;
LightSrc light_src[8];
alignas(16) GLvec4 const_color[6]; // A vec4 color for each of the six tev stages
@ -150,7 +153,7 @@ private:
};
static_assert(
sizeof(UniformData) == 0x3C0,
sizeof(UniformData) == 0x3E0,
"The size of the UniformData structure has changed, update the structure in the shader");
static_assert(sizeof(UniformData) < 16384,
"UniformData structure must be less than 16kb as per the OpenGL spec");
@ -180,6 +183,16 @@ private:
void SyncFogColor();
void SyncFogLUT();
/// Sync the procedural texture noise configuration to match the PICA register
void SyncProcTexNoise();
/// Sync the procedural texture lookup tables
void SyncProcTexNoiseLUT();
void SyncProcTexColorMap();
void SyncProcTexAlphaMap();
void SyncProcTexLUT();
void SyncProcTexDiffLUT();
/// Syncs the alpha test states to match the PICA register
void SyncAlphaTest();
@ -248,6 +261,11 @@ private:
UniformData data;
bool lut_dirty[6];
bool fog_lut_dirty;
bool proctex_noise_lut_dirty;
bool proctex_color_map_dirty;
bool proctex_alpha_map_dirty;
bool proctex_lut_dirty;
bool proctex_diff_lut_dirty;
bool dirty;
} uniform_block_data = {};
@ -262,4 +280,19 @@ private:
OGLTexture fog_lut;
std::array<GLuint, 128> fog_lut_data{};
OGLTexture proctex_noise_lut;
std::array<GLvec2, 128> proctex_noise_lut_data{};
OGLTexture proctex_color_map;
std::array<GLvec2, 128> proctex_color_map_data{};
OGLTexture proctex_alpha_map;
std::array<GLvec2, 128> proctex_alpha_map_data{};
OGLTexture proctex_lut;
std::array<GLvec4, 256> proctex_lut_data{};
OGLTexture proctex_diff_lut;
std::array<GLvec4, 256> proctex_diff_lut_data{};
};

View File

@ -114,6 +114,22 @@ PicaShaderConfig PicaShaderConfig::BuildFromRegs(const Pica::Regs& regs) {
state.lighting.bump_renorm = regs.lighting.config0.disable_bump_renorm == 0;
state.lighting.clamp_highlights = regs.lighting.config0.clamp_highlights != 0;
state.proctex.enable = regs.texturing.main_config.texture3_enable;
if (state.proctex.enable) {
state.proctex.coord = regs.texturing.main_config.texture3_coordinates;
state.proctex.u_clamp = regs.texturing.proctex.u_clamp;
state.proctex.v_clamp = regs.texturing.proctex.v_clamp;
state.proctex.color_combiner = regs.texturing.proctex.color_combiner;
state.proctex.alpha_combiner = regs.texturing.proctex.alpha_combiner;
state.proctex.separate_alpha = regs.texturing.proctex.separate_alpha;
state.proctex.noise_enable = regs.texturing.proctex.noise_enable;
state.proctex.u_shift = regs.texturing.proctex.u_shift;
state.proctex.v_shift = regs.texturing.proctex.v_shift;
state.proctex.lut_width = regs.texturing.proctex_lut.width;
state.proctex.lut_offset = regs.texturing.proctex_lut_offset;
state.proctex.lut_filter = regs.texturing.proctex_lut.filter;
}
return res;
}
@ -132,8 +148,7 @@ static std::string TexCoord(const PicaShaderConfig& config, int texture_unit) {
if (texture_unit == 2 && config.state.texture2_use_coord1) {
return "texcoord[1]";
}
// TODO: if texture unit 3 (procedural texture) implementation also uses this function,
// config.state.texture3_coordinates should be repected here.
return "texcoord[" + std::to_string(texture_unit) + "]";
}
@ -175,6 +190,14 @@ static void AppendSource(std::string& out, const PicaShaderConfig& config,
case Source::Texture2:
out += "texture(tex[2], " + TexCoord(config, 2) + ")";
break;
case Source::Texture3:
if (config.state.proctex.enable) {
out += "ProcTex()";
} else {
LOG_ERROR(Render_OpenGL, "Using Texture3 without enabling it");
out += "vec4(0.0)";
}
break;
case Source::PreviousBuffer:
out += "combiner_buffer";
break;
@ -483,9 +506,18 @@ static void WriteLighting(std::string& out, const PicaShaderConfig& config) {
if (lighting.bump_mode == LightingRegs::LightingBumpMode::NormalMap) {
// Bump mapping is enabled using a normal map, read perturbation vector from the selected
// texture
if (lighting.bump_selector == 3) {
if (config.state.proctex.enable) {
out += "vec3 surface_normal = 2.0 * ProcTex().rgb - 1.0;\n";
} else {
LOG_ERROR(Render_OpenGL, "Using Texture3 without enabling it");
out += "vec3 surface_normal = vec3(-1.0);\n";
}
} else {
std::string bump_selector = std::to_string(lighting.bump_selector);
out += "vec3 surface_normal = 2.0 * texture(tex[" + bump_selector + "], " +
TexCoord(config, lighting.bump_selector) + ").rgb - 1.0;\n";
}
// Recompute Z-component of perturbation if 'renorm' is enabled, this provides a higher
// precision result
@ -693,6 +725,221 @@ static void WriteLighting(std::string& out, const PicaShaderConfig& config) {
out += "secondary_fragment_color = clamp(specular_sum, vec4(0.0), vec4(1.0));\n";
}
using ProcTexClamp = TexturingRegs::ProcTexClamp;
using ProcTexShift = TexturingRegs::ProcTexShift;
using ProcTexCombiner = TexturingRegs::ProcTexCombiner;
using ProcTexFilter = TexturingRegs::ProcTexFilter;
void AppendProcTexShiftOffset(std::string& out, const std::string& v, ProcTexShift mode,
ProcTexClamp clamp_mode) {
std::string offset = (clamp_mode == ProcTexClamp::MirroredRepeat) ? "1.0" : "0.5";
switch (mode) {
case ProcTexShift::None:
out += "0";
break;
case ProcTexShift::Odd:
out += offset + " * ((int(" + v + ") / 2) % 2)";
break;
case ProcTexShift::Even:
out += offset + " * (((int(" + v + ") + 1) / 2) % 2)";
break;
default:
LOG_CRITICAL(HW_GPU, "Unknown shift mode %u", static_cast<u32>(mode));
out += "0";
break;
}
}
void AppendProcTexClamp(std::string& out, const std::string& var, ProcTexClamp mode) {
switch (mode) {
case ProcTexClamp::ToZero:
out += var + " = " + var + " > 1.0 ? 0 : " + var + ";\n";
break;
case ProcTexClamp::ToEdge:
out += var + " = " + "min(" + var + ", 1.0);\n";
break;
case ProcTexClamp::SymmetricalRepeat:
out += var + " = " + "fract(" + var + ");\n";
break;
case ProcTexClamp::MirroredRepeat: {
out +=
var + " = int(" + var + ") % 2 == 0 ? fract(" + var + ") : 1.0 - fract(" + var + ");\n";
break;
}
case ProcTexClamp::Pulse:
out += var + " = " + var + " > 0.5 ? 1.0 : 0.0;\n";
break;
default:
LOG_CRITICAL(HW_GPU, "Unknown clamp mode %u", static_cast<u32>(mode));
out += var + " = " + "min(" + var + ", 1.0);\n";
break;
}
}
void AppendProcTexCombineAndMap(std::string& out, ProcTexCombiner combiner,
const std::string& map_lut) {
std::string combined;
switch (combiner) {
case ProcTexCombiner::U:
combined = "u";
break;
case ProcTexCombiner::U2:
combined = "(u * u)";
break;
case TexturingRegs::ProcTexCombiner::V:
combined = "v";
break;
case TexturingRegs::ProcTexCombiner::V2:
combined = "(v * v)";
break;
case TexturingRegs::ProcTexCombiner::Add:
combined = "((u + v) * 0.5)";
break;
case TexturingRegs::ProcTexCombiner::Add2:
combined = "((u * u + v * v) * 0.5)";
break;
case TexturingRegs::ProcTexCombiner::SqrtAdd2:
combined = "min(sqrt(u * u + v * v), 1.0)";
break;
case TexturingRegs::ProcTexCombiner::Min:
combined = "min(u, v)";
break;
case TexturingRegs::ProcTexCombiner::Max:
combined = "max(u, v)";
break;
case TexturingRegs::ProcTexCombiner::RMax:
combined = "min(((u + v) * 0.5 + sqrt(u * u + v * v)) * 0.5, 1.0)";
break;
default:
LOG_CRITICAL(HW_GPU, "Unknown combiner %u", static_cast<u32>(combiner));
combined = "0.0";
break;
}
out += "ProcTexLookupLUT(" + map_lut + ", " + combined + ")";
}
void AppendProcTexSampler(std::string& out, const PicaShaderConfig& config) {
// LUT sampling uitlity
// For NoiseLUT/ColorMap/AlphaMap, coord=0.0 is lut[0], coord=127.0/128.0 is lut[127] and
// coord=1.0 is lut[127]+lut_diff[127]. For other indices, the result is interpolated using
// value entries and difference entries.
out += R"(
float ProcTexLookupLUT(sampler1D lut, float coord) {
coord *= 128;
float index_i = clamp(floor(coord), 0.0, 127.0);
float index_f = coord - index_i; // fract() cannot be used here because 128.0 needs to be
// extracted as index_i = 127.0 and index_f = 1.0
vec2 entry = texelFetch(lut, int(index_i), 0).rg;
return clamp(entry.r + entry.g * index_f, 0.0, 1.0);
}
)";
// Noise utility
if (config.state.proctex.noise_enable) {
// See swrasterizer/proctex.cpp for more information about these functions
out += R"(
int ProcTexNoiseRand1D(int v) {
const int table[] = int[](0,4,10,8,4,9,7,12,5,15,13,14,11,15,2,11);
return ((v % 9 + 2) * 3 & 0xF) ^ table[(v / 9) & 0xF];
}
float ProcTexNoiseRand2D(vec2 point) {
const int table[] = int[](10,2,15,8,0,7,4,5,5,13,2,6,13,9,3,14);
int u2 = ProcTexNoiseRand1D(int(point.x));
int v2 = ProcTexNoiseRand1D(int(point.y));
v2 += ((u2 & 3) == 1) ? 4 : 0;
v2 ^= (u2 & 1) * 6;
v2 += 10 + u2;
v2 &= 0xF;
v2 ^= table[u2];
return -1.0 + float(v2) * 2.0/ 15.0;
}
float ProcTexNoiseCoef(vec2 x) {
vec2 grid = 9.0 * proctex_noise_f * abs(x + proctex_noise_p);
vec2 point = floor(grid);
vec2 frac = grid - point;
float g0 = ProcTexNoiseRand2D(point) * (frac.x + frac.y);
float g1 = ProcTexNoiseRand2D(point + vec2(1.0, 0.0)) * (frac.x + frac.y - 1.0);
float g2 = ProcTexNoiseRand2D(point + vec2(0.0, 1.0)) * (frac.x + frac.y - 1.0);
float g3 = ProcTexNoiseRand2D(point + vec2(1.0, 1.0)) * (frac.x + frac.y - 2.0);
float x_noise = ProcTexLookupLUT(proctex_noise_lut, frac.x);
float y_noise = ProcTexLookupLUT(proctex_noise_lut, frac.y);
float x0 = mix(g0, g1, x_noise);
float x1 = mix(g2, g3, x_noise);
return mix(x0, x1, y_noise);
}
)";
}
out += "vec4 ProcTex() {\n";
out += "vec2 uv = abs(texcoord[" + std::to_string(config.state.proctex.coord) + "]);\n";
// Get shift offset before noise generation
out += "float u_shift = ";
AppendProcTexShiftOffset(out, "uv.y", config.state.proctex.u_shift,
config.state.proctex.u_clamp);
out += ";\n";
out += "float v_shift = ";
AppendProcTexShiftOffset(out, "uv.x", config.state.proctex.v_shift,
config.state.proctex.v_clamp);
out += ";\n";
// Generate noise
if (config.state.proctex.noise_enable) {
out += "uv += proctex_noise_a * ProcTexNoiseCoef(uv);\n";
out += "uv = abs(uv);\n";
}
// Shift
out += "float u = uv.x + u_shift;\n";
out += "float v = uv.y + v_shift;\n";
// Clamp
AppendProcTexClamp(out, "u", config.state.proctex.u_clamp);
AppendProcTexClamp(out, "v", config.state.proctex.v_clamp);
// Combine and map
out += "float lut_coord = ";
AppendProcTexCombineAndMap(out, config.state.proctex.color_combiner, "proctex_color_map");
out += ";\n";
// Look up color
// For the color lut, coord=0.0 is lut[offset] and coord=1.0 is lut[offset+width-1]
out += "lut_coord *= " + std::to_string(config.state.proctex.lut_width - 1) + ";\n";
// TODO(wwylele): implement mipmap
switch (config.state.proctex.lut_filter) {
case ProcTexFilter::Linear:
case ProcTexFilter::LinearMipmapLinear:
case ProcTexFilter::LinearMipmapNearest:
out += "int lut_index_i = int(lut_coord) + " +
std::to_string(config.state.proctex.lut_offset) + ";\n";
out += "float lut_index_f = fract(lut_coord);\n";
out += "vec4 final_color = texelFetch(proctex_lut, lut_index_i, 0) + lut_index_f * "
"texelFetch(proctex_diff_lut, lut_index_i, 0);\n";
break;
case ProcTexFilter::Nearest:
case ProcTexFilter::NearestMipmapLinear:
case ProcTexFilter::NearestMipmapNearest:
out += "lut_coord += " + std::to_string(config.state.proctex.lut_offset) + ";\n";
out += "vec4 final_color = texelFetch(proctex_lut, int(round(lut_coord)), 0);\n";
break;
}
if (config.state.proctex.separate_alpha) {
// Note: in separate alpha mode, the alpha channel skips the color LUT look up stage. It
// uses the output of CombineAndMap directly instead.
out += "float final_alpha = ";
AppendProcTexCombineAndMap(out, config.state.proctex.alpha_combiner, "proctex_alpha_map");
out += ";\n";
out += "return vec4(final_color.xyz, final_alpha);\n}\n";
} else {
out += "return final_color;\n}\n";
}
}
std::string GenerateFragmentShader(const PicaShaderConfig& config) {
const auto& state = config.state;
@ -735,6 +982,9 @@ layout (std140) uniform shader_data {
int scissor_x2;
int scissor_y2;
vec3 fog_color;
vec2 proctex_noise_f;
vec2 proctex_noise_a;
vec2 proctex_noise_p;
vec3 lighting_global_ambient;
LightSrc light_src[NUM_LIGHTS];
vec4 const_color[NUM_TEV_STAGES];
@ -744,12 +994,21 @@ layout (std140) uniform shader_data {
uniform sampler2D tex[3];
uniform sampler1D lut[6];
uniform usampler1D fog_lut;
uniform sampler1D proctex_noise_lut;
uniform sampler1D proctex_color_map;
uniform sampler1D proctex_alpha_map;
uniform sampler1D proctex_lut;
uniform sampler1D proctex_diff_lut;
// Rotate the vector v by the quaternion q
vec3 quaternion_rotate(vec4 q, vec3 v) {
return v + 2.0 * cross(q.xyz, cross(q.xyz, v) + q.w * v);
}
})";
if (config.state.proctex.enable)
AppendProcTexSampler(out, config);
out += R"(
void main() {
vec4 primary_fragment_color = vec4(0.0);
vec4 secondary_fragment_color = vec4(0.0);

View File

@ -113,6 +113,19 @@ union PicaShaderConfig {
} lut_d0, lut_d1, lut_fr, lut_rr, lut_rg, lut_rb;
} lighting;
struct {
bool enable;
u32 coord;
Pica::TexturingRegs::ProcTexClamp u_clamp, v_clamp;
Pica::TexturingRegs::ProcTexCombiner color_combiner, alpha_combiner;
bool separate_alpha;
bool noise_enable;
Pica::TexturingRegs::ProcTexShift u_shift, v_shift;
u32 lut_width;
u32 lut_offset;
Pica::TexturingRegs::ProcTexFilter lut_filter;
} proctex;
} state;
};
#if (__GNUC__ >= 5) || defined(__clang__) || defined(_MSC_VER)

View File

@ -58,6 +58,12 @@ OpenGLState::OpenGLState() {
fog_lut.texture_1d = 0;
proctex_lut.texture_1d = 0;
proctex_diff_lut.texture_1d = 0;
proctex_color_map.texture_1d = 0;
proctex_alpha_map.texture_1d = 0;
proctex_noise_lut.texture_1d = 0;
draw.read_framebuffer = 0;
draw.draw_framebuffer = 0;
draw.vertex_array = 0;
@ -201,6 +207,36 @@ void OpenGLState::Apply() const {
glBindTexture(GL_TEXTURE_1D, fog_lut.texture_1d);
}
// ProcTex Noise LUT
if (proctex_noise_lut.texture_1d != cur_state.proctex_noise_lut.texture_1d) {
glActiveTexture(GL_TEXTURE10);
glBindTexture(GL_TEXTURE_1D, proctex_noise_lut.texture_1d);
}
// ProcTex Color Map
if (proctex_color_map.texture_1d != cur_state.proctex_color_map.texture_1d) {
glActiveTexture(GL_TEXTURE11);
glBindTexture(GL_TEXTURE_1D, proctex_color_map.texture_1d);
}
// ProcTex Alpha Map
if (proctex_alpha_map.texture_1d != cur_state.proctex_alpha_map.texture_1d) {
glActiveTexture(GL_TEXTURE12);
glBindTexture(GL_TEXTURE_1D, proctex_alpha_map.texture_1d);
}
// ProcTex LUT
if (proctex_lut.texture_1d != cur_state.proctex_lut.texture_1d) {
glActiveTexture(GL_TEXTURE13);
glBindTexture(GL_TEXTURE_1D, proctex_lut.texture_1d);
}
// ProcTex Diff LUT
if (proctex_diff_lut.texture_1d != cur_state.proctex_diff_lut.texture_1d) {
glActiveTexture(GL_TEXTURE14);
glBindTexture(GL_TEXTURE_1D, proctex_diff_lut.texture_1d);
}
// Framebuffer
if (draw.read_framebuffer != cur_state.draw.read_framebuffer) {
glBindFramebuffer(GL_READ_FRAMEBUFFER, draw.read_framebuffer);

View File

@ -71,6 +71,26 @@ public:
GLuint texture_1d; // GL_TEXTURE_BINDING_1D
} fog_lut;
struct {
GLuint texture_1d; // GL_TEXTURE_BINDING_1D
} proctex_noise_lut;
struct {
GLuint texture_1d; // GL_TEXTURE_BINDING_1D
} proctex_color_map;
struct {
GLuint texture_1d; // GL_TEXTURE_BINDING_1D
} proctex_alpha_map;
struct {
GLuint texture_1d; // GL_TEXTURE_BINDING_1D
} proctex_lut;
struct {
GLuint texture_1d; // GL_TEXTURE_BINDING_1D
} proctex_diff_lut;
struct {
GLuint read_framebuffer; // GL_READ_FRAMEBUFFER_BINDING
GLuint draw_framebuffer; // GL_DRAW_FRAMEBUFFER_BINDING