]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/commands_backend.cpp
76469ab395be70ef4da6c8273eacf9604af91159
[libs/gl.git] / source / backends / opengl / commands_backend.cpp
1 #include <algorithm>
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>
9 #include "batch.h"
10 #include "commands_backend.h"
11 #include "error.h"
12 #include "framebuffer.h"
13 #include "gl.h"
14 #include "pipelinestate.h"
15 #include "query.h"
16
17 using namespace std;
18
19 namespace Msp {
20 namespace GL {
21
22 void OpenGLCommands::submit_frame()
23 {
24         glFlush();
25 }
26
27 void OpenGLCommands::use_pipeline(const PipelineState *ps)
28 {
29         pipeline_state = ps;
30         if(!pipeline_state)
31                 OpenGLPipelineState::clear();
32 }
33
34 void OpenGLCommands::clear(const ClearValue *values)
35 {
36         const Framebuffer *target = (pipeline_state ? pipeline_state->get_framebuffer() : 0);
37         if(!target)
38                 throw invalid_operation("OpenGLCommands::clear");
39
40         if(!values)
41                 return;
42
43         static Require _req(MSP_clear_buffer);
44
45         pipeline_state->apply();
46
47         unsigned i = 0;
48         for(FrameAttachment a: target->get_format())
49         {
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);
54                 else
55                         glClearBufferfv(GL_COLOR, i++, &values->color.r);
56                 ++values;
57         }
58 }
59
60 void OpenGLCommands::draw(const Batch &batch)
61 {
62         if(!pipeline_state)
63                 throw invalid_operation("OpenGLCommands::draw");
64
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);
68 }
69
70 void OpenGLCommands::draw_instanced(const Batch &batch, unsigned count)
71 {
72         if(!pipeline_state)
73                 throw invalid_operation("OpenGLCommands::draw_instanced");
74
75         static Require req(ARB_draw_instanced);
76
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);
80 }
81
82 void OpenGLCommands::dispatch(unsigned count_x, unsigned count_y, unsigned count_z)
83 {
84         if(!pipeline_state)
85                 throw invalid_operation("OpenGLCommands::dispatch_compute");
86
87         static Require req(ARB_compute_shader);
88
89         pipeline_state->apply();
90         glDispatchCompute(count_x, count_y, count_z);
91 }
92
93 void OpenGLCommands::resolve_multisample(Framebuffer &target)
94 {
95         const Framebuffer *source = (pipeline_state ? pipeline_state->get_framebuffer() : 0);
96         if(!source)
97                 throw invalid_operation("OpenGLCommands::draw");
98
99         static Require _req(EXT_framebuffer_blit);
100
101         unsigned width = min(source->get_width(), target.get_width());
102         unsigned height = min(source->get_height(), target.get_height());
103         unsigned buffers = get_gl_buffer_bits(source->get_format())&get_gl_buffer_bits(target.get_format());
104
105         if(ARB_direct_state_access)
106         {
107                 target.refresh();
108                 glBlitNamedFramebuffer(source->id, target.id, 0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
109         }
110         else
111         {
112                 glBindFramebuffer(GL_READ_FRAMEBUFFER, source->id);
113                 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target.id);
114
115                 target.refresh();
116
117                 glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
118
119                 glBindFramebuffer(GL_FRAMEBUFFER, source->id);
120         }
121 }
122
123 void OpenGLCommands::begin_query(const QueryPool &pool, unsigned index)
124 {
125         if(index>=pool.queries.size())
126                 throw out_of_range("OpenGLCommands::begin_query");
127         glBeginQuery(pool.gl_type, pool.queries[index]);
128 }
129
130 void OpenGLCommands::end_query(const QueryPool &pool, unsigned)
131 {
132         glEndQuery(pool.gl_type);
133 }
134
135 } // namespace GL
136 } // namespace Msp