]> git.tdb.fi Git - libs/gl.git/commitdiff
Load various built-in things through Resources
authorMikko Rasa <tdb@tdb.fi>
Fri, 19 Feb 2021 16:40:30 +0000 (18:40 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 19 Feb 2021 16:40:30 +0000 (18:40 +0200)
There's so many of them that managing them manually is becoming too
tedious.  Especially with some planned shader changes.

42 files changed:
Build
builtin_data/_ambientocclusion.glsl [new file with mode: 0644]
builtin_data/_ambientocclusion_combine.glsl [new file with mode: 0644]
builtin_data/_ambientocclusion_occlude.glsl [new file with mode: 0644]
builtin_data/_bloom.glsl [new file with mode: 0644]
builtin_data/_bloom_blur.glsl [new file with mode: 0644]
builtin_data/_bloom_combine.glsl [new file with mode: 0644]
builtin_data/_colorcurve.glsl [new file with mode: 0644]
builtin_data/_fullscreen_quad.mesh [new file with mode: 0644]
builtin_data/_linear_clamp.samp [new file with mode: 0644]
builtin_data/_linear_clamp_shadow.samp [new file with mode: 0644]
builtin_data/_nearest_clamp.samp [new file with mode: 0644]
builtin_data/_occluder.glsl [new file with mode: 0644]
builtin_data/_occluder.mesh [new file with mode: 0644]
shaderlib/ambientocclusion.glsl [deleted file]
shaderlib/ambientocclusion_combine.glsl [deleted file]
shaderlib/ambientocclusion_occlude.glsl [deleted file]
shaderlib/bloom.glsl [deleted file]
shaderlib/bloom_blur.glsl [deleted file]
shaderlib/bloom_combine.glsl [deleted file]
shaderlib/colorcurve.glsl [deleted file]
shaderlib/occluder.glsl [deleted file]
source/builders/pipelinebuilder.cpp
source/builders/pipelinetemplate.cpp
source/builders/pipelinetemplate.h
source/effects/ambientocclusion.cpp
source/effects/ambientocclusion.h
source/effects/bloom.cpp
source/effects/bloom.h
source/effects/colorcurve.cpp
source/effects/colorcurve.h
source/effects/effect.cpp
source/effects/effect.h
source/effects/environmentmap.cpp
source/effects/environmentmap.h
source/effects/postprocessor.cpp
source/effects/postprocessor.h
source/effects/shadowmap.cpp
source/effects/shadowmap.h
source/render/occludedscene.cpp
source/render/occludedscene.h
source/resources/resources.cpp

diff --git a/Build b/Build
index 02f502b4e892532e918f79139fc11fb17c4b141b..19831c3e54cc5eed723f2f91acd9572ccd288f63 100644 (file)
--- a/Build
+++ b/Build
@@ -32,6 +32,8 @@ package "mspgl"
        generate "RES"
        {
                in_suffix ".glsl";
+               in_suffix ".samp";
+               in_suffix ".mesh";
                out_suffix ".cpp";
                command "mspdatatool";
                arguments "-i" "-n" "Msp::GL";
@@ -50,6 +52,7 @@ package "mspgl"
                source "source/glsl";
                source "source/builders";
                source "extensions";
+               source "builtin_data";
                source "shaderlib";
                build_info
                {
diff --git a/builtin_data/_ambientocclusion.glsl b/builtin_data/_ambientocclusion.glsl
new file mode 100644 (file)
index 0000000..89d1335
--- /dev/null
@@ -0,0 +1,31 @@
+const int max_samples = 32;
+
+uniform mat4 projection_matrix;
+
+uniform sampler2D depth;
+uniform sampler2D occlusion;
+uniform sampler2D rotate;
+uniform AmbientOcclusionParams
+{
+       mat4 inverse_projection;
+       float darkness;
+       vec3 sample_points[max_samples];
+       int n_samples;
+       float occlusion_radius;
+       float edge_depth_threshold;
+};
+
+#pragma MSP stage(fragment)
+vec3 project(vec3 position)
+{
+       if(position.z>=0.0)
+               return vec3(0.0, 0.0, -1.0);
+       vec4 pp = projection_matrix*vec4(position, 1.0);
+       return pp.xyz/pp.w;
+}
+
+vec3 unproject(vec3 position)
+{
+       vec4 upp = inverse_projection*vec4(position, 1.0);
+       return upp.xyz/upp.w;
+}
diff --git a/builtin_data/_ambientocclusion_combine.glsl b/builtin_data/_ambientocclusion_combine.glsl
new file mode 100644 (file)
index 0000000..616bada
--- /dev/null
@@ -0,0 +1,29 @@
+import postprocess;
+import _ambientocclusion;
+
+#pragma MSP stage(fragment)
+void main()
+{
+       vec3 center = unproject(vec3(vertex.xy, texture(depth, texcoord).r));
+       vec2 tex_scale = 1.0/vec2(textureSize(occlusion, 0));
+       float sum = 0.0;
+       float count = 0.0;
+       for(int i=0; i<4; ++i)
+               for(int j=0; j<4; ++j)
+               {
+                       vec2 offset = vec2(float(i), float(j))-1.5;
+                       vec2 sample_coord = texcoord+offset*tex_scale;
+                       float occ = texture(occlusion, sample_coord).r;
+                       float sample = texture(depth, sample_coord).r;
+                       float z_range = occlusion_radius*length(offset)*edge_depth_threshold;
+                       float min_depth = project(vec3(center.xy, center.z+z_range)).z;
+                       float max_depth = project(vec3(center.xy, center.z-z_range)).z;
+                       if(sample>=min_depth && sample<=max_depth)
+                       {
+                               sum += occ;
+                               count += 1.0;
+                       }
+               }
+       vec4 src_color = texture(source, texcoord);
+       frag_color = vec4(src_color.rgb*mix(1.0, min(sum*2.0/count, 1.0), darkness), src_color.a);
+}
diff --git a/builtin_data/_ambientocclusion_occlude.glsl b/builtin_data/_ambientocclusion_occlude.glsl
new file mode 100644 (file)
index 0000000..e190032
--- /dev/null
@@ -0,0 +1,25 @@
+import postprocess;
+import _ambientocclusion;
+
+#pragma MSP stage(fragment)
+void main()
+{
+       vec4 rv = texture(rotate, gl_FragCoord.xy/4.0)*2.0-1.0;
+       mat3 transform = mat3(rv.xy, 0.0, rv.zx, 0.0, 0.0, 0.0, rv.w)*occlusion_radius;
+       vec3 center = unproject(vec3(vertex.xy, texture(depth, texcoord).r));
+       float min_depth = project(vec3(center.xy, center.z+occlusion_radius)).z;
+       float occlusion_sum = 0.0;
+       float count = 0.0;
+       for(int i=0; i<n_samples; ++i)
+       {
+               vec3 psp = project(center+transform*sample_points[i]);
+               float sample = texture(depth, psp.xy*0.5+0.5).r;
+               if(sample>=min_depth)
+               {
+                       if(sample<psp.z)
+                               occlusion_sum += 1.0;
+                       count += 1.0;
+               }
+       }
+       frag_color = vec4(1.0-occlusion_sum/count, 0.0, 0.0, 1.0);
+}
diff --git a/builtin_data/_bloom.glsl b/builtin_data/_bloom.glsl
new file mode 100644 (file)
index 0000000..262cb29
--- /dev/null
@@ -0,0 +1,8 @@
+uniform sampler2D blurred;
+uniform vec2 delta;
+uniform BloomParams
+{
+       float factors[19];
+       int size;
+       float strength;
+};
diff --git a/builtin_data/_bloom_blur.glsl b/builtin_data/_bloom_blur.glsl
new file mode 100644 (file)
index 0000000..ce1cf75
--- /dev/null
@@ -0,0 +1,10 @@
+import postprocess;
+import _bloom;
+
+#pragma MSP stage(fragment)
+void main()
+{
+       frag_color = vec4(0.0, 0.0, 0.0, 0.0);
+       for(int i=0; i<=size*2; ++i)
+               frag_color += texture(source, texcoord+delta*float(i-size))*factors[i];
+}
diff --git a/builtin_data/_bloom_combine.glsl b/builtin_data/_bloom_combine.glsl
new file mode 100644 (file)
index 0000000..641459b
--- /dev/null
@@ -0,0 +1,8 @@
+import postprocess;
+import _bloom;
+
+#pragma MSP stage(fragment)
+void main()
+{
+       frag_color = mix(texture(source, texcoord), texture(blurred, texcoord), strength);
+}
diff --git a/builtin_data/_colorcurve.glsl b/builtin_data/_colorcurve.glsl
new file mode 100644 (file)
index 0000000..23b5577
--- /dev/null
@@ -0,0 +1,24 @@
+import postprocess;
+
+uniform sampler1D curve;
+uniform ToneMapping
+{
+       float exposure;
+       vec3 brightness_response;
+};
+
+#pragma MSP stage(fragment)
+void main()
+{
+       vec4 incoming = texture(source, texcoord);
+       float maxc = max(incoming.r, max(incoming.g, incoming.b));
+       if(maxc>0.0)
+       {
+               vec3 saturated = incoming.rgb/maxc;
+               maxc = pow(maxc*exposure+brightness_response.y, brightness_response.x)-brightness_response.z;
+               float c = min(maxc, 1.0);
+               float minc = min(saturated.r, min(saturated.g, saturated.b));
+               incoming.rgb = mix(saturated, vec3(1.0), min((maxc-c)/(1.0-minc), 1.0))*c;
+       }
+       frag_color = vec4(texture(curve, incoming.r).r, texture(curve, incoming.g).r, texture(curve, incoming.b).r, incoming.a);
+}
diff --git a/builtin_data/_fullscreen_quad.mesh b/builtin_data/_fullscreen_quad.mesh
new file mode 100644 (file)
index 0000000..635afda
--- /dev/null
@@ -0,0 +1,11 @@
+vertices VERTEX2
+{
+       vertex -1 1;
+       vertex -1 -1;
+       vertex 1 1;
+       vertex 1 -1;
+};
+batch TRIANGLE_STRIP
+{
+       indices 0 1 2 3;
+};
diff --git a/builtin_data/_linear_clamp.samp b/builtin_data/_linear_clamp.samp
new file mode 100644 (file)
index 0000000..d9fe459
--- /dev/null
@@ -0,0 +1,2 @@
+filter LINEAR;
+wrap CLAMP_TO_EDGE;
diff --git a/builtin_data/_linear_clamp_shadow.samp b/builtin_data/_linear_clamp_shadow.samp
new file mode 100644 (file)
index 0000000..211be2e
--- /dev/null
@@ -0,0 +1,3 @@
+filter LINEAR;
+wrap CLAMP_TO_EDGE;
+compare LEQUAL;
diff --git a/builtin_data/_nearest_clamp.samp b/builtin_data/_nearest_clamp.samp
new file mode 100644 (file)
index 0000000..f23f95f
--- /dev/null
@@ -0,0 +1,2 @@
+filter NEAREST;
+wrap CLAMP_TO_EDGE;
diff --git a/builtin_data/_occluder.glsl b/builtin_data/_occluder.glsl
new file mode 100644 (file)
index 0000000..e46ca02
--- /dev/null
@@ -0,0 +1,11 @@
+import msp_interface;
+#pragma MSP stage(vertex)
+void main()
+{
+       gl_Position = projection_matrix*eye_obj_matrix*vertex;
+}
+#pragma MSP stage(fragment)
+void main()
+{
+       frag_color = vec4(1.0);
+}
diff --git a/builtin_data/_occluder.mesh b/builtin_data/_occluder.mesh
new file mode 100644 (file)
index 0000000..9614df4
--- /dev/null
@@ -0,0 +1,39 @@
+vertices VERTEX3
+{
+       vertex 0.0 -0.5257311 -0.8506508;
+       vertex 0.0 0.5257311 -0.8506508;
+       vertex 0.0 -0.5257311 0.8506508;
+       vertex 0.0 0.5257311 0.8506508;
+       vertex -0.8506508 0.0 -0.5257311;
+       vertex -0.8506508 0.0 0.5257311;
+       vertex 0.8506508 0.0 -0.5257311;
+       vertex 0.8506508 0.0 0.5257311;
+       vertex -0.5257311 -0.8506508 0.0;
+       vertex 0.5257311 -0.8506508 0.0;
+       vertex -0.5257311 0.8506508 0.0;
+       vertex 0.5257311 0.8506508 0.0;
+};
+batch TRIANGLES
+{
+       indices 0 1 6;
+       indices 1 0 4;
+       indices 2 3 5;
+       indices 3 2 7;
+       indices 4 5 10;
+       indices 5 4 8;
+       indices 6 7 9;
+       indices 7 6 11;
+       indices 8 9 2;
+       indices 9 8 0;
+       indices 10 11 1;
+       indices 11 10 3;
+       indices 0 8 4;
+       indices 0 6 9;
+       indices 1 4 10;
+       indices 1 11 6;
+       indices 2 5 8;
+       indices 2 9 7;
+       indices 3 10 5;
+       indices 3 7 11;
+};
+winding COUNTERCLOCKWISE;
diff --git a/shaderlib/ambientocclusion.glsl b/shaderlib/ambientocclusion.glsl
deleted file mode 100644 (file)
index 89d1335..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-const int max_samples = 32;
-
-uniform mat4 projection_matrix;
-
-uniform sampler2D depth;
-uniform sampler2D occlusion;
-uniform sampler2D rotate;
-uniform AmbientOcclusionParams
-{
-       mat4 inverse_projection;
-       float darkness;
-       vec3 sample_points[max_samples];
-       int n_samples;
-       float occlusion_radius;
-       float edge_depth_threshold;
-};
-
-#pragma MSP stage(fragment)
-vec3 project(vec3 position)
-{
-       if(position.z>=0.0)
-               return vec3(0.0, 0.0, -1.0);
-       vec4 pp = projection_matrix*vec4(position, 1.0);
-       return pp.xyz/pp.w;
-}
-
-vec3 unproject(vec3 position)
-{
-       vec4 upp = inverse_projection*vec4(position, 1.0);
-       return upp.xyz/upp.w;
-}
diff --git a/shaderlib/ambientocclusion_combine.glsl b/shaderlib/ambientocclusion_combine.glsl
deleted file mode 100644 (file)
index c1e7f27..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-import postprocess;
-import ambientocclusion;
-
-#pragma MSP stage(fragment)
-void main()
-{
-       vec3 center = unproject(vec3(vertex.xy, texture(depth, texcoord).r));
-       vec2 tex_scale = 1.0/vec2(textureSize(occlusion, 0));
-       float sum = 0.0;
-       float count = 0.0;
-       for(int i=0; i<4; ++i)
-               for(int j=0; j<4; ++j)
-               {
-                       vec2 offset = vec2(float(i), float(j))-1.5;
-                       vec2 sample_coord = texcoord+offset*tex_scale;
-                       float occ = texture(occlusion, sample_coord).r;
-                       float sample = texture(depth, sample_coord).r;
-                       float z_range = occlusion_radius*length(offset)*edge_depth_threshold;
-                       float min_depth = project(vec3(center.xy, center.z+z_range)).z;
-                       float max_depth = project(vec3(center.xy, center.z-z_range)).z;
-                       if(sample>=min_depth && sample<=max_depth)
-                       {
-                               sum += occ;
-                               count += 1.0;
-                       }
-               }
-       vec4 src_color = texture(source, texcoord);
-       frag_color = vec4(src_color.rgb*mix(1.0, min(sum*2.0/count, 1.0), darkness), src_color.a);
-}
diff --git a/shaderlib/ambientocclusion_occlude.glsl b/shaderlib/ambientocclusion_occlude.glsl
deleted file mode 100644 (file)
index 2a10bde..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-import postprocess;
-import ambientocclusion;
-
-#pragma MSP stage(fragment)
-void main()
-{
-       vec4 rv = texture(rotate, gl_FragCoord.xy/4.0)*2.0-1.0;
-       mat3 transform = mat3(rv.xy, 0.0, rv.zx, 0.0, 0.0, 0.0, rv.w)*occlusion_radius;
-       vec3 center = unproject(vec3(vertex.xy, texture(depth, texcoord).r));
-       float min_depth = project(vec3(center.xy, center.z+occlusion_radius)).z;
-       float occlusion_sum = 0.0;
-       float count = 0.0;
-       for(int i=0; i<n_samples; ++i)
-       {
-               vec3 psp = project(center+transform*sample_points[i]);
-               float sample = texture(depth, psp.xy*0.5+0.5).r;
-               if(sample>=min_depth)
-               {
-                       if(sample<psp.z)
-                               occlusion_sum += 1.0;
-                       count += 1.0;
-               }
-       }
-       frag_color = vec4(1.0-occlusion_sum/count, 0.0, 0.0, 1.0);
-}
diff --git a/shaderlib/bloom.glsl b/shaderlib/bloom.glsl
deleted file mode 100644 (file)
index 262cb29..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-uniform sampler2D blurred;
-uniform vec2 delta;
-uniform BloomParams
-{
-       float factors[19];
-       int size;
-       float strength;
-};
diff --git a/shaderlib/bloom_blur.glsl b/shaderlib/bloom_blur.glsl
deleted file mode 100644 (file)
index aadde90..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-import postprocess;
-import bloom;
-
-#pragma MSP stage(fragment)
-void main()
-{
-       frag_color = vec4(0.0, 0.0, 0.0, 0.0);
-       for(int i=0; i<=size*2; ++i)
-               frag_color += texture(source, texcoord+delta*float(i-size))*factors[i];
-}
diff --git a/shaderlib/bloom_combine.glsl b/shaderlib/bloom_combine.glsl
deleted file mode 100644 (file)
index 4066863..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-import postprocess;
-import bloom;
-
-#pragma MSP stage(fragment)
-void main()
-{
-       frag_color = mix(texture(source, texcoord), texture(blurred, texcoord), strength);
-}
diff --git a/shaderlib/colorcurve.glsl b/shaderlib/colorcurve.glsl
deleted file mode 100644 (file)
index 23b5577..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-import postprocess;
-
-uniform sampler1D curve;
-uniform ToneMapping
-{
-       float exposure;
-       vec3 brightness_response;
-};
-
-#pragma MSP stage(fragment)
-void main()
-{
-       vec4 incoming = texture(source, texcoord);
-       float maxc = max(incoming.r, max(incoming.g, incoming.b));
-       if(maxc>0.0)
-       {
-               vec3 saturated = incoming.rgb/maxc;
-               maxc = pow(maxc*exposure+brightness_response.y, brightness_response.x)-brightness_response.z;
-               float c = min(maxc, 1.0);
-               float minc = min(saturated.r, min(saturated.g, saturated.b));
-               incoming.rgb = mix(saturated, vec3(1.0), min((maxc-c)/(1.0-minc), 1.0))*c;
-       }
-       frag_color = vec4(texture(curve, incoming.r).r, texture(curve, incoming.g).r, texture(curve, incoming.b).r, incoming.a);
-}
diff --git a/shaderlib/occluder.glsl b/shaderlib/occluder.glsl
deleted file mode 100644 (file)
index e46ca02..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-import msp_interface;
-#pragma MSP stage(vertex)
-void main()
-{
-       gl_Position = projection_matrix*eye_obj_matrix*vertex;
-}
-#pragma MSP stage(fragment)
-void main()
-{
-       frag_color = vec4(1.0);
-}
index 51e9cc855f8b73d1cb7b4cd49f616a4cc3faea58..12a5312f199af1db05eed01adfb86ec4b2a765d6 100644 (file)
@@ -66,7 +66,7 @@ void PipelineBuilder::build(Pipeline &pipeline) const
                        pipeline.add_postprocessor(*proc);
                else if(i->postprocessor_template)
                {
-                       proc = i->postprocessor_template->create(pipeline.get_width(), pipeline.get_height());
+                       proc = i->postprocessor_template->create(tmpl.get_resources(), pipeline.get_width(), pipeline.get_height());
                        if(proc)
                                pipeline.add_postprocessor_owned(proc);
                }
index b78b4c40a2fca807017fbbbc29b182408e8f0533..29011e31dcf09b4190175493a09da3a11be1d1a3 100644 (file)
@@ -6,6 +6,7 @@
 #include "colorcurve.h"
 #include "lighting.h"
 #include "pipelinetemplate.h"
+#include "resources.h"
 #include "tests.h"
 
 using namespace std;
@@ -14,6 +15,7 @@ namespace Msp {
 namespace GL {
 
 PipelineTemplate::PipelineTemplate():
+       resources(0),
        hdr(false),
        alpha(false),
        required_multisample(0),
@@ -26,6 +28,13 @@ PipelineTemplate::~PipelineTemplate()
                delete i->postprocessor_template;
 }
 
+Resources &PipelineTemplate::get_resources() const
+{
+       if(!resources)  
+               throw logic_error("no resources");
+       return *resources;
+}
+
 
 PipelineTemplate::PostProcessorRegistry &PipelineTemplate::get_postprocessor_registry()
 {
@@ -57,19 +66,8 @@ PipelineTemplate::PostProcLoader::PostProcLoader()
 }
 
 
-PipelineTemplate::Loader::Loader(PipelineTemplate &t):
-       DataFile::CollectionObjectLoader<PipelineTemplate>(t, 0)
-{
-       init();
-}
-
 PipelineTemplate::Loader::Loader(PipelineTemplate &t, Collection &c):
-       DataFile::CollectionObjectLoader<PipelineTemplate>(t, &c)
-{
-       init();
-}
-
-void PipelineTemplate::Loader::init()
+       DataFile::CollectionObjectLoader<PipelineTemplate, Resources>(t, &c)
 {
        add("hdr", &PipelineTemplate::hdr);
        add("alpha", &PipelineTemplate::alpha);
@@ -77,6 +75,8 @@ void PipelineTemplate::Loader::init()
        add("multisample", &Loader::multisample_range);
        add("pass", &Loader::pass);
        add("postprocessor", &Loader::postprocessor);
+
+       obj.resources = &c;
 }
 
 void PipelineTemplate::Loader::postprocessor_loaded()
index fb24ea29df547f468ff05ba142631c7bd215107d..177c0b145356f05e613d4f256efcb5e62ff2c47f 100644 (file)
@@ -46,13 +46,10 @@ private:
        };
 
 public:
-       class Loader: public DataFile::CollectionObjectLoader<PipelineTemplate>, public PostProcLoader
+       class Loader: public DataFile::CollectionObjectLoader<PipelineTemplate, Resources>, public PostProcLoader
        {
        public:
-               Loader(PipelineTemplate &);
                Loader(PipelineTemplate &, Collection &);
-       private:
-               void init();
 
                virtual void postprocessor_loaded();
                void multisample(unsigned);
@@ -105,6 +102,7 @@ public:
 private:
        typedef DataFile::LoadableTypeRegistry<PostProcLoader, PostProcLoader::AddPostProc> PostProcessorRegistry;
 
+       Resources *resources;
        bool hdr;
        bool alpha;
        unsigned required_multisample;
@@ -116,6 +114,7 @@ public:
        PipelineTemplate();
        ~PipelineTemplate();
 
+       Resources &get_resources() const;
        bool get_hdr() const { return hdr; }
        bool get_alpha() const { return alpha; }
        unsigned get_required_multisample() const { return required_multisample; }
index 8c1dd616c4b53ba40b49aea69bfd237bd89e1b17..e5de04b4846eebf5fe5ff24569e637f1760ce4ee 100644 (file)
@@ -3,6 +3,7 @@
 #include "blend.h"
 #include "camera.h"
 #include "renderer.h"
+#include "resources.h"
 #include "shader.h"
 #include "tests.h"
 
@@ -11,15 +12,15 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
+AmbientOcclusion::AmbientOcclusion(Resources &resources, unsigned w, unsigned h, float):
        occlude_target(w, h, (RENDER_COLOR,R8)),
-       occlude_shader("ambientocclusion_occlude.glsl"),
-       combine_shader("ambientocclusion_combine.glsl"),
-       quad(get_fullscreen_quad()),
-       linear_sampler(get_linear_sampler()),
-       nearest_sampler(get_nearest_sampler())
+       occlude_shader(resources.get<Program>("_ambientocclusion_occlude.glsl")),
+       combine_shader(resources.get<Program>("_ambientocclusion_combine.glsl")),
+       quad(resources.get<Mesh>("_fullscreen_quad.mesh")),
+       linear_sampler(resources.get<Sampler>("_linear_clamp.samp")),
+       nearest_sampler(resources.get<Sampler>("_nearest_clamp.samp"))
 {
-       texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR), linear_sampler.get());
+       texturing.attach(2, occlude_target.get_target_texture(RENDER_COLOR), &linear_sampler);
 
        unsigned seed = 1;
        rotate_lookup.storage(RGBA8, 4, 4, 1);
@@ -36,7 +37,7 @@ AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float):
        }
        rotate_lookup.image(0, data);
 
-       texturing.attach(3, rotate_lookup, nearest_sampler.get());
+       texturing.attach(3, rotate_lookup, &nearest_sampler);
 
        shdata.uniform("source", 0);
        shdata.uniform("depth", 1);
@@ -91,8 +92,8 @@ void AmbientOcclusion::set_edge_depth_threshold(float edt)
 
 void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth)
 {
-       texturing.attach(0, color, nearest_sampler.get());
-       texturing.attach(1, depth, nearest_sampler.get());
+       texturing.attach(0, color, &nearest_sampler);
+       texturing.attach(1, depth, &nearest_sampler);
 
        if(renderer.get_camera())
                shdata.uniform("inverse_projection", invert(renderer.get_camera()->get_projection_matrix()));
@@ -103,11 +104,11 @@ void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const
 
        {
                BindRestore bind_fbo(occlude_target.get_framebuffer());
-               quad->draw(renderer);
+               quad.draw(renderer);
        }
 
        renderer.set_shader_program(&combine_shader);
-       quad->draw(renderer);
+       quad.draw(renderer);
 }
 
 
