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