]> git.tdb.fi Git - libs/gl.git/blob - source/core/commands.cpp
529cd3921fa5ef0243a6a78a6ffe2be6c1f6654d
[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/ext_framebuffer_blit.h>
5 #include <msp/gl/extensions/ext_framebuffer_object.h>
6 #include "batch.h"
7 #include "commands.h"
8 #include "gl.h"
9 #include "pipelinestate.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 Commands::Commands():
17         pipeline_state(0)
18 { }
19
20 void Commands::use_pipeline(const PipelineState &ps)
21 {
22         pipeline_state = &ps;
23 }
24
25 void Commands::clear(BufferBits buffers)
26 {
27         pipeline_state->apply();
28         glClear(buffers);
29 }
30
31 void Commands::draw(const Batch &batch)
32 {
33         pipeline_state->apply();
34         void *data_ptr = reinterpret_cast<void *>(batch.get_offset());
35         glDrawElements(batch.get_gl_primitive_type(), batch.size(), batch.get_gl_index_type(), data_ptr);
36 }
37
38 void Commands::draw_instanced(const Batch &batch, unsigned count)
39 {
40         static Require req(ARB_draw_instanced);
41
42         pipeline_state->apply();
43         void *data_ptr = reinterpret_cast<void *>(batch.get_offset());
44         glDrawElementsInstanced(batch.get_gl_primitive_type(), batch.size(), batch.get_gl_index_type(), data_ptr, count);
45 }
46
47 void Commands::resolve_multisample(Framebuffer &target, BufferBits buffers)
48 {
49         static Require _req(EXT_framebuffer_blit);
50
51         const Framebuffer *source = pipeline_state->get_framebuffer();
52
53         unsigned width = min(source->get_width(), target.get_width());
54         unsigned height = min(source->get_height(), target.get_height());
55
56         if(ARB_direct_state_access)
57                 glBlitNamedFramebuffer(source->id, target.id, 0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
58         else
59         {
60                 glBindFramebuffer(GL_READ_FRAMEBUFFER, source->id);
61                 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, target.id);
62
63                 target.refresh();
64
65                 glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, buffers, GL_NEAREST);
66
67                 glBindFramebuffer(GL_FRAMEBUFFER, source->id);
68         }
69 }
70
71 } // namespace GL
72 } // namespace Msp