Skip to content

Commit

Permalink
Branch optimization: regard last if condition of if statements as true.
Browse files Browse the repository at this point in the history
  • Loading branch information
stripe2933 committed Jan 24, 2025
1 parent bd57a20 commit 8727ab0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
1 change: 1 addition & 0 deletions shaders/branch.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define last_branch(expression) (true)
7 changes: 3 additions & 4 deletions shaders/mask_depth.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_shader_8bit_storage : require

#include "branch.glsl"
#define VERTEX_SHADER
#include "indexing.glsl"
#include "types.glsl"
Expand Down Expand Up @@ -70,10 +71,9 @@ vec2 getTexcoord(uint texcoordIndex){
if (mappingInfo.componentType == uint8_t(3)) { // 5123: UNSIGNED SHORT
return vec2(U16Vec2Ref(fetchAddress).data) / 65535.0;
}
if (mappingInfo.componentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
if last_branch(mappingInfo.componentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
return vec2(U8Vec2Ref(fetchAddress).data) / 255.0;
}
return vec2(0.0);
}
#endif

Expand All @@ -86,10 +86,9 @@ float getColorAlpha() {
if (PRIMITIVE.colorComponentType == uint8_t(3)) { // 5123: UNSIGNED SHORT
return float(Uint16Ref(PRIMITIVE.pColorBuffer + (fetchByteOffset + 6)).data) / 65535.0;
}
if (PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
if last_branch(PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
return float(Uint8Ref(PRIMITIVE.pColorBuffer + (fetchByteOffset + 3)).data) / 255.0;
}
return 1.0;
}
#endif

Expand Down
7 changes: 3 additions & 4 deletions shaders/mask_jump_flood_seed.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_shader_8bit_storage : require

#include "branch.glsl"
#define VERTEX_SHADER
#include "indexing.glsl"
#include "types.glsl"
Expand Down Expand Up @@ -69,10 +70,9 @@ vec2 getTexcoord(uint texcoordIndex){
if (mappingInfo.componentType == uint8_t(3)) { // 5123: UNSIGNED SHORT
return vec2(U16Vec2Ref(fetchAddress).data) / 65535.0;
}
if (mappingInfo.componentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
if last_branch(mappingInfo.componentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
return vec2(U8Vec2Ref(fetchAddress).data) / 255.0;
}
return vec2(0.0);
}
#endif

Expand All @@ -85,10 +85,9 @@ float getColorAlpha() {
if (PRIMITIVE.colorComponentType == uint8_t(3)) { // 5123: UNSIGNED SHORT
return float(Uint16Ref(PRIMITIVE.pColorBuffer + (fetchByteOffset + 6)).data) / 65535.0;
}
if (PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
if last_branch(PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
return float(Uint8Ref(PRIMITIVE.pColorBuffer + (fetchByteOffset + 3)).data) / 255.0;
}
return 1.0;
}
#endif

Expand Down
11 changes: 5 additions & 6 deletions shaders/primitive.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_shader_8bit_storage : require

#include "branch.glsl"
#define VERTEX_SHADER
#include "indexing.glsl"
#include "types.glsl"
Expand Down Expand Up @@ -96,10 +97,9 @@ vec2 getTexcoord(uint texcoordIndex){
if (mappingInfo.componentType == uint8_t(3)) { // 5123: UNSIGNED SHORT
return vec2(U16Vec2Ref(fetchAddress).data) / 65535.0;
}
if (mappingInfo.componentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
if last_branch(mappingInfo.componentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
return vec2(U8Vec2Ref(fetchAddress).data) / 255.0;
}
return vec2(0.0);
}
#endif

Expand All @@ -113,22 +113,21 @@ vec4 getColor() {
if (PRIMITIVE.colorComponentType == uint8_t(3)) { // 5123: UNSIGNED SHORT
return vec4(U16Vec4Ref(fetchAddress).data) / 65535.0;
}
if (PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
if last_branch(PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
return vec4(U8Vec4Ref(fetchAddress).data) / 255.0;
}
}
if (PRIMITIVE.colorComponentCount == uint8_t(3)) {
if last_branch(PRIMITIVE.colorComponentCount == uint8_t(3)) {
if (PRIMITIVE.colorComponentType == uint8_t(6)) { // 5126: FLOAT
return vec4(Vec3Ref(fetchAddress).data, 1.0);
}
if (PRIMITIVE.colorComponentType == uint8_t(3)) { // 5123: UNSIGNED SHORT
return vec4(vec3(U16Vec4Ref(fetchAddress).data) / 65535.0, 1.0);
}
if (PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
if last_branch(PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
return vec4(vec3(U8Vec4Ref(fetchAddress).data) / 255.0, 1.0);
}
}
return vec4(1.0);
}
#endif

Expand Down
11 changes: 5 additions & 6 deletions shaders/unlit_primitive.vert
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_shader_8bit_storage : require

#include "branch.glsl"
#define VERTEX_SHADER
#include "indexing.glsl"
#include "types.glsl"
Expand Down Expand Up @@ -73,10 +74,9 @@ vec2 getTexcoord(uint texcoordIndex){
if (mappingInfo.componentType == uint8_t(3)) { // 5123: UNSIGNED SHORT
return vec2(U16Vec2Ref(fetchAddress).data) / 65535.0;
}
if (mappingInfo.componentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
if last_branch(mappingInfo.componentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
return vec2(U8Vec2Ref(fetchAddress).data) / 255.0;
}
return vec2(0.0);
}
#endif

Expand All @@ -90,22 +90,21 @@ vec4 getColor() {
if (PRIMITIVE.colorComponentType == uint8_t(3)) { // 5123: UNSIGNED SHORT
return vec4(U16Vec4Ref(fetchAddress).data) / 65535.0;
}
if (PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
if last_branch(PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
return vec4(U8Vec4Ref(fetchAddress).data) / 255.0;
}
}
if (PRIMITIVE.colorComponentCount == uint8_t(3)) {
if last_branch(PRIMITIVE.colorComponentCount == uint8_t(3)) {
if (PRIMITIVE.colorComponentType == uint8_t(6)) { // 5126: FLOAT
return vec4(Vec3Ref(fetchAddress).data, 1.0);
}
if (PRIMITIVE.colorComponentType == uint8_t(3)) { // 5123: UNSIGNED SHORT
return vec4(vec3(U16Vec4Ref(fetchAddress).data) / 65535.0, 1.0);
}
if (PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
if last_branch(PRIMITIVE.colorComponentType == uint8_t(1)) { // 5121: UNSIGNED BYTE
return vec4(vec3(U8Vec4Ref(fetchAddress).data) / 255.0, 1.0);
}
}
return vec4(1.0);
}
#endif

Expand Down

0 comments on commit 8727ab0

Please sign in to comment.