#include <cmath>
+#include <msp/core/algorithm.h>
#include <msp/core/maputils.h>
#include <msp/datafile/collection.h>
#include <msp/fs/utils.h>
Animation::~Animation()
{
- for(vector<TimedKeyFrame>::iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
- if(i->owned)
- delete i->keyframe;
- for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
- delete *i;
+ for(TimedKeyFrame &k: keyframes)
+ if(k.owned)
+ delete k.keyframe;
+ for(Curve *c: curves)
+ delete c;
}
void Animation::set_armature(const Armature &a)
ctrn.set_scale(last_trn.get_scale()*(1-x)+trn.get_scale()*x);
ckf->set_transform(ctrn);
- for(KeyFrame::UniformMap::const_iterator j=kf_unis.begin(); j!=kf_unis.end(); ++j)
+ for(const auto &kvp: kf_unis)
{
- KeyFrame::UniformMap::const_iterator k = last_unis.find(j->first);
+ auto k = last_unis.find(kvp.first);
if(k==last_unis.end())
continue;
- KeyFrame::AnimatedUniform uni(j->second.size, 0.0f);
+ KeyFrame::AnimatedUniform uni(kvp.second.size, 0.0f);
for(unsigned c=0; c<uni.size; ++c)
- uni.values[c] = k->second.values[c]*(1-x)+j->second.values[c]*x;
+ uni.values[c] = k->second.values[c]*(1-x)+kvp.second.values[c]*x;
- ckf->set_uniform(j->first, uni);
+ ckf->set_uniform(kvp.first, uni);
}
add_keyframe(t, ckf, true, true);
throw invalid_argument("Animation::add_keyframe");
const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms();
- for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+ for(const UniformInfo &u: uniforms)
{
- KeyFrame::UniformMap::const_iterator j = kf_uniforms.find(i->name);
- if(j!=kf_uniforms.end() && j->second.size!=i->size)
+ auto j = kf_uniforms.find(u.name);
+ if(j!=kf_uniforms.end() && j->second.size!=u.size)
throw invalid_argument("Animation::add_keyframe");
}
keyframes.push_back(tkf);
- for(KeyFrame::UniformMap::const_iterator i=kf_uniforms.begin(); i!=kf_uniforms.end(); ++i)
+ for(const auto &kvp: kf_uniforms)
{
- bool found = false;
- for(vector<UniformInfo>::const_iterator j=uniforms.begin(); (!found && j!=uniforms.end()); ++j)
- found = (j->name==i->first);
-
- if(!found)
- uniforms.push_back(UniformInfo(i->first, i->second.size));
+ if(find_member(uniforms, kvp.first, &UniformInfo::name)==uniforms.end())
+ uniforms.push_back(UniformInfo(kvp.first, kvp.second.size));
}
}
void Animation::create_curves()
{
- for(vector<Curve *>::iterator i=curves.begin(); i!=curves.end(); ++i)
- delete *i;
+ for(Curve *c: curves)
+ delete c;
curves.clear();
curves.reserve(6+uniforms.size());
create_curve(SCALE, Transform::SCALE, &extract_scale);
uniform_curve_offset = curves.size();
- for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+ for(const UniformInfo &u: uniforms)
{
- if(i->size==1)
- create_curve<1>(UNIFORM, -1, ExtractUniform<1>(i->name));
- else if(i->size==2)
- create_curve<2>(UNIFORM, -1, ExtractUniform<2>(i->name));
- else if(i->size==3)
- create_curve<3>(UNIFORM, -1, ExtractUniform<3>(i->name));
- else if(i->size==4)
- create_curve<4>(UNIFORM, -1, ExtractUniform<4>(i->name));
+ if(u.size==1)
+ create_curve<1>(UNIFORM, -1, ExtractUniform<1>(u.name));
+ else if(u.size==2)
+ create_curve<2>(UNIFORM, -1, ExtractUniform<2>(u.name));
+ else if(u.size==3)
+ create_curve<3>(UNIFORM, -1, ExtractUniform<3>(u.name));
+ else if(u.size==4)
+ create_curve<4>(UNIFORM, -1, ExtractUniform<4>(u.name));
}
}
{
Transform::ComponentMask all = mask;
Transform::ComponentMask any = Transform::NONE;
- for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
+ for(const TimedKeyFrame &k: keyframes)
{
- all = all&i->keyframe->get_transform().get_mask();
- any = any|i->keyframe->get_transform().get_mask();
+ all = all&k.keyframe->get_transform().get_mask();
+ any = any|k.keyframe->get_transform().get_mask();
}
if(all==mask)
vector<Knot> knots;
unsigned n_control = 0;
- for(vector<TimedKeyFrame>::const_iterator i=keyframes.begin(); i!=keyframes.end(); ++i)
+ for(const TimedKeyFrame &k: keyframes)
{
- if(i->control && knots.empty())
+ if(k.control && knots.empty())
continue;
typename Interpolate::SplineValue<float, N>::Type value;
- if(extract(*i->keyframe, value))
+ if(extract(*k.keyframe, value))
{
typename Knot::Value dvalue = value;
- float x = i->time/Time::sec;
- if(i->control)
+ float x = k.time/Time::sec;
+ if(k.control)
{
++n_control;
if(n_control>2)
bool Animation::ExtractUniform<N>::operator()(const KeyFrame &kf, typename Interpolate::SplineValue<float, N>::Type &value) const
{
const KeyFrame::UniformMap &kf_uniforms = kf.get_uniforms();
- const KeyFrame::UniformMap::const_iterator i = kf_uniforms.find(name);
+ auto i = kf_uniforms.find(name);
if(i==kf_uniforms.end())
return false;
AnimationPlayer::Target &AnimationPlayer::get_slot(Placeable &obj)
{
- ObjectMap::iterator i = objects.find(&obj);
+ auto i = objects.find(&obj);
if(i!=objects.end())
return i->second;
- return objects.insert(ObjectMap::value_type(&obj, Target(obj))).first->second;
+ return objects.insert(make_pair(&obj, Target(obj))).first->second;
}
AnimationPlayer::Target &AnimationPlayer::play_(Placeable &obj, const Animation &anim, bool stacked, float speed)
unsigned AnimationPlayer::get_n_active_animations(const AnimatedObject &obj) const
{
- ObjectMap::const_iterator i = objects.find(&obj);
+ auto i = objects.find(&obj);
return (i!=objects.end() ? i->second.animations.size() : 0);
}
void AnimationPlayer::unobserve_events(AnimatedObject &obj, AnimationEventObserver &observer)
{
- ObjectMap::iterator i = objects.find(&obj);
+ auto i = objects.find(&obj);
if(i==objects.end())
return;
- vector<AnimationEventObserver *>::iterator j = find(i->second.event_observers, &observer);
+ auto j = find(i->second.event_observers, &observer);
if(j!=i->second.event_observers.end())
i->second.event_observers.erase(j);
}
void AnimationPlayer::unobserve_events(AnimationEventObserver &observer)
{
- for(ObjectMap::iterator i=objects.begin(); i!=objects.end(); ++i)
+ for(auto &kvp: objects)
{
- vector<AnimationEventObserver *>::iterator j = find(i->second.event_observers, &observer);
- if(j!=i->second.event_observers.end())
- i->second.event_observers.erase(j);
+ auto j = find(kvp.second.event_observers, &observer);
+ if(j!=kvp.second.event_observers.end())
+ kvp.second.event_observers.erase(j);
}
}
void AnimationPlayer::stop(Placeable &obj, const Animation &anim)
{
- ObjectMap::iterator i = objects.find(&obj);
+ auto i = objects.find(&obj);
if(i==objects.end())
return;
- for(vector<PlayingAnimation>::iterator j=i->second.animations.begin(); j!=i->second.animations.end(); ++j)
- if(j->animation==&anim)
- {
- i->second.animations.erase(j);
- break;
- }
+ auto j = find_member(i->second.animations, &anim, &PlayingAnimation::animation);
+ if(j!=i->second.animations.end())
+ i->second.animations.erase(j);
if(i->second.animations.empty())
objects.erase(i);
void AnimationPlayer::tick(const Time::TimeDelta &dt)
{
- for(ObjectMap::iterator i=objects.begin(); i!=objects.end(); )
+ for(auto i=objects.begin(); i!=objects.end(); )
{
if(i->second.stacked)
tick_stacked(i->second, dt);
void AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt)
{
Matrix matrix = target.base_matrix;
- for(vector<PlayingAnimation>::iterator i=target.animations.begin(); i!=target.animations.end(); ++i)
+ for(PlayingAnimation &a: target.animations)
{
- i->iterator += dt*i->speed;
- matrix *= i->iterator.get_matrix();
+ a.iterator += dt*a.speed;
+ matrix *= a.iterator.get_matrix();
if(target.object)
{
- unsigned n_uniforms = i->animation->get_n_uniforms();
+ unsigned n_uniforms = a.animation->get_n_uniforms();
for(unsigned j=0; j<n_uniforms; ++j)
- set_object_uniform(*target.object, i->animation->get_uniform_name(j), i->iterator.get_uniform(j));
+ set_object_uniform(*target.object, a.animation->get_uniform_name(j), a.iterator.get_uniform(j));
}
}
target.placeable.set_matrix(matrix);
matrix = Matrix();
/* XXX This is in all likelihood incorrect. The stacking should be
performed on local matrices. */
- for(vector<PlayingAnimation>::iterator j=target.animations.begin(); j!=target.animations.end(); ++j)
- if(j->animation->get_armature())
- matrix *= j->iterator.get_pose_matrix(i);
+ for(const PlayingAnimation &a: target.animations)
+ if(a.animation->get_armature())
+ matrix *= a.iterator.get_pose_matrix(i);
target.object->set_pose_matrix(i, matrix);
}
}
- for(vector<PlayingAnimation>::iterator i=target.animations.begin(); i!=target.animations.end(); )
+ for(auto i=target.animations.begin(); i!=target.animations.end(); )
{
i->iterator.dispatch_events(target);
void AnimationPlayer::Target::animation_event(Placeable *, const string &name, const Variant &value)
{
- for(vector<AnimationEventObserver *>::const_iterator i=event_observers.begin(); i!=event_observers.end(); ++i)
- (*i)->animation_event(&placeable, name, value);
+ for(AnimationEventObserver *o: event_observers)
+ o->animation_event(&placeable, name, value);
}
} // namespace GL
virtual void animation_event(Placeable *, const std::string &, const Variant &);
};
- typedef std::map<const Placeable *, Target> ObjectMap;
-
- ObjectMap objects;
+ std::map<const Placeable *, Target> objects;
private:
Target &get_slot(Placeable &);
const Armature::Link &Armature::get_link(unsigned index) const
{
- for(vector<Link>::const_iterator i=links.begin(); i!=links.end(); ++i)
- if(i->get_index()==index)
- return *i;
+ for(const Link &l: links)
+ if(l.get_index()==index)
+ return l;
throw key_error(typeid(list<Link>));
}
const Armature::Link &Armature::get_link(const string &name) const
{
- for(vector<Link>::const_iterator i=links.begin(); i!=links.end(); ++i)
- if(i->get_name()==name)
- return *i;
+ for(const Link &l: links)
+ if(l.get_name()==name)
+ return l;
throw key_error(typeid(list<Link>));
}
unsigned Armature::get_max_link_index() const
{
unsigned max_index = 0;
- for(vector<Link>::const_iterator i=links.begin(); i!=links.end(); ++i)
- max_index = max(max_index, i->get_index());
+ for(const Link &l: links)
+ max_index = max(max_index, l.get_index());
return max_index;
}
float x = 0;
unsigned prev = 0;
- for(string::const_iterator i=str.begin(); i!=str.end();)
+ for(auto i=str.begin(); i!=str.end();)
{
unsigned c = dec.decode_char(str, i);
if(prev)
unsigned prev = 0;
unsigned next = 0;
- for(string::const_iterator i=str.begin(); (next || i!=str.end());)
+ for(auto i=str.begin(); (next || i!=str.end());)
{
unsigned c = (next ? next : dec.decode_char(str, i));
next = (i!=str.end() ? dec.decode_char(str, i) : 0);
next = 0;
}
- GlyphMap::const_iterator j = glyphs.find(c);
+ auto j = glyphs.find(c);
if(j==glyphs.end())
continue;
float Font::get_glyph_advance(unsigned code, unsigned next) const
{
- GlyphMap::const_iterator i = glyphs.find(code);
+ auto i = glyphs.find(code);
if(i==glyphs.end())
return 0;
if(next)
{
- KerningMap::const_iterator j = kerning.find(CodePair(code, next));
+ auto j = kerning.find(CodePair(code, next));
if(j!=kerning.end())
advance += j->second;
}
unsigned Font::get_ligature(unsigned code, unsigned next) const
{
- LigatureMap::const_iterator i = ligatures.find(CodePair(code, next));
+ auto i = ligatures.find(CodePair(code, next));
return (i!=ligatures.end() ? i->second : 0);
}
SequenceBuilder::SequenceBuilder(const SequenceTemplate &t):
tmpl(t)
{
- const vector<SequenceTemplate::Step> &steps = tmpl.get_steps();
- for(vector<SequenceTemplate::Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
- renderables[i->slot_name] = i->default_renderable;
- const vector<SequenceTemplate::PostProcessor> &postprocs = tmpl.get_postprocessors();
- for(SequenceTemplate::PostProcessorArray::const_iterator i=postprocs.begin(); i!=postprocs.end(); ++i)
- if(!i->slot_name.empty())
- postprocessors[i->slot_name] = 0;
+ for(const SequenceTemplate::Step &s: tmpl.get_steps())
+ renderables[s.slot_name] = s.default_renderable;
+ for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors())
+ if(!p.slot_name.empty())
+ postprocessors[p.slot_name] = 0;
}
void SequenceBuilder::set_renderable(const string &name, Renderable &rend)
sequence.set_debug_name(debug_name);
#endif
- const vector<SequenceTemplate::Step> &steps = tmpl.get_steps();
- for(vector<SequenceTemplate::Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
+ for(const SequenceTemplate::Step &s: tmpl.get_steps())
{
- Renderable *renderable = get_item(renderables, i->slot_name);
+ Renderable *renderable = get_item(renderables, s.slot_name);
if(!renderable)
continue;
- Sequence::Step &step = sequence.add_step(i->tag, *renderable);
- step.set_blend(i->blend);
- step.set_depth_test(i->depth_test);
- step.set_stencil_test(i->stencil_test);
- step.set_lighting(i->lighting);
+ Sequence::Step &step = sequence.add_step(s.tag, *renderable);
+ step.set_blend(s.blend);
+ step.set_depth_test(s.depth_test);
+ step.set_stencil_test(s.stencil_test);
+ step.set_lighting(s.lighting);
}
- const SequenceTemplate::PostProcessorArray &postprocs = tmpl.get_postprocessors();
- for(SequenceTemplate::PostProcessorArray::const_iterator i=postprocs.begin(); i!=postprocs.end(); ++i)
+#ifdef DEBUG
+ unsigned index = 0;
+#endif
+ for(const SequenceTemplate::PostProcessor &p: tmpl.get_postprocessors())
{
PostProcessor *proc = 0;
- if(!i->slot_name.empty())
- proc = get_item(postprocessors, i->slot_name);
+ if(!p.slot_name.empty())
+ proc = get_item(postprocessors, p.slot_name);
if(proc)
sequence.add_postprocessor(*proc);
- else if(i->postprocessor_template)
+ else if(p.postprocessor_template)
{
- proc = i->postprocessor_template->create(sequence.get_width(), sequence.get_height());
+ proc = p.postprocessor_template->create(sequence.get_width(), sequence.get_height());
if(proc)
{
#ifdef DEBUG
if(!debug_name.empty())
- proc->set_debug_name(format("%s/%d.pproc", debug_name, i-postprocs.begin()));
+ proc->set_debug_name(format("%s/%d.pproc", debug_name, index++));
#endif
sequence.add_postprocessor_owned(proc);
}
SequenceTemplate::~SequenceTemplate()
{
- for(PostProcessorArray::iterator i=postprocessors.begin(); i!=postprocessors.end(); ++i)
- delete i->postprocessor_template;
+ for(const PostProcessor &p: postprocessors)
+ delete p.postprocessor_template;
}
PostProcessor(GL::PostProcessor::Template * = 0);
};
- typedef std::vector<PostProcessor> PostProcessorArray;
-
private:
typedef TypeRegistry<PostProcLoader::AddPostProc, PostProcLoader &> PostProcessorRegistry;
unsigned required_multisample;
unsigned max_multisample;
std::vector<Step> steps;
- PostProcessorArray postprocessors;
+ std::vector<PostProcessor> postprocessors;
public:
SequenceTemplate();
unsigned get_required_multisample() const { return required_multisample; }
unsigned get_maximum_multisample() const { return max_multisample; }
const std::vector<Step> &get_steps() const { return steps; }
- const PostProcessorArray &get_postprocessors() const { return postprocessors; }
+ const std::vector<PostProcessor> &get_postprocessors() const { return postprocessors; }
template<typename T>
static void register_postprocessor(const std::string &);
return *this;
data.reserve(data.size()+ind.size()*get_index_size());
- for(vector<unsigned>::const_iterator i=ind.begin(); i!=ind.end(); ++i)
- append_index(*i);
+ for(unsigned i: ind)
+ append_index(i);
update_offset();
dirty = true;
void Clipping::detach(const ClipPlane &p)
{
- vector<AttachedPlane>::iterator i = find_member(planes, &p, &AttachedPlane::plane);
+ auto i = find_member(planes, &p, &AttachedPlane::plane);
if(i!=planes.end())
planes.erase(i);
}
void Framebuffer::check_size()
{
bool first = true;
- for(vector<Attachment>::iterator i=attachments.begin(); i!=attachments.end(); ++i)
- if(i->tex)
+ for(Attachment &a: attachments)
+ if(a.tex)
{
- GLenum type = i->tex->get_target();
+ GLenum type = a.tex->get_target();
unsigned w = 0;
unsigned h = 0;
if(type==GL_TEXTURE_2D)
{
- Texture2D *tex = static_cast<Texture2D *>(i->tex);
- w = max(tex->get_width()>>i->level, 1U);
- h = max(tex->get_height()>>i->level, 1U);
+ Texture2D *tex = static_cast<Texture2D *>(a.tex);
+ w = max(tex->get_width()>>a.level, 1U);
+ h = max(tex->get_height()>>a.level, 1U);
}
else if(type==GL_TEXTURE_2D_MULTISAMPLE)
{
- Texture2DMultisample *tex = static_cast<Texture2DMultisample *>(i->tex);
+ Texture2DMultisample *tex = static_cast<Texture2DMultisample *>(a.tex);
w = tex->get_width();
h = tex->get_height();
}
else if(type==GL_TEXTURE_3D || type==GL_TEXTURE_2D_ARRAY)
{
- Texture3D *tex = static_cast<Texture3D *>(i->tex);
- w = max(tex->get_width()>>i->level, 1U);
- h = max(tex->get_height()>>i->level, 1U);
+ Texture3D *tex = static_cast<Texture3D *>(a.tex);
+ w = max(tex->get_width()>>a.level, 1U);
+ h = max(tex->get_height()>>a.level, 1U);
}
else if(type==GL_TEXTURE_CUBE_MAP)
{
- w = max(static_cast<TextureCube *>(i->tex)->get_size()>>i->level, 1U);
+ w = max(static_cast<TextureCube *>(a.tex)->get_size()>>a.level, 1U);
h = w;
}
bool reallocate = (batches.size()==batches.capacity());
if(reallocate)
{
- for(vector<Batch>::iterator i=batches.end(); i!=batches.begin(); )
+ for(auto i=batches.end(); i!=batches.begin(); )
(--i)->use_buffer(0);
}
if(reallocate)
{
prev = 0;
- for(vector<Batch>::iterator i=batches.begin(); i!=batches.end(); ++i)
+ for(Batch &a: batches)
{
- i->use_buffer(ibuf, prev);
- prev = &*i;
+ a.use_buffer(ibuf, prev);
+ prev = &a;
}
}
else
batches.back().set_index_type(existing_type);
else
{
- for(vector<Batch>::iterator i=batches.begin(); i!=batches.end(); ++i)
- i->set_index_type(added_type);
+ for(Batch &a: batches)
+ a.set_index_type(added_type);
}
}
if(!count)
{
- for(vector<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
- renderer.draw(*i);
+ for(const Batch &b: batches)
+ renderer.draw(b);
}
else
{
- for(vector<Batch>::const_iterator i=batches.begin(); i!=batches.end(); ++i)
- renderer.draw_instanced(*i, count);
+ for(const Batch &b: batches)
+ renderer.draw_instanced(b, count);
}
}
add("winding", &Mesh::face_winding);
}
-void Mesh::Loader::storage(const vector<VertexAttribute> &a)
+void Mesh::Loader::storage(const vector<VertexAttribute> &attrs)
{
- if(a.empty())
+ if(attrs.empty())
throw invalid_argument("No vertex attributes");
VertexFormat fmt;
- for(vector<VertexAttribute>::const_iterator i=a.begin(); i!=a.end(); ++i)
- fmt = (fmt, *i);
+ for(VertexAttribute a: attrs)
+ fmt = (fmt, a);
obj.storage(fmt);
}
+#include <msp/core/algorithm.h>
#include <msp/io/print.h>
#include "module.h"
#include "resources.h"
void SpirVModule::remap_pointers_from(const SpirVModule &other)
{
- for(vector<EntryPoint>::iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
- for(vector<const Variable *>::iterator j=i->globals.begin(); j!=i->globals.end(); ++j)
- *j = &variables[*j-&other.variables.front()];
+ for(EntryPoint &e: entry_points)
+ for(const Variable *&v: e.globals)
+ v = &variables[v-&other.variables.front()];
- for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
- if(i->struct_type)
- i->struct_type = &structs[i->struct_type-&other.structs.front()];
+ for(Variable &v: variables)
+ if(v.struct_type)
+ v.struct_type = &structs[v.struct_type-&other.structs.front()];
- for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
- for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
- if(j->struct_type)
- j->struct_type = &structs[j->struct_type-&other.structs.front()];
+ for(Structure &s: structs)
+ for(StructMember &m: s.members)
+ if(m.struct_type)
+ m.struct_type = &structs[m.struct_type-&other.structs.front()];
}
void SpirVModule::load_code(IO::Base &io)
if(code[0]==SPIRV_MAGIC_REVERSED)
{
- for(vector<UInt32>::iterator i=code.begin(); i!=code.end(); ++i)
- *i = ((*i&0xFF)<<24) || ((*i&0xFF00)<<8) | ((*i>>8)&0xFF00) | ((*i>>24)&0xFF);
+ for(UInt32 &c: code)
+ c = ((c&0xFF)<<24) || ((c&0xFF00)<<8) | ((c>>8)&0xFF00) | ((c>>24)&0xFF);
}
else if(code[0]!=SPIRV_MAGIC)
throw invalid_module("SPIR-V magic number not found");
reflection.reflect_code(code);
map<const Constant *, unsigned> spec_indices;
- for(map<unsigned, Constant>::const_iterator i=reflection.constants.begin(); i!=reflection.constants.end(); ++i)
- if(i->second.constant_id>=0)
+ for(const auto &kvp: reflection.constants)
+ if(kvp.second.constant_id>=0)
{
- spec_indices[&i->second] = spec_constants.size();
- spec_constants.push_back(i->second);
+ spec_indices[&kvp.second] = spec_constants.size();
+ spec_constants.push_back(kvp.second);
}
map<const Structure *, unsigned> struct_indices;
structs.reserve(reflection.structs.size());
- for(map<unsigned, Structure>::const_iterator i=reflection.structs.begin(); i!=reflection.structs.end(); ++i)
+ for(const auto &kvp: reflection.structs)
{
- struct_indices[&i->second] = structs.size();
- structs.push_back(i->second);
+ struct_indices[&kvp.second] = structs.size();
+ structs.push_back(kvp.second);
}
- for(vector<Structure>::iterator i=structs.begin(); i!=structs.end(); ++i)
+ for(Structure &s: structs)
{
- for(vector<StructMember>::iterator j=i->members.begin(); j!=i->members.end(); ++j)
+ for(StructMember &m: s.members)
{
- if(j->struct_type)
+ if(m.struct_type)
{
- map<const Structure *, unsigned>::const_iterator k = struct_indices.find(j->struct_type);
- j->struct_type = (k!=struct_indices.end() ? &structs[k->second] : 0);
+ auto i = struct_indices.find(m.struct_type);
+ m.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
}
- if(j->array_size_spec)
+ if(m.array_size_spec)
{
- map<const Constant *, unsigned>::const_iterator k = spec_indices.find(j->array_size_spec);
- j->array_size_spec = (k!=spec_indices.end() ? &spec_constants[k->second] : 0);
+ auto i = spec_indices.find(m.array_size_spec);
+ m.array_size_spec = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
}
}
- const StructMember *last_member = &i->members.back();
+ const StructMember *last_member = &s.members.back();
unsigned last_offset = last_member->offset;
while(last_member->struct_type)
{
last_member = lm;
}
- i->size = last_offset+get_type_size(last_member->type);
- i->size = (i->size+15)&~15;
+ s.size = last_offset+get_type_size(last_member->type);
+ s.size = (s.size+15)&~15;
}
map<const Variable *, unsigned> var_indices;
variables.reserve(reflection.variables.size());
- for(map<unsigned, Variable>::const_iterator i=reflection.variables.begin(); i!=reflection.variables.end(); ++i)
+ for(const auto &kvp: reflection.variables)
{
- int dup_index = -1;
- for(vector<Variable>::const_iterator j=variables.begin(); (dup_index<0 && j!=variables.end()); ++j)
- if(*j==i->second)
- dup_index = j-variables.begin();
-
- if(dup_index>=0)
- var_indices[&i->second] = dup_index;
+ auto i = find_if(variables, [&kvp](const Variable &v){ return v==kvp.second; });
+ if(i!=variables.end())
+ var_indices[&kvp.second] = i-variables.begin();
else
{
- var_indices[&i->second] = variables.size();
- variables.push_back(i->second);
+ var_indices[&kvp.second] = variables.size();
+ variables.push_back(kvp.second);
}
}
- for(vector<Variable>::iterator i=variables.begin(); i!=variables.end(); ++i)
+ for(Variable &v: variables)
{
- if(i->struct_type)
+ if(v.struct_type)
{
- map<const Structure *, unsigned>::const_iterator j = struct_indices.find(i->struct_type);
- i->struct_type = (j!=struct_indices.end() ? &structs[j->second] : 0);
+ auto i = struct_indices.find(v.struct_type);
+ v.struct_type = (i!=struct_indices.end() ? &structs[i->second] : 0);
}
- if(i->array_size_spec)
+ if(v.array_size_spec)
{
- map<const Constant *, unsigned>::const_iterator j = spec_indices.find(i->array_size_spec);
- i->array_size_spec = (j!=spec_indices.end() ? &spec_constants[j->second] : 0);
+ auto i = spec_indices.find(v.array_size_spec);
+ v.array_size_spec = (i!=spec_indices.end() ? &spec_constants[i->second] : 0);
}
}
entry_points.reserve(reflection.entry_points.size());
- for(map<unsigned, EntryPoint>::const_iterator i=reflection.entry_points.begin(); i!=reflection.entry_points.end(); ++i)
+ for(const auto &kvp: reflection.entry_points)
{
- entry_points.push_back(i->second);
+ entry_points.push_back(kvp.second);
EntryPoint &entry = entry_points.back();
- for(vector<const Variable *>::iterator j=entry.globals.begin(); j!=entry.globals.end(); ++j)
+ for(const Variable *&v: entry.globals)
{
- map<const Variable *, unsigned>::const_iterator k = var_indices.find(*j);
- *j = (k!=var_indices.end() ? &variables[k->second] : 0);
+ auto i = var_indices.find(v);
+ v = (i!=var_indices.end() ? &variables[i->second] : 0);
}
}
}
op += 2;
strct.members.resize(op_end-op);
- vector<StructMember>::iterator mem = strct.members.begin();
+ auto mem = strct.members.begin();
for(; op!=op_end; ++op, ++mem)
{
TypeInfo &type = types[*op];
if((tex!=0)!=(samp!=0))
throw invalid_argument("PipelineState::set_texture");
- vector<BoundTexture>::iterator i = lower_bound_member(textures, binding, &BoundTexture::binding);
+ auto i = lower_bound_member(textures, binding, &BoundTexture::binding);
if(i==textures.end() || i->binding!=binding)
i = textures.insert(i, BoundTexture(binding));
if(tex!=i->texture || samp!=i->sampler)
void PipelineState::set_uniform_block_(int binding, const UniformBlock *block)
{
- vector<BoundUniformBlock>::iterator i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
+ auto i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
if(i==uniform_blocks.end() || i->binding!=binding)
i = uniform_blocks.insert(i, BoundUniformBlock(binding));
if(block!=i->block || binding<0)
if(mask&TEXTURES)
{
- for(vector<BoundTexture>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
- if(i->changed || mask==~0U)
+ for(const BoundTexture &t: textures)
+ if(t.changed || mask==~0U)
{
- if(i->texture && i->sampler)
+ if(t.texture && t.sampler)
{
if(ARB_direct_state_access)
- glBindTextureUnit(i->binding, i->texture->get_id());
+ glBindTextureUnit(t.binding, t.texture->get_id());
else
{
- glActiveTexture(GL_TEXTURE0+i->binding);
- if(bound_tex_targets[i->binding] && static_cast<int>(i->texture->get_target())!=bound_tex_targets[i->binding])
- glBindTexture(bound_tex_targets[i->binding], 0);
- glBindTexture(i->texture->get_target(), i->texture->get_id());
+ glActiveTexture(GL_TEXTURE0+t.binding);
+ if(bound_tex_targets[t.binding] && static_cast<int>(t.texture->get_target())!=bound_tex_targets[t.binding])
+ glBindTexture(bound_tex_targets[t.binding], 0);
+ glBindTexture(t.texture->get_target(), t.texture->get_id());
}
- bound_tex_targets[i->binding] = i->texture->get_target();
+ bound_tex_targets[t.binding] = t.texture->get_target();
- glBindSampler(i->binding, i->sampler->get_id());
- i->sampler->refresh();
+ glBindSampler(t.binding, t.sampler->get_id());
+ t.sampler->refresh();
}
- i->changed = false;
+ t.changed = false;
}
}
if(mask&UNIFORMS)
{
- for(vector<BoundUniformBlock>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
- if(i->changed || mask==~0U)
+ for(const BoundUniformBlock &u: uniform_blocks)
+ if(u.changed || mask==~0U)
{
- if(i->block)
+ if(u.block)
{
- if(i->binding>=0)
+ if(u.binding>=0)
{
- const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(i->block);
- glBindBufferRange(GL_UNIFORM_BUFFER, i->binding, block->get_buffer()->get_id(), block->get_offset(), block->get_data_size());
- bound_uniform_blocks[i->binding] = 1;
+ const BufferBackedUniformBlock *block = static_cast<const BufferBackedUniformBlock *>(u.block);
+ glBindBufferRange(GL_UNIFORM_BUFFER, u.binding, block->get_buffer()->get_id(), block->get_offset(), block->get_data_size());
+ bound_uniform_blocks[u.binding] = 1;
}
else
- static_cast<const DefaultUniformBlock *>(i->block)->apply();
+ static_cast<const DefaultUniformBlock *>(u.block)->apply();
}
- i->changed = false;
+ u.changed = false;
}
}
#endif
vector<SL::Stage::Type> stages = compiler.get_stages();
- for(vector<SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
+ for(SL::Stage::Type st: stages)
{
unsigned stage_id = 0;
- switch(*i)
+ switch(st)
{
case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break;
case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
default: throw invalid_operation("Program::add_glsl_stages");
}
- string stage_src = compiler.get_stage_glsl(*i);
+ string stage_src = compiler.get_stage_glsl(st);
const char *src_ptr = stage_src.data();
int src_len = stage_src.size();
glShaderSource(stage_id, 1, &src_ptr, &src_len);
- if(*i==SL::Stage::VERTEX)
+ if(st==SL::Stage::VERTEX)
{
- const map<string, unsigned> &attribs = compiler.get_vertex_attributes();
- for(map<string, unsigned>::const_iterator j=attribs.begin(); j!=attribs.end(); ++j)
- glBindAttribLocation(id, j->second, j->first.c_str());
+ for(const auto &kvp: compiler.get_vertex_attributes())
+ glBindAttribLocation(id, kvp.second, kvp.first.c_str());
}
- if(*i==SL::Stage::FRAGMENT && EXT_gpu_shader4)
+ if(st==SL::Stage::FRAGMENT && EXT_gpu_shader4)
{
- const map<string, unsigned> &frag_outs = compiler.get_fragment_outputs();
- for(map<string, unsigned>::const_iterator j=frag_outs.begin(); j!=frag_outs.end(); ++j)
- glBindFragDataLocation(id, j->second, j->first.c_str());
+ for(const auto &kvp: compiler.get_fragment_outputs())
+ glBindFragDataLocation(id, kvp.second, kvp.first.c_str());
}
compile_glsl_stage(stage_id);
module = &mod;
- const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
unsigned n_stages = 0;
unsigned used_stage_ids[MAX_STAGES];
- for(vector<SpirVModule::EntryPoint>::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
+ for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
{
unsigned stage_id = 0;
- switch(i->stage)
+ switch(e.stage)
{
case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
vector<unsigned> spec_value_array;
spec_id_array.reserve(spec_consts.size());
spec_value_array.reserve(spec_consts.size());
- for(vector<SpirVModule::Constant>::const_iterator i=spec_consts.begin(); i!=spec_consts.end(); ++i)
+ for(const SpirVModule::Constant &c: spec_consts)
{
- map<string, int>::const_iterator j = spec_values.find(i->name);
- if(j!=spec_values.end())
+ auto i = spec_values.find(c.name);
+ if(i!=spec_values.end())
{
- spec_id_array.push_back(i->constant_id);
- spec_value_array.push_back(j->second);
- transient->spec_values[i->constant_id] = j->second;
+ spec_id_array.push_back(c.constant_id);
+ spec_value_array.push_back(i->second);
+ transient->spec_values[c.constant_id] = i->second;
}
}
- vector<SpirVModule::EntryPoint>::const_iterator j=entry_points.begin();
+ auto j = mod.get_entry_points().begin();
for(unsigned i=0; i<MAX_STAGES; ++i)
if(stage_ids[i])
glSpecializeShader(stage_ids[i], j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
{
for(unsigned i=0; i<uniform_blocks.size(); ++i)
{
- map<string, unsigned>::const_iterator j = transient->blocks.find(uniform_blocks[i].name);
+ auto j = transient->blocks.find(uniform_blocks[i].name);
if(j!=transient->blocks.end())
{
glUniformBlockBinding(id, i, j->second);
if(!ARB_separate_shader_objects)
glUseProgram(id);
- for(map<string, unsigned>::const_iterator i=transient->textures.begin(); i!=transient->textures.end(); ++i)
+ for(const auto &kvp: transient->textures)
{
- int location = get_uniform_location(i->first);
+ int location = get_uniform_location(kvp.first);
if(location>=0)
{
if(ARB_separate_shader_objects)
- glProgramUniform1i(id, location, i->second);
+ glProgramUniform1i(id, location, kvp.second);
else
- glUniform1i(location, i->second);
+ glUniform1i(location, kvp.second);
}
}
}
delete transient;
transient = 0;
- for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- require_type(i->type);
- for(vector<AttributeInfo>::const_iterator i=attributes.begin(); i!=attributes.end(); ++i)
- require_type(i->type);
+ for(const UniformInfo &u: uniforms)
+ require_type(u.type);
+ for(const AttributeInfo &a: attributes)
+ require_type(a.type);
}
void Program::query_uniforms()
uniform_blocks.push_back(UniformBlockInfo());
UniformBlockInfo &default_block = uniform_blocks.back();
- for(vector<UniformInfo>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- if(!i->block)
+ for(UniformInfo &u: uniforms)
+ if(!u.block)
{
- i->location = glGetUniformLocation(id, i->name.c_str());
- i->block = &default_block;
- default_block.uniforms.push_back(&*i);
+ u.location = glGetUniformLocation(id, u.name.c_str());
+ u.block = &default_block;
+ default_block.uniforms.push_back(&u);
- if(is_image(i->type) && i->location>=0)
- glGetUniformiv(id, i->location, &i->binding);
+ if(is_image(u.type) && u.location>=0)
+ glGetUniformiv(id, u.location, &u.binding);
}
default_block.layout_hash = compute_layout_hash(default_block.uniforms);
glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
vector<int> indices(value);
glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
- for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
+ for(int j: indices)
{
- if(!uniforms_by_index[*j])
+ if(!uniforms_by_index[j])
throw logic_error("Program::link");
- info.uniforms.push_back(uniforms_by_index[*j]);
- uniforms_by_index[*j]->block = &info;
+ info.uniforms.push_back(uniforms_by_index[j]);
+ uniforms_by_index[j]->block = &info;
}
vector<unsigned> query_indices(indices.begin(), indices.end());
uniforms_by_index[indices[j]]->offset = values[j];
query_indices.clear();
- for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
- if(uniforms_by_index[*j]->array_size>1)
- query_indices.push_back(*j);
+ for(int j: indices)
+ if(uniforms_by_index[j]->array_size>1)
+ query_indices.push_back(j);
if(!query_indices.empty())
{
glGetActiveUniformsiv(id, query_indices.size(), &query_indices[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
}
query_indices.clear();
- for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
+ for(int j: indices)
{
- DataType t = uniforms_by_index[*j]->type;
+ DataType t = uniforms_by_index[j]->type;
if(is_matrix(t))
- query_indices.push_back(*j);
+ query_indices.push_back(j);
}
if(!query_indices.empty())
{
uniform_blocks.push_back(UniformBlockInfo());
vector<vector<string> > block_uniform_names(1);
- const vector<SpirVModule::Variable> &variables = mod.get_variables();
- for(vector<SpirVModule::Variable>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
+ for(const SpirVModule::Variable &v: mod.get_variables())
{
- if(i->storage==SpirVModule::UNIFORM && i->struct_type)
+ if(v.storage==SpirVModule::UNIFORM && v.struct_type)
{
uniform_blocks.push_back(UniformBlockInfo());
UniformBlockInfo &info = uniform_blocks.back();
- info.name = i->struct_type->name;
- info.bind_point = i->binding;
- info.data_size = i->struct_type->size;
+ info.name = v.struct_type->name;
+ info.bind_point = v.binding;
+ info.data_size = v.struct_type->size;
string prefix;
- if(!i->name.empty())
- prefix = i->struct_type->name+".";
+ if(!v.name.empty())
+ prefix = v.struct_type->name+".";
block_uniform_names.push_back(vector<string>());
- collect_block_uniforms(*i->struct_type, prefix, 0, block_uniform_names.back());
+ collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
}
- else if(i->storage==SpirVModule::UNIFORM_CONSTANT && i->location>=0)
+ else if(v.storage==SpirVModule::UNIFORM_CONSTANT && v.location>=0)
{
- block_uniform_names[0].push_back(i->name);
+ block_uniform_names[0].push_back(v.name);
uniforms.push_back(UniformInfo());
UniformInfo &info = uniforms.back();
- info.name = i->name;
- info.tag = i->name;
- info.location = i->location;
- info.binding = i->binding;
- info.array_size = i->array_size;
- info.type = i->type;
+ info.name = v.name;
+ info.tag = v.name;
+ info.location = v.location;
+ info.binding = v.binding;
+ info.array_size = v.array_size;
+ info.type = v.type;
}
}
for(unsigned i=0; i<uniform_blocks.size(); ++i)
{
UniformBlockInfo &block = uniform_blocks[i];
- const vector<string> &names = block_uniform_names[i];
- for(vector<string>::const_iterator j=names.begin(); j!=names.end(); ++j)
+ for(const string &n: block_uniform_names[i])
{
// The element is already known to be present
- UniformInfo &uni = *lower_bound_member(uniforms, Tag(*j), &UniformInfo::tag);
+ UniformInfo &uni = *lower_bound_member(uniforms, Tag(n), &UniformInfo::tag);
block.uniforms.push_back(&uni);
uni.block = █
}
void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
{
- for(vector<SpirVModule::StructMember>::const_iterator i=strct.members.begin(); i!=strct.members.end(); ++i)
+ for(const SpirVModule::StructMember &m: strct.members)
{
- unsigned offset = base_offset+i->offset;
- if(i->struct_type)
+ unsigned offset = base_offset+m.offset;
+ if(m.struct_type)
{
- unsigned array_size = i->array_size;
- if(i->array_size_spec)
+ unsigned array_size = m.array_size;
+ if(m.array_size_spec)
{
- array_size = i->array_size_spec->i_value;
+ array_size = m.array_size_spec->i_value;
if(transient)
{
- map<unsigned, int>::const_iterator j = transient->spec_values.find(i->array_size_spec->constant_id);
+ auto j = transient->spec_values.find(m.array_size_spec->constant_id);
if(j!=transient->spec_values.end())
array_size = j->second;
}
if(array_size)
{
- for(unsigned j=0; j<array_size; ++j, offset+=i->array_stride)
- collect_block_uniforms(*i->struct_type, format("%s%s[%d].", prefix, i->name, j), offset, uniform_names);
+ for(unsigned j=0; j<array_size; ++j, offset+=m.array_stride)
+ collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
}
else
- collect_block_uniforms(*i->struct_type, prefix+i->name+".", offset, uniform_names);
+ collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
}
else
{
- string name = prefix+i->name;
+ string name = prefix+m.name;
uniform_names.push_back(name);
uniforms.push_back(UniformInfo());
UniformInfo &info = uniforms.back();
info.name = name;
info.tag = name;
info.offset = offset;
- info.array_size = i->array_size;
- info.array_stride = i->array_stride;
- info.matrix_stride = i->matrix_stride;
- info.type = i->type;
+ info.array_size = m.array_size;
+ info.array_stride = m.array_stride;
+ info.matrix_stride = m.matrix_stride;
+ info.type = m.type;
}
}
}
{
const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
- const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
- for(vector<SpirVModule::EntryPoint>::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
- if(i->stage==SpirVModule::VERTEX && i->name=="main")
+ for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
+ if(e.stage==SpirVModule::VERTEX && e.name=="main")
{
- for(vector<const SpirVModule::Variable *>::const_iterator j=i->globals.begin(); j!=i->globals.end(); ++j)
- if((*j)->storage==SpirVModule::INPUT)
+ for(const SpirVModule::Variable *v: e.globals)
+ if(v->storage==SpirVModule::INPUT)
{
attributes.push_back(AttributeInfo());
AttributeInfo &info = attributes.back();
- info.name = (*j)->name;
- info.location = (*j)->location;
- info.array_size = (*j)->array_size;
- info.type = (*j)->type;
+ info.name = v->name;
+ info.location = v->location;
+ info.array_size = v->array_size;
+ info.type = v->type;
}
}
}
void Program::update_layout_hash()
{
string layout_descriptor;
- for(vector<UniformBlockInfo>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
- layout_descriptor += format("%d:%x\n", i->bind_point, i->layout_hash);
+ for(const UniformBlockInfo &b: uniform_blocks)
+ layout_descriptor += format("%d:%x\n", b.bind_point, b.layout_hash);
uniform_layout_hash = hash32(layout_descriptor);
}
Program::LayoutHash Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
{
string layout_descriptor;
- for(vector<const UniformInfo *>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
- layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->array_size);
+ for(const UniformInfo *u: uniforms)
+ layout_descriptor += format("%d:%s:%x:%d\n", u->location, u->name, u->type, u->array_size);
return hash32(layout_descriptor);
}
const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
{
- for(vector<UniformBlockInfo>::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
- if(i->name==name)
- return *i;
- throw key_error(name);
+ auto i = find_member(uniform_blocks, name, &UniformBlockInfo::name);
+ if(i==uniform_blocks.end())
+ throw key_error(name);
+ return *i;
}
const Program::UniformInfo &Program::get_uniform_info(const string &name) const
{
- vector<UniformInfo>::const_iterator i = lower_bound_member(uniforms, Tag(name), &UniformInfo::tag);
+ auto i = lower_bound_member(uniforms, Tag(name), &UniformInfo::tag);
if(i==uniforms.end() || i->name!=name)
throw key_error(name);
return *i;
const Program::UniformInfo &Program::get_uniform_info(Tag tag) const
{
- vector<UniformInfo>::const_iterator i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
+ auto i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
if(i==uniforms.end() || i->tag!=tag)
throw key_error(tag);
return *i;
if(name[name.size()-1]==']')
throw invalid_argument("Program::get_uniform_location");
- vector<UniformInfo>::const_iterator i = lower_bound_member(uniforms, Tag(name), &UniformInfo::tag);
+ auto i = lower_bound_member(uniforms, Tag(name), &UniformInfo::tag);
return i!=uniforms.end() && i->name==name && i->block->bind_point<0 ? i->location : -1;
}
int Program::get_uniform_location(Tag tag) const
{
- vector<UniformInfo>::const_iterator i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
+ auto i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
return i!=uniforms.end() && i->tag==tag && i->block->bind_point<0 ? i->location : -1;
}
int Program::get_uniform_binding(Tag tag) const
{
- vector<UniformInfo>::const_iterator i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
+ auto i = lower_bound_member(uniforms, tag, &UniformInfo::tag);
return i!=uniforms.end() && i->tag==tag ? i->binding : -1;
}
const Program::AttributeInfo &Program::get_attribute_info(const string &name) const
{
- vector<AttributeInfo>::const_iterator i = lower_bound_member(attributes, name, &AttributeInfo::name);
+ auto i = lower_bound_member(attributes, name, &AttributeInfo::name);
if(i==attributes.end() || i->name!=name)
throw key_error(name);
return *i;
if(name[name.size()-1]==']')
throw invalid_argument("Program::get_attribute_location");
- vector<AttributeInfo>::const_iterator i = lower_bound_member(attributes, name, &AttributeInfo::name);
+ auto i = lower_bound_member(attributes, name, &AttributeInfo::name);
return i!=attributes.end() && i->name==name ? i->location : -1;
}
string Tag::str() const
{
#ifdef DEBUG
- map<Tag, string>::const_iterator i=tag_names.find(*this);
+ auto i=tag_names.find(*this);
if(i!=tag_names.end())
return i->second;
#endif
bool convert_attribute_type(string::const_iterator &i, string::const_iterator end, const char *name, VertexAttribute &attr, DataType type)
{
- string::const_iterator j = i;
+ auto j = i;
for(; (j!=end && *j!='_' && *name); ++name, ++j)
if(*j!=*name)
return false;
bool convert_attribute(const string &str, const char *name, int min_size, int max_size, VertexAttribute &attr, VertexAttribute base_attr)
{
- string::const_iterator i = str.begin();
+ auto i = str.begin();
for(; *name; ++name, ++i)
if(*i!=*name)
return false;
+#include <msp/core/algorithm.h>
#include <msp/gl/resources.h>
#include <msp/io/seekable.h>
#include "builtin.h"
if(type==Stage::SHARED)
return &module->shared;
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- if(i->type==type)
- return &*i;
- return 0;
+ auto i = find_member(module->stages, type, &Stage::type);
+ return (i!=module->stages.end() ? &*i : 0);
}
} // namespace SL
if(specialized && mode!=PROGRAM)
throw invalid_operation("Compiler::compile");
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- generate(*i);
+ for(Stage &s: module->stages)
+ generate(s);
ConstantIdAssigner().apply(*module, features);
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- validate(*i);
+ for(Stage &s: module->stages)
+ validate(s);
GlobalInterfaceValidator().apply(*module);
bool valid = true;
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- if(!check_errors(*i))
+ for(Stage &s: module->stages)
+ if(!check_errors(s))
valid = false;
if(!valid)
throw invalid_shader_source(get_diagnostics());
if(specialized)
{
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- ConstantSpecializer().apply(*i, spec_values);
+ for(Stage &s: module->stages)
+ ConstantSpecializer().apply(s, spec_values);
}
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
+ for(auto i=module->stages.begin(); i!=module->stages.end(); )
{
OptimizeResult result = optimize(*i);
if(result==REDO_PREVIOUS)
}
LocationAllocator().apply(*module, features);
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- finalize(*i, mode);
+ for(Stage &s: module->stages)
+ finalize(s, mode);
compiled = true;
}
unsigned source_count = module->source_map.get_count();
for(unsigned i=1; i<source_count; ++i)
glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+ for(Stage &s: module->stages)
{
- glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
- glsl += Formatter().apply(*i);
+ glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(s.type));
+ glsl += Formatter().apply(s);
glsl += '\n';
}
{
vector<Stage::Type> stage_types;
stage_types.reserve(module->stages.size());
- for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- stage_types.push_back(i->type);
+ for(const Stage &s: module->stages)
+ stage_types.push_back(s.type);
return stage_types;
}
{
if(!compiled)
throw invalid_operation("Compiler::get_stage_glsl");
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- if(i->type==stage_type)
- return Formatter().apply(*i);
+ auto i = find_member(module->stages, stage_type, &Stage::type);
+ if(i!=module->stages.end())
+ return Formatter().apply(*i);
throw key_error(Stage::get_stage_name(stage_type));
}
{
if(!compiled)
throw invalid_operation("Compiler::get_vertex_attributes");
- for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- if(i->type==Stage::VERTEX)
- return i->locations;
+ auto i = find_member(module->stages, Stage::VERTEX, &Stage::type);
+ if(i!=module->stages.end())
+ return i->locations;
throw invalid_operation("Compiler::get_vertex_attributes");
}
{
if(!compiled)
throw invalid_operation("Compiler::get_fragment_outputs");
- for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- if(i->type==Stage::FRAGMENT)
- return i->locations;
+ auto i = find_member(module->stages, Stage::FRAGMENT, &Stage::type);
+ if(i!=module->stages.end())
+ return i->locations;
throw invalid_operation("Compiler::get_fragment_outputs");
}
string Compiler::get_stage_debug(Stage::Type stage_type) const
{
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- if(i->type==stage_type)
- return DumpTree().apply(*i);
+ auto i = find_member(module->stages, stage_type, &Stage::type);
+ if(i!=module->stages.end())
+ return DumpTree().apply(*i);
throw key_error(Stage::get_stage_name(stage_type));
}
string Compiler::get_diagnostics() const
{
string combined;
- for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- for(vector<Diagnostic>::const_iterator j=i->diagnostics.begin(); j!=i->diagnostics.end(); ++j)
- if(j->source!=INTERNAL_SOURCE)
- append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(j->source), j->line, j->message));
+ for(const Stage &s: module->stages)
+ for(const Diagnostic &d: s.diagnostics)
+ if(d.source!=INTERNAL_SOURCE)
+ append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(d.source), d.line, d.message));
return combined;
}
module->source_map.merge_from(mod.source_map);
vector<Import *> imports;
- for(NodeList<Statement>::const_iterator i=mod.shared.content.body.begin(); i!=mod.shared.content.body.end(); ++i)
- if(Import *imp = dynamic_cast<Import *>(i->get()))
+ for(const RefPtr<Statement> &s: mod.shared.content.body)
+ if(Import *imp = dynamic_cast<Import *>(s.get()))
imports.push_back(imp);
- for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
- import(mod_cache, (*i)->module);
+ for(Import *i: imports)
+ import(mod_cache, i->module);
append_stage(mod.shared);
- for(list<Stage>::const_iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
- append_stage(*i);
+ for(const Stage &s: mod.stages)
+ append_stage(s);
}
void Compiler::append_stage(const Stage &stage)
target = &module->shared;
else
{
- list<Stage>::iterator i;
- for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
+ auto i = find_if(module->stages, [&stage](const Stage &s){ return s.type>=stage.type; });
if(i==module->stages.end() || i->type>stage.type)
{
- list<Stage>::iterator j = module->stages.insert(i, stage.type);
+ auto j = module->stages.insert(i, stage.type);
if(i!=module->stages.end())
i->previous = &*j;
i = j;
if(stage.required_features.glsl_version>target->required_features.glsl_version)
target->required_features.glsl_version = stage.required_features.glsl_version;
- for(NodeList<Statement>::const_iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
- if(!dynamic_cast<Import *>(i->get()))
- target->content.body.push_back(*i);
+ for(const RefPtr<Statement> &s: stage.content.body)
+ if(!dynamic_cast<Import *>(s.get()))
+ target->content.body.push_back(s);
}
void Compiler::import(ModuleCache &mod_cache, const string &name)
bool Compiler::check_errors(Stage &stage)
{
stable_sort(stage.diagnostics, &diagnostic_line_order);
-
- for(vector<Diagnostic>::const_iterator i=stage.diagnostics.begin(); i!=stage.diagnostics.end(); ++i)
- if(i->severity==Diagnostic::ERR)
- return false;
-
- return true;
+ return !any_of(stage.diagnostics.begin(), stage.diagnostics.end(),
+ [](const Diagnostic &d){ return d.severity==Diagnostic::ERR; });
}
bool Compiler::diagnostic_line_order(const Diagnostic &diag1, const Diagnostic &diag2)
void Compiler::inject_block(Block &target, const Block &source)
{
- NodeList<Statement>::iterator insert_point = target.body.begin();
- for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
- target.body.insert(insert_point, (*i)->clone());
+ auto insert_point = target.body.begin();
+ for(const RefPtr<Statement> &s: source.body)
+ target.body.insert(insert_point, s->clone());
}
} // namespace SL
begin_sub();
append(format("Version: %d.%02d", stage.required_features.glsl_version.major, stage.required_features.glsl_version.minor));
- for(map<string, TypeDeclaration *>::const_iterator i=stage.types.begin(); i!=stage.types.end(); ++i)
- append(format("Type: %%%d %s", get_label(*i->second), i->first));
+ for(const auto &kvp: stage.types)
+ append(format("Type: %%%d %s", get_label(*kvp.second), kvp.first));
- for(map<string, InterfaceBlock *>::const_iterator i=stage.interface_blocks.begin(); i!=stage.interface_blocks.end(); ++i)
- append(format("Interface block: %%%d %s", get_label(*i->second), i->first));
+ for(const auto &kvp: stage.interface_blocks)
+ append(format("Interface block: %%%d %s", get_label(*kvp.second), kvp.first));
- for(map<string, FunctionDeclaration *>::const_iterator i=stage.functions.begin(); i!=stage.functions.end(); ++i)
- append(format("Function: %%%d %s", get_label(*i->second), i->first));
+ for(const auto &kvp: stage.functions)
+ append(format("Function: %%%d %s", get_label(*kvp.second), kvp.first));
last_branch();
stage.content.visit(*this);
void DumpTree::append(const string &line)
{
StringCodec::Utf8::Encoder enc;
- for(vector<TreeChars>::const_iterator i=tree.begin(); i!=tree.end(); )
+ for(auto i=tree.begin(); i!=tree.end(); )
{
enc.encode_char(*i++, formatted);
enc.encode_char((i==tree.end() ? REACH : EMPTY), formatted);
void DumpTree::append_subtree(const vector<Branch> &branches)
{
begin_sub();
- for(vector<Branch>::const_iterator i=branches.begin(); i!=branches.end(); )
+ for(auto i=branches.begin(); i!=branches.end(); )
{
- vector<Branch>::const_iterator j = increment(i, branches);
+ auto j = increment(i, branches);
if(!j->text.empty())
{
append(j->text);
template<typename T>
typename T::const_iterator DumpTree::increment(typename T::const_iterator &iter, const T &container)
{
- typename T::const_iterator ret = iter++;
+ auto ret = iter++;
if(iter==container.end())
last_branch();
return ret;
append(block, format("Block %s", (block.use_braces ? "{}" : "(inline)")));
begin_sub();
- for(map<string, VariableDeclaration *>::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i)
- append(format("Variable: %%%d %s %s", get_label(*i->second), i->second->type, i->first));
+ for(const auto &kvp: block.variables)
+ append(format("Variable: %%%d %s %s", get_label(*kvp.second), kvp.second->type, kvp.first));
- for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); )
+ for(auto i=block.body.cbegin(); i!=block.body.cend(); )
{
- NodeList<Statement>::const_iterator j = increment(i, block.body);
+ auto j = increment(i, block.body);
(*j)->visit(*this);
}
append(call, head);
begin_sub();
- for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); )
+ for(auto i=call.arguments.cbegin(); i!=call.arguments.cend(); )
{
- NodeArray<Expression>::const_iterator j = increment(i, call.arguments);
+ auto j = increment(i, call.arguments);
(*j)->visit(*this);
}
end_sub();
{
append(layout, "Layout");
begin_sub();
- for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); )
+ for(auto i=layout.qualifiers.cbegin(); i!=layout.qualifiers.cend(); )
{
- vector<Layout::Qualifier>::const_iterator j = increment(i, layout.qualifiers);
+ auto j = increment(i, layout.qualifiers);
string qualifier = j->name;
if(j->has_value)
qualifier += format("=%d", j->value);
begin_sub();
if(func.return_type_declaration)
append(format("Return type: %%%d %s", get_label(*func.return_type_declaration), func.return_type_declaration->name));
- for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
- (*i)->visit(*this);
+ for(const RefPtr<VariableDeclaration> &p: func.parameters)
+ p->visit(*this);
last_branch();
if(func.definition==&func)
func.body.visit(*this);
bool has_matrix_order = false;
if(var.layout)
{
- vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
- for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
+ for(Layout::Qualifier &q: var.layout->qualifiers)
{
- if(i->name=="offset" && i->has_value)
+ if(q.name=="offset" && q.has_value)
{
- layout_offset = &i->value;
- if(i->value>=offset)
- offset = i->value;
+ layout_offset = &q.value;
+ if(q.value>=offset)
+ offset = q.value;
}
- else if(i->name=="column_major" || i->name=="row_major")
+ else if(q.name=="column_major" || q.name=="row_major")
has_matrix_order = true;
}
}
void LocationAllocator::apply(Module &module, const Features &features)
{
- for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
- apply(*i);
+ for(Stage &s: module.stages)
+ apply(s);
allocate_locations("uniform");
- for(vector<InterfaceBlock *>::const_iterator i=unbound_blocks.begin(); i!=unbound_blocks.end(); ++i)
- bind_uniform((*i)->layout, (*i)->block_name, features.uniform_binding_range);
- for(vector<VariableDeclaration *>::const_iterator i=unbound_textures.begin(); i!=unbound_textures.end(); ++i)
- bind_uniform((*i)->layout, (*i)->name, features.texture_binding_range);
+ for(InterfaceBlock *b: unbound_blocks)
+ bind_uniform(b->layout, b->block_name, features.uniform_binding_range);
+ for(VariableDeclaration *t: unbound_textures)
+ bind_uniform(t->layout, t->name, features.texture_binding_range);
}
void LocationAllocator::apply(Stage &stage)
void LocationAllocator::allocate_locations(const string &iface)
{
- vector<VariableDeclaration *>::iterator write = unplaced_variables.begin();
+ auto write = unplaced_variables.begin();
unsigned next = 0;
- for(vector<VariableDeclaration *>::const_iterator i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
+ for(auto i=unplaced_variables.begin(); i!=unplaced_variables.end(); ++i)
{
if((*i)->interface!=iface)
{
if((*i)->interface=="uniform")
{
- map<string, Uniform>::const_iterator j = uniforms.find((*i)->name);
+ auto j = uniforms.find((*i)->name);
if(j!=uniforms.end() && j->second.location>=0)
{
add_layout_value((*i)->layout, "location", j->second.location);
void LocationAllocator::bind_uniform(RefPtr<Layout> &layout, const string &name, unsigned range)
{
- map<string, Uniform>::const_iterator i = uniforms.find(name);
+ auto i = uniforms.find(name);
if(i!=uniforms.end() && i->second.bind_point>=0)
add_layout_value(layout, "binding", i->second.bind_point);
else
void PrecisionConverter::visit(Block &block)
{
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ for(auto i=block.body.begin(); i!=block.body.end(); ++i)
{
if(&block==&stage->content)
insert_point = i;
void LegacyConverter::visit(Block &block)
{
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ for(auto i=block.body.begin(); i!=block.body.end(); ++i)
{
if(&block==&stage->content)
uniform_insert_point = i;
{
if(var.layout)
{
- for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
+ for(auto i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); )
{
if(i->name=="location")
{
{
if(iface.layout)
{
- for(vector<Layout::Qualifier>::const_iterator i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
+ for(auto i=iface.layout->qualifiers.begin(); i!=iface.layout->qualifiers.end(); )
{
if(i->name=="location" && !supports_interface_block_location())
i = iface.layout->qualifiers.erase(i);
+#include <msp/core/algorithm.h>
#include <msp/core/hash.h>
#include <msp/core/raii.h>
#include "generate.h"
void ConstantIdAssigner::apply(Module &module, const Features &features)
{
- for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
- i->content.visit(*this);
+ for(Stage &s: module.stages)
+ s.content.visit(*this);
- for(vector<VariableDeclaration *>::iterator i=auto_constants.begin(); i!=auto_constants.end(); ++i)
+ for(VariableDeclaration *v: auto_constants)
{
- unsigned id = hash32((*i)->name)%features.constant_id_range;
+ unsigned id = hash32(v->name)%features.constant_id_range;
while(used_ids.count(id))
id = (id+1)%features.constant_id_range;
- vector<Layout::Qualifier> &qualifiers = (*i)->layout->qualifiers;
- for(vector<Layout::Qualifier>::iterator j=qualifiers.begin(); j!=qualifiers.end(); ++j)
- if(j->name=="constant_id")
- {
- j->value = id;
- break;
- }
+ auto i = find_member(v->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
+ if(i!=v->layout->qualifiers.end())
+ i->value = id;
used_ids.insert(id);
}
{
if(var.layout)
{
- vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
- for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
- if(i->name=="constant_id" && i->has_value)
- {
- if(i->value==-1)
- auto_constants.push_back(&var);
- else
- used_ids.insert(i->value);
- break;
- }
+ auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name);
+ if(i!=var.layout->qualifiers.end() && i->has_value)
+ {
+ if(i->value==-1)
+ auto_constants.push_back(&var);
+ else
+ used_ids.insert(i->value);
+ }
}
}
void InterfaceGenerator::visit(Block &block)
{
SetForScope<Block *> set_block(current_block, &block);
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ for(auto i=block.body.begin(); i!=block.body.end(); ++i)
{
assignment_insert_point = i;
if(&block==&stage->content)
return;
const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
- map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
+ auto i = prev_vars.find(var.name);
if(i==prev_vars.end() || i->second->interface!="out")
i = prev_vars.find(in_prefix+var.name);
if(i!=prev_vars.end() && i->second->interface=="out")
}
const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
- map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
+ auto j = prev_blocks.find(var.name);
if(j!=prev_blocks.end() && j->second->interface=="out")
{
generate_interface(*j->second);
return;
}
- for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
- if(j->second->instance_name.empty() && j->second->struct_declaration)
+ for(const auto &kvp: prev_blocks)
+ if(kvp.second->instance_name.empty() && kvp.second->struct_declaration)
{
- const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
+ const map<string, VariableDeclaration *> &iface_vars = kvp.second->struct_declaration->members.variables;
i = iface_vars.find(var.name);
if(i!=iface_vars.end())
{
- generate_interface(*j->second);
+ generate_interface(*kvp.second);
return;
}
}
if(!var.linked_declaration && stage->previous)
{
const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
- map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
+ auto i = prev_vars.find(var.name);
if(i!=prev_vars.end() && i->second->interface=="out")
{
var.linked_declaration = i->second;
if(!iface.linked_block && stage->previous)
{
const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
- map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out "+iface.block_name);
+ auto i = prev_blocks.find("out "+iface.block_name);
if(i!=prev_blocks.end())
{
iface.linked_block = i->second;
if(stage->previous)
{
- const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
- for(map<string, VariableDeclaration *>::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i)
+ for(const auto &kvp: stage->previous->content.variables)
{
- if(i->second->interface!="out")
+ if(kvp.second->interface!="out")
continue;
/* Pass through output variables from the previous stage, but only
those which are not already linked to an input here. */
- if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
- pass_vars.push_back(i->second);
+ if(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name))
+ pass_vars.push_back(kvp.second);
}
}
insert_assignment("gl_Position", memacc);
}
- for(vector<VariableDeclaration *>::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i)
+ for(VariableDeclaration *v: pass_vars)
{
- string out_name = change_prefix((*i)->name, out_prefix);
- generate_interface(**i, "out", out_name);
+ string out_name = change_prefix(v->name, out_prefix);
+ generate_interface(*v, "out", out_name);
VariableReference *ref = new VariableReference;
- ref->name = (*i)->name;
+ ref->name = v->name;
if(pass.subscript)
{
BinaryExpression *subscript = new BinaryExpression;
ModuleCache::ModuleCache(const ModuleCache &other)
{
- for(map<string, Module *>::const_iterator i=other.modules.begin(); i!=other.modules.end(); ++i)
- modules[i->first] = new Module(*i->second);
+ for(const auto &kvp: other.modules)
+ modules[kvp.first] = new Module(*kvp.second);
}
ModuleCache &ModuleCache::operator=(const ModuleCache &other)
{
- for(map<string, Module *>::iterator i=modules.begin(); i!=modules.end(); ++i)
- delete i->second;
+ for(auto &kvp: modules)
+ delete kvp.second;
modules.clear();
- for(map<string, Module *>::const_iterator i=other.modules.begin(); i!=other.modules.end(); ++i)
- modules[i->first] = new Module(*i->second);
+ for(const auto &kvp: other.modules)
+ modules[kvp.first] = new Module(*kvp.second);
return *this;
}
ModuleCache::~ModuleCache()
{
- for(map<string, Module *>::iterator i=modules.begin(); i!=modules.end(); ++i)
- delete i->second;
+ for(auto &kvp: modules)
+ delete kvp.second;
}
const Module &ModuleCache::add_module(const string &source, const string &src_name)
const Module &ModuleCache::get_module(const string &name)
{
string fn = name+".glsl";
- map<string, Module *>::const_iterator i = modules.find(fn);
+ auto i = modules.find(fn);
if(i!=modules.end())
return *i->second;
+#include <msp/core/algorithm.h>
#include <msp/core/raii.h>
#include <msp/strings/format.h>
#include <msp/strings/utils.h>
if(var.layout)
{
vector<Layout::Qualifier> &qualifiers = var.layout->qualifiers;
- for(vector<Layout::Qualifier>::iterator i=qualifiers.begin(); (!specializable && i!=qualifiers.end()); ++i)
- if(i->name=="constant_id")
- {
- specializable = true;
- qualifiers.erase(i);
- }
-
- if(qualifiers.empty())
- var.layout = 0;
+ auto i = find_member(qualifiers, string("constant_id"), &Layout::Qualifier::name);
+ if(i!=qualifiers.end())
+ {
+ specializable = true;
+ qualifiers.erase(i);
+ if(qualifiers.empty())
+ var.layout = 0;
+ }
}
if(specializable)
{
- map<string, int>::const_iterator i = values->find(var.name);
+ auto i = values->find(var.name);
if(i!=values->end())
{
RefPtr<Literal> literal = new Literal;
void InlineableFunctionLocator::visit(FunctionDeclaration &func)
{
- bool has_out_params = false;
- for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); (!has_out_params && i!=func.parameters.end()); ++i)
- has_out_params = ((*i)->interface=="out");
+ bool has_out_params = any_of(func.parameters.begin(), func.parameters.end(),
+ [](const RefPtr<VariableDeclaration> &p){ return p->interface=="out"; });
unsigned &count = refcounts[func.definition];
if((count<=1 || func.source==BUILTIN_SOURCE) && !has_out_params)
vector<RefPtr<VariableDeclaration> > params;
params.reserve(source_func->parameters.size());
- for(NodeArray<VariableDeclaration>::iterator i=source_func->parameters.begin(); i!=source_func->parameters.end(); ++i)
+ for(const RefPtr<VariableDeclaration> &p: source_func->parameters)
{
- RefPtr<VariableDeclaration> var = (*i)->clone();
+ RefPtr<VariableDeclaration> var = p->clone();
var->interface.clear();
SetForScope<Pass> set_pass(pass, RENAME);
params.push_back(var);
}
- for(NodeList<Statement>::iterator i=source_func->body.body.begin(); i!=source_func->body.body.end(); ++i)
+ for(const RefPtr<Statement> &s: source_func->body.body)
{
r_inlined_statement = 0;
- (*i)->visit(*this);
+ s->visit(*this);
if(!r_inlined_statement)
- r_inlined_statement = (*i)->clone();
+ r_inlined_statement = s->clone();
SetForScope<Pass> set_pass(pass, RENAME);
r_inlined_statement->visit(*this);
{
if(pass==RENAME)
{
- map<string, VariableDeclaration *>::const_iterator i = staging_block.variables.find(var.name);
+ auto i = staging_block.variables.find(var.name);
if(i!=staging_block.variables.end())
var.name = i->second->name;
}
{
SetForScope<Block *> set_block(current_block, &block);
SetForScope<NodeList<Statement>::iterator> save_insert_point(insert_point, block.body.begin());
- for(NodeList<Statement>::iterator i=block.body.begin(); (!r_inlined_here && i!=block.body.end()); ++i)
+ for(auto i=block.body.begin(); (!r_inlined_here && i!=block.body.end()); ++i)
{
insert_point = i;
(*i)->visit(*this);
void FunctionInliner::visit(FunctionCall &call)
{
- for(NodeArray<Expression>::iterator i=call.arguments.begin(); (!r_inlined_here && i!=call.arguments.end()); ++i)
+ for(auto i=call.arguments.begin(); (!r_inlined_here && i!=call.arguments.end()); ++i)
visit(*i);
if(r_inlined_here)
s.content.visit(*this);
bool any_inlined = false;
- for(list<ExpressionInfo>::iterator i=expressions.begin(); i!=expressions.end(); ++i)
- if(i->expression && (i->trivial || i->uses.size()==1))
+ for(ExpressionInfo &e: expressions)
+ if(e.expression && (e.trivial || e.uses.size()==1))
{
- for(vector<ExpressionUse>::iterator j=i->uses.begin(); j!=i->uses.end(); ++j)
- if(!j->blocked)
+ for(ExpressionUse &u: e.uses)
+ if(!u.blocked)
{
- *j->reference = i->expression->clone();
+ *u.reference = e.expression->clone();
any_inlined = true;
}
}
{
if(var.declaration && access_read)
{
- map<Assignment::Target, ExpressionInfo *>::iterator i = assignments.find(var.declaration);
+ auto i = assignments.find(var.declaration);
if(i!=assignments.end())
r_ref_info = i->second;
}
r_trivial = true;
visit(assign.right);
- map<Assignment::Target, ExpressionInfo *>::iterator i = assignments.find(assign.target);
+ auto i = assignments.find(assign.target);
if(i!=assignments.end())
{
if(iteration_body && i->second->expression)
/* Block inlining into previous references within the iteration
statement. On iterations after the first they would refer to the
assignment within the iteration. */
- for(vector<ExpressionUse>::iterator j=i->second->uses.begin(); j!=i->second->uses.end(); ++j)
- for(Block *k=j->ref_scope; (!j->blocked && k); k=k->parent)
- j->blocked = (k==iteration_body);
+ for(ExpressionUse &u: i->second->uses)
+ for(Block *k=u.ref_scope; (!u.blocked && k); k=k->parent)
+ u.blocked = (k==iteration_body);
}
expressions.push_back(ExpressionInfo());
bool constant = var.constant;
if(constant && var.layout)
{
- for(vector<Layout::Qualifier>::const_iterator i=var.layout->qualifiers.begin(); (constant && i!=var.layout->qualifiers.end()); ++i)
- constant = (i->name!="constant_id");
+ constant = !any_of(var.layout->qualifiers.begin(), var.layout->qualifiers.end(),
+ [](const Layout::Qualifier &q){ return q.name=="constant_id"; });
}
/* Only inline global variables if they're constant and have trivial
void ConstantConditionEliminator::visit(Block &block)
{
SetForScope<Block *> set_block(current_block, &block);
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ for(auto i=block.body.begin(); i!=block.body.end(); ++i)
{
insert_point = i;
(*i)->visit(*this);
void UnreachableCodeRemover::visit(Block &block)
{
- NodeList<Statement>::iterator i = block.body.begin();
+ auto i = block.body.begin();
for(; (reachable && i!=block.body.end()); ++i)
(*i)->visit(*this);
for(; i!=block.body.end(); ++i)
stage = &s;
s.content.visit(*this);
- for(list<AssignmentInfo>::const_iterator i=assignments.begin(); i!=assignments.end(); ++i)
- if(i->used_by.empty())
- unused_nodes.insert(i->node);
+ for(const AssignmentInfo &a: assignments)
+ if(a.used_by.empty())
+ unused_nodes.insert(a.node);
- for(BlockVariableMap::const_iterator i=variables.begin(); i!=variables.end(); ++i)
+ for(const auto &kvp: variables)
{
- if(i->second.output)
+ if(kvp.second.output)
{
/* The last visible assignments of output variables are used by the
next stage or the API. */
- for(vector<AssignmentInfo *>::const_iterator j=i->second.assignments.begin(); j!=i->second.assignments.end(); ++j)
- unused_nodes.erase((*j)->node);
+ for(AssignmentInfo *a: kvp.second.assignments)
+ unused_nodes.erase(a->node);
}
- if(!i->second.output && !i->second.referenced)
+ if(!kvp.second.output && !kvp.second.referenced)
{
// Don't remove variables from inside interface blocks.
- if(!i->second.interface_block)
- unused_nodes.insert(i->first);
+ if(!kvp.second.interface_block)
+ unused_nodes.insert(kvp.first);
}
- else if(i->second.interface_block)
+ else if(kvp.second.interface_block)
// Interface blocks are kept if even one member is used.
- unused_nodes.erase(i->second.interface_block);
+ unused_nodes.erase(kvp.second.interface_block);
}
NodeRemover().apply(s, unused_nodes);
if(!assignment_target)
{
bool loop_external = false;
- for(vector<AssignmentInfo *>::const_iterator i=var_info.assignments.begin(); i!=var_info.assignments.end(); ++i)
+ for(AssignmentInfo *a: var_info.assignments)
{
bool covered = true;
- for(unsigned j=0; (covered && j<(*i)->target.chain_len && j<target.chain_len); ++j)
+ for(unsigned j=0; (covered && j<a->target.chain_len && j<target.chain_len); ++j)
{
- Assignment::Target::ChainType type1 = static_cast<Assignment::Target::ChainType>((*i)->target.chain[j]&0xC0);
+ Assignment::Target::ChainType type1 = static_cast<Assignment::Target::ChainType>(a->target.chain[j]&0xC0);
Assignment::Target::ChainType type2 = static_cast<Assignment::Target::ChainType>(target.chain[j]&0xC0);
if(type1==Assignment::Target::SWIZZLE || type2==Assignment::Target::SWIZZLE)
{
- unsigned index1 = (*i)->target.chain[j]&0x3F;
+ unsigned index1 = a->target.chain[j]&0x3F;
unsigned index2 = target.chain[j]&0x3F;
if(type1==Assignment::Target::SWIZZLE && type2==Assignment::Target::SWIZZLE)
covered = index1&index2;
covered as true */
}
else
- covered = ((*i)->target.chain[j]==target.chain[j]);
+ covered = (a->target.chain[j]==target.chain[j]);
}
if(covered)
{
- (*i)->used_by.push_back(&node);
- if((*i)->in_loop<in_loop)
+ a->used_by.push_back(&node);
+ if(a->in_loop<in_loop)
loop_external = true;
}
}
if(stage->type==Stage::GEOMETRY && call.name=="EmitVertex")
{
- for(map<Statement *, VariableInfo>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
- if(i->second.output)
- referenced(i->first, call);
+ for(const auto &kvp: variables)
+ if(kvp.second.output)
+ referenced(kvp.first, call);
}
}
void UnusedVariableRemover::merge_variables(const BlockVariableMap &other_vars)
{
- for(BlockVariableMap::const_iterator i=other_vars.begin(); i!=other_vars.end(); ++i)
+ for(const auto &kvp: other_vars)
{
- BlockVariableMap::iterator j = variables.find(i->first);
+ auto j = variables.find(kvp.first);
if(j!=variables.end())
{
/* The merged blocks started as copies of each other so any common
assignments must be in the beginning. */
unsigned k = 0;
- for(; (k<i->second.assignments.size() && k<j->second.assignments.size()); ++k)
- if(i->second.assignments[k]!=j->second.assignments[k])
+ for(; (k<kvp.second.assignments.size() && k<j->second.assignments.size()); ++k)
+ if(kvp.second.assignments[k]!=j->second.assignments[k])
break;
// Remaining assignments are unique to each block; merge them.
- j->second.assignments.insert(j->second.assignments.end(), i->second.assignments.begin()+k, i->second.assignments.end());
- j->second.referenced |= i->second.referenced;
+ j->second.assignments.insert(j->second.assignments.end(), kvp.second.assignments.begin()+k, kvp.second.assignments.end());
+ j->second.referenced |= kvp.second.referenced;
}
else
- variables.insert(*i);
+ variables.insert(kvp);
}
}
BlockVariableMap saved_vars = variables;
// Assignments from other functions should not be visible.
- for(BlockVariableMap::iterator i=variables.begin(); i!=variables.end(); ++i)
- i->second.assignments.resize(i->second.initialized);
+ for(auto &kvp: variables)
+ kvp.second.assignments.resize(kvp.second.initialized);
TraversingVisitor::visit(func);
swap(variables, saved_vars);
merge_variables(saved_vars);
/* Always treat function parameters as referenced. Removing unused
parameters is not currently supported. */
- for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+ for(const RefPtr<VariableDeclaration> &p: func.parameters)
{
- BlockVariableMap::iterator j = variables.find(i->get());
+ auto j = variables.find(p.get());
if(j!=variables.end())
j->second.referenced = true;
}
/* Visit the external references of the loop again to record assignments
done in the loop as used. */
- for(vector<Node *>::const_iterator i=saved_refs.begin(); i!=saved_refs.end(); ++i)
- (*i)->visit(*this);
+ for(Node *n: saved_refs)
+ n->visit(*this);
/* Merge assignments from the iteration, without clearing previous state.
Further analysis is needed to determine which parts of the iteration body
void Formatter::append(const string &text)
{
formatted += text;
- for(string::const_iterator i=text.begin(); i!=text.end(); ++i)
- if(*i=='\n')
+ for(char c: text)
+ if(c=='\n')
++source_line;
}
SetForScope<unsigned> set(indent, indent+(indent>0 || use_braces));
string spaces(indent*2, ' ');
bool first = true;
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ for(const RefPtr<Statement> &s: block.body)
{
- if(omit_builtin && (*i)->source<=BUILTIN_SOURCE)
+ if(omit_builtin && s->source<=BUILTIN_SOURCE)
continue;
if(!first)
append('\n');
first = false;
- set_source((*i)->source, (*i)->line);
+ set_source(s->source, s->line);
append(spaces);
- (*i)->visit(*this);
+ s->visit(*this);
}
if(use_braces)
void Formatter::visit(FunctionCall &call)
{
append(format("%s(", call.name));
- for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
+ for(auto i=call.arguments.begin(); i!=call.arguments.end(); ++i)
{
if(i!=call.arguments.begin())
append(", ");
void Formatter::visit(Layout &layout)
{
append("layout(");
- for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
+ for(auto i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
{
if(i!=layout.qualifiers.begin())
append(", ");
void Formatter::visit(FunctionDeclaration &func)
{
append(format("%s %s(", func.return_type_declaration->name, func.name));
- for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+ for(auto i=func.parameters.begin(); i!=func.parameters.end(); ++i)
{
if(i!=func.parameters.begin())
append(", ");
if(const Stage *builtin = get_builtins(Stage::SHARED))
{
- for(map<string, TypeDeclaration *>::const_iterator i=builtin->types.begin(); i!=builtin->types.end(); ++i)
- global_types.insert(i->first);
+ for(const auto &kvp: builtin->types)
+ global_types.insert(kvp.first);
}
tokenizer.begin(source, name);
cur_stage = &module->stages.back();
stage_types.clear();
- for(vector<const Module *>::const_iterator i=imported_modules.begin(); i!=imported_modules.end(); ++i)
+ for(const Module *m: imported_modules)
{
- list<Stage>::const_iterator j = find_member((*i)->stages, stage, &Stage::type);
- if(j!=(*i)->stages.end())
+ auto j = find_member(m->stages, stage, &Stage::type);
+ if(j!=m->stages.end())
{
- for(map<string, TypeDeclaration *>::const_iterator k=j->types.begin(); k!=j->types.end(); ++k)
- stage_types.insert(k->first);
+ for(const auto &kvp: j->types)
+ stage_types.insert(kvp.first);
}
}
}
{
const Module &imported_mod = mod_cache->get_module(import->module);
imported_modules.push_back(&imported_mod);
- for(map<string, TypeDeclaration *>::const_iterator i=imported_mod.shared.types.begin(); i!=imported_mod.shared.types.end(); ++i)
- global_types.insert(i->first);
+ for(const auto &kvp: imported_mod.shared.types)
+ global_types.insert(kvp.first);
}
return import;
+#include <msp/core/algorithm.h>
#include "reflect.h"
using namespace std;
else
{
r_result = true;
- NodeList<Statement>::const_iterator i = strct1->members.body.begin();
- NodeList<Statement>::const_iterator j = strct.members.body.begin();
+ auto i = strct1->members.body.begin();
+ auto j = strct.members.body.begin();
for(; (r_result && i!=strct1->members.body.end()); ++i, ++j)
compare(**i, **j);
}
void LocationCounter::visit(StructDeclaration &strct)
{
unsigned total = 0;
- for(NodeList<Statement>::const_iterator i=strct.members.body.begin(); i!=strct.members.body.end(); ++i)
+ for(const RefPtr<Statement> &s: strct.members.body)
{
r_count = 1;
- (*i)->visit(*this);
+ s->visit(*this);
total += r_count;
}
r_count = total;
{
unsigned total = 0;
unsigned max_align = 1;
- for(NodeList<Statement>::iterator i=strct.members.body.begin(); i!=strct.members.body.end(); ++i)
+ for(const RefPtr<Statement> &s: strct.members.body)
{
r_size = 0;
r_alignment = 1;
r_offset = -1;
- (*i)->visit(*this);
+ s->visit(*this);
if(r_offset)
total = r_offset;
total += r_alignment-1;
{
if(var.layout)
{
- const vector<Layout::Qualifier> qualifiers = var.layout->qualifiers;
- for(vector<Layout::Qualifier>::const_iterator i=qualifiers.begin(); (r_offset<0 && i!=qualifiers.end()); ++i)
- if(i->name=="offset")
- r_offset = i->value;
+ auto i = find_member(var.layout->qualifiers, string("offset"), &Layout::Qualifier::name);
+ if(i!=var.layout->qualifiers.end())
+ r_offset = i->value;
}
if(var.type_declaration)
-#include <algorithm>
+#include <msp/core/algorithm.h>
#include <msp/core/raii.h>
#include <msp/strings/utils.h>
#include "reflect.h"
TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type)
{
- map<TypeDeclaration *, TypeDeclaration *>::iterator i = array_types.find(&type);
+ auto i = array_types.find(&type);
if(i!=array_types.end())
return i->second;
void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array)
{
TypeDeclaration *resolved = 0;
- map<string, TypeDeclaration *>::iterator i = stage->types.find(name);
+ auto i = stage->types.find(name);
if(i!=stage->types.end())
{
- map<TypeDeclaration *, TypeDeclaration *>::iterator j = alias_map.find(i->second);
+ auto j = alias_map.find(i->second);
resolved = (j!=alias_map.end() ? j->second : i->second);
}
void TypeResolver::visit(Block &block)
{
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ for(auto i=block.body.begin(); i!=block.body.end(); ++i)
{
if(!block.parent)
type_insert_point = i;
s.interface_blocks.clear();
r_any_resolved = false;
s.content.visit(*this);
- for(vector<VariableDeclaration *>::const_iterator i=redeclared_builtins.begin(); i!=redeclared_builtins.end(); ++i)
- (*i)->source = GENERATED_SOURCE;
+ for(VariableDeclaration *v: redeclared_builtins)
+ v->source = GENERATED_SOURCE;
NodeRemover().apply(s, nodes_to_remove);
return r_any_resolved;
}
one. */
for(Block *block=current_block; (!declaration && block); block=block->parent)
{
- map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
+ auto i = block->variables.find(var.name);
if(i!=block->variables.end())
declaration = i->second;
}
if(!declaration)
{
const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
- map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
+ auto i = blocks.find(var.name);
if(i==blocks.end())
{
// Look for the variable in anonymous interface blocks.
void VariableResolver::visit(InterfaceBlockReference &iface)
{
- map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
+ auto i = stage->interface_blocks.find(iface.name);
InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0);
r_any_resolved |= (declaration!=iface.declaration);
iface.declaration = declaration;
int index = -1;
if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
{
- map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
+ auto i = strct->members.variables.find(memacc.member);
if(i!=strct->members.variables.end())
{
declaration = i->second;
index = 0;
- for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
+ for(auto j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j)
++index;
if(record_target)
bool ok = true;
UInt8 components[4] = { };
for(unsigned i=0; (ok && i<memacc.member.size()); ++i)
- ok = ((components[i] = (find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
+ ok = ((components[i] = (std::find(component_names, component_names+12, memacc.member[i])-component_names)/3) < 4);
if(ok)
{
void VariableResolver::merge_layouts(Layout &to_layout, const Layout &from_layout)
{
- for(vector<Layout::Qualifier>::const_iterator i=from_layout.qualifiers.begin(); i!=from_layout.qualifiers.end(); ++i)
+ for(const Layout::Qualifier &q: from_layout.qualifiers)
{
- bool found = false;
- for(vector<Layout::Qualifier>::iterator j=to_layout.qualifiers.begin(); (!found && j!=to_layout.qualifiers.end()); ++j)
- if(j->name==i->name)
- {
- j->has_value = i->value;
- j->value = i->value;
- found = true;
- }
-
- if(!found)
- to_layout.qualifiers.push_back(*i);
+ auto i = find_member(to_layout.qualifiers, q.name, &Layout::Qualifier::name);
+ if(i!=to_layout.qualifiers.end())
+ {
+ i->has_value = q.value;
+ i->value = q.value;
+ }
+ else
+ to_layout.qualifiers.push_back(q);
}
}
BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size, bool sign)
{
- for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
- if((*i)->kind==kind && (*i)->size==size && (*i)->sign==sign)
- return *i;
- return 0;
+ auto i = find_if(basic_types,
+ [kind, size, sign](const BasicTypeDeclaration *t){ return t->kind==kind && t->size==size && t->sign==sign; });
+ return (i!=basic_types.end() ? *i : 0);
}
BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size)
{
- for(vector<BasicTypeDeclaration *>::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i)
- if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size)
- return *i;
- return 0;
+ auto i = find_if(basic_types,
+ [&elem_type, kind, size](BasicTypeDeclaration *t){ return get_element_type(*t)==&elem_type && t->kind==kind && t->size==size; });
+ return (i!=basic_types.end() ? *i : 0);
}
void ExpressionResolver::convert_to(RefPtr<Expression> &expr, BasicTypeDeclaration &type)
void ExpressionResolver::visit(Block &block)
{
SetForScope<Block *> set_block(current_block, &block);
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ for(auto i=block.body.begin(); i!=block.body.end(); ++i)
{
insert_point = i;
(*i)->visit(*this);
if(call.arguments.empty())
return;
- map<string, TypeDeclaration *>::const_iterator i = stage->types.find(call.name);
+ auto i = stage->types.find(call.name);
if(i==stage->types.end())
return;
else if(call.arguments.size()==1 && i->second==call.arguments[0]->type)
args.reserve(call.arguments.size());
unsigned arg_component_total = 0;
bool has_matrices = false;
- for(NodeArray<Expression>::const_iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j)
+ for(const RefPtr<Expression> &a: call.arguments)
{
ArgumentInfo info;
- if(!(info.type=dynamic_cast<BasicTypeDeclaration *>((*j)->type)))
+ if(!(info.type=dynamic_cast<BasicTypeDeclaration *>(a->type)))
return;
if(is_scalar(*info.type) || info.type->kind==BasicTypeDeclaration::BOOL)
info.component_count = 1;
if(convert_args)
{
// The argument list may have changed so can't rely on args.
- for(NodeArray<Expression>::iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j)
- if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>((*j)->type))
+ for(RefPtr<Expression> &a: call.arguments)
+ if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>(a->type))
{
BasicTypeDeclaration *elem_arg = get_element_type(*basic_arg);
if(elem_arg!=elem)
- convert_to_element(*j, *elem);
+ convert_to_element(a, *elem);
}
}
}
if(call.arguments.size()!=strct->members.body.size())
return;
- unsigned k = 0;
- for(NodeList<Statement>::const_iterator j=strct->members.body.begin(); j!=strct->members.body.end(); ++j, ++k)
+ auto j = call.arguments.begin();
+ for(const RefPtr<Statement> &s: strct->members.body)
{
- if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(j->get()))
+ if(VariableDeclaration *var = dynamic_cast<VariableDeclaration *>(s.get()))
{
- if(!call.arguments[k]->type || call.arguments[k]->type!=var->type_declaration)
+ if(!(*j)->type || (*j)->type!=var->type_declaration)
return;
}
else
return;
+ ++j;
}
}
{
string arg_types;
bool has_signature = true;
- for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
+ for(auto i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i)
{
if((*i)->type)
append(arg_types, ",", (*i)->type->name);
if(has_signature)
{
- map<string, FunctionDeclaration *>::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types));
+ auto i = stage->functions.find(format("%s(%s)", call.name, arg_types));
declaration = (i!=stage->functions.end() ? i->second : 0);
if(!declaration)
if(func.signature.empty())
{
string param_types;
- for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
+ for(const RefPtr<VariableDeclaration> &p: func.parameters)
{
- if((*i)->type_declaration)
- append(param_types, ",", (*i)->type_declaration->name);
+ if(p->type_declaration)
+ append(param_types, ",", p->type_declaration->name);
else
return;
}
stage_decl = &func;
// Set all previous declarations to use this definition.
- for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
+ for(FunctionDeclaration *f: decls)
{
- r_any_resolved |= (func.definition!=(*i)->definition);
- (*i)->definition = func.definition;
- (*i)->body.body.clear();
+ r_any_resolved |= (func.definition!=f->definition);
+ f->definition = func.definition;
+ f->body.body.clear();
}
}
else
static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$");
vector<string> lines = split(errors, '\n');
string translated;
- for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
+ for(const string &l: lines)
{
- RegMatch m = r_message.match(*i);
+ RegMatch m = r_message.match(l);
if(m)
{
unsigned index = 0;
translated += format("%s:%d: %s", src, line, m[6].str);
}
else
- translated += *i;
+ translated += l;
translated += '\n';
}
+#include <msp/core/algorithm.h>
#include <msp/core/maputils.h>
#include <msp/core/raii.h>
#include "reflect.h"
{
use_capability(CAP_SHADER);
- for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
+ for(Stage &s: module.stages)
{
- stage = &*i;
+ stage = &s;
interface_layouts.clear();
- i->content.visit(*this);
+ s.content.visit(*this);
}
writer.finalize(SPIRV_GENERATOR_MSP, next_id);
SpirVGenerator::Id SpirVGenerator::allocate_id(Node &node, Id type_id)
{
- map<Node *, Declaration>::iterator i = declared_ids.find(&node);
+ auto i = declared_ids.find(&node);
if(i!=declared_ids.end())
{
if(i->second.type_id)
SpirVGenerator::Id SpirVGenerator::allocate_forward_id(Node &node)
{
- map<Node *, Declaration>::iterator i = declared_ids.find(&node);
+ auto i = declared_ids.find(&node);
if(i!=declared_ids.end())
return i->second.id;
bool SpirVGenerator::is_scalar_type(Id type_id, BasicTypeDeclaration::Kind kind) const
{
- map<TypeKey, Id>::const_iterator i = standard_type_ids.find(TypeKey(kind, true));
+ auto i = standard_type_ids.find(TypeKey(kind, true));
return (i!=standard_type_ids.end() && i->second==type_id);
}
void SpirVGenerator::prune_loads(Id min_id)
{
- for(map<const VariableDeclaration *, Id>::iterator i=variable_load_ids.begin(); i!=variable_load_ids.end(); )
+ for(auto i=variable_load_ids.begin(); i!=variable_load_ids.end(); )
{
if(i->second>=min_id)
variable_load_ids.erase(i++);
void SpirVGenerator::visit(Block &block)
{
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
- (*i)->visit(*this);
+ for(const RefPtr<Statement> &s: block.body)
+ s->visit(*this);
}
void SpirVGenerator::visit(Literal &literal)
throw internal_error("composite access through pointer in constant context");
Id int32_type_id = get_standard_type_id(BasicTypeDeclaration::INT, 1);
- for(vector<unsigned>::iterator i=r_composite_chain.begin(); i!=r_composite_chain.end(); ++i)
- *i = (*i<0x400000 ? get_constant_id(int32_type_id, static_cast<int>(*i)) : *i&0x3FFFFF);
+ for(unsigned &i: r_composite_chain)
+ i = (i<0x400000 ? get_constant_id(int32_type_id, static_cast<int>(i)) : i&0x3FFFFF);
/* Find the storage class of the base and obtain appropriate pointer type
for the result. */
const Declaration &base_decl = get_item(declared_ids, r_composite_base);
- map<TypeKey, Id>::const_iterator i = pointer_type_ids.begin();
+ auto i = pointer_type_ids.begin();
for(; (i!=pointer_type_ids.end() && i->second!=base_decl.type_id); ++i) ;
if(i==pointer_type_ids.end())
throw internal_error("could not find storage class");
throw internal_error("assignment to temporary composite");
else
{
- for(vector<unsigned>::iterator i=r_composite_chain.begin(); i!=r_composite_chain.end(); ++i)
- for(map<ConstantKey, Id>::iterator j=constant_ids.begin(); (*i>=0x400000 && j!=constant_ids.end()); ++j)
- if(j->second==(*i&0x3FFFFF))
- *i = j->first.int_value;
+ for(unsigned i: r_composite_chain)
+ for(auto j=constant_ids.begin(); (i>=0x400000 && j!=constant_ids.end()); ++j)
+ if(j->second==(i&0x3FFFFF))
+ i = j->first.int_value;
opcode = OP_COMPOSITE_EXTRACT;
}
Id access_id = begin_expression(opcode, access_type_id, 1+r_composite_chain.size());
writer.write(r_composite_base_id);
- for(vector<unsigned>::const_iterator i=r_composite_chain.begin(); i!=r_composite_chain.end(); ++i)
- writer.write(*i);
+ for(unsigned i: r_composite_chain)
+ writer.write(i);
end_expression(opcode);
r_constant_result = false;
vector<Id> argument_ids;
argument_ids.reserve(call.arguments.size());
bool all_args_const = true;
- for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
+ for(const RefPtr<Expression> &a: call.arguments)
{
- (*i)->visit(*this);
+ a->visit(*this);
argument_ids.push_back(r_expression_result_id);
all_args_const &= r_constant_result;
}
else if(call.declaration->source==BUILTIN_SOURCE)
{
string arg_types;
- for(NodeArray<Expression>::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
- if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>((*i)->type))
+ for(const RefPtr<Expression> &a: call.arguments)
+ if(BasicTypeDeclaration *basic_arg = dynamic_cast<BasicTypeDeclaration *>(a->type))
{
BasicTypeDeclaration &elem_arg = *get_element_type(*basic_arg);
switch(elem_arg.kind)
{
r_expression_result_id = begin_expression(OP_FUNCTION_CALL, result_type_id, 1+call.arguments.size());
writer.write(get_id(*call.declaration->definition));
- for(vector<Id>::const_iterator i=argument_ids.begin(); i!=argument_ids.end(); ++i)
- writer.write(*i);
+ for(Id i: argument_ids)
+ writer.write(i);
end_expression(OP_FUNCTION_CALL);
// Any global variables the called function uses might have changed value
set<Node *> dependencies = DependencyCollector().apply(*call.declaration->definition);
- for(set<Node *>::const_iterator i=dependencies.begin(); i!=dependencies.end(); ++i)
- if(const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(*i))
+ for(Node *n: dependencies)
+ if(const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(n))
variable_load_ids.erase(var);
}
}
writer.write(ext_id);
writer.write(opcode);
writer.write(get_id(*var->declaration));
- for(vector<Id>::const_iterator i=argument_ids.begin(); ++i!=argument_ids.end(); )
+ for(auto i=argument_ids.begin(); ++i!=argument_ids.end(); )
writer.write(*i);
end_expression(OP_EXT_INST);
}
bool SpirVGenerator::check_duplicate_type(TypeDeclaration &type)
{
- for(map<Node *, Declaration>::const_iterator i=declared_ids.begin(); i!=declared_ids.end(); ++i)
- if(TypeDeclaration *type2 = dynamic_cast<TypeDeclaration *>(i->first))
+ for(const auto &kvp: declared_ids)
+ if(TypeDeclaration *type2 = dynamic_cast<TypeDeclaration *>(kvp.first))
if(TypeComparer().apply(type, *type2))
{
- insert_unique(declared_ids, &type, i->second);
+ insert_unique(declared_ids, &type, kvp.second);
return true;
}
bool builtin = (strct.interface_block && !strct.interface_block->block_name.compare(0, 3, "gl_"));
vector<Id> member_type_ids;
member_type_ids.reserve(strct.members.body.size());
- for(NodeList<Statement>::const_iterator i=strct.members.body.begin(); i!=strct.members.body.end(); ++i)
+ for(const RefPtr<Statement> &s: strct.members.body)
{
- const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(i->get());
+ const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(s.get());
if(!var)
continue;
{
if(var->layout)
{
- const vector<Layout::Qualifier> &qualifiers = var->layout->qualifiers;
- for(vector<Layout::Qualifier>::const_iterator j=qualifiers.begin(); j!=qualifiers.end(); ++j)
+ for(const Layout::Qualifier &q: var->layout->qualifiers)
{
- if(j->name=="offset")
- writer.write_op_member_decorate(type_id, index, DECO_OFFSET, j->value);
- else if(j->name=="column_major")
+ if(q.name=="offset")
+ writer.write_op_member_decorate(type_id, index, DECO_OFFSET, q.value);
+ else if(q.name=="column_major")
writer.write_op_member_decorate(type_id, index, DECO_COL_MAJOR);
- else if(j->name=="row_major")
+ else if(q.name=="row_major")
writer.write_op_member_decorate(type_id, index, DECO_ROW_MAJOR);
}
}
writer.begin_op(content.globals, OP_TYPE_STRUCT);
writer.write(type_id);
- for(vector<Id>::const_iterator i=member_type_ids.begin(); i!=member_type_ids.end(); ++i)
- writer.write(*i);
+ for(Id i: member_type_ids)
+ writer.write(i);
writer.end_op(OP_TYPE_STRUCT);
}
int spec_id = -1;
if(layout_ql)
{
- for(vector<Layout::Qualifier>::const_iterator i=layout_ql->begin(); (spec_id<0 && i!=layout_ql->end()); ++i)
- if(i->name=="constant_id")
- spec_id = i->value;
+ auto i = find_member(*layout_ql, string("constant_id"), &Layout::Qualifier::name);
+ if(i!=layout_ql->end())
+ spec_id = i->value;
}
Id type_id = get_variable_type_id(var);
if(layout_ql)
{
- for(vector<Layout::Qualifier>::const_iterator i=layout_ql->begin(); i!=layout_ql->end(); ++i)
+ for(const Layout::Qualifier &q: *layout_ql)
{
- if(i->name=="location")
- writer.write_op_decorate(var_id, DECO_LOCATION, i->value);
- else if(i->name=="set")
- writer.write_op_decorate(var_id, DECO_DESCRIPTOR_SET, i->value);
- else if(i->name=="binding")
- writer.write_op_decorate(var_id, DECO_BINDING, i->value);
+ if(q.name=="location")
+ writer.write_op_decorate(var_id, DECO_LOCATION, q.value);
+ else if(q.name=="set")
+ writer.write_op_decorate(var_id, DECO_DESCRIPTOR_SET, q.value);
+ else if(q.name=="binding")
+ writer.write_op_decorate(var_id, DECO_BINDING, q.value);
}
}
if(iface.layout)
{
- const vector<Layout::Qualifier> &qualifiers = iface.layout->qualifiers;
- for(vector<Layout::Qualifier>::const_iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i)
- if(i->name=="binding")
- writer.write_op_decorate(block_id, DECO_BINDING, i->value);
+ auto i = find_member(iface.layout->qualifiers, string("binding"), &Layout::Qualifier::name);
+ if(i!=iface.layout->qualifiers.end())
+ writer.write_op_decorate(block_id, DECO_BINDING, i->value);
}
}
writer.write_string(func.name);
set<Node *> dependencies = DependencyCollector().apply(func);
- for(set<Node *>::const_iterator i=dependencies.begin(); i!=dependencies.end(); ++i)
+ for(Node *n: dependencies)
{
- if(const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(*i))
+ if(const VariableDeclaration *var = dynamic_cast<const VariableDeclaration *>(n))
{
if(!var->interface.empty())
- writer.write(get_id(**i));
+ writer.write(get_id(*n));
}
- else if(dynamic_cast<InterfaceBlock *>(*i))
- writer.write(get_id(**i));
+ else if(dynamic_cast<InterfaceBlock *>(n))
+ writer.write(get_id(*n));
}
writer.end_op(OP_ENTRY_POINT);
else if(stage->type==Stage::GEOMETRY)
use_capability(CAP_GEOMETRY);
- for(vector<const InterfaceLayout *>::const_iterator i=interface_layouts.begin(); i!=interface_layouts.end(); ++i)
+ for(const InterfaceLayout *i: interface_layouts)
{
- const vector<Layout::Qualifier> &qualifiers = (*i)->layout.qualifiers;
- for(vector<Layout::Qualifier>::const_iterator j=qualifiers.begin(); j!=qualifiers.end(); ++j)
+ for(const Layout::Qualifier &q: i->layout.qualifiers)
{
- if(j->name=="point")
+ if(q.name=="point")
writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id,
- ((*i)->interface=="in" ? EXEC_INPUT_POINTS : EXEC_OUTPUT_POINTS));
- else if(j->name=="lines")
+ (i->interface=="in" ? EXEC_INPUT_POINTS : EXEC_OUTPUT_POINTS));
+ else if(q.name=="lines")
writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_LINES);
- else if(j->name=="lines_adjacency")
+ else if(q.name=="lines_adjacency")
writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_LINES_ADJACENCY);
- else if(j->name=="triangles")
+ else if(q.name=="triangles")
writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_TRIANGLES);
- else if(j->name=="triangles_adjacency")
+ else if(q.name=="triangles_adjacency")
writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_INPUT_TRIANGLES_ADJACENCY);
- else if(j->name=="line_strip")
+ else if(q.name=="line_strip")
writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_LINE_STRIP);
- else if(j->name=="triangle_strip")
+ else if(q.name=="triangle_strip")
writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_TRIANGLE_STRIP);
- else if(j->name=="max_vertices")
- writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_VERTICES, j->value);
+ else if(q.name=="max_vertices")
+ writer.write_op(content.exec_modes, OP_EXECUTION_MODE, func_id, EXEC_OUTPUT_VERTICES, q.value);
}
}
}
Id return_type_id = get_id(*func.return_type_declaration);
vector<unsigned> param_type_ids;
param_type_ids.reserve(func.parameters.size());
- for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
- param_type_ids.push_back(get_variable_type_id(**i));
+ for(const RefPtr<VariableDeclaration> &p: func.parameters)
+ param_type_ids.push_back(get_variable_type_id(*p));
string sig_with_return = func.return_type+func.signature;
Id &type_id = function_type_ids[sig_with_return];
writer.begin_op(content.globals, OP_TYPE_FUNCTION);
writer.write(type_id);
writer.write(return_type_id);
- for(vector<unsigned>::const_iterator i=param_type_ids.begin(); i!=param_type_ids.end(); ++i)
- writer.write(*i);
+ for(unsigned i: param_type_ids)
+ writer.write(i);
writer.end_op(OP_TYPE_FUNCTION);
writer.write_op_name(type_id, sig_with_return);
+#include <msp/core/algorithm.h>
#include <msp/core/maputils.h>
#include "syntax.h"
#include "visitor.h"
NodeContainer<C>::NodeContainer(const NodeContainer &c):
C(c)
{
- for(typename C::iterator i=this->begin(); i!=this->end(); ++i)
- *i = (*i)->clone();
+ for(auto &i: *this)
+ i = i->clone();
}
int get_layout_value(const Layout &layout, const string &name, int def_value)
{
- for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
- if(i->name==name)
- return i->value;
- return def_value;
+ auto i = find_member(layout.qualifiers, name, &Layout::Qualifier::name);
+ return (i!=layout.qualifiers.end() ? i->value : def_value);
}
void add_to_chain(Assignment::Target &target, Assignment::Target::ChainType type, unsigned index)
{
SetForScope<deque<string> > clear_tokens(next_tokens, deque<string>());
- string::const_iterator line_end = iter;
+ auto line_end = iter;
for(; (line_end!=source_end && *line_end!='\n'); ++line_end) ;
SetForScope<string::const_iterator> stop_at_line_end(source_end, line_end);
void DeclarationValidator::visit(Layout &layout)
{
- for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
+ for(const Layout::Qualifier &q: layout.qualifiers)
{
bool allowed = false;
string err_descr;
bool value = true;
- if(i->name=="location")
+ if(q.name=="location")
allowed = (variable && scope==GLOBAL);
- else if(i->name=="binding" || i->name=="set")
+ else if(q.name=="binding" || q.name=="set")
{
- if(i->name=="set")
+ if(q.name=="set")
{
error(layout, "Layout qualifier 'set' not allowed when targeting OpenGL");
continue;
err_descr = "non-uniform interface block";
}
}
- else if(i->name=="constant_id")
+ else if(q.name=="constant_id")
{
allowed = (variable && scope==GLOBAL);
if(allowed)
}
}
}
- else if(i->name=="offset")
+ else if(q.name=="offset")
allowed = (variable && scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
- else if(i->name=="align")
+ else if(q.name=="align")
allowed = (scope==INTERFACE_BLOCK && iface_block->interface=="uniform");
- else if(i->name=="points")
+ else if(q.name=="points")
{
allowed = (stage->type==Stage::GEOMETRY && iface_layout && (iface_layout->interface=="in" || iface_layout->interface=="out"));
value = false;
}
- else if(i->name=="lines" || i->name=="lines_adjacency" || i->name=="triangles" || i->name=="triangles_adjacency")
+ else if(q.name=="lines" || q.name=="lines_adjacency" || q.name=="triangles" || q.name=="triangles_adjacency")
{
allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
value = false;
}
- else if(i->name=="line_strip" || i->name=="triangle_strip")
+ else if(q.name=="line_strip" || q.name=="triangle_strip")
{
allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
value = false;
}
- else if(i->name=="invocations")
+ else if(q.name=="invocations")
allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="in");
- else if(i->name=="max_vertices")
+ else if(q.name=="max_vertices")
allowed = (stage->type==Stage::GEOMETRY && iface_layout && iface_layout->interface=="out");
- else if(i->name=="std140" || i->name=="std430")
+ else if(q.name=="std140" || q.name=="std430")
{
allowed = (iface_block && !variable && iface_block->interface=="uniform");
value = false;
}
- else if(i->name=="column_major" || i->name=="row_major")
+ else if(q.name=="column_major" || q.name=="row_major")
{
allowed = (variable && scope==INTERFACE_BLOCK);
if(allowed)
else
err_descr = "unknown declaration";
}
- error(layout, format("Layout qualifier '%s' not allowed on %s", i->name, err_descr));
+ error(layout, format("Layout qualifier '%s' not allowed on %s", q.name, err_descr));
}
- else if(value && !i->has_value)
- error(layout, format("Layout qualifier '%s' requires a value", i->name));
- else if(!value && i->has_value)
- error(layout, format("Layout qualifier '%s' does not allow a value", i->name));
+ else if(value && !q.has_value)
+ error(layout, format("Layout qualifier '%s' requires a value", q.name));
+ else if(!value && q.has_value)
+ error(layout, format("Layout qualifier '%s' does not allow a value", q.name));
}
}
void DeclarationValidator::visit(FunctionDeclaration &func)
{
SetForScope<ScopeType> set_scope(scope, FUNCTION_PARAM);
- for(NodeArray<VariableDeclaration>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
- (*i)->visit(*this);
+ for(const RefPtr<VariableDeclaration> &p: func.parameters)
+ p->visit(*this);
scope = FUNCTION;
func.body.visit(*this);
}
Statement *IdentifierValidator::find_definition(const string &name)
{
BlockDeclarationMap *decls = &declarations[current_block];
- BlockDeclarationMap::const_iterator i = decls->find(name);
+ auto i = decls->find(name);
if(i==decls->end() && anonymous_block)
{
decls = &declarations[current_block->parent];
void IdentifierValidator::visit(InterfaceBlock &iface)
{
string key = format("%s %s", iface.interface, iface.block_name);
- map<string, InterfaceBlock *>::const_iterator i = interface_blocks.find(key);
+ auto i = interface_blocks.find(key);
if(i!=interface_blocks.end())
multiple_definition(format("interface block '%s %s'", iface.interface, iface.block_name), iface, *i->second);
else
if(iface.instance_name.empty() && iface.struct_declaration)
{
// Inject anonymous interface block members into the global scope
- const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
- for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
- check_definition(j->first, *j->second);
+ for(const auto &kvp: iface.struct_declaration->members.variables)
+ check_definition(kvp.first, *kvp.second);
}
}
void IdentifierValidator::visit(FunctionDeclaration &func)
{
string key = func.name+func.signature;
- map<string, FunctionDeclaration *>::const_iterator i = overloaded_functions.find(key);
+ auto i = overloaded_functions.find(key);
if(i==overloaded_functions.end())
overloaded_functions.insert(make_pair(key, &func));
else if(func.return_type_declaration && i->second->return_type_declaration!=func.return_type_declaration)
bool have_declaration = call.constructor;
if(!call.constructor)
{
- map<string, FunctionDeclaration *>::iterator i = stage->functions.lower_bound(call.name);
+ auto i = stage->functions.lower_bound(call.name);
have_declaration = (i!=stage->functions.end() && i->second->name==call.name);
}
{
bool valid_types = true;
string signature;
- for(NodeArray<Expression>::const_iterator j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
+ for(auto j=call.arguments.begin(); (valid_types && j!=call.arguments.end()); ++j)
{
if((*j)->type)
append(signature, ", ", (*j)->type->name);
void FlowControlValidator::visit(Block &block)
{
- for(NodeList<Statement>::const_iterator i=block.body.begin(); i!=block.body.end(); ++i)
+ for(const RefPtr<Statement> &s: block.body)
{
if(!reachable)
{
- diagnose(**i, Diagnostic::WARN, "Unreachable code detected");
+ diagnose(*s, Diagnostic::WARN, "Unreachable code detected");
break;
}
- (*i)->visit(*this);
+ s->visit(*this);
}
}
int StageInterfaceValidator::get_location(const Layout &layout)
{
- for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
- if(i->name=="location")
- return i->value;
- return -1;
+ return get_layout_value(layout, "location", -1);
}
void StageInterfaceValidator::visit(VariableDeclaration &var)
unsigned loc_count = LocationCounter().apply(var);
for(unsigned i=0; i<loc_count; ++i)
{
- map<unsigned, VariableDeclaration *>::const_iterator j = used.find(location+i);
+ auto j = used.find(location+i);
if(j!=used.end())
{
error(var, format("Overlapping location %d for '%s %s'", location+i, var.interface, var.name));
void GlobalInterfaceValidator::apply(Module &module)
{
- for(list<Stage>::iterator i=module.stages.begin(); i!=module.stages.end(); ++i)
+ for(Stage &s: module.stages)
{
- stage = &*i;
- i->content.visit(*this);
+ stage = &s;
+ s.content.visit(*this);
}
}
void GlobalInterfaceValidator::check_uniform(const Uniform &uni)
{
- map<string, const Uniform *>::const_iterator i = used_names.find(uni.name);
+ auto i = used_names.find(uni.name);
if(i!=used_names.end())
{
if(uni.location>=0 && i->second->location>=0 && i->second->location!=uni.location)
if(uni.location>=0)
{
- map<unsigned, const Uniform *>::const_iterator j = used_locations.find(uni.location);
+ auto j = used_locations.find(uni.location);
if(j!=used_locations.end())
{
if(j->second->name!=uni.name)
if(uni.bind_point>=0)
{
map<unsigned, const Uniform *> &used = used_bindings[uni.desc_set];
- map<unsigned, const Uniform *>::const_iterator j = used.find(uni.bind_point);
+ auto j = used.find(uni.bind_point);
if(j!=used.end())
{
if(j->second->name!=uni.name)
if(&block!=current_block)
enter(block);
SetForScope<Block *> set_block(current_block, &block);
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
- (*i)->visit(*this);
+ for(const RefPtr<Statement> &s: block.body)
+ s->visit(*this);
}
void TraversingVisitor::visit(RefPtr<Expression> &expr)
void TraversingVisitor::visit(FunctionCall &call)
{
- for(NodeArray<Expression>::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i)
- visit(*i);
+ for(RefPtr<Expression> &a: call.arguments)
+ visit(a);
}
void TraversingVisitor::visit(ExpressionStatement &expr)
{
enter(func.body);
SetForScope<Block *> set_block(current_block, &func.body);
- for(NodeArray<VariableDeclaration>::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
- (*i)->visit(*this);
+ for(const RefPtr<VariableDeclaration> &p: func.parameters)
+ p->visit(*this);
func.body.visit(*this);
}
template<typename T>
void NodeRemover::remove_from_map(map<string, T *> &vars, const string &key, T &node)
{
- typename map<string, T *>::iterator i = vars.find(key);
+ auto i = vars.find(key);
if(i!=vars.end() && i->second==&node)
vars.erase(i);
}
void NodeRemover::visit(Block &block)
{
SetForScope<Block *> set_block(current_block, &block);
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
+ for(auto i=block.body.begin(); i!=block.body.end(); )
{
(*i)->visit(*this);
if(to_remove->count(i->get()))
void NodeReorderer::visit(Block &block)
{
- NodeList<Statement>::iterator insert_point = block.body.end();
- for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); )
+ auto insert_point = block.body.end();
+ for(auto i=block.body.begin(); i!=block.body.end(); )
{
(*i)->visit(*this);
if(insert_point!=block.body.end() && to_reorder->count(i->get()))
- {
- NodeList<Statement>::iterator j = i++;
- block.body.splice(insert_point, block.body, j);
- }
+ block.body.splice(insert_point, block.body, i++);
else
{
if(i->get()==reorder_before)
void Lighting::detach(const Light &l)
{
- vector<AttachedLight>::iterator i = find_member(lights, &l, &AttachedLight::light);
+ auto i = find_member(lights, &l, &AttachedLight::light);
if(i!=lights.end())
lights.erase(i);
}
map<string, int> spec_values;
fill_program_info(module_name, spec_values);
- for(map<string, int>::const_iterator i=extra_spec.begin(); i!=extra_spec.end(); ++i)
- spec_values[i->first] = i->second;
+ for(const auto &kvp: extra_spec)
+ spec_values[kvp.first] = kvp.second;
string info = module_name;
- for(map<string, int>::const_iterator i=spec_values.begin(); i!=spec_values.end(); ++i)
- info += format(",%s:%d", i->first, i->second);
+ for(const auto &kvp: spec_values)
+ info += format(",%s:%d", kvp.first, kvp.second);
Resources &res = Resources::get_global();
string name = format("_material_%016x.shader", hash64(info));
Tag RenderPass::get_slotted_uniform_tag(Tag slot) const
{
- map<Tag, Tag>::const_iterator i = uniform_slots.find(slot);
+ auto i = uniform_slots.find(slot);
if(i==uniform_slots.end())
return Tag();
return i->second;
void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
{
- vector<TextureSlot>::iterator i = find_member(textures, tag, &TextureSlot::tag);
+ auto i = find_member(textures, tag, &TextureSlot::tag);
if(i==textures.end())
{
textures.push_back(TextureSlot(tag));
Tag RenderPass::get_texture_tag(const string &slot) const
{
- vector<TextureSlot>::const_iterator i = find_member(textures, slot, &TextureSlot::slot_name);
+ auto i = find_member(textures, slot, &TextureSlot::slot_name);
return (i!=textures.end() ? i->tag : Tag());
}
throw invalid_operation("RenderPass::set_texture");
const vector<Program::UniformInfo> &uniforms = shprog->get_uniforms();
- for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- if(is_image(i->type) && i->binding==static_cast<int>(index))
- return set_texture(i->tag, tex, samp);
+ for(const Program::UniformInfo &u: uniforms)
+ if(is_image(u.type) && u.binding==static_cast<int>(index))
+ return set_texture(u.tag, tex, samp);
if(shdata)
{
- const vector<Tag> &tags = shdata->get_uniform_tags();
- for(vector<Tag>::const_iterator i=tags.begin(); i!=tags.end(); ++i)
+ for(Tag t: shdata->get_uniform_tags())
{
- vector<Program::UniformInfo>::const_iterator j = find_member(uniforms, *i, &Program::UniformInfo::tag);
+ auto j = find_member(uniforms, t, &Program::UniformInfo::tag);
if(j==uniforms.end() || !is_image(j->type))
continue;
- if(const Uniform1i *uni1i = dynamic_cast<const Uniform1i *>(shdata->find_uniform(*i)))
+ if(const Uniform1i *uni1i = dynamic_cast<const Uniform1i *>(shdata->find_uniform(t)))
if(uni1i->get()==static_cast<int>(index))
- return set_texture(*i, tex, samp);
+ return set_texture(t, tex, samp);
}
}
}
int RenderPass::get_texture_index(const string &n) const
{
- vector<TextureSlot>::const_iterator i = find_member(textures, n, &TextureSlot::slot_name);
+ auto i = find_member(textures, n, &TextureSlot::slot_name);
return (shprog && i!=textures.end() ? shprog->get_uniform_binding(i->tag) : -1);
}
void RenderPass::apply(Renderer &renderer) const
{
- for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
- renderer.set_texture(i->tag, i->texture, i->sampler);
+ for(const TextureSlot &t: textures)
+ renderer.set_texture(t.tag, t.texture, t.sampler);
renderer.set_shader_program(shprog, shdata.get());
if(material)
renderer.add_shader_data(material->get_shader_data());
void RenderPass::Loader::texture(const string &n)
{
- vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
+ auto i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
if(i==obj.textures.end())
{
obj.textures.push_back(TextureSlot(n));
string name;
if(obj.shprog)
{
- const vector<Program::UniformInfo> &uniforms = obj.shprog->get_uniforms();
- for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- if(is_image(i->type) && i->binding>=0)
+ for(const Program::UniformInfo &u: obj.shprog->get_uniforms())
+ if(is_image(u.type) && u.binding>=0)
{
if(!name.empty())
{
name.clear();
break;
}
- name = i->name;
+ name = u.name;
}
}
const RenderPass *Technique::find_pass(Tag tag) const
{
- PassMap::const_iterator i = passes.find(tag);
+ auto i = passes.find(tag);
return (i!=passes.end() ? &i->second : 0);
}
bool Technique::replace_texture(const string &slot, const Texture &tex)
{
bool replaced = false;
- for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
+ for(auto &kvp: passes)
{
- Tag tag = i->second.get_texture_tag(slot);
+ Tag tag = kvp.second.get_texture_tag(slot);
if(tag.id)
{
- i->second.set_texture(tag, &tex);
+ kvp.second.set_texture(tag, &tex);
replaced = true;
}
}
bool Technique::replace_material(const string &slot, const Material &mat)
{
bool replaced = false;
- for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
+ for(auto &kvp: passes)
{
- const string &pass_slot = i->second.get_material_slot_name();
+ const string &pass_slot = kvp.second.get_material_slot_name();
if(!pass_slot.empty() && pass_slot==slot)
{
- i->second.set_material(&mat);
+ kvp.second.set_material(&mat);
replaced = true;
}
}
{
bool replaced = false;
const vector<Tag> &uniform_tags = shdata.get_uniform_tags();
- for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i)
+ for(auto &kvp: passes)
{
RefPtr<ProgramData> new_shdata;
- for(vector<Tag>::const_iterator j=uniform_tags.begin(); j!=uniform_tags.end(); ++j)
+ for(Tag t: uniform_tags)
{
- Tag tag = i->second.get_slotted_uniform_tag(*j);
+ Tag tag = kvp.second.get_slotted_uniform_tag(t);
if(!tag.id)
continue;
if(!new_shdata)
- new_shdata = new ProgramData(*i->second.get_shader_data());
+ new_shdata = new ProgramData(*kvp.second.get_shader_data());
- new_shdata->uniform(tag, shdata.get_uniform(*j));
+ new_shdata->uniform(tag, shdata.get_uniform(t));
replaced = true;
}
if(new_shdata)
- i->second.set_shader_program(i->second.get_shader_program(), new_shdata.get());
+ kvp.second.set_shader_program(kvp.second.get_shader_program(), new_shdata.get());
}
return replaced;
bool Technique::has_shaders() const
{
- for(PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
- if(i->second.get_shader_program())
+ for(const auto &kvp: passes)
+ if(kvp.second.get_shader_program())
return true;
return false;
}
void Technique::set_debug_name(const std::string &name)
{
#ifdef DEBUG
- for(map<Tag, RenderPass>::iterator i=passes.begin(); i!=passes.end(); ++i)
- i->second.set_debug_name(format("%s [pass:%s]", name, i->first.str()));
+ for(auto &kvp: passes)
+ kvp.second.set_debug_name(format("%s [pass:%s]", name, kvp.first.str()));
#else
(void)name;
#endif
RenderPass &pass = get_item(obj.passes, slot);
if(const Material *base_mat = pass.get_material())
{
- for(PassMap::iterator i=obj.passes.begin(); i!=obj.passes.end(); ++i)
- if(i->second.get_material()==base_mat)
- i->second.set_material(&mat);
+ for(auto &kvp: obj.passes)
+ if(kvp.second.get_material()==base_mat)
+ kvp.second.set_material(&mat);
}
else
pass.set_material(&mat);
void uniforms();
};
-public:
- typedef std::map<Tag, RenderPass> PassMap;
-
-private:
- PassMap passes;
+ std::map<Tag, RenderPass> passes;
public:
RenderPass &add_pass(Tag);
bool has_pass(Tag) const;
const RenderPass &get_pass(Tag) const;
const RenderPass *find_pass(Tag) const;
- const PassMap &get_passes() const { return passes; }
+ const std::map<Tag, RenderPass> &get_passes() const { return passes; }
bool replace_texture(const std::string &, const Texture &);
bool replace_material(const std::string &, const Material &);
bool replace_uniforms(const ProgramData &);
matrix_offset(0)
{
const Technique *tech = object.get_technique();
- const Technique::PassMap &passes = tech->get_passes();
- for(Technique::PassMap::const_iterator i=passes.begin(); i!=passes.end(); ++i)
+ for(const auto &kvp: tech->get_passes())
{
- const Program *shprog = i->second.get_shader_program();
+ const Program *shprog = kvp.second.get_shader_program();
if(!shprog)
throw invalid_argument("InstanceArray::InstanceArray");
InstanceArray::~InstanceArray()
{
- for(vector<ObjectInstance *>::iterator i=instances.begin(); i!=instances.end(); ++i)
- delete *i;
+ for(ObjectInstance *i: instances)
+ delete i;
delete vtx_setup;
delete instance_data;
delete instance_buffer;
void InstanceArray::remove(ObjectInstance &inst)
{
- vector<ObjectInstance *>::iterator i = find(instances, &inst);
+ auto i = find(instances, &inst);
if(i==instances.end())
throw key_error(&inst);
}
else
{
- for(vector<ObjectInstance *>::const_iterator i=instances.begin(); i!=instances.end(); ++i)
+ for(ObjectInstance *i: instances)
{
- const Matrix &m = *(*i)->get_matrix();
+ const Matrix &m = *i->get_matrix();
for(unsigned j=0; j<3; ++j)
glVertexAttrib4f(matrix_location+j, m(j, 0), m(j, 1), m(j, 2), m(j, 3));
- (*i)->render(renderer, tag);
+ i->render(renderer, tag);
}
}
}
void Object::update_bounding_sphere()
{
vector<Vector3> points;
- for(vector<LevelOfDetail>::const_iterator i=lods.begin(); i!=lods.end(); ++i)
+ for(const LevelOfDetail &l: lods)
{
- if(!i->mesh || !i->mesh->is_loaded())
+ if(!l.mesh || !l.mesh->is_loaded())
continue;
- const VertexArray &vertices = i->mesh->get_vertices();
+ const VertexArray &vertices = l.mesh->get_vertices();
int offset = vertices.get_format().offset(VERTEX3);
bool three = true;
{
vector<unsigned> queries;
queries.reserve(occluded_cache.size());
- for(OccludedArray::iterator i=occluded_cache.begin(); i!=occluded_cache.end(); ++i)
- queries.push_back(i->query);
+ for(OccludedRenderable &o: occluded_cache)
+ queries.push_back(o.query);
glDeleteQueries(queries.size(), &queries[0]);
}
occluded_cache[old_size+i].query = new_queries[i];
}
- OccludedArray::iterator j = occluded_cache.begin();
- for(RenderableSet::iterator i=renderables.begin(); i!=renderables.end(); ++i, ++j)
- j->renderable = *i;
+ auto j = occluded_cache.begin();
+ for(Renderable *r: renderables)
+ j++->renderable = r;
for(; j!=occluded_cache.end(); ++j)
{
j->renderable = 0;
void OccludedScene::setup_frame(Renderer &renderer)
{
populate_cache();
- for(OccludedArray::const_iterator i=occluded_cache.begin(); i!=occluded_cache.end(); ++i)
- i->renderable->setup_frame(renderer);
+ for(const OccludedRenderable &o: occluded_cache)
+ o.renderable->setup_frame(renderer);
}
void OccludedScene::finish_frame()
{
- for(OccludedArray::const_iterator i=occluded_cache.begin(); i!=occluded_cache.end(); ++i)
- i->renderable->finish_frame();
+ for(const OccludedRenderable &o: occluded_cache)
+ o.renderable->finish_frame();
}
void OccludedScene::render(Renderer &renderer, Tag tag) const
const Camera *camera = renderer.get_camera();
if(!camera)
{
- for(OccludedArray::const_iterator i=occluded_cache.begin(); i!=occluded_cache.end(); ++i)
- renderer.render(*i->renderable, tag);
+ for(const OccludedRenderable &o: occluded_cache)
+ renderer.render(*o.renderable, tag);
return;
}
// Perform frustum culling and render any major occluders
bool use_frustum = setup_frustum(renderer);
- for(OccludedArray::iterator i=occluded_cache.begin(); (i!=occluded_cache.end() && i->renderable); ++i)
+ for(auto i=occluded_cache.begin(); (i!=occluded_cache.end() && i->renderable); ++i)
{
i->in_frustum = (!use_frustum || !frustum_cull(*i->renderable));
if(!i->in_frustum)
}
// Move all objects within the frustum to the beginning of the array
- for(OccludedArray::iterator i=occluded_cache.begin(), j=i+renderables.size()-1; i!=j; )
+ for(auto i=occluded_cache.begin(), j=i+renderables.size()-1; i!=j; )
{
if(i->in_frustum)
++i;
glDepthMask(false);
// Fire off the occlusion queries
- for(OccludedArray::const_iterator i=occluded_cache.begin(); (i!=occluded_cache.end() && i->in_frustum); ++i)
+ for(auto i=occluded_cache.begin(); (i!=occluded_cache.end() && i->in_frustum); ++i)
if(!i->occluder)
{
glBeginQuery(GL_ANY_SAMPLES_PASSED, i->query);
}
// Render anything that has a chance of being visible
- for(OccludedArray::const_iterator i=occluded_cache.begin(); (i!=occluded_cache.end() && i->in_frustum); ++i)
+ for(auto i=occluded_cache.begin(); (i!=occluded_cache.end() && i->in_frustum); ++i)
if(!i->occluder)
{
unsigned any_passed = 0;
OccludedRenderable();
};
- typedef std::set<Renderable *> RenderableSet;
- typedef std::vector<OccludedRenderable> OccludedArray;
-
const Mesh &bounding_mesh;
const Program &bounding_shader;
- RenderableSet renderables;
+ std::set<Renderable *> renderables;
float occluder_min_size;
- mutable OccludedArray occluded_cache;
+ mutable std::vector<OccludedRenderable> occluded_cache;
mutable bool cache_dirty;
public:
-#include <algorithm>
+#include <msp/core/algorithm.h>
#include "orderedscene.h"
#include "renderer.h"
void OrderedScene::remove(Renderable &r)
{
- RenderableList::iterator end = std::remove(renderables.begin(), renderables.end(), &r);
+ auto end = std::remove(renderables.begin(), renderables.end(), &r);
renderables.erase(end, renderables.end());
}
void OrderedScene::insert(unsigned index, Renderable &r)
{
- RenderableList::iterator i = renderables.begin();
+ auto i = renderables.begin();
for(; (i!=renderables.end() && index); ++i, --index) ;
renderables.insert(i, &r);
}
void OrderedScene::insert_after(Renderable &after, Renderable &r)
{
- RenderableList::iterator i = renderables.begin();
- for(; (i!=renderables.end() && *i!=&after); ++i) ;
+ auto i = find(renderables, &after);
renderables.insert(i, &r);
}
void OrderedScene::setup_frame(Renderer &renderer)
{
- for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
- (*i)->setup_frame(renderer);
+ for(Renderable *r: renderables)
+ r->setup_frame(renderer);
}
void OrderedScene::finish_frame()
{
- for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
- (*i)->finish_frame();
+ for(Renderable *r: renderables)
+ r->finish_frame();
}
void OrderedScene::render(Renderer &renderer, Tag tag) const
{
if(setup_frustum(renderer))
{
- for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
- if(!frustum_cull(**i))
- renderer.render(**i, tag);
+ for(Renderable *r: renderables)
+ if(!frustum_cull(*r))
+ renderer.render(*r, tag);
}
else
{
- for(RenderableList::const_iterator i=renderables.begin(); i!=renderables.end(); ++i)
- renderer.render(**i, tag);
+ for(Renderable *r: renderables)
+ renderer.render(*r, tag);
}
}
using Scene::Loader;
private:
- typedef std::list<Renderable *> RenderableList;
-
- RenderableList renderables;
+ std::list<Renderable *> renderables;
public:
virtual void add(Renderable &);
buffer(0),
dirty(0)
{
- for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- i->value = i->value->clone();
+ for(TaggedUniform &u: uniforms)
+ u.value = u.value->clone();
}
ProgramData::ProgramData(const ProgramData &other, const Program *p):
{
if(tied_program)
{
- for(vector<TaggedUniform>::const_iterator i=other.uniforms.begin(); i!=other.uniforms.end(); ++i)
- validate_tag(i->tag);
+ for(const TaggedUniform &u: other.uniforms)
+ validate_tag(u.tag);
}
uniforms = other.uniforms;
- for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- i->value = i->value->clone();
+ for(TaggedUniform &u: uniforms)
+ u.value = u.value->clone();
}
ProgramData &ProgramData::operator=(const ProgramData &other)
tied_program = other.tied_program;
uniforms = other.uniforms;
- for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- i->value = i->value->clone();
+ for(TaggedUniform &u: uniforms)
+ u.value = u.value->clone();
- for(vector<SharedBlock>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
- delete i->block;
+ for(SharedBlock &b: blocks)
+ delete b.block;
programs.clear();
last_buffer_block = 0;
ProgramData::~ProgramData()
{
- for(vector<TaggedUniform>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- delete i->value;
- for(vector<SharedBlock>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
+ for(TaggedUniform &u: uniforms)
+ delete u.value;
+ for(SharedBlock &b: blocks)
{
- if(i->indices.type_flag==0xFE)
- delete[] i->indices.dynamic.values;
- delete i->block;
+ if(b.indices.type_flag==0xFE)
+ delete[] b.indices.dynamic.values;
+ delete b.block;
}
delete buffer;
}
throw too_many_uniforms(tag.str());
}
- vector<TaggedUniform>::iterator j = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
+ auto j = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
TaggedUniform nu;
nu.tag = tag;
void ProgramData::remove_uniform(Tag tag)
{
- vector<TaggedUniform>::const_iterator i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
+ auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
if(i==uniforms.end() || i->tag!=tag)
return;
{
vector<Tag> tags;
tags.reserve(uniforms.size());
- for(vector<TaggedUniform>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- tags.push_back(i->tag);
+ for(const TaggedUniform &u: uniforms)
+ tags.push_back(u.tag);
return tags;
}
int ProgramData::find_uniform_index(Tag tag) const
{
- vector<TaggedUniform>::const_iterator i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
+ auto i = lower_bound_member(uniforms, tag, &TaggedUniform::tag);
return ((i!=uniforms.end() && i->tag==tag) ? i-uniforms.begin() : -1);
}
vector<ProgramData::ProgramBlock>::iterator ProgramData::get_program(const Program &prog) const
{
Program::LayoutHash prog_hash = prog.get_uniform_layout_hash();
- vector<ProgramBlock>::iterator i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash);
+ auto i = lower_bound_member(programs, prog_hash, &ProgramBlock::prog_hash);
if(i!=programs.end() && i->prog_hash==prog_hash)
return i;
the hashes so they can be matched up later. */
vector<Program::LayoutHash> block_hashes;
block_hashes.reserve(programs.size());
- for(vector<ProgramBlock>::iterator j=programs.begin(); j!=programs.end(); ++j)
- block_hashes.push_back(j->block_index>=0 ? blocks[j->block_index].block_hash : 0);
+ for(const ProgramBlock &b: programs)
+ block_hashes.push_back(b.block_index>=0 ? blocks[b.block_index].block_hash : 0);
for(unsigned j=0; j<block_infos.size(); ++j)
{
block_hashes[index+1+j] = info.layout_hash;
programs[index+1+j].bind_point = info.bind_point;
- vector<SharedBlock>::iterator k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash);
+ auto k = lower_bound_member(blocks, info.layout_hash, &SharedBlock::block_hash);
if(k==blocks.end() || k->block_hash!=info.layout_hash)
{
k = blocks.insert(k, SharedBlock(info.layout_hash));
unsigned hash = block_hashes[j];
if(hash)
{
- vector<SharedBlock>::const_iterator k = lower_bound_member(blocks, hash, &SharedBlock::block_hash);
+ auto k = lower_bound_member(blocks, hash, &SharedBlock::block_hash);
programs[j].block_index = k-blocks.begin();
}
else
vector<ProgramData::ProgramBlock>::const_iterator ProgramData::prepare_program(const Program &prog) const
{
BufferBackedUniformBlock *old_last_block = last_buffer_block;
- vector<ProgramBlock>::iterator prog_begin = get_program(prog);
+ auto prog_begin = get_program(prog);
Mask force_dirty = (dirty==ALL_ONES ? ALL_ONES : 0U);
Mask affected = (dirty&prog_begin->masks.used) | force_dirty;
program will cause this to happen if there's any dirty uniforms. */
if(affected)
{
- for(vector<SharedBlock>::iterator i=blocks.begin(); i!=blocks.end(); ++i)
- i->dirty |= (dirty&i->used) | force_dirty;
- for(vector<ProgramBlock>::iterator i=programs.begin(); i!=programs.end(); ++i)
- if(i->block_index<0)
- i->masks.dirty |= (dirty&i->masks.used) | force_dirty;
+ for(SharedBlock &b: blocks)
+ b.dirty |= (dirty&b.used) | force_dirty;
+ for(ProgramBlock &b: programs)
+ if(b.block_index<0)
+ b.masks.dirty |= (dirty&b.masks.used) | force_dirty;
dirty = 0;
}
/* The set of uniforms has changed since this program was last used.
Refresh uniform indices within the program's blocks. */
prog_begin->masks.used = 0;
- vector<ProgramBlock>::iterator j = prog_begin+1;
- for(vector<Program::UniformBlockInfo>::const_iterator i=block_infos.begin(); i!=block_infos.end(); ++i, ++j)
+ auto j = prog_begin+1;
+ for(const Program::UniformBlockInfo &b: block_infos)
{
SharedBlock &shared = blocks[j->block_index];
if(shared.dirty==ALL_ONES)
- update_block_uniform_indices(shared, *i);
+ update_block_uniform_indices(shared, b);
prog_begin->masks.used |= shared.used;
j->block = (shared.used ? shared.block : 0);
+ ++j;
}
}
// Update the contents of all dirty blocks.
bool buffered_blocks_updated = false;
- vector<ProgramBlock>::iterator j = prog_begin+1;
- for(vector<Program::UniformBlockInfo>::const_iterator i=block_infos.begin(); i!=block_infos.end(); ++i, ++j)
+ auto j = prog_begin+1;
+ for(const Program::UniformBlockInfo &b: block_infos)
{
SharedBlock &shared = blocks[j->block_index];
if(shared.dirty)
{
- update_block(shared, *i);
+ update_block(shared, b);
shared.dirty = 0;
buffered_blocks_updated |= (j->bind_point>=0);
}
+ ++j;
}
prog_begin->masks.dirty = 0;
void ProgramData::apply(const Program &prog, PipelineState &state) const
{
- vector<ProgramBlock>::const_iterator prog_begin = prepare_program(prog);
+ auto prog_begin = prepare_program(prog);
Program::LayoutHash prog_hash = prog_begin->prog_hash;
- for(vector<ProgramBlock>::const_iterator i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
+ for(auto i=prog_begin+1; (i!=programs.end() && i->prog_hash==prog_hash); ++i)
if(i->block)
{
if(i->bind_point<0)
flush_textures();
}
- for(vector<BoundTexture>::iterator i=texture_stack.end(); i!=texture_stack.begin(); )
+ for(auto i=texture_stack.end(); i!=texture_stack.begin(); )
if((--i)->tag==tag)
{
i->replaced = texture_stack.size();
if(state->texture_count<texture_stack.size())
flush_textures();
- for(vector<BoundTexture>::const_iterator i=texture_stack.begin(); i!=texture_stack.end(); ++i)
+ for(const BoundTexture &t: texture_stack)
{
- int unit = (i->tag.id ? state->shprog->get_uniform_binding(i->tag) : i->unit);
+ int unit = (t.tag.id ? state->shprog->get_uniform_binding(t.tag) : t.unit);
if(unit>=0)
- pipeline_state.set_texture(unit, i->texture, i->sampler);
+ pipeline_state.set_texture(unit, t.texture, t.sampler);
}
bool shdata_changed = changed&SHADER_DATA;
- for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
+ for(auto i=shdata_stack.begin(); (!shdata_changed && i!=shdata_stack.end()); ++i)
shdata_changed = (i->shdata->get_generation()!=i->generation);
bool extra_shdata = (shdata_stack.size()>state->shdata_count);
{
if(extra_shdata)
shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end());
- for(vector<BoundProgramData>::const_iterator i=shdata_stack.begin(); i!=shdata_stack.end(); ++i)
+ for(const BoundProgramData &d: shdata_stack)
{
- i->shdata->apply(*state->shprog, pipeline_state);
- i->generation = i->shdata->get_generation();
+ d.shdata->apply(*state->shprog, pipeline_state);
+ d.generation = d.shdata->get_generation();
}
changed &= ~SHADER_DATA;
}
RenderTarget::~RenderTarget()
{
- for(vector<Texture *>::iterator i=textures.begin(); i!=textures.end(); ++i)
- delete *i;
+ for(Texture *t: textures)
+ delete t;
}
const Texture2D &RenderTarget::get_target_texture(unsigned i) const
Sequence::~Sequence()
{
- for(vector<PostProcStep>::iterator i=postproc.begin(); i!=postproc.end(); ++i)
- if(i->owned)
- delete i->postproc;
+ for(PostProcStep &p: postproc)
+ if(p.owned)
+ delete p.postproc;
delete target[0];
delete target[1];
delete target_ms;
void Sequence::setup_frame(Renderer &renderer)
{
- for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
- if(Renderable *renderable = i->get_renderable())
+ for(const Step &s: steps)
+ if(Renderable *renderable = s.get_renderable())
renderable->setup_frame(renderer);
}
void Sequence::finish_frame()
{
- for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
- if(Renderable *renderable = i->get_renderable())
+ for(const Step &s: steps)
+ if(Renderable *renderable = s.get_renderable())
renderable->finish_frame();
}
renderer.clear();
}
- for(vector<Step>::const_iterator i=steps.begin(); i!=steps.end(); ++i)
+ for(const Step &s: steps)
{
Renderer::Push _push2(renderer);
- renderer.set_depth_test(&i->get_depth_test());
- renderer.set_stencil_test(&i->get_stencil_test());
- renderer.set_blend(&i->get_blend());
+ renderer.set_depth_test(&s.get_depth_test());
+ renderer.set_stencil_test(&s.get_stencil_test());
+ renderer.set_blend(&s.get_blend());
- if (const Lighting *lighting = i->get_lighting())
+ if (const Lighting *lighting = s.get_lighting())
renderer.add_shader_data(lighting->get_shader_data());
- renderer.set_clipping(i->get_clipping());
+ renderer.set_clipping(s.get_clipping());
- if(const Renderable *renderable = i->get_renderable())
- renderer.render(*renderable, i->get_tag());
+ if(const Renderable *renderable = s.get_renderable())
+ renderer.render(*renderable, s.get_tag());
}
if(target[0])
void SimpleScene::setup_frame(Renderer &renderer)
{
populate_cache();
- for(RenderableArray::const_iterator i=cache.begin(); i!=cache.end(); ++i)
- (*i)->setup_frame(renderer);
+ for(Renderable *r: cache)
+ r->setup_frame(renderer);
}
void SimpleScene::finish_frame()
{
- for(RenderableArray::const_iterator i=cache.begin(); i!=cache.end(); ++i)
- (*i)->finish_frame();
+ for(Renderable *r: cache)
+ r->finish_frame();
}
void SimpleScene::render(Renderer &renderer, Tag tag) const
populate_cache();
if(setup_frustum(renderer))
{
- for(RenderableArray::const_iterator i=cache.begin(); i!=cache.end(); ++i)
- if(!frustum_cull(**i))
- renderer.render(**i, tag);
+ for(Renderable *r: cache)
+ if(!frustum_cull(*r))
+ renderer.render(*r, tag);
}
else
{
- for(RenderableArray::const_iterator i=cache.begin(); i!=cache.end(); ++i)
- renderer.render(**i, tag);
+ for(Renderable *r: cache)
+ renderer.render(*r, tag);
}
}
using Scene::Loader;
private:
- typedef std::set<Renderable *> RenderableSet;
- typedef std::vector<Renderable *> RenderableArray;
-
- RenderableSet renderables;
- mutable RenderableArray cache;
+ std::set<Renderable *> renderables;
+ mutable std::vector<Renderable *> cache;
public:
virtual void add(Renderable &);
void ZSortedScene::setup_frame(Renderer &renderer)
{
populate_cache();
- for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
- i->renderable->setup_frame(renderer);
+ for(const SortedRenderable &r: sorted_cache)
+ r.renderable->setup_frame(renderer);
}
void ZSortedScene::finish_frame()
{
- for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
- i->renderable->finish_frame();
+ for(const SortedRenderable &r: sorted_cache)
+ r.renderable->finish_frame();
}
void ZSortedScene::render(Renderer &renderer, Tag tag) const
const Camera *camera = renderer.get_camera();
if(!camera)
{
- for(SortedArray::const_iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
- renderer.render(*i->renderable, tag);
+ for(const SortedRenderable &r: sorted_cache)
+ renderer.render(*r.renderable, tag);
return;
}
float sign = 1.0f-order*2.0f;
bool use_frustum = setup_frustum(renderer);
- for(SortedArray::iterator i=sorted_cache.begin(); i!=sorted_cache.end(); ++i)
+ for(SortedRenderable &r: sorted_cache)
{
- i->in_frustum = (!use_frustum || !frustum_cull(*i->renderable));
- if(!i->in_frustum)
+ r.in_frustum = (!use_frustum || !frustum_cull(*r.renderable));
+ if(!r.in_frustum)
continue;
- if(const Matrix *matrix = i->renderable->get_matrix())
+ if(const Matrix *matrix = r.renderable->get_matrix())
{
- if(const Geometry::BoundingSphere<float, 3> *bsphere = i->renderable->get_bounding_sphere())
- i->depth = dot(*matrix*bsphere->get_center()-camera_pos, look_dir)+bsphere->get_radius()*radius_factor;
+ if(const Geometry::BoundingSphere<float, 3> *bsphere = r.renderable->get_bounding_sphere())
+ r.depth = dot(*matrix*bsphere->get_center()-camera_pos, look_dir)+bsphere->get_radius()*radius_factor;
else
- i->depth = dot(*matrix*Vector3()-camera_pos, look_dir);
- i->depth *= sign;
+ r.depth = dot(*matrix*Vector3()-camera_pos, look_dir);
+ r.depth *= sign;
}
else
- i->depth = 0;
+ r.depth = 0;
}
- for(SortedArray::iterator i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i)
+ for(auto i=sorted_cache.begin(), j=i; i!=sorted_cache.end(); ++i)
if(i->in_frustum)
{
if(i!=j)
if(j!=sorted_cache.begin() && *j<*(j-1))
{
SortedRenderable sr = *j;
- SortedArray::iterator k = j-1;
+ auto k = j-1;
*j = *k;
while(k!=sorted_cache.begin() && sr<*(k-1))
{
++j;
}
- for(SortedArray::const_iterator i=sorted_cache.begin(); (i!=sorted_cache.end() && i->in_frustum); ++i)
+ for(auto i=sorted_cache.begin(); (i!=sorted_cache.end() && i->in_frustum); ++i)
renderer.render(*i->renderable, tag);
}
bool operator<(const SortedRenderable &o) const { return depth<o.depth; }
};
- typedef std::set<Renderable *> RenderableSet;
- typedef std::vector<SortedRenderable> SortedArray;
-
- RenderableSet renderables;
+ std::set<Renderable *> renderables;
SortOrder order;
DepthReference reference;
- mutable SortedArray sorted_cache;
+ mutable std::vector<SortedRenderable> sorted_cache;
public:
ZSortedScene();
-#include <algorithm>
+#include <msp/core/algorithm.h>
#include <typeinfo>
#include <msp/debug/demangle.h>
#include <msp/strings/format.h>
if(async_loads)
{
managed.state = ManagedResource::LOAD_QUEUED;
- LoadQueue::iterator i;
- for(i=queue.begin(); (i!=queue.end() && (*i)->load_priority>=managed.load_priority); ++i) ;
+ auto i = find_if(queue, [&managed](ManagedResource *q){ return q->load_priority>managed.load_priority; });
queue.insert(i, &managed);
}
else
ManagedResource::State state = managed.state;
if(state==ManagedResource::LOAD_QUEUED)
{
- LoadQueue::iterator i = find(queue.begin(), queue.end(), &managed);
+ auto i = find(queue, &managed);
if(i!=queue.end())
queue.erase(i);
}
else if(state>ManagedResource::LOAD_QUEUED && state<ManagedResource::LOADED)
thread.remove_resource(managed);
- for(vector<ResourceObserver *>::const_iterator i=managed.observers.begin(); i!=managed.observers.end(); ++i)
- (*i)->resource_removed(r);
+ for(ResourceObserver *o: managed.observers)
+ o->resource_removed(r);
MutexLock lock(map_mutex);
remove_existing(resources, &r);
unload_by_age();
next_unload = frame;
- for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
- if(i->second.state==ManagedResource::LOADED)
- next_unload = min(next_unload, i->second.last_used);
+ for(const auto &kvp: resources)
+ if(kvp.second.state==ManagedResource::LOADED)
+ next_unload = min(next_unload, kvp.second.last_used);
next_unload = (next_unload<frame ? next_unload+max_retain_frames : 0);
}
if(queue.front()->last_used+min_retain_frames<frame)
{
- for(LoadQueue::iterator i=queue.begin(); i!=queue.end(); ++i)
- (*i)->state = ManagedResource::NOT_LOADED;
+ for(ManagedResource *r: queue)
+ r->state = ManagedResource::NOT_LOADED;
queue.clear();
return;
}
{
unsigned unload_limit = frame-max_retain_frames;
- for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
- if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
+ for(auto &kvp: resources)
+ if(kvp.second.state==ManagedResource::LOADED && kvp.second.last_used<unload_limit)
{
- i->second.unload();
- total_data_size -= i->second.data_size;
+ kvp.second.unload();
+ total_data_size -= kvp.second.data_size;
}
}
{
ManagedResource *best = 0;
UInt64 best_impact = 0;
- for(ResourceMap::iterator i=resources.begin(); i!=resources.end(); ++i)
- if(i->second.state==ManagedResource::LOADED && i->second.last_used<unload_limit)
+ for(auto &kvp: resources)
+ if(kvp.second.state==ManagedResource::LOADED && kvp.second.last_used<unload_limit)
{
- UInt64 impact = i->second.data_size*(frame-i->second.last_used);
+ UInt64 impact = kvp.second.data_size*(frame-kvp.second.last_used);
if(!best || impact>best_impact)
{
- best = &i->second;
+ best = &kvp.second;
best_impact = impact;
}
}
state = LOADED;
data_size = resource->get_data_size();
- for(vector<ResourceObserver *>::const_iterator i=observers.begin(); i!=observers.end(); ++i)
- (*i)->resource_loaded(*resource);
+ for(ResourceObserver *o: observers)
+ o->resource_loaded(*resource);
}
else
{
resource->unload();
state = NOT_LOADED;
- for(vector<ResourceObserver *>::const_iterator i=observers.begin(); i!=observers.end(); ++i)
- (*i)->resource_unloaded(*resource);
+ for(ResourceObserver *o: observers)
+ o->resource_unloaded(*resource);
}
void ResourceManager::ManagedResource::add_observer(ResourceObserver &w)
void ResourceManager::ManagedResource::remove_observer(ResourceObserver &w)
{
- vector<ResourceObserver *>::iterator end = remove(observers.begin(), observers.end(), &w);
+ auto end = remove(observers.begin(), observers.end(), &w);
if(end!=observers.end())
observers.erase(end, observers.end());
}
}
}
-ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &que)
+ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(list<ManagedResource *> &que)
{
MutexLock lock(queue_mutex);
if(que.empty())
{
MutexLock lock(queue_mutex);
- LoadQueue::iterator i = find(async_queue.begin(), async_queue.end(), &r);
+ auto i = find(async_queue, &r);
if(i==async_queue.end())
{
- i = find(sync_queue.begin(), sync_queue.end(), &r);
+ i = find(sync_queue, &r);
if(i!=sync_queue.end())
{
sync_queue.erase(i);
void remove_observer(ResourceObserver &);
};
- typedef std::list<ManagedResource *> LoadQueue;
-
class LoadingThread: public Thread
{
private:
Semaphore sem;
Mutex queue_mutex;
- LoadQueue async_queue;
- LoadQueue sync_queue;
+ std::list<ManagedResource *> async_queue;
+ std::list<ManagedResource *> sync_queue;
unsigned capacity;
unsigned size;
std::list<resource_load_error> error_queue;
private:
virtual void main();
- ManagedResource *front(LoadQueue &);
+ ManagedResource *front(std::list<ManagedResource *> &);
public:
void add_resource(ManagedResource &);
void terminate();
};
- typedef std::map<const Resource *, ManagedResource> ResourceMap;
-
LoadingPolicy policy;
bool async_loads;
mutable Mutex map_mutex;
- ResourceMap resources;
- LoadQueue queue;
+ std::map<const Resource *, ManagedResource> resources;
+ std::list<ManagedResource *> queue;
UInt64 total_data_size;
UInt64 size_limit;
unsigned frame;
void GlslCompilerHelper::load_all_test_cases(const FS::Path &tests_dir)
{
- list<string> test_files = FS::list_filtered(tests_dir, "\\.glsl$");
- test_files.sort();
+ vector<string> test_files = FS::list_filtered(tests_dir, "\\.glsl$");
+ sort(test_files);
for(const auto &fn: test_files)
load_test_case((tests_dir/fn).str());
}