Renderer::Renderer()
{
state_stack.reserve(16);
- state_stack.push_back(State());
shdata_stack.reserve(32);
- state = &state_stack.back();
- add_shader_data(standard_shdata);
+ texture_stack.reserve(32);
}
Renderer::~Renderer()
{
- end();
+}
+
+void Renderer::begin()
+{
+ if(current_state)
+ throw invalid_operation("Renderer::begin");
+
+ state_stack.push_back(State());
+ current_state = &state_stack.back();
+ add_shader_data(standard_shdata);
}
void Renderer::end()
{
- if(state_stack.size()>1)
+ if(!current_state || state_stack.size()>1)
throw invalid_operation("Renderer::end");
- *state = State();
+ current_state = 0;
+ state_stack.clear();
+ texture_stack.clear();
shdata_stack.clear();
- add_shader_data(standard_shdata);
commands.use_pipeline(0);
}
void Renderer::push_state()
{
+ if(state_stack.empty())
+ throw invalid_operation("Renderer::push_state");
+
state_stack.push_back(state_stack.back());
- state = &state_stack.back();
+ current_state = &state_stack.back();
}
void Renderer::pop_state()
throw stack_underflow("Renderer::pop_state");
state_stack.pop_back();
- state = &state_stack.back();
+ current_state = &state_stack.back();
changed |= MATRIX;
}
+Renderer::State &Renderer::get_state() const
+{
+#ifdef DEBUG
+ if(!current_state)
+ throw invalid_operation("Renderer::get_state");
+#endif
+ return *current_state;
+}
+
void Renderer::set_camera(const Camera &c)
{
- state->camera = &c;
+ get_state().camera = &c;
add_shader_data(c.get_shader_data());
set_matrix(Matrix());
}
void Renderer::set_matrix(const Matrix &matrix)
{
- state->model_matrix = matrix;
+ get_state().model_matrix = matrix;
changed |= MATRIX;
}
void Renderer::transform(const Matrix &matrix)
{
- state->model_matrix *= matrix;
+ get_state().model_matrix *= matrix;
changed |= MATRIX;
}
void Renderer::set_framebuffer(const Framebuffer *f)
{
- state->framebuffer = f;
+ get_state().framebuffer = f;
}
void Renderer::set_viewport(const Rect *v)
{
- state->viewport = v;
+ get_state().viewport = v;
}
void Renderer::set_scissor(const Rect *s)
{
- state->scissor = s;
+ get_state().scissor = s;
}
void Renderer::set_shader_program(const Program *p, const ProgramData *d)
{
- state->shprog = p;
+ get_state().shprog = p;
if(p && d)
add_shader_data(*d);
}
void Renderer::add_shader_data(const ProgramData &d)
{
- if(state->shdata_count<shdata_stack.size())
+ State &state = get_state();
+
+ if(state.shdata_count<shdata_stack.size())
{
const BoundProgramData &top = shdata_stack.back();
if(top.shdata==&d && top.generation==d.get_generation())
{
- ++state->shdata_count;
+ ++state.shdata_count;
return;
}
}
flush_shader_data();
shdata_stack.push_back(&d);
- state->shdata_count = shdata_stack.size();
+ state.shdata_count = shdata_stack.size();
changed |= SHADER_DATA;
}
void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
{
+ State &state = get_state();
+
if(tex)
if(ResourceManager *res_mgr = tex->get_manager())
res_mgr->resource_used(*tex);
- if(texture_stack.size()>state->texture_count)
+ if(texture_stack.size()>state.texture_count)
{
- BoundTexture &bt = texture_stack[state->texture_count];
+ BoundTexture &bt = texture_stack[state.texture_count];
if(bt.tag==tag && bt.texture==tex && bt.sampler==samp)
{
- ++state->texture_count;
+ ++state.texture_count;
return;
}
else
bound_tex.tag = tag;
bound_tex.texture = tex;
bound_tex.sampler = samp;
- state->texture_count = texture_stack.size();
+ state.texture_count = texture_stack.size();
}
void Renderer::flush_shader_data()
{
- if(shdata_stack.size()>state->shdata_count)
- shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
+ const State &state = get_state();
+
+ if(shdata_stack.size()>state.shdata_count)
+ shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
}
void Renderer::flush_textures()
{
- for(unsigned i=0; i<state->texture_count; ++i)
- if(texture_stack[i].replaced>=static_cast<int>(state->texture_count))
+ const State &state = get_state();
+
+ 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());
+ texture_stack.erase(texture_stack.begin()+state.texture_count, texture_stack.end());
}
void Renderer::set_vertex_setup(const VertexSetup *vs)
{
- state->vertex_setup = vs;
+ get_state().vertex_setup = vs;
}
void Renderer::set_front_face(FaceWinding winding)
{
- state->front_face = winding;
+ get_state().front_face = winding;
}
void Renderer::set_face_cull(CullMode cull)
{
- state->face_cull = cull;
+ get_state().face_cull = cull;
}
void Renderer::set_depth_test(const DepthTest *dt)
{
- state->depth_test = dt;
+ get_state().depth_test = dt;
}
void Renderer::set_stencil_test(const StencilTest *st)
{
- state->stencil_test = st;
+ get_state().stencil_test = st;
}
void Renderer::set_blend(const Blend *b)
{
- state->blend = b;
+ get_state().blend = b;
}
void Renderer::set_object_lod_bias(unsigned b)
{
- state->object_lod_bias = b;
+ get_state().object_lod_bias = b;
}
void Renderer::clear(const ClearValue *values)
{
- pipeline_state.set_framebuffer(state->framebuffer);
- pipeline_state.set_viewport(state->viewport);
- pipeline_state.set_scissor(state->scissor);
+ const State &state = get_state();
+
+ pipeline_state.set_framebuffer(state.framebuffer);
+ pipeline_state.set_viewport(state.viewport);
+ pipeline_state.set_scissor(state.scissor);
commands.use_pipeline(&pipeline_state);
commands.clear(values);
}
void Renderer::resolve_multisample(Framebuffer &target)
{
- if(!state->framebuffer)
+ 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();
+ 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");
- pipeline_state.set_framebuffer(state->framebuffer);
+ pipeline_state.set_framebuffer(state.framebuffer);
commands.use_pipeline(&pipeline_state);
commands.resolve_multisample(target);
}
void Renderer::apply_state()
{
- if(!state->shprog)
+ const State &state = get_state();
+
+ if(!state.shprog)
throw invalid_operation("Renderer::apply_state");
- pipeline_state.set_framebuffer(state->framebuffer);
- pipeline_state.set_viewport(state->viewport);
- pipeline_state.set_scissor(state->scissor);
+ pipeline_state.set_framebuffer(state.framebuffer);
+ pipeline_state.set_viewport(state.viewport);
+ pipeline_state.set_scissor(state.scissor);
- bool shprog_changed = (state->shprog!=pipeline_state.get_shader_program());
- pipeline_state.set_shader_program(state->shprog);
+ bool shprog_changed = (state.shprog!=pipeline_state.get_shader_program());
+ pipeline_state.set_shader_program(state.shprog);
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", state.model_matrix);
+ LinAl::SquareMatrix<float, 3> nm = state.model_matrix.block<3, 3>(0, 0);
nm = transpose(invert(nm));
standard_shdata.uniform("world_obj_normal_matrix", nm);
changed &= ~MATRIX;
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);
+ bool extra_shdata = (shdata_stack.size()>state.shdata_count);
if(shdata_changed || shprog_changed || extra_shdata)
{
if(extra_shdata)
- shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
+ shdata_stack.erase(shdata_stack.begin()+state.shdata_count, shdata_stack.end());
for(const BoundProgramData &d: shdata_stack)
{
- d.shdata->apply(*state->shprog, pipeline_state);
+ d.shdata->apply(*state.shprog, pipeline_state);
d.generation = d.shdata->get_generation();
}
changed &= ~SHADER_DATA;
}
- if(state->vertex_setup)
+ if(state.vertex_setup)
{
- if(const VertexArray *array = state->vertex_setup->get_vertex_array())
+ if(const VertexArray *array = state.vertex_setup->get_vertex_array())
array->refresh();
- if(const VertexArray *array = state->vertex_setup->get_instance_array())
+ if(const VertexArray *array = state.vertex_setup->get_instance_array())
array->refresh();
}
- pipeline_state.set_vertex_setup(state->vertex_setup);
+ pipeline_state.set_vertex_setup(state.vertex_setup);
- pipeline_state.set_front_face(state->front_face);
- pipeline_state.set_face_cull(state->face_cull);
+ pipeline_state.set_front_face(state.front_face);
+ pipeline_state.set_face_cull(state.face_cull);
- if(state->texture_count<texture_stack.size())
+ if(state.texture_count<texture_stack.size())
flush_textures();
for(const BoundTexture &t: texture_stack)
if(t.texture && t.replaced<0)
{
if(t.binding<0 || shprog_changed)
- t.binding = state->shprog->get_uniform_binding(t.tag);
+ t.binding = state.shprog->get_uniform_binding(t.tag);
if(t.binding>=0)
pipeline_state.set_texture(t.binding, t.texture, t.sampler);
}
- pipeline_state.set_depth_test(state->depth_test);
- pipeline_state.set_stencil_test(state->stencil_test);
- pipeline_state.set_blend(state->blend);
+ pipeline_state.set_depth_test(state.depth_test);
+ pipeline_state.set_stencil_test(state.stencil_test);
+ pipeline_state.set_blend(state.blend);
}