Differential D6714 Diff 21365 source/blender/draw/engines/basic/shaders/conservative_depth_geom.glsl
Changeset View
Changeset View
Standalone View
Standalone View
source/blender/draw/engines/basic/shaders/conservative_depth_geom.glsl
- This file was added.
| /* Adaptation of Conservative Rasterization | |||||
| * from GPU Gems 2 | |||||
| * Using method 2. | |||||
| * https://developer.nvidia.com/gpugems/gpugems2/part-v-image-oriented-computing/chapter-42-conservative-rasterization | |||||
| */ | |||||
| layout(triangles) in; | |||||
| layout(triangle_strip, max_vertices = 3) out; | |||||
| RESOURCE_ID_VARYING | |||||
| uniform vec2 sizeViewport; | |||||
| uniform vec2 sizeViewportInv; | |||||
| flat out vec4 bbox; | |||||
| #define FLT_MAX 9999999999999.0 | |||||
campbellbarton: Suggest calling this `BOUNDS_MAX` or similar, otherwise set to `3.40282347E+38F`. | |||||
| /** | |||||
| * Adds a new point to the bounding box. This method can be simplified if orthogonal | |||||
| * projection is assumed | |||||
| */ | |||||
| void bbox_min_max(vec4 pos, inout vec4 bbox) | |||||
| { | |||||
| if (pos.w < 0) { | |||||
| bbox.xy = min(bbox.xy, pos.xy); | |||||
| bbox.zw = max(bbox.zw, pos.xy); | |||||
| } | |||||
| else { | |||||
| bbox.xy = min(bbox.xy, pos.xy / pos.w); | |||||
| bbox.zw = max(bbox.zw, pos.xy / pos.w); | |||||
| } | |||||
| } | |||||
| void main() | |||||
| { | |||||
| vec4 curr_pos, prev_pos, next_pos; | |||||
campbellbartonUnsubmitted Not Done Inline ActionsUnused variables. campbellbarton: Unused variables. | |||||
| /* Compute the screen space bounding box of the triangle. */ | |||||
| bbox = vec4(FLT_MAX, FLT_MAX, -FLT_MAX, -FLT_MAX); | |||||
| bbox_min_max(gl_in[0].gl_Position, bbox); | |||||
| bbox_min_max(gl_in[1].gl_Position, bbox); | |||||
| bbox_min_max(gl_in[2].gl_Position, bbox); | |||||
| /* Convert to pixel space and enlarge the BBox. */ | |||||
| bbox = (bbox * 0.5 + 0.5) * sizeViewport.xyxy; | |||||
| /* Detect failure cases where triangles would produce no fragments. */ | |||||
| bvec2 is_subpixel = lessThan(bbox.zw - bbox.xy, vec2(1.0)); | |||||
| /* Compute plane normal in ndc space. */ | |||||
| vec3 pos0 = gl_in[0].gl_Position.xyz / gl_in[0].gl_Position.w; | |||||
| vec3 pos1 = gl_in[1].gl_Position.xyz / gl_in[1].gl_Position.w; | |||||
| vec3 pos2 = gl_in[2].gl_Position.xyz / gl_in[2].gl_Position.w; | |||||
| vec3 plane = normalize(cross(pos1 - pos0, pos2 - pos0)); | |||||
| /* View aligned triangle. */ | |||||
| bool is_coplanar = abs(plane.z) < 0.00001; | |||||
campbellbartonUnsubmitted Not Done Inline ActionsCould use define for the constant. campbellbarton: Could use define for the constant. | |||||
| for (int i = 0; i < 3; i++) { | |||||
| gl_Position = gl_in[i].gl_Position; | |||||
| if (all(is_subpixel)) { | |||||
| vec2 ofs = (i == 0) ? vec2(-1.0) : ((i == 1) ? vec2(2.0, -1.0) : vec2(-1.0, 2.0)); | |||||
| /* HACK: Fix cases where the triangle is too small make it cover at least one pixel. */ | |||||
| gl_Position.xy += sizeViewportInv.xy * gl_Position.w * ofs; | |||||
| } | |||||
| /* Test if the triangle is almost parralele with the view to avoid precision issues. */ | |||||
| else if (any(is_subpixel) || is_coplanar) { | |||||
| /* HACK: Fix cases where the triangle is Parallel to the view by deforming it slightly. */ | |||||
| vec2 ofs = (i == 0) ? vec2(-1.0) : ((i == 1) ? vec2(1.0, -1.0) : vec2(1.0)); | |||||
| gl_Position.xy += sizeViewportInv.xy * gl_Position.w * ofs; | |||||
| } | |||||
| else { | |||||
| /* Triangle expansion should happen here, but we decide to not implement it for | |||||
| * depth precision & performance reasons. */ | |||||
| } | |||||
| #ifdef USE_WORLD_CLIP_PLANES | |||||
| world_clip_planes_set_clip_distance(gl_in[i].gl_ClipDistance); | |||||
| #endif | |||||
| EmitVertex(); | |||||
| } | |||||
| EndPrimitive(); | |||||
| } | |||||
| No newline at end of file | |||||
Suggest calling this BOUNDS_MAX or similar, otherwise set to 3.40282347E+38F.