]> git.tdb.fi Git - libs/gl.git/commitdiff
Add instancing support to standard shaders and RenderMethod
authorMikko Rasa <tdb@tdb.fi>
Thu, 17 Mar 2022 20:04:52 +0000 (22:04 +0200)
committerMikko Rasa <tdb@tdb.fi>
Thu, 17 Mar 2022 20:06:06 +0000 (22:06 +0200)
shaderlib/common.glsl
shaderlib/occluder.glsl
source/materials/rendermethod.cpp
source/materials/rendermethod.h

index 44ef150acc314c746041ba1c74e5338c287e190f..6bac9c3720e478fe3beb5a186da3b53800e902f2 100644 (file)
@@ -10,6 +10,7 @@ layout(set=1) uniform sampler2D normal_map;
 layout(set=0) uniform samplerCube environment_map;
 layout(set=0) uniform samplerCube irradiance_map;
 
+layout(constant_id=auto) const bool use_instancing = false;
 layout(constant_id=auto) const bool use_normal_map = false;
 
 #pragma MSP stage(vertex)
@@ -25,12 +26,18 @@ virtual vec3 get_vertex_normal()
 
 virtual mat4 get_vertex_transform()
 {
-       return world_obj_matrix;
+       if(use_instancing)
+               return transpose(mat4(instance_transform[0], instance_transform[1], instance_transform[2], vec4(0.0, 0.0, 0.0, 1.0)));
+       else
+               return world_obj_matrix;
 }
 
 virtual mat3 get_normal_transform()
 {
-       return world_obj_normal_matrix;
+       if(use_instancing)
+               return transpose(mat3(instance_transform[0].xyz, instance_transform[1].xyz, instance_transform[2].xyz));
+       else
+               return world_obj_normal_matrix;
 }
 
 void standard_transform()
index 6d6cd56074e7c67aa1f3d9fc3eb2e525fd75fef1..a4d876ecef40062decfb2cb255b0200980ed3413 100644 (file)
@@ -8,6 +8,7 @@ layout(set=1) uniform AlphaCutoff
 layout(set=1) uniform sampler2D alpha_map;
 
 layout(constant_id=auto) const bool use_alpha_cutoff = false;
+layout(constant_id=auto) const bool use_instancing = false;
 
 #pragma MSP stage(vertex)
 virtual vec4 get_vertex_position()
@@ -17,7 +18,10 @@ virtual vec4 get_vertex_position()
 
 virtual mat4 get_vertex_transform()
 {
-       return world_obj_matrix;
+       if(use_instancing)
+               return transpose(mat4(instance_transform[0], instance_transform[1], instance_transform[2], vec4(0.0, 0.0, 0.0, 1.0)));
+       else
+               return world_obj_matrix;
 }
 
 virtual void clipping(vec3 eye_vertex)
index ddbe4f1e457e83760dc699bce85ad52dd64ac29e..66424856bdc12abbc9642846e63273da900c29aa 100644 (file)
@@ -34,6 +34,8 @@ void RenderMethod::maybe_create_material_shader()
                extra_spec["use_shadow_map"] = true;
        if(image_based_lighting)
                extra_spec["use_image_based_lighting"] = true;
+       if(instancing)
+               extra_spec["use_instancing"] = true;
 
        shprog = material->create_compatible_shader(extra_spec);
 
@@ -154,6 +156,7 @@ void RenderMethod::Loader::init_actions()
        add("face_cull", &RenderMethod::face_cull);
        add("shader",   &Loader::shader);
        add("image_based_lighting", &RenderMethod::image_based_lighting);
+       add("instancing", &RenderMethod::instancing);
        add("material", &Loader::material_inline);
        add("material", &Loader::material);
        add("material_slot", &RenderMethod::material_slot);
index 9ac9337fe321989ca187ccce126fda422b363277..8fd289fe47c4a6638c7bc25e3e9fa6dc7e25bc7e 100644 (file)
@@ -89,6 +89,7 @@ private:
        Blend blend;
        bool receive_shadows = false;
        bool image_based_lighting = false;
+       bool instancing = false;
 
        void maybe_create_material_shader();
        void set_material_textures();
@@ -126,6 +127,12 @@ public:
 
        bool get_image_based_lighting() const { return image_based_lighting; }
 
+       /** Toggles instanced rendering.  Only affects shaders created from
+       materials.  Should be used with InstanceArray. */
+       void set_instancing(bool);
+
+       bool get_instancing() const { return instancing; }
+
        void apply(Renderer &) const;
 
        void set_debug_name(const std::string &);