Differential D6714 Diff 21269 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 THRESHOLD 0.0000125 | |||||
| #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; | |||||
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; | |||||
| bbox += vec4(-1.0, -1.0, 1.0, 1.0); | |||||
| for (int i = 0; i < 3; i++) { | |||||
| prev_pos = gl_in[(i + 2) % 3].gl_Position; | |||||
| curr_pos = gl_in[(i + 3) % 3].gl_Position; | |||||
| next_pos = gl_in[(i + 4) % 3].gl_Position; | |||||
| /* Compute the (x,y,w,d) triangle plane. */ | |||||
| vec4 p_plane; | |||||
Not Done Inline ActionsCould use define for the constant. campbellbarton: Could use define for the constant. | |||||
| p_plane.xyz = normalize(cross(curr_pos.xyw - prev_pos.xyw, next_pos.xyw - prev_pos.xyw)); | |||||
| p_plane.w = -dot(p_plane.xyz, prev_pos.xyw); | |||||
| vec3 plane[2]; | |||||
| plane[0] = cross(curr_pos.xyw - prev_pos.xyw, prev_pos.xyw); | |||||
| plane[1] = cross(next_pos.xyw - curr_pos.xyw, curr_pos.xyw); | |||||
| /* Test if the triangle is "almost backfacing" to avoid precision issues. */ | |||||
| if (dot(curr_pos.xyw, p_plane.xyz) < THRESHOLD) { | |||||
| gl_Position = curr_pos; | |||||
| /* HACK: Fix cases where the triangle is parallel to the view | |||||
| * by deforming the triangle slightly. */ | |||||
| gl_Position.xy += sizeViewportInv.xy * gl_Position.w * ((i % 2 == 0) ? -1.0 : 1.0); | |||||
| } | |||||
| else { | |||||
| /* Push the planes back enough to enclose the sample points of all pixels the triangle | |||||
| * intersects. The pushfactor is dependant on the slope (x,y). */ | |||||
| plane[0].z -= dot(sizeViewportInv, abs(plane[0].xy)); | |||||
| plane[1].z -= dot(sizeViewportInv, abs(plane[1].xy)); | |||||
| /* Compute the intersection point of the two planes. | |||||
| * Gives us the final position of the vertex. */ | |||||
| gl_Position.xyw = cross(plane[0], plane[1]); | |||||
| gl_Position /= abs(gl_Position.w); | |||||
| gl_Position.z = 1; | |||||
| } | |||||
| #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.