Skip to content

Commit

Permalink
Shadergen: Add clipping
Browse files Browse the repository at this point in the history
  • Loading branch information
wheremyfoodat committed Jul 15, 2024
1 parent c02b382 commit 441aa23
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions include/PICA/pica_frag_uniforms.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ namespace PICA {

alignas(16) vec4 constantColors[tevStageCount];
alignas(16) vec4 tevBufferColor;
alignas(16) vec4 clipCoords;
};
} // namespace PICA
34 changes: 24 additions & 10 deletions src/core/PICA/shader_gen_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
using namespace PICA;
using namespace PICA::ShaderGen;

static constexpr const char* uniformDefinition = R"(
layout(std140) uniform FragmentUniforms {
int alphaReference;
float depthScale;
float depthOffset;
vec4 constantColors[6];
vec4 tevBufferColor;
vec4 clipCoords;
};
)";

std::string FragmentGenerator::getVertexShader(const PICARegs& regs) {
std::string ret = "";

Expand All @@ -20,6 +32,8 @@ std::string FragmentGenerator::getVertexShader(const PICARegs& regs) {
)";
}

ret += uniformDefinition;

ret += R"(
layout(location = 0) in vec4 a_coords;
layout(location = 1) in vec4 a_quaternion;
Expand All @@ -39,7 +53,9 @@ std::string FragmentGenerator::getVertexShader(const PICARegs& regs) {
out vec3 v_view;
out vec2 v_texcoord2;
//out float gl_ClipDistance[2];
#ifndef USING_GLES
out float gl_ClipDistance[2];
#endif
vec4 abgr8888ToVec4(uint abgr) {
const float scale = 1.0 / 255.0;
Expand All @@ -65,6 +81,11 @@ std::string FragmentGenerator::getVertexShader(const PICARegs& regs) {
v_normal = normalize(rotateVec3ByQuaternion(vec3(0.0, 0.0, 1.0), a_quaternion));
v_tangent = normalize(rotateVec3ByQuaternion(vec3(1.0, 0.0, 0.0), a_quaternion));
v_bitangent = normalize(rotateVec3ByQuaternion(vec3(0.0, 1.0, 0.0), a_quaternion));
#ifndef USING_GLES
gl_ClipDistance[0] = -a_coords.z;
gl_ClipDistance[1] = dot(clipCoords, a_coords);
#endif
}
)";

Expand Down Expand Up @@ -109,17 +130,10 @@ std::string FragmentGenerator::generate(const PICARegs& regs) {
#ifndef USING_GLES
uniform sampler1DArray u_tex_lighting_lut;
#endif
layout(std140) uniform FragmentUniforms {
int alphaReference;
float depthScale;
float depthOffset;
vec4 constantColors[6];
vec4 tevBufferColor;
};
)";

ret += uniformDefinition;

// Emit main function for fragment shader
// When not initialized, source 13 is set to vec4(0.0) and 15 is set to the vertex colour
ret += R"(
Expand Down
7 changes: 7 additions & 0 deletions src/core/renderer_gl/renderer_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,13 @@ OpenGL::Program& RendererGL::getSpecializedShader() {
uniforms.depthScale = f24::fromRaw(regs[PICA::InternalRegs::DepthScale] & 0xffffff).toFloat32();
uniforms.depthOffset = f24::fromRaw(regs[PICA::InternalRegs::DepthOffset] & 0xffffff).toFloat32();

if (regs[InternalRegs::ClipEnable] & 1) {
uniforms.clipCoords[0] = f24::fromRaw(regs[PICA::InternalRegs::ClipData0] & 0xffffff).toFloat32();
uniforms.clipCoords[1] = f24::fromRaw(regs[PICA::InternalRegs::ClipData1] & 0xffffff).toFloat32();
uniforms.clipCoords[2] = f24::fromRaw(regs[PICA::InternalRegs::ClipData2] & 0xffffff).toFloat32();
uniforms.clipCoords[3] = f24::fromRaw(regs[PICA::InternalRegs::ClipData3] & 0xffffff).toFloat32();
}

// Set up the constant color for the 6 TEV stages
for (int i = 0; i < 6; i++) {
static constexpr std::array<u32, 6> ioBases = {
Expand Down

0 comments on commit 441aa23

Please sign in to comment.