2 #include <msp/gl/extensions/arb_compute_shader.h>
3 #include <msp/gl/extensions/arb_direct_state_access.h>
4 #include <msp/gl/extensions/arb_draw_instanced.h>
5 #include <msp/gl/extensions/arb_occlusion_query.h>
6 #include <msp/gl/extensions/ext_framebuffer_blit.h>
7 #include <msp/gl/extensions/ext_framebuffer_object.h>
8 #include <msp/gl/extensions/msp_clear_buffer.h>
10 #include "commands_backend.h"
12 #include "framebuffer.h"
14 #include "pipelinestate.h"
22 void OpenGLCommands::submit_frame()
27 void OpenGLCommands::use_pipeline(const PipelineState *ps)
31 OpenGLPipelineState::clear();
34 void OpenGLCommands::clear(const ClearValue *values)
36 const Framebuffer *target = (pipeline_state ? pipeline_state->get_framebuffer() : 0);
38 throw invalid_operation("OpenGLCommands::clear");
43 static Require _req(MSP_clear_buffer);
45 pipeline_state->apply();
48 for(FrameAttachment a: target->get_format())
50 if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
51 glClearBufferfv(GL_DEPTH, 0, &values->depth_stencil.depth);
52 else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
53 glClearBufferiv(GL_STENCIL, 0, &values->depth_stencil.stencil);
55 glClearBufferfv(GL_COLOR, i++, &values->color.r);
60 void OpenGLCommands::draw(const Batch &batch)
63 throw invalid_operation("OpenGLCommands::draw");
65 pipeline_state->apply();
66 void *data_ptr = reinterpret_cast<void *>(batch.get_offset());
67 glDrawElements(batch.gl_prim_type, batch.size(), batch.gl_index_type, data_ptr);
70 void OpenGLCommands::draw_instanced(const Batch &batch, unsigned count)
73 throw invalid_operation("OpenGLCommands::draw_instanced");
75 static Require req(ARB_draw_instanced);
77 pipeline_state->apply();
78 void *data_ptr = reinterpret_cast<void *>(batch.get_offset());
79 glDrawElementsInstanced(batch.gl_prim_type, batch.size(), batch.gl_index_type, data_ptr, count);
82 void OpenGLCommands::dispatch(unsigned count_x, unsigned count_y, unsigned count_z)
85 throw invalid_operation("OpenGLCommands::dispatch_compute");
87 static Require req(ARB_compute_shader);
89 pipeline_state->apply();
90 glDispatchCompute(count_x, count_y, count_z);
93 void OpenGLCommands::resolve_multisample()
95 const Framebuffer *framebuffer = (pipeline_state ? pipeline_state->get_framebuffer() : 0);
97 throw invalid_operation("OpenGLCommands::resolve_multisample");
99 static Require _req(EXT_framebuffer_blit);
101 unsigned width = framebuffer->get_width();
102 unsigned height = framebuffer->get_height();
103 unsigned buffers = get_gl_buffer_bits(framebuffer->get_format());
105 if(ARB_direct_state_access)
107 framebuffer->refresh();
108 glBlitNamedFramebuffer(framebuffer->id, framebuffer->resolve_id, 0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
112 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer->id);
114 framebuffer->refresh();
116 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer->resolve_id);
117 glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
118 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer->id);
122 void OpenGLCommands::begin_query(const QueryPool &pool, unsigned index)
124 if(index>=pool.queries.size())
125 throw out_of_range("OpenGLCommands::begin_query");
126 glBeginQuery(pool.gl_type, pool.queries[index]);
129 void OpenGLCommands::end_query(const QueryPool &pool, unsigned)
131 glEndQuery(pool.gl_type);