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