]> git.tdb.fi Git - libs/gl.git/blobdiff - source/render/renderer.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / render / renderer.cpp
index 336b631aaf91c467dbca5577abd81f2b79ebaebe..7e010b3f917b0a22a34d5bf1de66a7408e29259e 100644 (file)
@@ -21,8 +21,12 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+const Tag Renderer::world_obj_matrix_tag("world_obj_matrix");
+const Tag Renderer::world_obj_normal_matrix_tag("world_obj_normal_matrix");
+
 Renderer::Renderer():
-       placeholder_texture(Resources::get_global().get<Texture>("_placeholder.png"))
+       placeholder_texture(Resources::get_global().get<Texture>("_placeholder.png")),
+       default_sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp"))
 {
        state_stack.reserve(16);
        shdata_stack.reserve(32);
@@ -40,7 +44,6 @@ void Renderer::begin()
 
        RendererBackend::begin();
 
-       add_shader_data(standard_shdata);
        commands.begin_frame(frame_index);
 }
 
@@ -68,7 +71,7 @@ void Renderer::push_state()
 
 void Renderer::pop_state()
 {
-       if(state_stack.size()==1)
+       if(state_stack.size()<=1)
                throw stack_underflow("Renderer::pop_state");
 
        uintptr_t old_pipeline = current_state->pipeline_key;
@@ -103,7 +106,6 @@ void Renderer::set_pipeline_key(uintptr_t key)
 void Renderer::set_camera(const Camera &c)
 {
        get_state().camera = &c;
-       add_shader_data(c.get_shader_data());
        set_matrix(Matrix());
 }
 
@@ -170,34 +172,48 @@ void Renderer::set_texture(Tag tag, const Texture *tex, int level, const Sampler
                        res_mgr->resource_used(*tex);
                if(!tex->is_loaded())
                        tex = &placeholder_texture;
+               if(!samp)
+                       samp = &default_sampler;
        }
+       else
+               samp = 0;
+
+       set_resource(texture_stack, state.texture_count, tag, { tex, samp, level });
+}
 
-       if(texture_stack.size()>state.texture_count)
+void Renderer::set_storage_texture(Tag tag, const Texture *tex)
+{
+       State &state = get_state();
+       set_resource(texture_stack, state.texture_count, tag, { tex, 0, 0 });
+}
+
+template<typename T>
+void Renderer::set_resource(vector<BoundResource<T>> &stack, unsigned &count, Tag tag, const T &res)
+{
+       if(stack.size()>count)
        {
-               BoundTexture &bt = texture_stack[state.texture_count];
-               if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
+               BoundResource<T> &top = stack[count];
+               if(top.tag==tag && top.resource==res)
                {
-                       ++state.texture_count;
+                       ++count;
                        return;
                }
                else
-                       flush_textures();
+                       flush_resources(stack, count);
        }
 
-       for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
+       for(auto i=stack.end(); i!=stack.begin(); )
                if((--i)->tag==tag)
                {
-                       i->replaced = texture_stack.size();
+                       i->replaced = stack.size();
                        break;
                }
 
-       texture_stack.emplace_back();
-       BoundTexture &bound_tex = texture_stack.back();
-       bound_tex.tag = tag;
-       bound_tex.texture = tex;
-       bound_tex.sampler = samp;
-       bound_tex.level = level;
-       state.texture_count = texture_stack.size();
+       stack.emplace_back();
+       BoundResource<T> &bound_res = stack.back();
+       bound_res.tag = tag;
+       bound_res.resource = res;
+       count = stack.size();
 }
 
 void Renderer::flush_shader_data()
@@ -208,15 +224,14 @@ void Renderer::flush_shader_data()
                shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
 }
 
-void Renderer::flush_textures()
+template<typename T>
+void Renderer::flush_resources(vector<BoundResource<T>> &stack, unsigned &count)
 {
-       const State &state = get_state();
+       for(unsigned i=0; i<count; ++i)
+               if(stack[i].replaced>=static_cast<int>(count))
+                       stack[i].replaced = -1;
 
-       for(unsigned i=0; i<state.texture_count; ++i)
-               if(texture_stack[i].replaced>=static_cast<int>(state.texture_count))
-                       texture_stack[i].replaced = -1;
-
-       texture_stack.erase(texture_stack.begin()+state.texture_count, texture_stack.end());
+       stack.erase(stack.begin()+count, stack.end());
 }
 
 void Renderer::set_vertex_setup(const VertexSetup *vs)
