From: Mikko Rasa Date: Fri, 19 Feb 2021 16:40:30 +0000 (+0200) Subject: Load various built-in things through Resources X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=9a63244c1342337915c4610401a24c09fa72cc3d;p=libs%2Fgl.git Load various built-in things through Resources There's so many of them that managing them manually is becoming too tedious. Especially with some planned shader changes. --- diff --git a/Build b/Build index 02f502b4..19831c3e 100644 --- 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 index 00000000..89d13357 --- /dev/null +++ b/builtin_data/_ambientocclusion.glsl @@ -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 index 00000000..616bada9 --- /dev/null +++ b/builtin_data/_ambientocclusion_combine.glsl @@ -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 index 00000000..e1900323 --- /dev/null +++ b/builtin_data/_ambientocclusion_occlude.glsl @@ -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=min_depth) + { + if(sample0.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 index 00000000..635afdab --- /dev/null +++ b/builtin_data/_fullscreen_quad.mesh @@ -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 index 00000000..d9fe459e --- /dev/null +++ b/builtin_data/_linear_clamp.samp @@ -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 index 00000000..211be2ed --- /dev/null +++ b/builtin_data/_linear_clamp_shadow.samp @@ -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 index 00000000..f23f95fa --- /dev/null +++ b/builtin_data/_nearest_clamp.samp @@ -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 index 00000000..e46ca02b --- /dev/null +++ b/builtin_data/_occluder.glsl @@ -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 index 00000000..9614df4c --- /dev/null +++ b/builtin_data/_occluder.mesh @@ -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 index 89d13357..00000000 --- a/shaderlib/ambientocclusion.glsl +++ /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 index c1e7f272..00000000 --- a/shaderlib/ambientocclusion_combine.glsl +++ /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 index 2a10bde3..00000000 --- a/shaderlib/ambientocclusion_occlude.glsl +++ /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=min_depth) - { - if(sample0.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 index e46ca02b..00000000 --- a/shaderlib/occluder.glsl +++ /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); -} diff --git a/source/builders/pipelinebuilder.cpp b/source/builders/pipelinebuilder.cpp index 51e9cc85..12a5312f 100644 --- a/source/builders/pipelinebuilder.cpp +++ b/source/builders/pipelinebuilder.cpp @@ -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); } diff --git a/source/builders/pipelinetemplate.cpp b/source/builders/pipelinetemplate.cpp index b78b4c40..29011e31 100644 --- a/source/builders/pipelinetemplate.cpp +++ b/source/builders/pipelinetemplate.cpp @@ -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(t, 0) -{ - init(); -} - PipelineTemplate::Loader::Loader(PipelineTemplate &t, Collection &c): - DataFile::CollectionObjectLoader(t, &c) -{ - init(); -} - -void PipelineTemplate::Loader::init() + DataFile::CollectionObjectLoader(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() diff --git a/source/builders/pipelinetemplate.h b/source/builders/pipelinetemplate.h index fb24ea29..177c0b14 100644 --- a/source/builders/pipelinetemplate.h +++ b/source/builders/pipelinetemplate.h @@ -46,13 +46,10 @@ private: }; public: - class Loader: public DataFile::CollectionObjectLoader, public PostProcLoader + class Loader: public DataFile::CollectionObjectLoader, 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 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; } diff --git a/source/effects/ambientocclusion.cpp b/source/effects/ambientocclusion.cpp index 8c1dd616..e5de04b4 100644 --- a/source/effects/ambientocclusion.cpp +++ b/source/effects/ambientocclusion.cpp @@ -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("_ambientocclusion_occlude.glsl")), + combine_shader(resources.get("_ambientocclusion_combine.glsl")), + quad(resources.get("_fullscreen_quad.mesh")), + linear_sampler(resources.get("_linear_clamp.samp")), + nearest_sampler(resources.get("_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 ao = new AmbientOcclusion(width/size_divisor, height/size_divisor); + RefPtr 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); diff --git a/source/effects/ambientocclusion.h b/source/effects/ambientocclusion.h index 6c2c5d7e..25fab5c9 100644 --- a/source/effects/ambientocclusion.h +++ b/source/effects/ambientocclusion.h @@ -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 quad; - RefPtr linear_sampler; - RefPtr 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 &); diff --git a/source/effects/bloom.cpp b/source/effects/bloom.cpp index ee7c76c3..1204c81a 100644 --- a/source/effects/bloom.cpp +++ b/source/effects/bloom.cpp @@ -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("_bloom_blur.glsl")), + combine_shader(resources.get("_bloom_combine.glsl")), + quad(resources.get("_fullscreen_quad.mesh")), + nearest_sampler(resources.get("_nearest_clamp.samp")), + linear_sampler(resources.get("_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 = new Bloom(width/size_divisor, height/size_divisor); + RefPtr bloom = new Bloom(res, width/size_divisor, height/size_divisor); bloom->set_radius(radius); bloom->set_strength(strength); return bloom.release(); diff --git a/source/effects/bloom.h b/source/effects/bloom.h index 6b10195b..27584886 100644 --- a/source/effects/bloom.h +++ b/source/effects/bloom.h @@ -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 quad; - RefPtr nearest_sampler; - RefPtr 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 diff --git a/source/effects/colorcurve.cpp b/source/effects/colorcurve.cpp index 8811b065..697c7257 100644 --- a/source/effects/colorcurve.cpp +++ b/source/effects/colorcurve.cpp @@ -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("_colorcurve.glsl")), + quad(resources.get("_fullscreen_quad.mesh")), + linear_sampler(resources.get("_linear_clamp.samp")), + nearest_sampler(resources.get("_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 = new ColorCurve; + RefPtr colorcurve = new ColorCurve(res); colorcurve->set_exposure_adjust(exposure_adjust); colorcurve->set_brightness_response(brightness_response); if(srgb) diff --git a/source/effects/colorcurve.h b/source/effects/colorcurve.h index 3f65b049..b99bb14e 100644 --- a/source/effects/colorcurve.h +++ b/source/effects/colorcurve.h @@ -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 quad; - RefPtr linear_sampler; - RefPtr 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. */ diff --git a/source/effects/effect.cpp b/source/effects/effect.cpp index ad789af0..46a0f236 100644 --- a/source/effects/effect.cpp +++ b/source/effects/effect.cpp @@ -4,8 +4,6 @@ namespace Msp { namespace GL { -WeakPtr 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 Effect::get_linear_sampler() -{ - RefPtr 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 diff --git a/source/effects/effect.h b/source/effects/effect.h index bc9e19ec..c68bc944 100644 --- a/source/effects/effect.h +++ b/source/effects/effect.h @@ -21,9 +21,6 @@ protected: Renderable &renderable; std::set enabled_passes; -private: - static WeakPtr 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 get_linear_sampler(); }; } // namespace GL diff --git a/source/effects/environmentmap.cpp b/source/effects/environmentmap.cpp index 3902eb8c..0216ffc2 100644 --- a/source/effects/environmentmap.cpp +++ b/source/effects/environmentmap.cpp @@ -2,6 +2,7 @@ #include #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("_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(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(); diff --git a/source/effects/environmentmap.h b/source/effects/environmentmap.h index c54968de..1aaa26b7 100644 --- a/source/effects/environmentmap.h +++ b/source/effects/environmentmap.h @@ -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; + 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); diff --git a/source/effects/postprocessor.cpp b/source/effects/postprocessor.cpp index 6af069e9..2a490a46 100644 --- a/source/effects/postprocessor.cpp +++ b/source/effects/postprocessor.cpp @@ -7,59 +7,11 @@ namespace Msp { namespace GL { -WeakPtr PostProcessor::fullscreen_quad; -WeakPtr PostProcessor::nearest_sampler; -WeakPtr PostProcessor::linear_sampler; - void PostProcessor::render(Renderer &, const Texture2D &color, const Texture2D &depth) { render(color, depth); } -RefPtr PostProcessor::get_fullscreen_quad() -{ - RefPtr 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 PostProcessor::get_nearest_sampler() -{ - RefPtr sampler = nearest_sampler; - if(!sampler) - { - sampler = new Sampler; - sampler->set_filter(NEAREST); - sampler->set_wrap(CLAMP_TO_EDGE); - nearest_sampler = sampler; - } - return sampler; -} - -RefPtr PostProcessor::get_linear_sampler() -{ - RefPtr 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) diff --git a/source/effects/postprocessor.h b/source/effects/postprocessor.h index c107d885..3060e609 100644 --- a/source/effects/postprocessor.h +++ b/source/effects/postprocessor.h @@ -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 fullscreen_quad; - static WeakPtr nearest_sampler; - static WeakPtr 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 get_fullscreen_quad(); - - static RefPtr get_nearest_sampler(); - static RefPtr get_linear_sampler(); }; } // namespace GL diff --git a/source/effects/shadowmap.cpp b/source/effects/shadowmap.cpp index eaf6e137..c2e1f295 100644 --- a/source/effects/shadowmap.cpp +++ b/source/effects/shadowmap.cpp @@ -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 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("_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()) diff --git a/source/effects/shadowmap.h b/source/effects/shadowmap.h index e142fc8c..7dab9eb9 100644 --- a/source/effects/shadowmap.h +++ b/source/effects/shadowmap.h @@ -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; + const Sampler &sampler; Vector3 target; float radius; float depth_bias; mutable ProgramData shdata; bool rendered; - static WeakPtr 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 diff --git a/source/render/occludedscene.cpp b/source/render/occludedscene.cpp index 143008cd..bfa9fed7 100644 --- a/source/render/occludedscene.cpp +++ b/source/render/occludedscene.cpp @@ -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("_occluder.mesh")), + bounding_shader(resources.get("_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() diff --git a/source/render/occludedscene.h b/source/render/occludedscene.h index 2898bb3f..000673dd 100644 --- a/source/render/occludedscene.h +++ b/source/render/occludedscene.h @@ -31,15 +31,15 @@ private: typedef std::set RenderableSet; typedef std::vector 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 &); diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index 114a08ec..dc034746 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -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; }