0.2.0-dev Download ZIP
130 lines ·2.8 KB Raw
| 1 | #[vertex] |
| 2 | |
| 3 | #version 450 |
| 4 | |
| 5 | #VERSION_DEFINES |
| 6 | |
| 7 | layout(location = 0) in highp vec3 vertex; |
| 8 | |
| 9 | #ifdef POSITIONAL_SHADOW |
| 10 | layout(push_constant, std430) uniform Constants { |
| 11 | mat2x4 modelview; |
| 12 | vec4 rotation; |
| 13 | vec2 direction; |
| 14 | float z_far; |
| 15 | uint pad; |
| 16 | float z_near; |
| 17 | uint cull_mode; |
| 18 | float pad3; |
| 19 | float pad4; |
| 20 | } |
| 21 | constants; |
| 22 | |
| 23 | layout(set = 0, binding = 0, std430) restrict readonly buffer OccluderTransforms { |
| 24 | mat2x4 transforms[]; |
| 25 | } |
| 26 | occluder_transforms; |
| 27 | |
| 28 | #else |
| 29 | |
| 30 | layout(push_constant, std430) uniform Constants { |
| 31 | mat4 projection; |
| 32 | mat2x4 modelview; |
| 33 | vec2 direction; |
| 34 | float z_far; |
| 35 | uint cull_mode; |
| 36 | } |
| 37 | constants; |
| 38 | |
| 39 | #endif |
| 40 | |
| 41 | #ifdef MODE_SHADOW |
| 42 | layout(location = 0) out highp float depth; |
| 43 | #endif |
| 44 | |
| 45 | void main() { |
| 46 | #ifdef POSITIONAL_SHADOW |
| 47 | float c = -(constants.z_far + constants.z_near) / (constants.z_far - constants.z_near); |
| 48 | float d = -2.0 * constants.z_far * constants.z_near / (constants.z_far - constants.z_near); |
| 49 | |
| 50 | mat4 projection = mat4(vec4(1.0, 0.0, 0.0, 0.0), |
| 51 | vec4(0.0, 1.0, 0.0, 0.0), |
| 52 | vec4(0.0, 0.0, c, -1.0), |
| 53 | vec4(0.0, 0.0, d, 0.0)); |
| 54 | |
| 55 | // Precomputed: |
| 56 | // Vector3 cam_target = Basis::from_euler(Vector3(0, 0, Math_TAU * ((i + 3) / 4.0))).xform(Vector3(0, 1, 0)); |
| 57 | // projection = projection * Projection(Transform3D().looking_at(cam_targets[i], Vector3(0, 0, -1)).affine_inverse()); |
| 58 | projection *= mat4(vec4(constants.rotation.x, 0.0, constants.rotation.y, 0.0), vec4(constants.rotation.z, 0.0, constants.rotation.w, 0.0), vec4(0.0, -1.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0)); |
| 59 | mat4 modelview = mat4(occluder_transforms.transforms[constants.pad]) * mat4(constants.modelview); |
| 60 | #else |
| 61 | mat4 projection = constants.projection; |
| 62 | mat4 modelview = mat4(constants.modelview[0], constants.modelview[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0)); |
| 63 | #endif |
| 64 | |
| 65 | highp vec4 vtx = vec4(vertex, 1.0) * modelview; |
| 66 | |
| 67 | #ifdef MODE_SHADOW |
| 68 | depth = dot(constants.direction, vtx.xy); |
| 69 | #endif |
| 70 | |
| 71 | gl_Position = projection * vtx; |
| 72 | } |
| 73 | |
| 74 | #[fragment] |
| 75 | |
| 76 | #version 450 |
| 77 | |
| 78 | #VERSION_DEFINES |
| 79 | |
| 80 | #ifdef POSITIONAL_SHADOW |
| 81 | layout(push_constant, std430) uniform Constants { |
| 82 | mat2x4 modelview; |
| 83 | vec4 rotation; |
| 84 | vec2 direction; |
| 85 | float z_far; |
| 86 | uint pad; |
| 87 | float z_near; |
| 88 | uint cull_mode; |
| 89 | float pad3; |
| 90 | float pad4; |
| 91 | } |
| 92 | constants; |
| 93 | |
| 94 | #else |
| 95 | |
| 96 | layout(push_constant, std430) uniform Constants { |
| 97 | mat4 projection; |
| 98 | mat2x4 modelview; |
| 99 | vec2 direction; |
| 100 | float z_far; |
| 101 | uint cull_mode; |
| 102 | } |
| 103 | constants; |
| 104 | |
| 105 | #endif |
| 106 | |
| 107 | #ifdef MODE_SHADOW |
| 108 | layout(location = 0) in highp float depth; |
| 109 | layout(location = 0) out highp float distance_buf; |
| 110 | #else |
| 111 | layout(location = 0) out highp float sdf_buf; |
| 112 | #endif |
| 113 | |
| 114 | #define POLYGON_CULL_DISABLED 0 |
| 115 | #define POLYGON_CULL_FRONT 1 |
| 116 | #define POLYGON_CULL_BACK 2 |
| 117 | |
| 118 | void main() { |
| 119 | #ifdef MODE_SHADOW |
| 120 | bool front_facing = gl_FrontFacing; |
| 121 | if (constants.cull_mode == POLYGON_CULL_BACK && !front_facing) { |
| 122 | discard; |
| 123 | } else if (constants.cull_mode == POLYGON_CULL_FRONT && front_facing) { |
| 124 | discard; |
| 125 | } |
| 126 | distance_buf = depth / constants.z_far; |
| 127 | #else |
| 128 | sdf_buf = 1.0; |
| 129 | #endif |
| 130 | } |