@@ -118,9 +119,9 @@ AmbientOcclusion::Template::Template():
        edge_depth_threshold(0.1f)
 { }
 
-AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const
+AmbientOcclusion *AmbientOcclusion::Template::create(Resources &res, unsigned width, unsigned height) const
 {
-       RefPtr<AmbientOcclusion> ao = new AmbientOcclusion(width/size_divisor, height/size_divisor);
+       RefPtr<AmbientOcclusion> ao = new AmbientOcclusion(res, width/size_divisor, height/size_divisor);
        ao->set_n_samples(n_samples);
        ao->set_occlusion_radius(occlusion_radius);
        ao->set_darkness(darkness);
index 6c2c5d7eea938daa6be0e6e5ce2387dd2a361660..25fab5c9f23952df4ce0516d77ac2a6993e3b0ab 100644 (file)
@@ -36,22 +36,22 @@ public:
 
                Template();
 
-               virtual AmbientOcclusion *create(unsigned, unsigned) const;
+               virtual AmbientOcclusion *create(Resources &, unsigned, unsigned) const;
        };
 
 private:
        Texture2D rotate_lookup;
        RenderTarget occlude_target;
        Texturing texturing;
-       Program occlude_shader;
-       Program combine_shader;
+       const Program &occlude_shader;
+       const Program &combine_shader;
        mutable ProgramData shdata;
