onilink_

Untitled

Apr 30th, 2021
1,337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. RenderState::RenderState(
  2.         Renderer& r, RenderLayer layer_index,
  3.         UniformList uniforms,
  4.         BlendMode blend_mode,
  5.         const std::string& shader_ind,
  6.         const std::string& tex1_ind,
  7.         const std::string& tex2_ind)
  8. {
  9.     /// @todo some factorization can be done with RenderStateSolid
  10.     // we need to find the associated shader and texture pointers
  11.     _shader = r._assets->shaders.shaderFromString(shader_ind);
  12.     if(!_shader) {
  13.         throw std::runtime_error("Shader " + shader_ind + " not exists");
  14.     }
  15.    
  16.     auto tex1 = r._assets->atlas.getAtlasFromString(tex1_ind);
  17.     auto tex2 = r._assets->atlas.getAtlasFromString(tex2_ind);
  18.    
  19.     if(!tex1) {
  20.         throw std::runtime_error("Atlas " + tex1_ind + " not exists");
  21.     }
  22.    
  23.     // find the render layer associated
  24.     RendererLayer& layer = r._layers.at( unsigned(layer_index) );
  25.    
  26.     // so we can setup the next DrawCall and check if it can
  27.     // be merged to the last one
  28.    
  29.     // check if we have to create a new state or if it can be merged
  30.     auto state_it = layer.states.find(_shader);
  31.     if(state_it == layer.states.end())
  32.     {
  33.         RendererLayer::State new_state;
  34.         new_state.dyn_renderer =
  35.                 std::make_shared<DynamicSpriteRenderer>(
  36.                     r._vbo_pool, _shader, _shader->vertex_stride);
  37.         state_it = layer.states.insert(
  38.                     std::make_pair(_shader, std::move(new_state))).first;
  39.     }
  40.    
  41.     Assert(!layer.states.empty());
  42.     RendererLayer::State& state = state_it->second;
  43.    
  44.     // And create the new draw call
  45.     bool can_merge_state = !state.draw_calls.empty();
  46.     if(can_merge_state) {
  47.         auto& prev_state = state.draw_calls.back();
  48.         /// @todo compare uniform lists so we can optimize the
  49.         /// merge condition if uniforms are the same
  50.         can_merge_state =
  51.                 uniforms.list.empty()
  52.                 && prev_state->uniforms.list.empty()
  53.                 && prev_state->blend_mode == blend_mode
  54.                 && prev_state->tex1 == tex1
  55.                 && prev_state->tex2 == tex2;
  56.     }
  57.    
  58.     if(!can_merge_state) {
  59.         shared_ptr<DrawCall> draw_call = std::make_shared<DrawCall>();
  60.         draw_call->uniforms = std::move(uniforms);
  61.         draw_call->blend_mode = blend_mode;
  62.         draw_call->tex1 = tex1;
  63.         draw_call->tex2 = tex2;
  64.         //draw_call->start = state.cursor;
  65.         draw_call->start = state.dyn_renderer->getCurrentCursorPosition();
  66.         draw_call->end = draw_call->start;
  67.         state.draw_calls.push_back( std::move(draw_call) );
  68.     }
  69.    
  70.     Assert(!state.draw_calls.empty());
  71.     _dyn_renderer = state.dyn_renderer;
  72.     _draw_call = state.draw_calls.back();
  73. }
Advertisement
Add Comment
Please, Sign In to add comment