Skip to content

Commit beaeabc

Browse files
committed
Added example 44: Conservative rasterization.
1 parent a5f0c91 commit beaeabc

File tree

10 files changed

+824
-0
lines changed

10 files changed

+824
-0
lines changed

Example44/.cproject

Lines changed: 347 additions & 0 deletions
Large diffs are not rendered by default.

Example44/.gitignore

Whitespace-only changes.

Example44/.project

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Example44</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
10+
<triggers>clean,full,incremental,</triggers>
11+
<arguments>
12+
<dictionary>
13+
<key>?name?</key>
14+
<value></value>
15+
</dictionary>
16+
<dictionary>
17+
<key>org.eclipse.cdt.make.core.append_environment</key>
18+
<value>true</value>
19+
</dictionary>
20+
<dictionary>
21+
<key>org.eclipse.cdt.make.core.autoBuildTarget</key>
22+
<value>all</value>
23+
</dictionary>
24+
<dictionary>
25+
<key>org.eclipse.cdt.make.core.buildArguments</key>
26+
<value></value>
27+
</dictionary>
28+
<dictionary>
29+
<key>org.eclipse.cdt.make.core.buildCommand</key>
30+
<value>make</value>
31+
</dictionary>
32+
<dictionary>
33+
<key>org.eclipse.cdt.make.core.buildLocation</key>
34+
<value>${workspace_loc:/Example10/DebugMac}</value>
35+
</dictionary>
36+
<dictionary>
37+
<key>org.eclipse.cdt.make.core.cleanBuildTarget</key>
38+
<value>clean</value>
39+
</dictionary>
40+
<dictionary>
41+
<key>org.eclipse.cdt.make.core.contents</key>
42+
<value>org.eclipse.cdt.make.core.activeConfigSettings</value>
43+
</dictionary>
44+
<dictionary>
45+
<key>org.eclipse.cdt.make.core.enableAutoBuild</key>
46+
<value>false</value>
47+
</dictionary>
48+
<dictionary>
49+
<key>org.eclipse.cdt.make.core.enableCleanBuild</key>
50+
<value>true</value>
51+
</dictionary>
52+
<dictionary>
53+
<key>org.eclipse.cdt.make.core.enableFullBuild</key>
54+
<value>true</value>
55+
</dictionary>
56+
<dictionary>
57+
<key>org.eclipse.cdt.make.core.fullBuildTarget</key>
58+
<value>all</value>
59+
</dictionary>
60+
<dictionary>
61+
<key>org.eclipse.cdt.make.core.stopOnError</key>
62+
<value>true</value>
63+
</dictionary>
64+
<dictionary>
65+
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
66+
<value>true</value>
67+
</dictionary>
68+
</arguments>
69+
</buildCommand>
70+
<buildCommand>
71+
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
72+
<triggers>full,incremental,</triggers>
73+
<arguments>
74+
</arguments>
75+
</buildCommand>
76+
</buildSpec>
77+
<natures>
78+
<nature>org.eclipse.cdt.core.cnature</nature>
79+
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
80+
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
81+
</natures>
82+
</projectDescription>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<project>
3+
<configuration id="cdt.managedbuild.config.gnu.mingw.exe.debug.894771756.809860878.1368823954.193522580" name="x64__Darwin__GCC_Debug">
4+
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
5+
<provider-reference id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser" ref="shared-provider"/>
6+
<provider-reference id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" ref="shared-provider"/>
7+
</extension>
8+
</configuration>
9+
<configuration id="cdt.managedbuild.config.gnu.mingw.exe.debug.894771756.809860878.1382563384.58461710" name="x64__Linux__GCC_Debug">
10+
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
11+
<provider-reference id="org.eclipse.cdt.ui.UserLanguageSettingsProvider" ref="shared-provider"/>
12+
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
13+
<provider-reference id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser" ref="shared-provider"/>
14+
<provider-reference id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" ref="shared-provider"/>
15+
</extension>
16+
</configuration>
17+
<configuration id="cdt.managedbuild.config.gnu.mingw.exe.debug.894771756.809860878.655587838" name="x86__Linux__GCC_Debug">
18+
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
19+
<provider-reference id="org.eclipse.cdt.ui.UserLanguageSettingsProvider" ref="shared-provider"/>
20+
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
21+
<provider-reference id="org.eclipse.cdt.managedbuilder.core.GCCBuildCommandParser" ref="shared-provider"/>
22+
<provider-reference id="org.eclipse.cdt.managedbuilder.core.GCCBuiltinSpecsDetector" ref="shared-provider"/>
23+
</extension>
24+
</configuration>
25+
<configuration id="cdt.managedbuild.config.gnu.mingw.exe.debug.894771756.2029477647" name="x86__Windows__MinGW_Debug">
26+
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
27+
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
28+
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
29+
</extension>
30+
</configuration>
31+
<configuration id="cdt.managedbuild.config.gnu.mingw.exe.release.289745933.692982481" name="x86__Windows__MinGW_Release">
32+
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
33+
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
34+
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
35+
</extension>
36+
</configuration>
37+
</project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#Wed Jan 11 19:59:32 CET 2012
2+
eclipse.preferences.version=1
3+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.894771756/CPATH/delimiter=;
4+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.894771756/CPATH/operation=remove
5+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.894771756/C_INCLUDE_PATH/delimiter=;
6+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.894771756/C_INCLUDE_PATH/operation=remove
7+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.894771756/append=true
8+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.894771756/appendContributed=true
9+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.release.289745933/CPATH/delimiter=;
10+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.release.289745933/CPATH/operation=remove
11+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.release.289745933/C_INCLUDE_PATH/delimiter=;
12+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.release.289745933/C_INCLUDE_PATH/operation=remove
13+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.release.289745933/append=true
14+
environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.release.289745933/appendContributed=true
15+
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.894771756/LIBRARY_PATH/delimiter=;
16+
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.894771756/LIBRARY_PATH/operation=remove
17+
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.894771756/append=true
18+
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.894771756/appendContributed=true
19+
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.release.289745933/LIBRARY_PATH/delimiter=;
20+
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.release.289745933/LIBRARY_PATH/operation=remove
21+
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.release.289745933/append=true
22+
environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.release.289745933/appendContributed=true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#version 150
2+
3+
in vec2 v_pos;
4+
in vec4 v_aabb;
5+
6+
in vec4 v_color;
7+
8+
out vec4 fragColor;
9+
10+
void main(void)
11+
{
12+
// Discard pixels, which are not inside the AABB.
13+
if (v_pos.x < v_aabb.x || v_pos.y < v_aabb.y || v_pos.x > v_aabb.z || v_pos.y > v_aabb.w)
14+
{
15+
discard;
16+
}
17+
18+
fragColor = v_color;
19+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#version 150
2+
3+
layout(triangles) in;
4+
5+
// Conservative and "normal" rasterized triangle are created.
6+
layout(triangle_strip, max_vertices = 6) out;
7+
8+
uniform mat4 u_projectionMatrix;
9+
uniform mat4 u_viewMatrix;
10+
11+
uniform vec2 u_halfPixelSize;
12+
13+
out vec2 v_pos;
14+
out vec4 v_aabb;
15+
16+
out vec4 v_color;
17+
18+
// See Gpu Gems 2, Chapter 42: Conservative Rasterization. (http://http.developer.nvidia.com/GPUGems2/gpugems2_chapter42.html)
19+
// See "Conservative and Tiled Rasterization Using a Modified Triangle Setup." Journal of Graphics Tools.
20+
// See Plane-Plane Intersection (http://mathworld.wolfram.com/Plane-PlaneIntersection.html)
21+
void main(void)
22+
{
23+
int i;
24+
25+
vec4 vertex[3];
26+
27+
vec2 pos[3];
28+
29+
// Axis aligned bounding box (AABB) initialized with maximum/minimum NDC values.
30+
vec4 aabb = vec4(1.0, 1.0, -1.0, -1.0);
31+
32+
for (i = 0; i < gl_in.length(); i++)
33+
{
34+
vertex[i] = u_projectionMatrix * u_viewMatrix * gl_in[i].gl_Position;
35+
36+
// Later NDC position if each fragment.
37+
pos[i] = vertex[i].xy / vertex[i].w;
38+
39+
// Get AABB of the triangle.
40+
aabb.xy = min(aabb.xy, pos[i].xy);
41+
42+
aabb.zw = max(aabb.zw, pos[i].xy);
43+
}
44+
45+
//
46+
// Calculate triangle for conservative rasterization.
47+
//
48+
49+
// Add offset of half pixel size to AABB.
50+
vec4 aabbConservative = aabb + vec4(-u_halfPixelSize, u_halfPixelSize);
51+
52+
//
53+
54+
vec3 plane[3];
55+
56+
for (i = 0; i < gl_in.length(); i++)
57+
{
58+
// Calculate the plane through each edge of the triangle.
59+
// The plane equation is A*x + B*y + C*w = 0. Note, that D component from the plane is zero, as it goes throught the origin. Also, w is used as the third dimension.
60+
//
61+
// Note: The plane goes through the origin, as (vertex[i].x, vertex[i].y, vertex[i].w) are vectors from the origin.
62+
// Note: A*x + B*y + C*w = 0 <=> A*x/w + B*y/w + C = 0 [w != 0] <=> A*xClip + B*yClip + C
63+
// A*xClip + B*yClip + C is a vector in 2D space. In this case, it is the normal of the edge in 2D space.
64+
//
65+
// By calculating the cross product of the two vectors (the end points of the edge), we gather the normal of the plane.
66+
plane[i] = cross(vertex[i].xyw, vertex[(i + 2) % 3].xyw);
67+
68+
// Move plane, by adjusting C.
69+
//
70+
// Note: A*(x+dx) + B*(y+dy) + C*w = 0 [Another plane with a specific distance d given by dx and dy] <=>
71+
// A*x + A*dx + B*y + B*dy + C*w = 0 <=>
72+
// A*xClip + B*yClip + C + A*dx + B*dy = 0
73+
//
74+
// Half pixel size is d. Absolute of plane's xy, as the sign is already in the normal of the plane.
75+
plane[i].z -= dot(u_halfPixelSize, abs(plane[i].xy));
76+
}
77+
78+
// Create conservative, red rasterized triangle ...
79+
80+
vec3 intersect;
81+
82+
for (i = 0; i < gl_in.length(); i++)
83+
{
84+
// As both planes go through the origin, the intersecting line goes also through the origin. This simplifies the intersection calculation.
85+
// The intersecting line is perpendicular to both planes (see Wolfram MathWorld),
86+
// so the intersection line is just the cross product of both normals.
87+
intersect = cross(plane[i], plane[(i+1) % 3]);
88+
89+
// The line is a direction (x, y, w) but projects to the same point in window space.
90+
//
91+
// Compare: (x, y, w) <=> (x/w, y/w, 1) => (xClip, yClip)
92+
//
93+
gl_Position.xyw = intersect;
94+
95+
// Later NDC position if each fragment.
96+
v_pos = intersect.xy / intersect.z;
97+
v_aabb = aabbConservative;
98+
99+
v_color = vec4(1.0, 0.0, 0.0, 1.0);
100+
101+
EmitVertex();
102+
}
103+
104+
EndPrimitive();
105+
106+
// ... and create "normal", blue rasterized triangle for comparison.
107+
108+
for (i = 0; i < gl_in.length(); i++)
109+
{
110+
gl_Position = vertex[i];
111+
112+
v_pos = pos[i];
113+
v_aabb = aabb;
114+
115+
v_color = vec4(0.0, 0.0, 1.0, 1.0);
116+
117+
EmitVertex();
118+
}
119+
120+
EndPrimitive();
121+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#version 150
2+
3+
in vec4 a_vertex;
4+
5+
void main(void)
6+
{
7+
gl_Position = a_vertex;
8+
}

0 commit comments

Comments
 (0)