]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/opengl/commands_backend.cpp
Support compute shaders and compute operations
[libs/gl.git] / source / backends / opengl / commands_backend.cpp
index fd66b99cacf35c63b2f0eb8394828d939aabb76a..76469ab395be70ef4da6c8273eacf9604af91159 100644 (file)
@@ -1,4 +1,5 @@
 #include <algorithm>
+#include <msp/gl/extensions/arb_compute_shader.h>
 #include <msp/gl/extensions/arb_direct_state_access.h>
 #include <msp/gl/extensions/arb_draw_instanced.h>
 #include <msp/gl/extensions/arb_occlusion_query.h>
@@ -18,9 +19,10 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-OpenGLCommands::OpenGLCommands():
-       pipeline_state(0)
-{ }
+void OpenGLCommands::submit_frame()
+{
+       glFlush();
+}
 
 void OpenGLCommands::use_pipeline(const PipelineState *ps)
 {
@@ -31,46 +33,35 @@ void OpenGLCommands::use_pipeline(const PipelineState *ps)
 
 void OpenGLCommands::clear(const ClearValue *values)
 {
-       const Framebuffer *target = pipeline_state->get_framebuffer();
+       const Framebuffer *target = (pipeline_state ? pipeline_state->get_framebuffer() : 0);
        if(!target)
                throw invalid_operation("OpenGLCommands::clear");
 
-       if(!ARB_direct_state_access)
-       {
-               static Require _req(MSP_clear_buffer);
-               pipeline_state->apply();
-       }
+       if(!values)
+               return;
+
+       static Require _req(MSP_clear_buffer);
+
+       pipeline_state->apply();
 
        unsigned i = 0;
        for(FrameAttachment a: target->get_format())
        {
                if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
-               {
-                       if(ARB_direct_state_access)
-                               glClearNamedFramebufferfv(target->id, GL_DEPTH, 0, &values->depth_stencil.depth);
-                       else
-                               glClearBufferfv(GL_DEPTH, 0, &values->depth_stencil.depth);
-               }
+                       glClearBufferfv(GL_DEPTH, 0, &values->depth_stencil.depth);
                else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
-               {
-                       if(ARB_direct_state_access)
-                               glClearNamedFramebufferiv(target->id, GL_STENCIL, 0, &values->depth_stencil.stencil);
-                       else
-                               glClearBufferiv(GL_STENCIL, 0, &values->depth_stencil.stencil);
-               }
+                       glClearBufferiv(GL_STENCIL, 0, &values->depth_stencil.stencil);
                else
-               {
-                       if(ARB_direct_state_access)
-                               glClearNamedFramebufferfv(target->id, GL_COLOR, i++, &values->color.r);
-                       else
-                               glClearBufferfv(GL_COLOR, i++, &values->color.r);
-               }
+                       glClearBufferfv(GL_COLOR, i++, &values->color.r);
                ++values;
        }
 }
 
 void OpenGLCommands::draw(const Batch &batch)
 {
+       if(!pipeline_state)
+               throw invalid_operation("OpenGLCommands::draw");
+
        pipeline_state->apply();
        void *data_ptr = reinterpret_cast<void *>(batch.get_offset());
        glDrawElements(batch.gl_prim_type, batch.size(), batch.gl_index_type, data_ptr);
@@ -78,6 +69,9 @@ void OpenGLCommands::draw(const Batch &batch)
 
 void OpenGLCommands::draw_instanced(const Batch &batch, unsigned count)
 {
+       if(!pipeline_state)
+               throw invalid_operation("OpenGLCommands::draw_instanced");
+
        static Require req(ARB_draw_instanced);
 
        pipeline_state->apply();
@@ -85,18 +79,34 @@ void OpenGLCommands::draw_instanced(const Batch &batch, unsigned count)
        glDrawElementsInstanced(batch.gl_prim_type, batch.size(), batch.gl_index_type, data_ptr, count);
 }
 
+void OpenGLCommands::dispatch(unsigned count_x, unsigned count_y, unsigned count_z)
+{
+       if(!pipeline_state)
+               throw invalid_operation("OpenGLCommands::dispatch_compute");
+
+       static Require req(ARB_compute_shader);
+
+       pipeline_state->apply();
+       glDispatchCompute(count_x, count_y, count_z);
+}
+
 void OpenGLCommands::resolve_multisample(Framebuffer &target)
 {
-       static Require _req(EXT_framebuffer_blit);
+       const Framebuffer *source = (pipeline_state ? pipeline_state->get_framebuffer() : 0);
+       if(!source)
+               throw invalid_operation("OpenGLCommands::draw");
 
-       const Framebuffer *source = pipeline_state->get_framebuffer();
+       static Require _req(EXT_framebuffer_blit);
 
        unsigned width = min(source->get_width(), target.get_width());
        unsigned height = min(source->get_height(), target.get_height());
        unsigned buffers = get_gl_buffer_bits(source->get_format())&get_gl_buffer_bits(target.get_format());
 
        if(ARB_direct_state_access)
+       {
+               target.refresh();
                glBlitNamedFramebuffer(source->id, target.id, 0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
+       }
        else
        {
                glBindFramebuffer(GL_READ_FRAMEBUFFER, source->id);