]> git.tdb.fi Git - libs/gl.git/blob - source/render/instancearray.cpp
Make VertexFormat capable of storing type information
[libs/gl.git] / source / render / instancearray.cpp
1 #include <msp/core/algorithm.h>
2 #include <msp/core/maputils.h>
3 #include <msp/gl/extensions/arb_draw_instanced.h>
4 #include <msp/gl/extensions/arb_instanced_arrays.h>
5 #include <msp/gl/extensions/arb_vertex_array_object.h>
6 #include <msp/gl/extensions/arb_vertex_shader.h>
7 #include "buffer.h"
8 #include "camera.h"
9 #include "instancearray.h"
10 #include "mesh.h"
11 #include "object.h"
12 #include "objectinstance.h"
13 #include "renderer.h"
14 #include "technique.h"
15 #include "vertexsetup.h"
16
17 using namespace std;
18
19 namespace Msp {
20 namespace GL {
21
22 InstanceArray::InstanceArray(const Object &o):
23         object(o),
24         instance_data(0),
25         instance_buffer(0),
26         vtx_setup(0),
27         matrix_location(-1),
28         matrix_offset(0)
29 {
30         const Technique *tech = object.get_technique();
31         const Technique::PassMap &passes = tech->get_passes();
32         for(Technique::PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
33         {
34                 const Program *shprog = i->second.get_shader_program();
35                 if(!shprog)
36                         throw invalid_argument("InstanceArray::InstanceArray");
37
38                 int loc = shprog->get_attribute_location("instance_transform");
39                 if(matrix_location<0)
40                         matrix_location = loc;
41                 else if(loc!=matrix_location)
42                         throw invalid_argument("InstanceArray::InstanceArray");
43         }
44
45         if(ARB_vertex_array_object && ARB_instanced_arrays && ARB_draw_instanced)
46         {
47                 instance_data = new VertexArray((RAW_ATTRIB4,matrix_location, RAW_ATTRIB4,matrix_location+1, RAW_ATTRIB4,matrix_location+2));
48                 const VertexFormat &fmt = instance_data->get_format();
49                 matrix_offset = fmt.offset((RAW_ATTRIB4,matrix_location));
50
51                 instance_buffer = new Buffer;
52                 instance_data->use_buffer(instance_buffer);
53
54                 const Mesh *mesh = object.get_mesh();
55
56                 vtx_setup = new VertexSetup;
57                 vtx_setup->set_format_instanced(mesh->get_vertices().get_format(), fmt);
58                 vtx_setup->set_vertex_array(mesh->get_vertices());
59                 vtx_setup->set_index_buffer(*mesh->get_index_buffer(), mesh->get_batches().front().get_index_type());
60                 vtx_setup->set_instance_array(*instance_data);
61         }
62         else
63                 static Require req(ARB_vertex_shader);
64 }
65
66 InstanceArray::~InstanceArray()
67 {
68         for(vector<ObjectInstance *>::iterator i=instances.begin(); i!=instances.end(); ++i)
69                 delete *i;
70         delete vtx_setup;
71         delete instance_data;
72         delete instance_buffer;
73 }
74
75 void InstanceArray::append(ObjectInstance *inst)
76 {
77         instances.push_back(inst);
78         if(instance_data)
79         {
80                 if(instance_data->size()<instances.size())
81                 {
82                         instance_data->append();
83                         unsigned req_size = instance_data->get_required_buffer_size();
84                         if(instance_buffer->get_size()>0 && instance_buffer->get_size()<req_size)
85                         {
86                                 delete instance_buffer;
87                                 instance_buffer = new Buffer;
88                                 instance_data->use_buffer(instance_buffer);
89                         }
90                 }
91                 update_instance_matrix(instances.size()-1);
92         }
93 }
94
95 void InstanceArray::remove(ObjectInstance &inst)
96 {
97         vector<ObjectInstance *>::iterator i = find(instances, &inst);
98         if(i==instances.end())
99                 throw key_error(&inst);
100
101         delete *i;
102         *i = instances.back();
103         instances.pop_back();
104 }
105
106 void InstanceArray::update_instance_matrix(unsigned index)
107 {
108         if(!instance_data)
109                 return;
110
111         const Matrix &m = *instances[index]->get_matrix();
112
113         float *d = reinterpret_cast<float *>(instance_data->modify(instances.size()-1)+matrix_offset);
114         for(unsigned i=0; i<12; ++i)
115                 d[i] = m(i/4, i%4);
116 }
117
118 void InstanceArray::render(Renderer &renderer, Tag tag) const
119 {
120         if(instances.empty())
121                 return;
122
123         if(instance_data)
124         {
125                 const Technique *tech = object.get_technique();
126                 if(!tech)
127                         throw logic_error("no technique");
128                 const RenderPass *pass = tech->find_pass(tag);
129                 if(!pass)
130                         return;
131
132                 const Mesh *mesh = object.get_mesh();
133                 mesh->get_vertices().refresh();
134                 if(instance_buffer->get_size()==0)
135                         instance_buffer->storage(instance_data->get_required_buffer_size());
136                 instance_data->refresh();
137
138                 Renderer::Push push(renderer);
139                 pass->apply(renderer);
140                 mesh->draw_instanced(renderer, *vtx_setup, instances.size());
141         }
142         else
143         {
144                 for(vector<ObjectInstance *>::const_iterator i=instances.begin(); i!=instances.end(); ++i)
145                 {
146                         const Matrix &m = *(*i)->get_matrix();
147                         for(unsigned j=0; j<3; ++j)
148                                 glVertexAttrib4f(matrix_location+j, m(j, 0), m(j, 1), m(j, 2), m(j, 3));
149                         (*i)->render(renderer, tag);
150                 }
151         }
152 }
153
154 } // namespace GL
155 } // namespace Msp