-       RefPtr<Mesh> quad;
-       RefPtr<Sampler> linear_sampler;
-       RefPtr<Sampler> nearest_sampler;
+       const Mesh &quad;
+       const Sampler &linear_sampler;
+       const Sampler &nearest_sampler;
 
 public:
-       AmbientOcclusion(unsigned, unsigned, float = 1.0f);
+       AmbientOcclusion(Resources &, unsigned, unsigned, float = 1.0f);
 
 private:
        static float random(unsigned &);
index ee7c76c3217bef3befc48e40b794d3f485cb4389..1204c81a9eadcb8b452d75af19e206a935f41738 100644 (file)
@@ -4,6 +4,7 @@
 #include "bloom.h"
 #include "misc.h"
 #include "renderer.h"
+#include "resources.h"
 #include "shader.h"
 #include "tests.h"
 #include "texunit.h"
@@ -13,12 +14,12 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-Bloom::Bloom(unsigned w, unsigned h):
-       blur_shader("bloom_blur.glsl"),
-       combine_shader("bloom_combine.glsl"),
-       quad(get_fullscreen_quad()),
-       nearest_sampler(get_nearest_sampler()),
-       linear_sampler(get_linear_sampler())
+Bloom::Bloom(Resources &resources, unsigned w, unsigned h):
+       blur_shader(resources.get<Program>("_bloom_blur.glsl")),
+       combine_shader(resources.get<Program>("_bloom_combine.glsl")),
+       quad(resources.get<Mesh>("_fullscreen_quad.mesh")),
+       nearest_sampler(resources.get<Sampler>("_nearest_clamp.samp")),
+       linear_sampler(resources.get<Sampler>("_linear_clamp.samp"))
 {
        blur_shdata[0].uniform("delta", 1.0f/w, 0.0f);
        blur_shdata[1].uniform("delta", 0.0f, 1.0f/h);
@@ -29,7 +30,7 @@ Bloom::Bloom(unsigned w, unsigned h):
        common_shdata.uniform("source", 0);
        common_shdata.uniform("blurred", 1);
 
-       combine_texturing.attach(1, target[1]->get_target_texture(RENDER_COLOR), linear_sampler.get());
+       combine_texturing.attach(1, target[1]->get_target_texture(RENDER_COLOR), &linear_sampler);
 
        set_radius(2.0f);
        set_strength(0.2f);
@@ -75,15 +76,15 @@ void Bloom::render(Renderer &renderer, const Texture2D &src, const Texture2D &)
        {
                BindRestore bind_fbo(target[i]->get_framebuffer());
                Renderer::Push push2(renderer);
-               renderer.set_texture(i ? &target[0]->get_target_texture(RENDER_COLOR) : &src, nearest_sampler.get());
+               renderer.set_texture(i ? &target[0]->get_target_texture(RENDER_COLOR) : &src, &nearest_sampler);
                renderer.add_shader_data(blur_shdata[i]);
-               quad->draw(renderer);
+               quad.draw(renderer);
        }
 
-       combine_texturing.attach(0, src, nearest_sampler.get());
+       combine_texturing.attach(0, src, &nearest_sampler);
        renderer.set_texturing(&combine_texturing);
        renderer.set_shader_program(&combine_shader);
-       quad->draw(renderer);
+       quad.draw(renderer);
 }
 
 