@@ -264,38 +279,45 @@ void Renderer::clear(const ClearValue *values)
 void Renderer::draw(const Batch &batch)
 {
        apply_state();
-       batch.refresh(frame_index);
-       PipelineState &ps = get_pipeline_state();
-       ps.set_primitive_type(batch.get_type());
-       commands.use_pipeline(&ps);
+       apply_batch(batch);
        commands.draw(batch);
 }
 
 void Renderer::draw_instanced(const Batch &batch, unsigned count)
 {
        apply_state();
+       apply_batch(batch);
+       commands.draw_instanced(batch, count);
+}
+
+void Renderer::apply_batch(const Batch &batch)
+{
        batch.refresh(frame_index);
        PipelineState &ps = get_pipeline_state();
        ps.set_primitive_type(batch.get_type());
+       if(batch.get_type()==PATCHES)
+               ps.set_patch_size(batch.get_patch_size());
        commands.use_pipeline(&ps);
-       commands.draw_instanced(batch, count);
 }
 
-void Renderer::resolve_multisample(Framebuffer &target)
+void Renderer::dispatch(unsigned count_x, unsigned count_y, unsigned count_z)
+{
+       apply_state();
+       PipelineState &ps = get_pipeline_state();
+       commands.use_pipeline(&ps);
+       commands.dispatch(count_x, count_y, count_z);
+}
+
+void Renderer::resolve_multisample()
 {
        const State &state = get_state();
 
        if(!state.framebuffer)
                throw invalid_operation("Renderer::resolve_multisample");
 
-       unsigned width = state.framebuffer->get_width();
-       unsigned height = state.framebuffer->get_height();
-       if(target.get_width()!=width || target.get_height()!=height)
-               throw incompatible_data("Renderer::resolve_multisample");
-
        apply_framebuffer();
        commands.use_pipeline(&get_pipeline_state());
-       commands.resolve_multisample(target);
+       commands.resolve_multisample();
 }
 
 void Renderer::begin_query(const QueryPool &pool, unsigned index)
@@ -333,7 +355,7 @@ void Renderer::apply_framebuffer()
 
 void Renderer::apply_state()
 {
-       const State &state = get_state();
+       State &state = get_state();
 
        if(!state.shprog)
                throw invalid_operation("Renderer::apply_state");
@@ -347,24 +369,34 @@ void Renderer::apply_state()
        bool shprog_changed = (state.shprog!=ps.get_shader_program());
        ps.set_shader_program(state.shprog);
 
+       bool shdata_changed = changed&SHADER_DATA;
+       for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
+               shdata_changed = (i->shdata->get_generation()!=i->generation);
+       bool extra_shdata = (shdata_stack.size()>state.shdata_count);
+
+       if(changed&CAMERA)
+       {
+               shdata_changed = true;
+               changed &= ~CAMERA;
+       }
+
        if(changed&MATRIX)
        {
-               standard_shdata.uniform("world_obj_matrix", state.model_matrix);
-               LinAl::SquareMatrix<float, 3> nm = state.model_matrix.block<3, 3>(0, 0);
+               standard_shdata.uniform(world_obj_matrix_tag, state.model_matrix);
+               LinAl::Matrix<float, 3, 3> nm = state.model_matrix.block<3, 3>(0, 0);
                nm = transpose(invert(nm));
-               standard_shdata.uniform("world_obj_normal_matrix", nm);
+               standard_shdata.uniform(world_obj_normal_matrix_tag, nm);
                changed &= ~MATRIX;
+               shdata_changed = true;
        }
 
-       bool shdata_changed = changed&SHADER_DATA;
-       for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
-               shdata_changed = (i->shdata->get_generation()!=i->generation);
-       bool extra_shdata = (shdata_stack.size()>state.shdata_count);
-
        if(shdata_changed || shprog_changed || pipeline_changed || extra_shdata)
        {
                if(extra_shdata)
                        shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
+               standard_shdata.apply(*state.shprog, ps, frame_index);
+               if(state.camera)
+                       state.camera->get_shader_data().apply(*state.shprog, ps, frame_index);
                for(const BoundProgramData &d: shdata_stack)
                {
                        d.shdata->apply(*state.shprog, ps, frame_index);
@@ -386,15 +418,20 @@ void Renderer::apply_state()
        ps.set_face_cull(state.face_cull);
 
        if(state.texture_count<texture_stack.size())
-               flush_textures();
+               flush_resources(texture_stack, state.texture_count);
 
-       for(const BoundTexture &t: texture_stack)
-               if(t.texture && t.replaced<0)
+       for(const BoundResource<SampledTexture> &t: texture_stack)
+               if(t.resource.texture && t.replaced<0)
                {
                        if(t.binding<0 || shprog_changed)
                                t.binding = state.shprog->get_uniform_binding(t.tag);
                        if(t.binding>=0)
-                               ps.set_texture(t.binding, t.texture, t.level, t.sampler);
+                       {
+                               if(t.resource.sampler)
+                                       ps.set_texture(t.binding, t.resource.texture, t.resource.level, t.resource.sampler);
+                               else
+                                       ps.set_storage_texture(t.binding, t.resource.texture);
+                       }
                }
 
        static const DepthTest default_depth_test;