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