@@ -92,9 +93,9 @@ Bloom::Template::Template():
        strength(0.2f)
 { }
 
-Bloom *Bloom::Template::create(unsigned width, unsigned height) const
+Bloom *Bloom::Template::create(Resources &res, unsigned width, unsigned height) const
 {
-       RefPtr<Bloom> bloom = new Bloom(width/size_divisor, height/size_divisor);
+       RefPtr<Bloom> bloom = new Bloom(res, width/size_divisor, height/size_divisor);
        bloom->set_radius(radius);
        bloom->set_strength(strength);
        return bloom.release();
index 6b10195b873e684d6af7634b85d8dd31a9f05879..27584886824ae3dc7ed8a255eee9c9fa0cf625ca 100644 (file)
@@ -37,22 +37,22 @@ public:
 
                Template();
 
-               virtual Bloom *create(unsigned, unsigned) const;
+               virtual Bloom *create(Resources &, unsigned, unsigned) const;
        };
 
 private:
        RenderTarget *target[2];
        ProgramData common_shdata;
-       Program blur_shader;
+       const Program &blur_shader;
        ProgramData blur_shdata[2];
-       Program combine_shader;
+       const Program &combine_shader;
+       const Mesh &quad;
+       const Sampler &nearest_sampler;
+       const Sampler &linear_sampler;
        Texturing combine_texturing;
