]> git.tdb.fi Git - libs/gl.git/blob - source/renderpass.cpp
Allow copying of Uniforms and ProgramData
[libs/gl.git] / source / renderpass.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/core/refptr.h>
9 #include <msp/datafile/collection.h>
10 #include <msp/strings/formatter.h>
11 #include "material.h"
12 #include "renderpass.h"
13 #include "program.h"
14 #include "programdata.h"
15 #include "texture.h"
16
17 using namespace std;
18
19 namespace Msp {
20 namespace GL {
21
22 const RenderPass *RenderPass::current=0;
23
24 RenderPass::RenderPass():
25         shprog(0),
26         shdata(0),
27         own_material(false),
28         material(0)
29 { }
30
31 RenderPass::RenderPass(const RenderPass &other):
32         shprog(other.shprog),
33         shdata(other.shdata ? new ProgramData(*other.shdata) : 0),
34         own_material(other.own_material),
35         material(own_material ? new Material(*other.material) : other.material),
36         textures(other.textures)
37 { }
38
39 /*RenderPass &RenderPass::operator=(const RenderPass &other)
40 {
41         shprog=other.shprog;
42         delete shdata;
43         shdata=(other.shdata ? new ProgramData(*other.shdata) : 0);
44         material=other.material;
45         use_material=other.use_material;
46         textures=other.textures;
47         use_textures=other.use_textures;
48
49         return *this;
50 }*/
51
52 RenderPass::~RenderPass()
53 {
54         delete shdata;
55         if(own_material)
56                 delete material;
57 }
58
59 void RenderPass::set_material(const Material *mat)
60 {
61         material=mat;
62 }
63
64 unsigned RenderPass::get_texture_index(const string &slot) const
65 {
66         for(unsigned i=0; i<textures.size(); ++i)
67                 if(textures[i].name==slot)
68                         return i;
69
70         throw KeyError("Unknown texture slot", slot);
71 }
72
73 void RenderPass::set_texture(const string &slot, const Texture *tex)
74 {
75         textures[get_texture_index(slot)]=tex;
76 }
77
78 void RenderPass::bind() const
79 {
80         if(this==current)
81                 return;
82
83         const RenderPass *old=current;
84         current=this;
85
86         if(shprog)
87         {
88                 shprog->bind();
89                 shdata->apply();
90         }
91         else if(old && !old->shprog)
92                 GL::Program::unbind();
93
94         if(material)
95                 material->bind();
96         else if(old && !old->material)
97                 GL::Material::unbind();
98
99         for(unsigned i=0; i<textures.size(); ++i)
100                 if(textures[i].texture)
101                         textures[i].texture->bind_to(i);
102         if(old)
103         {
104                 for(unsigned i=textures.size(); i<old->textures.size(); ++i)
105                         GL::Texture::unbind_from(i);
106         }
107 }
108
109 void RenderPass::unbind()
110 {
111         if(current)
112         {
113                 if(current->shprog)
114                         GL::Program::unbind();
115
116                 if(current->material)
117                         GL::Material::unbind();
118
119                 for(unsigned i=current->textures.size(); i--; )
120                         GL::Texture::unbind_from(i);
121
122                 current=0;
123         }
124 }
125
126
127 RenderPass::Loader::Loader(RenderPass &p, Collection &c):
128         DataFile::CollectionObjectLoader<RenderPass>(p, &c)
129 {
130         add("shader",   &RenderPass::shprog);
131         add("material", &Loader::material);
132         add("material", &RenderPass::material);
133         add("texture",  &Loader::texture);
134         add("uniforms", &Loader::uniforms);
135 }
136
137 void RenderPass::Loader::finish()
138 {
139         if(obj.shprog)
140         {
141                 if(!obj.shdata)
142                         obj.shdata=new ProgramData;
143
144                 for(unsigned i=0; i<obj.textures.size(); ++i)
145                 {
146                         unsigned loc=obj.shprog->get_uniform_location(obj.textures[i].name);
147                         obj.shdata->uniform(loc, static_cast<int>(i));
148                 }
149         }
150 }
151
152 void RenderPass::Loader::material()
153 {
154         // XXX Potential memory management trouble with multiple material statements
155         RefPtr<Material> mat=new Material;
156         load_sub(*mat);
157         obj.material=mat.release();
158         obj.own_material=true;
159 }
160
161 void RenderPass::Loader::texture(const string &n)
162 {
163         const Texture *tex=(n.empty() ? 0 : get_collection().get<Texture>(n));
164         TextureSlot slot(tex);
165         slot.name=(obj.textures.empty() ? "texture" : format("texture%d", obj.textures.size()));
166         load_sub(slot);
167         obj.textures.push_back(slot);
168 }
169
170 void RenderPass::Loader::uniforms()
171 {
172         if(!obj.shprog)
173                 throw InvalidState("Can't load uniforms without a shader program");
174         if(!obj.shdata)
175                 obj.shdata=new ProgramData;
176         load_sub(*obj.shdata, *obj.shprog);
177 }
178
179
180 RenderPass::TextureSlot::TextureSlot(const Texture *t):
181         texture(t)
182 { }
183
184
185 RenderPass::TextureSlot::Loader::Loader(TextureSlot &s):
186         DataFile::ObjectLoader<TextureSlot>(s)
187 {
188         add("name", &TextureSlot::name);
189 }
190
191 } // namespace GL
192 } // namespace Msp