2 #include <msp/gl/extensions/arb_direct_state_access.h>
3 #include <msp/gl/extensions/arb_draw_instanced.h>
4 #include <msp/gl/extensions/arb_occlusion_query.h>
5 #include <msp/gl/extensions/ext_framebuffer_blit.h>
6 #include <msp/gl/extensions/ext_framebuffer_object.h>
7 #include <msp/gl/extensions/msp_clear_buffer.h>
9 #include "commands_backend.h"
11 #include "framebuffer.h"
13 #include "pipelinestate.h"
21 OpenGLCommands::OpenGLCommands():
25 void OpenGLCommands::use_pipeline(const PipelineState *ps)
29 OpenGLPipelineState::clear();
32 void OpenGLCommands::clear(const ClearValue *values)
34 const Framebuffer *target = pipeline_state->get_framebuffer();
36 throw invalid_operation("OpenGLCommands::clear");
38 static Require _req(MSP_clear_buffer);
40 pipeline_state->apply();
43 for(FrameAttachment a: target->get_format())
45 if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
46 glClearBufferfv(GL_DEPTH, 0, &values->depth_stencil.depth);
47 else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
48 glClearBufferiv(GL_STENCIL, 0, &values->depth_stencil.stencil);
50 glClearBufferfv(GL_COLOR, i++, &values->color.r);
55 void OpenGLCommands::draw(const Batch &batch)
57 pipeline_state->apply();
58 void *data_ptr = reinterpret_cast<void *>(batch.get_offset());
59 glDrawElements(batch.gl_prim_type, batch.size(), batch.gl_index_type, data_ptr);
62 void OpenGLCommands::draw_instanced(const Batch &batch, unsigned count)
64 static Require req(ARB_draw_instanced);
66 pipeline_state->apply();
67 void *data_ptr = reinterpret_cast<void *>(batch.get_offset());
68 glDrawElementsInstanced(batch.gl_prim_type, batch.size(), batch.gl_index_type, data_ptr, count);
71 void OpenGLCommands::resolve_multisample(Framebuffer &target)
73 static Require _req(EXT_framebuffer_blit);
75 const Framebuffer *source = pipeline_state->get_framebuffer();
77 unsigned width = min(source->get_width(), target.get_width());
78 unsigned height = min(source->get_height(), target.get_height());
79 unsigned buffers = get_gl_buffer_bits(source->get_format())&get_gl_buffer_bits(target.get_format());
81 if(ARB_direct_state_access)
82 glBlitNamedFramebuffer(source->id, target.id, 0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
85 glBindFramebuffer(GL_READ_FRAMEBUFFER, source->id);
86 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target.id);
90 glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
92 glBindFramebuffer(GL_FRAMEBUFFER, source->id);
96 void OpenGLCommands::begin_query(const QueryPool &pool, unsigned index)
98 if(index>=pool.queries.size())
99 throw out_of_range("OpenGLCommands::begin_query");
100 glBeginQuery(pool.gl_type, pool.queries[index]);
103 void OpenGLCommands::end_query(const QueryPool &pool, unsigned)
105 glEndQuery(pool.gl_type);