-       RefPtr<Mesh> quad;
-       RefPtr<Sampler> nearest_sampler;
-       RefPtr<Sampler> linear_sampler;
 
 public:
-       Bloom(unsigned, unsigned);
+       Bloom(Resources &, unsigned, unsigned);
        ~Bloom();
 
        /** Sets the σ value of the gaussian blur.  Values much larger than 4.0 are
index 8811b065fd3b6f0dddddd86a8e091e08d97bc224..697c7257fe84059dcc0eeef53244af28d0bc5e52 100644 (file)
@@ -3,6 +3,7 @@
 #include "colorcurve.h"
 #include "mesh.h"
 #include "renderer.h"
+#include "resources.h"
 #include "shader.h"
 #include "texture2d.h"
 
@@ -11,17 +12,17 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-ColorCurve::ColorCurve():
-       shprog("colorcurve.glsl"),
-       quad(get_fullscreen_quad()),
-       linear_sampler(get_linear_sampler()),
-       nearest_sampler(get_nearest_sampler())
+ColorCurve::ColorCurve(Resources &resources):
+       shprog(resources.get<Program>("_colorcurve.glsl")),
+       quad(resources.get<Mesh>("_fullscreen_quad.mesh")),
+       linear_sampler(resources.get<Sampler>("_linear_clamp.samp")),
+       nearest_sampler(resources.get<Sampler>("_nearest_clamp.samp"))
 {
        shdata.uniform("source", 0);
        shdata.uniform("curve", 1);
 
        curve.storage(LUMINANCE8, 256, 1);
-       texturing.attach(1, curve, linear_sampler.get());
+       texturing.attach(1, curve, &linear_sampler);
 
        set_exposure_adjust(0.0f);
        set_brightness_response(0.4f);
@@ -71,12 +72,12 @@ void ColorCurve::set_linear()
 
 void ColorCurve::render(Renderer &renderer, const Texture2D &color_buf, const Texture2D &)
 {
-       texturing.attach(0, color_buf, nearest_sampler.get());
+       texturing.attach(0, color_buf, &nearest_sampler);
 
        Renderer::Push push(renderer);
        renderer.set_shader_program(&shprog, &shdata);
        renderer.set_texturing(&texturing);
-       quad->draw(renderer);
+       quad.draw(renderer);
 }
 
 
@@ -87,9 +88,9 @@ ColorCurve::Template::Template():
        srgb(false)
 { }
 
-ColorCurve *ColorCurve::Template::create(unsigned, unsigned) const
+ColorCurve *ColorCurve::Template::create(Resources &res, unsigned, unsigned) const
 {
-       RefPtr<ColorCurve> colorcurve = new ColorCurve;
+       RefPtr<ColorCurve> colorcurve = new ColorCurve(res);
        colorcurve->set_exposure_adjust(exposure_adjust);
        colorcurve->set_brightness_response(brightness_response);
        if(srgb)
index 3f65b04916897c6d85bde7ec4f33fece160a3cd1..b99bb14e2bac432e89cc78abae5b8e1f7ceab059 100644 (file)
@@ -41,20 +41,20 @@ public:
 
                Template();
 
-               virtual ColorCurve *create(unsigned, unsigned) const;
+               virtual ColorCurve *create(Resources &, unsigned, unsigned) const;
        };
 
 private:
-       Program shprog;
+       const Program &shprog;
        ProgramData shdata;
        Texture1D curve;
        Texturing texturing;
-       RefPtr<Mesh> quad;
-       RefPtr<Sampler> linear_sampler;
-       RefPtr<Sampler> nearest_sampler;
+       const Mesh &quad;
+       const Sampler &linear_sampler;
+       const Sampler &nearest_sampler;
 
 public:
-       ColorCurve();
+       ColorCurve(Resources &);
 
        /** Set exposure adjustment in EV units.  Positive values brighten the
        image, negative values darken it.  Zero is neutral. */
