]> git.tdb.fi Git - libs/gl.git/blob - source/backends/opengl/commands_backend.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / backends / opengl / commands_backend.cpp
1 #include <algorithm>
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>
8 #include "batch.h"
9 #include "commands_backend.h"
10 #include "error.h"
11 #include "framebuffer.h"
12 #include "gl.h"
13 #include "pipelinestate.h"
14 #include "query.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 void OpenGLCommands::submit_frame()
22 {
23         glFlush();
24 }
25
26 void OpenGLCommands::use_pipeline(const PipelineState *ps)
27 {
28         pipeline_state = ps;
29         if(!pipeline_state)
30                 OpenGLPipelineState::clear();
31 }
32
33 void OpenGLCommands::clear(const ClearValue *values)
34 {
35         const Framebuffer *target = (pipeline_state ? pipeline_state->get_framebuffer() : 0);
36         if(!target)
37                 throw invalid_operation("OpenGLCommands::clear");
38
39         if(!values)
40                 return;
41
42         static Require _req(MSP_clear_buffer);
43
44         pipeline_state->apply();
45
46         unsigned i = 0;
47         for(FrameAttachment a: target->get_format())
48         {
49                 if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
50                         glClearBufferfv(GL_DEPTH, 0, &values->depth_stencil.depth);
51                 else if(get_attach_point(a)==get_attach_point(STENCIL_ATTACHMENT))
52                         glClearBufferiv(GL_STENCIL, 0, &values->depth_stencil.stencil);
53                 else
54                         glClearBufferfv(GL_COLOR, i++, &values->color.r);
55                 ++values;
56         }
57 }
58
59 void OpenGLCommands::draw(const Batch &batch)
60 {
61         if(!pipeline_state)
62                 throw invalid_operation("OpenGLCommands::draw");
63
64         pipeline_state->apply();
65         void *data_ptr = reinterpret_cast<void *>(batch.get_offset());
66         glDrawElements(batch.gl_prim_type, batch.size(), batch.gl_index_type, data_ptr);
67 }
68
69 void OpenGLCommands::draw_instanced(const Batch &batch, unsigned count)
70 {
71         if(!pipeline_state)
72                 throw invalid_operation("OpenGLCommands::draw_instanced");
73
74         static Require req(ARB_draw_instanced);
75
76         pipeline_state->apply();
77         void *data_ptr = reinterpret_cast<void *>(batch.get_offset());
78         glDrawElementsInstanced(batch.gl_prim_type, batch.size(), batch.gl_index_type, data_ptr, count);
79 }
80
81 void OpenGLCommands::resolve_multisample(Framebuffer &target)
82 {
83         const Framebuffer *source = (pipeline_state ? pipeline_state->get_framebuffer() : 0);
84         if(!source)
85                 throw invalid_operation("OpenGLCommands::draw");
86
87         static Require _req(EXT_framebuffer_blit);
88
89         unsigned width = min(source->get_width(), target.get_width());
90         unsigned height = min(source->get_height(), target.get_height());
91         unsigned buffers = get_gl_buffer_bits(source->get_format())&get_gl_buffer_bits(target.get_format());
92
93         if(ARB_direct_state_access)
94                 glBlitNamedFramebuffer(source->id, target.id, 0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
95         else
96         {
97                 glBindFramebuffer(GL_READ_FRAMEBUFFER, source->id);
98                 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target.id);
99
100                 target.refresh();
101
102                 glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
103
104                 glBindFramebuffer(GL_FRAMEBUFFER, source->id);
105         }
106 }
107
108 void OpenGLCommands::begin_query(const QueryPool &pool, unsigned index)
109 {
110         if(index>=pool.queries.size())
111                 throw out_of_range("OpenGLCommands::begin_query");
112         glBeginQuery(pool.gl_type, pool.queries[index]);
113 }
114
115 void OpenGLCommands::end_query(const QueryPool &pool, unsigned)
116 {
117         glEndQuery(pool.gl_type);
118 }
119
120 } // namespace GL
121 } // namespace Msp