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