index ad789af06019785deb0d1a68f16236b3c37157af..46a0f2362c0700bdcc47873db7283bbff9724558 100644 (file)
@@ -4,8 +4,6 @@
 namespace Msp {
 namespace GL {
 
-WeakPtr<Sampler> Effect::linear_sampler;
-
 Effect::Effect(Renderable &r):
        renderable(r)
 {
@@ -22,18 +20,5 @@ void Effect::disable_for_pass(const Tag &tag)
        enabled_passes.erase(tag);
 }
 
-RefPtr<Sampler> Effect::get_linear_sampler()
-{
-       RefPtr<Sampler> sampler = linear_sampler;
-       if(!sampler)
-       {
-               sampler = new Sampler;
-               sampler->set_filter(LINEAR);
-               sampler->set_wrap(CLAMP_TO_EDGE);
-               linear_sampler = sampler;
-       }
-       return sampler;
-}
-
 } // namespace GL
 } // namespace Msp
index bc9e19ec818f2a392889866bdf36428c7c00024b..c68bc944f8edd0a8dc2490e74e1234bace23fa12 100644 (file)
@@ -21,9 +21,6 @@ protected:
        Renderable &renderable;
        std::set<Tag> enabled_passes;
 
-private:
-       static WeakPtr<Sampler> linear_sampler;
-
 protected:
        Effect(Renderable &);
 public:
