Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- RenderState::RenderState(
- Renderer& r, RenderLayer layer_index,
- UniformList uniforms,
- BlendMode blend_mode,
- const std::string& shader_ind,
- const std::string& tex1_ind,
- const std::string& tex2_ind)
- {
- /// @todo some factorization can be done with RenderStateSolid
- // we need to find the associated shader and texture pointers
- _shader = r._assets->shaders.shaderFromString(shader_ind);
- if(!_shader) {
- throw std::runtime_error("Shader " + shader_ind + " not exists");
- }
- auto tex1 = r._assets->atlas.getAtlasFromString(tex1_ind);
- auto tex2 = r._assets->atlas.getAtlasFromString(tex2_ind);
- if(!tex1) {
- throw std::runtime_error("Atlas " + tex1_ind + " not exists");
- }
- // find the render layer associated
- RendererLayer& layer = r._layers.at( unsigned(layer_index) );
- // so we can setup the next DrawCall and check if it can
- // be merged to the last one
- // check if we have to create a new state or if it can be merged
- auto state_it = layer.states.find(_shader);
- if(state_it == layer.states.end())
- {
- RendererLayer::State new_state;
- new_state.dyn_renderer =
- std::make_shared<DynamicSpriteRenderer>(
- r._vbo_pool, _shader, _shader->vertex_stride);
- state_it = layer.states.insert(
- std::make_pair(_shader, std::move(new_state))).first;
- }
- Assert(!layer.states.empty());
- RendererLayer::State& state = state_it->second;
- // And create the new draw call
- bool can_merge_state = !state.draw_calls.empty();
- if(can_merge_state) {
- auto& prev_state = state.draw_calls.back();
- /// @todo compare uniform lists so we can optimize the
- /// merge condition if uniforms are the same
- can_merge_state =
- uniforms.list.empty()
- && prev_state->uniforms.list.empty()
- && prev_state->blend_mode == blend_mode
- && prev_state->tex1 == tex1
- && prev_state->tex2 == tex2;
- }
- if(!can_merge_state) {
- shared_ptr<DrawCall> draw_call = std::make_shared<DrawCall>();
- draw_call->uniforms = std::move(uniforms);
- draw_call->blend_mode = blend_mode;
- draw_call->tex1 = tex1;
- draw_call->tex2 = tex2;
- //draw_call->start = state.cursor;
- draw_call->start = state.dyn_renderer->getCurrentCursorPosition();
- draw_call->end = draw_call->start;
- state.draw_calls.push_back( std::move(draw_call) );
- }
- Assert(!state.draw_calls.empty());
- _dyn_renderer = state.dyn_renderer;
- _draw_call = state.draw_calls.back();
- }
Advertisement
Add Comment
Please, Sign In to add comment