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