@@ -37,9 +34,6 @@ public:
 
        virtual void setup_frame(Renderer &r) { renderable.setup_frame(r); }
        virtual void finish_frame() { renderable.finish_frame(); }
-
-protected:
-       static RefPtr<Sampler> get_linear_sampler();
 };
 
 } // namespace GL
index 3902eb8c95b240434036bdd0915e9433cd373149..0216ffc22aad0ca7b31c0527b437d3c616d828a4 100644 (file)
@@ -2,6 +2,7 @@
 #include <cmath>
 #include "environmentmap.h"
 #include "renderer.h"
+#include "resources.h"
 #include "texunit.h"
 
 using namespace std;
@@ -9,11 +10,11 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-EnvironmentMap::EnvironmentMap(unsigned s, Renderable &r, Renderable &e):
+EnvironmentMap::EnvironmentMap(Resources &resources, unsigned s, Renderable &r, Renderable &e):
        Effect(r),
        size(s),
        environment(e),
-       sampler(get_linear_sampler()),
+       sampler(resources.get<Sampler>("_linear_clamp.samp")),
        rendered(false),
        update_interval(1),
        update_delay(0)
@@ -107,7 +108,7 @@ void EnvironmentMap::render(Renderer &renderer, const Tag &tag) const
 
        unsigned unit = renderer.allocate_effect_texunit();
        shdata.uniform("environment", static_cast<int>(unit));
-       Bind _bind_sampler(*sampler, unit);
+       Bind _bind_sampler(sampler, unit);
        Bind _bind_env(env_tex, unit);
 
        const Matrix &camera_matrix = renderer.get_camera()->get_object_matrix();
index c54968de35d5cfc7bf11be0ee14559c34cf766fc..1aaa26b7e68f425b29a4fe7f2e3a442cd9792eb6 100644 (file)
@@ -14,6 +14,7 @@ namespace Msp {
 namespace GL {
 
 class Renderable;
+class Resources;
 
 /**
 Creates a cube map texture of the surroundings of the renderable.  This texture
@@ -32,7 +33,7 @@ private:
        TextureCube env_tex;
        Renderbuffer depth_buf;
        Framebuffer fbo[6];
-       RefPtr<Sampler> sampler;
+       const Sampler &sampler;
        Camera camera;
        mutable ProgramData shdata;
        bool rendered;
@@ -40,7 +41,7 @@ private:
        unsigned update_delay;
 
 public:
-       EnvironmentMap(unsigned size, Renderable &rend, Renderable &env);
+       EnvironmentMap(Resources &, unsigned size, Renderable &rend, Renderable &env);
 
        void set_depth_clip(float, float);
        void set_update_interval(unsigned);
index 6af069e97df613c76fda5a4ed8399b88c4e69b87..2a490a46780f1157a5ac84a9ab3307b8ed6661b2 100644 (file)
@@ -7,59 +7,11 @@
 namespace Msp {
 namespace GL {
 
-WeakPtr<Mesh> PostProcessor::fullscreen_quad;
-WeakPtr<Sampler> PostProcessor::nearest_sampler;
-WeakPtr<Sampler> PostProcessor::linear_sampler;
-
 void PostProcessor::render(Renderer &, const Texture2D &color, const Texture2D &depth)
 {
        render(color, depth);
 }
 
-RefPtr<Mesh> PostProcessor::get_fullscreen_quad()
-{
-       RefPtr<Mesh> mesh = fullscreen_quad;
-       if(!mesh)
-       {
-               mesh = new Mesh(VERTEX2);
-               MeshBuilder builder(*mesh);
-               builder.begin(TRIANGLE_STRIP);
-               builder.vertex(-1, 1);
-               builder.vertex(-1, -1);
-               builder.vertex(1, 1);
-               builder.vertex(1, -1);
-               builder.end();
-               fullscreen_quad = mesh;
-       }
-       return mesh;
-}
-
-RefPtr<Sampler> PostProcessor::get_nearest_sampler()
-{
-       RefPtr<Sampler> sampler = nearest_sampler;
-       if(!sampler)
-       {
-               sampler = new Sampler;
-               sampler->set_filter(NEAREST);
-               sampler->set_wrap(CLAMP_TO_EDGE);
-               nearest_sampler = sampler;
-       }
-       return sampler;
-}
-
-RefPtr<Sampler> PostProcessor::get_linear_sampler()
-{
-       RefPtr<Sampler> sampler = linear_sampler;
-       if(!sampler)
-       {
-               sampler = new Sampler;
-               sampler->set_filter(LINEAR);
-               sampler->set_wrap(CLAMP_TO_EDGE);
-               linear_sampler = sampler;
-       }
-       return sampler;
-}
-
 
 PostProcessor::Template::Template():
        size_divisor(1)
index c107d885f658e028522fdb0fe463bbd755d60d1d..3060e609868344ce4ec3379f34b07005e9e9d001 100644 (file)
@@ -8,6 +8,7 @@ namespace GL {
 
 class Mesh;
 class Renderer;
+class Resources;
 class Sampler;
 class Shader;
 class Texture2D;
@@ -33,14 +34,9 @@ public:
                Template();
                virtual ~Template() { }
 
-               virtual PostProcessor *create(unsigned, unsigned) const = 0;
+               virtual PostProcessor *create(Resources &, unsigned, unsigned) const = 0;
        };
 
-private:
-       static WeakPtr<Mesh> fullscreen_quad;
-       static WeakPtr<Sampler> nearest_sampler;
-       static WeakPtr<Sampler> linear_sampler;
-
 protected:
        PostProcessor() { }
 public:
@@ -50,14 +46,6 @@ public:
        virtual void render(const Texture2D &, const Texture2D &) { }
 
        virtual void render(Renderer &, const Texture2D &, const Texture2D &);
-
-protected:
-       /** Returns a mesh consisting of a single quad, covering the entire screen.
-       The vertices are in normalized device coordinates. */
-       static RefPtr<Mesh> get_fullscreen_quad();
-
-       static RefPtr<Sampler> get_nearest_sampler();
-       static RefPtr<Sampler> get_linear_sampler();
 };
 
 } // namespace GL
index eaf6e1373cb126a387d535e9e2b1352199eed2e7..c2e1f2957a63f4de2a623d2db1af26f58529f5bf 100644 (file)
@@ -3,6 +3,7 @@
 #include "camera.h"
 #include "light.h"
 #include "renderer.h"
+#include "resources.h"
 #include "scene.h"
 #include "shadowmap.h"
 #include "tests.h"
@@ -12,25 +13,15 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-WeakPtr<Sampler> ShadowMap::shadow_sampler;
-
-ShadowMap::ShadowMap(unsigned s, Renderable &r, const Light &l):
+ShadowMap::ShadowMap(Resources &resources, unsigned s, Renderable &r, const Light &l):
        Effect(r),
        size(s),
        light(l),
+       sampler(resources.get<Sampler>("_linear_clamp_shadow.samp")),
        radius(1),
        depth_bias(4),
        rendered(false)
 {
-       sampler = shadow_sampler;
-       if(!sampler)
-       {
-               sampler = new Sampler;
-               sampler->set_filter(LINEAR);
-               sampler->set_compare(LEQUAL);
-               sampler->set_wrap(CLAMP_TO_EDGE);
-               shadow_sampler = sampler;
-       }
        depth_buf.storage(DEPTH_COMPONENT32F, size, size, 1);
        fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0);
        fbo.require_complete();
@@ -108,7 +99,7 @@ void ShadowMap::render(Renderer &renderer, const Tag &tag) const
        int iunit = unit;
        shdata.uniform("shadow_map", iunit);
 
-       Bind _bind_sampler(*sampler, unit);
+       Bind _bind_sampler(sampler, unit);
        Bind _bind_depth(depth_buf, unit);
 
        if(const Camera *camera = renderer.get_camera())
index e142fc8cafdd56023459035f53d48e73205c997b..7dab9eb965a6e9266156437741bc40854133c37e 100644 (file)
@@ -11,6 +11,7 @@ namespace Msp {
 namespace GL {
 
 class Light;
+class Resources;
 class Scene;
 
 /**
@@ -27,17 +28,15 @@ private:
        Framebuffer fbo;
        Matrix shadow_matrix;
        Texture2D depth_buf;
-       RefPtr<Sampler> sampler;
+       const Sampler &sampler;
        Vector3 target;
        float radius;
        float depth_bias;
        mutable ProgramData shdata;
        bool rendered;
 
-       static WeakPtr<Sampler> shadow_sampler;
-
 public:
-       ShadowMap(unsigned, Renderable &, const Light &);
+       ShadowMap(Resources &, unsigned, Renderable &, const Light &);
 
        /** Sets the ShadowMap target point and radius.  The transformation matrix is
        computed so that a sphere with the specified parameters will be completely
index 143008cdc92af5a5a1a01a1627d7732fc60ebcbe..bfa9fed76a5b7a19b2bf728fa03c04c80c811554 100644 (file)
@@ -4,6 +4,7 @@
 #include "camera.h"
 #include "occludedscene.h"
 #include "renderer.h"
+#include "resources.h"
 #include "sphere.h"
 
 using namespace std;
@@ -11,19 +12,14 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-OccludedScene::OccludedScene():
-       bounding_mesh((VERTEX3, NORMAL3)),
-       bounding_shader("occluder.glsl"),
+OccludedScene::OccludedScene(Resources &resources):
+       bounding_mesh(resources.get<Mesh>("_occluder.mesh")),
+       bounding_shader(resources.get<Program>("_occluder.glsl")),
        occluder_min_size(0.25f),
        cache_dirty(false)
 {
        static Require req(ARB_occlusion_query);
        static Require req2(ARB_occlusion_query2);
-
-       /* Use a slightly larger radius to ensure that all parts of the renderable
-       fit inside the icosahedron */
-       IcoSphereBuilder(1.26f, 1).build(bounding_mesh);
-       bounding_mesh.set_winding(&WindingTest::counterclockwise());
 }
 
 OccludedScene::~OccludedScene()
index 2898bb3ff92032c703c916232ae7459845c0fe8c..000673ddfbb3740f76f9cf511c4606d7a5ec8d0e 100644 (file)
@@ -31,15 +31,15 @@ private:
        typedef std::set<Renderable *> RenderableSet;
        typedef std::vector<OccludedRenderable> OccludedArray;
 
-       Mesh bounding_mesh;
-       Program bounding_shader;
+       const Mesh &bounding_mesh;
+       const Program &bounding_shader;
        RenderableSet renderables;
        float occluder_min_size;
        mutable OccludedArray occluded_cache;
        mutable bool cache_dirty;
 
 public:
-       OccludedScene();
+       OccludedScene(Resources &);
        ~OccludedScene();
 
        virtual void add(Renderable &);
index 114a08ecf838f0f6fd6fbf5c517fa64e9a07fe5f..dc034746652c0559a60972b8613e4d93956bb054 100644 (file)
@@ -28,6 +28,7 @@ namespace Msp {
 namespace GL {
 
 void init_shaderlib(DataFile::BuiltinSource &);
+void init_builtin_data(DataFile::BuiltinSource &);
 
 Resources::Resources():
        default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
@@ -65,6 +66,7 @@ const DataFile::CollectionSource &Resources::get_builtins()
 
        if(!init_done)
        {
+               init_builtin_data(builtins);
                init_shaderlib(builtins);
                init_done = true;
        }