From e9a898f315b5d1396f196d785913a283c30940f2 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 18 Sep 2021 01:57:58 +0300 Subject: [PATCH] Use C++11 features with containers --- source/animation/animation.cpp | 77 +++++----- source/animation/animationplayer.cpp | 53 ++++--- source/animation/animationplayer.h | 4 +- source/animation/armature.cpp | 16 +-- source/builders/font.cpp | 12 +- source/builders/sequencebuilder.cpp | 43 +++--- source/builders/sequencetemplate.cpp | 4 +- source/builders/sequencetemplate.h | 6 +- source/core/batch.cpp | 4 +- source/core/clipping.cpp | 2 +- source/core/framebuffer.cpp | 22 +-- source/core/mesh.cpp | 28 ++-- source/core/module.cpp | 103 +++++++------- source/core/pipelinestate.cpp | 46 +++--- source/core/program.cpp | 206 +++++++++++++-------------- source/core/tag.cpp | 2 +- source/core/vertexformat.cpp | 4 +- source/glsl/builtin.cpp | 7 +- source/glsl/compiler.cpp | 101 +++++++------ source/glsl/debug.cpp | 40 +++--- source/glsl/finalize.cpp | 41 +++--- source/glsl/generate.cpp | 72 +++++----- source/glsl/modulecache.cpp | 18 +-- source/glsl/optimize.cpp | 139 +++++++++--------- source/glsl/output.cpp | 18 +-- source/glsl/parser.cpp | 18 +-- source/glsl/reflect.cpp | 20 +-- source/glsl/resolve.cpp | 102 +++++++------ source/glsl/sourcemap.cpp | 6 +- source/glsl/spirv.cpp | 150 ++++++++++--------- source/glsl/syntax.cpp | 11 +- source/glsl/tokenizer.cpp | 2 +- source/glsl/validate.cpp | 82 +++++------ source/glsl/visitor.cpp | 25 ++-- source/materials/lighting.cpp | 2 +- source/materials/material.cpp | 8 +- source/materials/renderpass.cpp | 36 +++-- source/materials/technique.cpp | 40 +++--- source/materials/technique.h | 8 +- source/render/instancearray.cpp | 17 ++- source/render/object.cpp | 6 +- source/render/occludedscene.cpp | 30 ++-- source/render/occludedscene.h | 7 +- source/render/orderedscene.cpp | 27 ++-- source/render/orderedscene.h | 4 +- source/render/programdata.cpp | 82 +++++------ source/render/renderer.cpp | 16 +-- source/render/rendertarget.cpp | 4 +- source/render/sequence.cpp | 30 ++-- source/render/simplescene.cpp | 18 +-- source/render/simplescene.h | 7 +- source/render/zsortedscene.cpp | 36 ++--- source/render/zsortedscene.h | 7 +- source/resources/resourcemanager.cpp | 53 ++++--- source/resources/resourcemanager.h | 14 +- tests/glsl/glslcompiler.cpp | 4 +- 56 files changed, 935 insertions(+), 1005 deletions(-) diff --git a/source/animation/animation.cpp b/source/animation/animation.cpp index b62f5dd9..a9278e3d 100644 --- a/source/animation/animation.cpp +++ b/source/animation/animation.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -22,11 +23,11 @@ Animation::Animation(): Animation::~Animation() { - for(vector::iterator i=keyframes.begin(); i!=keyframes.end(); ++i) - if(i->owned) - delete i->keyframe; - for(vector::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) @@ -116,17 +117,17 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, float 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; csecond.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); @@ -147,10 +148,10 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool throw invalid_argument("Animation::add_keyframe"); const KeyFrame::UniformMap &kf_uniforms = kf->get_uniforms(); - for(vector::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"); } @@ -165,21 +166,17 @@ void Animation::add_keyframe(const Time::TimeDelta &t, const KeyFrame *kf, bool 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::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::iterator i=curves.begin(); i!=curves.end(); ++i) - delete *i; + for(Curve *c: curves) + delete c; curves.clear(); curves.reserve(6+uniforms.size()); @@ -188,16 +185,16 @@ void Animation::create_curves() create_curve(SCALE, Transform::SCALE, &extract_scale); uniform_curve_offset = curves.size(); - for(vector::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)); } } @@ -205,10 +202,10 @@ void Animation::create_curve(CurveTarget target, Transform::ComponentMask mask, { Transform::ComponentMask all = mask; Transform::ComponentMask any = Transform::NONE; - for(vector::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) @@ -232,17 +229,17 @@ void Animation::create_curve(CurveTarget target, int component, const T &extract vector knots; unsigned n_control = 0; - for(vector::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::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) @@ -414,7 +411,7 @@ template bool Animation::ExtractUniform::operator()(const KeyFrame &kf, typename Interpolate::SplineValue::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; diff --git a/source/animation/animationplayer.cpp b/source/animation/animationplayer.cpp index ac163218..42605bea 100644 --- a/source/animation/animationplayer.cpp +++ b/source/animation/animationplayer.cpp @@ -11,11 +11,11 @@ namespace GL { 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) @@ -59,7 +59,7 @@ void AnimationPlayer::play_stacked(Placeable &obj, const Animation &anim, float 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); } @@ -72,22 +72,22 @@ void AnimationPlayer::observe_events(AnimatedObject &obj, AnimationEventObserver 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::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::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); } } @@ -98,16 +98,13 @@ void AnimationPlayer::stop(Placeable &obj) 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::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); @@ -115,7 +112,7 @@ void AnimationPlayer::stop(Placeable &obj, const Animation &anim) 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); @@ -158,16 +155,16 @@ void AnimationPlayer::tick_single(Target &target, const Time::TimeDelta &dt) void AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt) { Matrix matrix = target.base_matrix; - for(vector::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; janimation->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); @@ -180,14 +177,14 @@ void AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt) matrix = Matrix(); /* XXX This is in all likelihood incorrect. The stacking should be performed on local matrices. */ - for(vector::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::iterator i=target.animations.begin(); i!=target.animations.end(); ) + for(auto i=target.animations.begin(); i!=target.animations.end(); ) { i->iterator.dispatch_events(target); @@ -232,8 +229,8 @@ AnimationPlayer::Target::Target(Placeable &p): void AnimationPlayer::Target::animation_event(Placeable *, const string &name, const Variant &value) { - for(vector::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 diff --git a/source/animation/animationplayer.h b/source/animation/animationplayer.h index c45027d9..4b0beb6c 100644 --- a/source/animation/animationplayer.h +++ b/source/animation/animationplayer.h @@ -42,9 +42,7 @@ private: virtual void animation_event(Placeable *, const std::string &, const Variant &); }; - typedef std::map ObjectMap; - - ObjectMap objects; + std::map objects; private: Target &get_slot(Placeable &); diff --git a/source/animation/armature.cpp b/source/animation/armature.cpp index 6e921e3f..f339ea2a 100644 --- a/source/animation/armature.cpp +++ b/source/animation/armature.cpp @@ -14,25 +14,25 @@ Armature::Link &Armature::add_link() const Armature::Link &Armature::get_link(unsigned index) const { - for(vector::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)); } const Armature::Link &Armature::get_link(const string &name) const { - for(vector::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)); } unsigned Armature::get_max_link_index() const { unsigned max_index = 0; - for(vector::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; } diff --git a/source/builders/font.cpp b/source/builders/font.cpp index 44ffafa1..dfa6bf9c 100644 --- a/source/builders/font.cpp +++ b/source/builders/font.cpp @@ -50,7 +50,7 @@ float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const 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) @@ -68,7 +68,7 @@ void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveB 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); @@ -79,7 +79,7 @@ void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveB next = 0; } - GlyphMap::const_iterator j = glyphs.find(c); + auto j = glyphs.find(c); if(j==glyphs.end()) continue; @@ -107,7 +107,7 @@ void Font::create_glyph_quad(const Glyph &glyph, PrimitiveBuilder &bld) const 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; @@ -115,7 +115,7 @@ float Font::get_glyph_advance(unsigned code, unsigned next) const 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; } @@ -125,7 +125,7 @@ float Font::get_glyph_advance(unsigned code, unsigned next) const 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); } diff --git a/source/builders/sequencebuilder.cpp b/source/builders/sequencebuilder.cpp index 5a68d414..8fd4b956 100644 --- a/source/builders/sequencebuilder.cpp +++ b/source/builders/sequencebuilder.cpp @@ -15,13 +15,11 @@ namespace GL { SequenceBuilder::SequenceBuilder(const SequenceTemplate &t): tmpl(t) { - const vector &steps = tmpl.get_steps(); - for(vector::const_iterator i=steps.begin(); i!=steps.end(); ++i) - renderables[i->slot_name] = i->default_renderable; - const vector &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) @@ -50,36 +48,37 @@ void SequenceBuilder::build(Sequence &sequence) const sequence.set_debug_name(debug_name); #endif - const vector &steps = tmpl.get_steps(); - for(vector::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); } diff --git a/source/builders/sequencetemplate.cpp b/source/builders/sequencetemplate.cpp index 13534d75..be8f3d71 100644 --- a/source/builders/sequencetemplate.cpp +++ b/source/builders/sequencetemplate.cpp @@ -24,8 +24,8 @@ SequenceTemplate::SequenceTemplate(): 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; } diff --git a/source/builders/sequencetemplate.h b/source/builders/sequencetemplate.h index 061fdfc3..9de2082d 100644 --- a/source/builders/sequencetemplate.h +++ b/source/builders/sequencetemplate.h @@ -108,8 +108,6 @@ public: PostProcessor(GL::PostProcessor::Template * = 0); }; - typedef std::vector PostProcessorArray; - private: typedef TypeRegistry PostProcessorRegistry; @@ -118,7 +116,7 @@ private: unsigned required_multisample; unsigned max_multisample; std::vector steps; - PostProcessorArray postprocessors; + std::vector postprocessors; public: SequenceTemplate(); @@ -129,7 +127,7 @@ public: unsigned get_required_multisample() const { return required_multisample; } unsigned get_maximum_multisample() const { return max_multisample; } const std::vector &get_steps() const { return steps; } - const PostProcessorArray &get_postprocessors() const { return postprocessors; } + const std::vector &get_postprocessors() const { return postprocessors; } template static void register_postprocessor(const std::string &); diff --git a/source/core/batch.cpp b/source/core/batch.cpp index 1469ab11..d99bb95e 100644 --- a/source/core/batch.cpp +++ b/source/core/batch.cpp @@ -96,8 +96,8 @@ Batch &Batch::append(const vector &ind) return *this; data.reserve(data.size()+ind.size()*get_index_size()); - for(vector::const_iterator i=ind.begin(); i!=ind.end(); ++i) - append_index(*i); + for(unsigned i: ind) + append_index(i); update_offset(); dirty = true; diff --git a/source/core/clipping.cpp b/source/core/clipping.cpp index 79e1aaf0..d23ecffc 100644 --- a/source/core/clipping.cpp +++ b/source/core/clipping.cpp @@ -26,7 +26,7 @@ void Clipping::attach(const ClipPlane &p) void Clipping::detach(const ClipPlane &p) { - vector::iterator i = find_member(planes, &p, &AttachedPlane::plane); + auto i = find_member(planes, &p, &AttachedPlane::plane); if(i!=planes.end()) planes.erase(i); } diff --git a/source/core/framebuffer.cpp b/source/core/framebuffer.cpp index 6e7d9dc9..0ce00bec 100644 --- a/source/core/framebuffer.cpp +++ b/source/core/framebuffer.cpp @@ -201,33 +201,33 @@ void Framebuffer::update() const void Framebuffer::check_size() { bool first = true; - for(vector::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(i->tex); - w = max(tex->get_width()>>i->level, 1U); - h = max(tex->get_height()>>i->level, 1U); + Texture2D *tex = static_cast(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(i->tex); + Texture2DMultisample *tex = static_cast(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(i->tex); - w = max(tex->get_width()>>i->level, 1U); - h = max(tex->get_height()>>i->level, 1U); + Texture3D *tex = static_cast(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(i->tex)->get_size()>>i->level, 1U); + w = max(static_cast(a.tex)->get_size()>>a.level, 1U); h = w; } diff --git a/source/core/mesh.cpp b/source/core/mesh.cpp index de05102a..f6a06021 100644 --- a/source/core/mesh.cpp +++ b/source/core/mesh.cpp @@ -129,7 +129,7 @@ void Mesh::add_batch(const Batch &b) bool reallocate = (batches.size()==batches.capacity()); if(reallocate) { - for(vector::iterator i=batches.end(); i!=batches.begin(); ) + for(auto i=batches.end(); i!=batches.begin(); ) (--i)->use_buffer(0); } @@ -138,10 +138,10 @@ void Mesh::add_batch(const Batch &b) if(reallocate) { prev = 0; - for(vector::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 @@ -156,8 +156,8 @@ void Mesh::add_batch(const Batch &b) batches.back().set_index_type(existing_type); else { - for(vector::iterator i=batches.begin(); i!=batches.end(); ++i) - i->set_index_type(added_type); + for(Batch &a: batches) + a.set_index_type(added_type); } } @@ -202,13 +202,13 @@ void Mesh::draw(Renderer &renderer, const VertexSetup *vs, unsigned count) const if(!count) { - for(vector::const_iterator i=batches.begin(); i!=batches.end(); ++i) - renderer.draw(*i); + for(const Batch &b: batches) + renderer.draw(b); } else { - for(vector::const_iterator i=batches.begin(); i!=batches.end(); ++i) - renderer.draw_instanced(*i, count); + for(const Batch &b: batches) + renderer.draw_instanced(b, count); } } @@ -274,14 +274,14 @@ Mesh::Loader::Loader(Mesh &m, bool g): add("winding", &Mesh::face_winding); } -void Mesh::Loader::storage(const vector &a) +void Mesh::Loader::storage(const vector &attrs) { - if(a.empty()) + if(attrs.empty()) throw invalid_argument("No vertex attributes"); VertexFormat fmt; - for(vector::const_iterator i=a.begin(); i!=a.end(); ++i) - fmt = (fmt, *i); + for(VertexAttribute a: attrs) + fmt = (fmt, a); obj.storage(fmt); } diff --git a/source/core/module.cpp b/source/core/module.cpp index 2474feae..601aae99 100644 --- a/source/core/module.cpp +++ b/source/core/module.cpp @@ -1,3 +1,4 @@ +#include #include #include "module.h" #include "resources.h" @@ -100,18 +101,18 @@ SpirVModule &SpirVModule::operator=(const SpirVModule &other) void SpirVModule::remap_pointers_from(const SpirVModule &other) { - for(vector::iterator i=entry_points.begin(); i!=entry_points.end(); ++i) - for(vector::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::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::iterator i=structs.begin(); i!=structs.end(); ++i) - for(vector::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) @@ -144,8 +145,8 @@ void SpirVModule::reflect() if(code[0]==SPIRV_MAGIC_REVERSED) { - for(vector::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"); @@ -154,38 +155,38 @@ void SpirVModule::reflect() reflection.reflect_code(code); map spec_indices; - for(map::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 struct_indices; structs.reserve(reflection.structs.size()); - for(map::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::iterator i=structs.begin(); i!=structs.end(); ++i) + for(Structure &s: structs) { - for(vector::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_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_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) { @@ -198,51 +199,47 @@ void SpirVModule::reflect() 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 var_indices; variables.reserve(reflection.variables.size()); - for(map::const_iterator i=reflection.variables.begin(); i!=reflection.variables.end(); ++i) + for(const auto &kvp: reflection.variables) { - int dup_index = -1; - for(vector::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::iterator i=variables.begin(); i!=variables.end(); ++i) + for(Variable &v: variables) { - if(i->struct_type) + if(v.struct_type) { - map::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_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::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::iterator j=entry.globals.begin(); j!=entry.globals.end(); ++j) + for(const Variable *&v: entry.globals) { - map::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); } } } @@ -479,7 +476,7 @@ void SpirVModule::Reflection::reflect_struct_type(CodeIterator op) op += 2; strct.members.resize(op_end-op); - vector::iterator mem = strct.members.begin(); + auto mem = strct.members.begin(); for(; op!=op_end; ++op, ++mem) { TypeInfo &type = types[*op]; diff --git a/source/core/pipelinestate.cpp b/source/core/pipelinestate.cpp index 318522d6..30d2ab1c 100644 --- a/source/core/pipelinestate.cpp +++ b/source/core/pipelinestate.cpp @@ -110,7 +110,7 @@ void PipelineState::set_texture(unsigned binding, const Texture *tex, const Samp if((tex!=0)!=(samp!=0)) throw invalid_argument("PipelineState::set_texture"); - vector::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) @@ -134,7 +134,7 @@ void PipelineState::set_uniform_block(unsigned binding, const BufferBackedUnifor void PipelineState::set_uniform_block_(int binding, const UniformBlock *block) { - vector::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) @@ -248,49 +248,49 @@ void PipelineState::apply(unsigned mask) const if(mask&TEXTURES) { - for(vector::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(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(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::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(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(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(i->block)->apply(); + static_cast(u.block)->apply(); } - i->changed = false; + u.changed = false; } } diff --git a/source/core/program.cpp b/source/core/program.cpp index ed2455f8..468a40f5 100644 --- a/source/core/program.cpp +++ b/source/core/program.cpp @@ -147,10 +147,10 @@ void Program::add_glsl_stages(const GlslModule &mod, const map &spe #endif vector stages = compiler.get_stages(); - for(vector::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; @@ -158,23 +158,21 @@ void Program::add_glsl_stages(const GlslModule &mod, const map &spe 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 &attribs = compiler.get_vertex_attributes(); - for(map::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 &frag_outs = compiler.get_fragment_outputs(); - for(map::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); @@ -213,13 +211,12 @@ void Program::add_spirv_stages(const SpirVModule &mod, const map &s module = &mod; - const vector &entry_points = mod.get_entry_points(); unsigned n_stages = 0; unsigned used_stage_ids[MAX_STAGES]; - for(vector::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; @@ -241,18 +238,18 @@ void Program::add_spirv_stages(const SpirVModule &mod, const map &s vector spec_value_array; spec_id_array.reserve(spec_consts.size()); spec_value_array.reserve(spec_consts.size()); - for(vector::const_iterator i=spec_consts.begin(); i!=spec_consts.end(); ++i) + for(const SpirVModule::Constant &c: spec_consts) { - map::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::const_iterator j=entry_points.begin(); + auto j = mod.get_entry_points().begin(); for(unsigned i=0; iname.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]); @@ -347,7 +344,7 @@ void Program::link() { for(unsigned i=0; i::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); @@ -357,15 +354,15 @@ void Program::link() if(!ARB_separate_shader_objects) glUseProgram(id); - for(map::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); } } } @@ -379,10 +376,10 @@ void Program::link() delete transient; transient = 0; - for(vector::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i) - require_type(i->type); - for(vector::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() @@ -429,15 +426,15 @@ void Program::query_uniforms() uniform_blocks.push_back(UniformBlockInfo()); UniformBlockInfo &default_block = uniform_blocks.back(); - for(vector::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); @@ -469,12 +466,12 @@ void Program::query_uniform_blocks(const vector &uniforms_by_inde glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value); vector indices(value); glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]); - for(vector::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 query_indices(indices.begin(), indices.end()); @@ -484,9 +481,9 @@ void Program::query_uniform_blocks(const vector &uniforms_by_inde uniforms_by_index[indices[j]]->offset = values[j]; query_indices.clear(); - for(vector::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]); @@ -495,11 +492,11 @@ void Program::query_uniform_blocks(const vector &uniforms_by_inde } query_indices.clear(); - for(vector::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()) { @@ -547,34 +544,33 @@ void Program::collect_uniforms() uniform_blocks.push_back(UniformBlockInfo()); vector > block_uniform_names(1); - const vector &variables = mod.get_variables(); - for(vector::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()); - 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; } } @@ -583,11 +579,10 @@ void Program::collect_uniforms() for(unsigned i=0; i &names = block_uniform_names[i]; - for(vector::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 = █ } @@ -600,18 +595,18 @@ void Program::collect_uniforms() void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector &uniform_names) { - for(vector::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::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; } @@ -619,25 +614,25 @@ void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const if(array_size) { - for(unsigned j=0; jarray_stride) - collect_block_uniforms(*i->struct_type, format("%s%s[%d].", prefix, i->name, j), offset, uniform_names); + for(unsigned j=0; jstruct_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; } } } @@ -646,19 +641,18 @@ void Program::collect_attributes() { const SpirVModule &mod = static_cast(*module); - const vector &entry_points = mod.get_entry_points(); - for(vector::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_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; } } } @@ -666,16 +660,16 @@ void Program::collect_attributes() void Program::update_layout_hash() { string layout_descriptor; - for(vector::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 &uniforms) { string layout_descriptor; - for(vector::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); } @@ -695,15 +689,15 @@ string Program::get_info_log() const const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const { - for(vector::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::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; @@ -711,7 +705,7 @@ const Program::UniformInfo &Program::get_uniform_info(const string &name) const const Program::UniformInfo &Program::get_uniform_info(Tag tag) const { - vector::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; @@ -722,25 +716,25 @@ int Program::get_uniform_location(const string &name) const if(name[name.size()-1]==']') throw invalid_argument("Program::get_uniform_location"); - vector::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::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::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::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; @@ -751,7 +745,7 @@ int Program::get_attribute_location(const string &name) const if(name[name.size()-1]==']') throw invalid_argument("Program::get_attribute_location"); - vector::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; } diff --git a/source/core/tag.cpp b/source/core/tag.cpp index 538190f8..44c7c8f8 100644 --- a/source/core/tag.cpp +++ b/source/core/tag.cpp @@ -38,7 +38,7 @@ Tag::Tag(const string &s): string Tag::str() const { #ifdef DEBUG - map::const_iterator i=tag_names.find(*this); + auto i=tag_names.find(*this); if(i!=tag_names.end()) return i->second; #endif diff --git a/source/core/vertexformat.cpp b/source/core/vertexformat.cpp index 7dcb7aba..3d15705a 100644 --- a/source/core/vertexformat.cpp +++ b/source/core/vertexformat.cpp @@ -125,7 +125,7 @@ VertexAttribute make_indexed_attribute(VertexAttribute attr, unsigned index) 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; @@ -138,7 +138,7 @@ bool convert_attribute_type(string::const_iterator &i, string::const_iterator en 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; diff --git a/source/glsl/builtin.cpp b/source/glsl/builtin.cpp index 83e480e5..8e18d6eb 100644 --- a/source/glsl/builtin.cpp +++ b/source/glsl/builtin.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "builtin.h" @@ -62,10 +63,8 @@ const Stage *get_builtins(Stage::Type type) if(type==Stage::SHARED) return &module->shared; - for(list::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 diff --git a/source/glsl/compiler.cpp b/source/glsl/compiler.cpp index 5b488f4d..4f2080ab 100644 --- a/source/glsl/compiler.cpp +++ b/source/glsl/compiler.cpp @@ -82,27 +82,27 @@ void Compiler::compile(Mode mode) if(specialized && mode!=PROGRAM) throw invalid_operation("Compiler::compile"); - for(list::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::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::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::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::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) @@ -112,8 +112,8 @@ void Compiler::compile(Mode mode) } LocationAllocator().apply(*module, features); - for(list::iterator i=module->stages.begin(); i!=module->stages.end(); ++i) - finalize(*i, mode); + for(Stage &s: module->stages) + finalize(s, mode); compiled = true; } @@ -128,10 +128,10 @@ string Compiler::get_combined_glsl() const unsigned source_count = module->source_map.get_count(); for(unsigned i=1; isource_map.get_name(i)); - for(list::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'; } @@ -142,8 +142,8 @@ vector Compiler::get_stages() const { vector stage_types; stage_types.reserve(module->stages.size()); - for(list::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; } @@ -151,9 +151,9 @@ string Compiler::get_stage_glsl(Stage::Type stage_type) const { if(!compiled) throw invalid_operation("Compiler::get_stage_glsl"); - for(list::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)); } @@ -170,9 +170,9 @@ const map &Compiler::get_vertex_attributes() const { if(!compiled) throw invalid_operation("Compiler::get_vertex_attributes"); - for(list::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"); } @@ -180,9 +180,9 @@ const map &Compiler::get_fragment_outputs() const { if(!compiled) throw invalid_operation("Compiler::get_fragment_outputs"); - for(list::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"); } @@ -207,19 +207,19 @@ const SourceMap &Compiler::get_source_map() const string Compiler::get_stage_debug(Stage::Type stage_type) const { - for(list::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::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i) - for(vector::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; } @@ -228,15 +228,15 @@ void Compiler::append_module(const Module &mod, ModuleCache &mod_cache) module->source_map.merge_from(mod.source_map); vector imports; - for(NodeList::const_iterator i=mod.shared.content.body.begin(); i!=mod.shared.content.body.end(); ++i) - if(Import *imp = dynamic_cast(i->get())) + for(const RefPtr &s: mod.shared.content.body) + if(Import *imp = dynamic_cast(s.get())) imports.push_back(imp); - for(vector::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::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) @@ -246,11 +246,10 @@ void Compiler::append_stage(const Stage &stage) target = &module->shared; else { - list::iterator i; - for(i=module->stages.begin(); (i!=module->stages.end() && i->typestages, [&stage](const Stage &s){ return s.type>=stage.type; }); if(i==module->stages.end() || i->type>stage.type) { - list::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; @@ -263,9 +262,9 @@ void Compiler::append_stage(const Stage &stage) if(stage.required_features.glsl_version>target->required_features.glsl_version) target->required_features.glsl_version = stage.required_features.glsl_version; - for(NodeList::const_iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i) - if(!dynamic_cast(i->get())) - target->content.body.push_back(*i); + for(const RefPtr &s: stage.content.body) + if(!dynamic_cast(s.get())) + target->content.body.push_back(s); } void Compiler::import(ModuleCache &mod_cache, const string &name) @@ -338,12 +337,8 @@ void Compiler::validate(Stage &stage) bool Compiler::check_errors(Stage &stage) { stable_sort(stage.diagnostics, &diagnostic_line_order); - - for(vector::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) @@ -407,9 +402,9 @@ void Compiler::finalize(Stage &stage, Mode mode) void Compiler::inject_block(Block &target, const Block &source) { - NodeList::iterator insert_point = target.body.begin(); - for(NodeList::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 &s: source.body) + target.body.insert(insert_point, s->clone()); } } // namespace SL diff --git a/source/glsl/debug.cpp b/source/glsl/debug.cpp index beafaddf..25434502 100644 --- a/source/glsl/debug.cpp +++ b/source/glsl/debug.cpp @@ -14,14 +14,14 @@ string DumpTree::apply(Stage &stage) begin_sub(); append(format("Version: %d.%02d", stage.required_features.glsl_version.major, stage.required_features.glsl_version.minor)); - for(map::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::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::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); @@ -31,7 +31,7 @@ string DumpTree::apply(Stage &stage) void DumpTree::append(const string &line) { StringCodec::Utf8::Encoder enc; - for(vector::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); @@ -48,9 +48,9 @@ void DumpTree::append(const Node &node, const string &line) void DumpTree::append_subtree(const vector &branches) { begin_sub(); - for(vector::const_iterator i=branches.begin(); i!=branches.end(); ) + for(auto i=branches.begin(); i!=branches.end(); ) { - vector::const_iterator j = increment(i, branches); + auto j = increment(i, branches); if(!j->text.empty()) { append(j->text); @@ -111,7 +111,7 @@ string DumpTree::format_type(TypeDeclaration *type) template 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; @@ -122,12 +122,12 @@ void DumpTree::visit(Block &block) append(block, format("Block %s", (block.use_braces ? "{}" : "(inline)"))); begin_sub(); - for(map::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::const_iterator i=block.body.begin(); i!=block.body.end(); ) + for(auto i=block.body.cbegin(); i!=block.body.cend(); ) { - NodeList::const_iterator j = increment(i, block.body); + auto j = increment(i, block.body); (*j)->visit(*this); } @@ -254,9 +254,9 @@ void DumpTree::visit(FunctionCall &call) append(call, head); begin_sub(); - for(NodeArray::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); ) + for(auto i=call.arguments.cbegin(); i!=call.arguments.cend(); ) { - NodeArray::const_iterator j = increment(i, call.arguments); + auto j = increment(i, call.arguments); (*j)->visit(*this); } end_sub(); @@ -282,9 +282,9 @@ void DumpTree::visit(Layout &layout) { append(layout, "Layout"); begin_sub(); - for(vector::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ) + for(auto i=layout.qualifiers.cbegin(); i!=layout.qualifiers.cend(); ) { - vector::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); @@ -406,8 +406,8 @@ void DumpTree::visit(FunctionDeclaration &func) 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::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) - (*i)->visit(*this); + for(const RefPtr &p: func.parameters) + p->visit(*this); last_branch(); if(func.definition==&func) func.body.visit(*this); diff --git a/source/glsl/finalize.cpp b/source/glsl/finalize.cpp index 192baec5..eee8a519 100644 --- a/source/glsl/finalize.cpp +++ b/source/glsl/finalize.cpp @@ -30,16 +30,15 @@ void StructOrganizer::visit(VariableDeclaration &var) bool has_matrix_order = false; if(var.layout) { - vector &qualifiers = var.layout->qualifiers; - for(vector::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; } } @@ -74,14 +73,14 @@ void StructOrganizer::visit(VariableDeclaration &var) void LocationAllocator::apply(Module &module, const Features &features) { - for(list::iterator i=module.stages.begin(); i!=module.stages.end(); ++i) - apply(*i); + for(Stage &s: module.stages) + apply(s); allocate_locations("uniform"); - for(vector::const_iterator i=unbound_blocks.begin(); i!=unbound_blocks.end(); ++i) - bind_uniform((*i)->layout, (*i)->block_name, features.uniform_binding_range); - for(vector::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) @@ -97,9 +96,9 @@ void LocationAllocator::apply(Stage &stage) void LocationAllocator::allocate_locations(const string &iface) { - vector::iterator write = unplaced_variables.begin(); + auto write = unplaced_variables.begin(); unsigned next = 0; - for(vector::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) { @@ -111,7 +110,7 @@ void LocationAllocator::allocate_locations(const string &iface) if((*i)->interface=="uniform") { - map::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); @@ -147,7 +146,7 @@ void LocationAllocator::allocate_locations(const string &iface) void LocationAllocator::bind_uniform(RefPtr &layout, const string &name, unsigned range) { - map::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 @@ -252,7 +251,7 @@ void PrecisionConverter::apply(Stage &s) void PrecisionConverter::visit(Block &block) { - for(NodeList::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; @@ -341,7 +340,7 @@ void LegacyConverter::unsupported(const string &reason) void LegacyConverter::visit(Block &block) { - for(NodeList::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; @@ -515,7 +514,7 @@ void LegacyConverter::visit(VariableDeclaration &var) { if(var.layout) { - for(vector::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") { @@ -621,7 +620,7 @@ void LegacyConverter::visit(InterfaceBlock &iface) { if(iface.layout) { - for(vector::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); diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 4550b51c..a17f7fb5 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "generate.h" @@ -10,22 +11,18 @@ namespace SL { void ConstantIdAssigner::apply(Module &module, const Features &features) { - for(list::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::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 &qualifiers = (*i)->layout->qualifiers; - for(vector::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); } @@ -35,16 +32,14 @@ void ConstantIdAssigner::visit(VariableDeclaration &var) { if(var.layout) { - vector &qualifiers = var.layout->qualifiers; - for(vector::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); + } } } @@ -80,7 +75,7 @@ void InterfaceGenerator::apply(Stage &s) void InterfaceGenerator::visit(Block &block) { SetForScope set_block(current_block, &block); - for(NodeList::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) @@ -198,7 +193,7 @@ void InterfaceGenerator::visit(VariableReference &var) return; const map &prev_vars = stage->previous->content.variables; - map::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") @@ -215,7 +210,7 @@ void InterfaceGenerator::visit(VariableReference &var) } const map &prev_blocks = stage->previous->interface_blocks; - map::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); @@ -224,14 +219,14 @@ void InterfaceGenerator::visit(VariableReference &var) 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 &iface_vars = j->second->struct_declaration->members.variables; + const map &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; } } @@ -270,7 +265,7 @@ void InterfaceGenerator::visit(VariableDeclaration &var) if(!var.linked_declaration && stage->previous) { const map &prev_vars = stage->previous->content.variables; - map::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; @@ -291,7 +286,7 @@ void InterfaceGenerator::visit(InterfaceBlock &iface) if(!iface.linked_block && stage->previous) { const map &prev_blocks = stage->previous->interface_blocks; - map::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; @@ -317,16 +312,15 @@ void InterfaceGenerator::visit(Passthrough &pass) if(stage->previous) { - const map &prev_vars = stage->previous->content.variables; - for(map::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); } } @@ -349,13 +343,13 @@ void InterfaceGenerator::visit(Passthrough &pass) insert_assignment("gl_Position", memacc); } - for(vector::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; diff --git a/source/glsl/modulecache.cpp b/source/glsl/modulecache.cpp index 116eb20a..a0686902 100644 --- a/source/glsl/modulecache.cpp +++ b/source/glsl/modulecache.cpp @@ -16,24 +16,24 @@ ModuleCache::ModuleCache(DataFile::Collection *r): ModuleCache::ModuleCache(const ModuleCache &other) { - for(map::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::iterator i=modules.begin(); i!=modules.end(); ++i) - delete i->second; + for(auto &kvp: modules) + delete kvp.second; modules.clear(); - for(map::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::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) @@ -57,7 +57,7 @@ const Module &ModuleCache::add_module(IO::Base &io, const string &src_name) const Module &ModuleCache::get_module(const string &name) { string fn = name+".glsl"; - map::const_iterator i = modules.find(fn); + auto i = modules.find(fn); if(i!=modules.end()) return *i->second; diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 593a3222..9388c041 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -26,20 +27,19 @@ void ConstantSpecializer::visit(VariableDeclaration &var) if(var.layout) { vector &qualifiers = var.layout->qualifiers; - for(vector::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::const_iterator i = values->find(var.name); + auto i = values->find(var.name); if(i!=values->end()) { RefPtr literal = new Literal; @@ -85,9 +85,8 @@ void InlineableFunctionLocator::visit(FunctionCall &call) void InlineableFunctionLocator::visit(FunctionDeclaration &func) { - bool has_out_params = false; - for(NodeArray::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 &p){ return p->interface=="out"; }); unsigned &count = refcounts[func.definition]; if((count<=1 || func.source==BUILTIN_SOURCE) && !has_out_params) @@ -141,9 +140,9 @@ string InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_fu vector > params; params.reserve(source_func->parameters.size()); - for(NodeArray::iterator i=source_func->parameters.begin(); i!=source_func->parameters.end(); ++i) + for(const RefPtr &p: source_func->parameters) { - RefPtr var = (*i)->clone(); + RefPtr var = p->clone(); var->interface.clear(); SetForScope set_pass(pass, RENAME); @@ -153,12 +152,12 @@ string InlineContentInjector::apply(Stage &stage, FunctionDeclaration &target_fu params.push_back(var); } - for(NodeList::iterator i=source_func->body.body.begin(); i!=source_func->body.body.end(); ++i) + for(const RefPtr &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 set_pass(pass, RENAME); r_inlined_statement->visit(*this); @@ -194,7 +193,7 @@ void InlineContentInjector::visit(VariableReference &var) { if(pass==RENAME) { - map::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; } @@ -289,7 +288,7 @@ void FunctionInliner::visit(Block &block) { SetForScope set_block(current_block, &block); SetForScope::iterator> save_insert_point(insert_point, block.body.begin()); - for(NodeList::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); @@ -298,7 +297,7 @@ void FunctionInliner::visit(Block &block) void FunctionInliner::visit(FunctionCall &call) { - for(NodeArray::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) @@ -364,13 +363,13 @@ bool ExpressionInliner::apply(Stage &s) s.content.visit(*this); bool any_inlined = false; - for(list::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::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; } } @@ -413,7 +412,7 @@ void ExpressionInliner::visit(VariableReference &var) { if(var.declaration && access_read) { - map::iterator i = assignments.find(var.declaration); + auto i = assignments.find(var.declaration); if(i!=assignments.end()) r_ref_info = i->second; } @@ -459,7 +458,7 @@ void ExpressionInliner::visit(Assignment &assign) r_trivial = true; visit(assign.right); - map::iterator i = assignments.find(assign.target); + auto i = assignments.find(assign.target); if(i!=assignments.end()) { if(iteration_body && i->second->expression) @@ -467,9 +466,9 @@ void ExpressionInliner::visit(Assignment &assign) /* 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::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()); @@ -510,8 +509,8 @@ void ExpressionInliner::visit(VariableDeclaration &var) bool constant = var.constant; if(constant && var.layout) { - for(vector::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 @@ -894,7 +893,7 @@ ConstantConditionEliminator::ConstantStatus ConstantConditionEliminator::check_c void ConstantConditionEliminator::visit(Block &block) { SetForScope set_block(current_block, &block); - for(NodeList::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); @@ -963,7 +962,7 @@ bool UnreachableCodeRemover::apply(Stage &stage) void UnreachableCodeRemover::visit(Block &block) { - NodeList::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) @@ -1063,29 +1062,29 @@ bool UnusedVariableRemover::apply(Stage &s) stage = &s; s.content.visit(*this); - for(list::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::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); @@ -1100,16 +1099,16 @@ void UnusedVariableRemover::referenced(const Assignment::Target &target, Node &n if(!assignment_target) { bool loop_external = false; - for(vector::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 && jtarget.chain_len && j((*i)->target.chain[j]&0xC0); + Assignment::Target::ChainType type1 = static_cast(a->target.chain[j]&0xC0); Assignment::Target::ChainType type2 = static_cast(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; @@ -1121,13 +1120,13 @@ void UnusedVariableRemover::referenced(const Assignment::Target &target, Node &n 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_loopused_by.push_back(&node); + if(a->in_looptype==Stage::GEOMETRY && call.name=="EmitVertex") { - for(map::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); } } @@ -1326,24 +1325,24 @@ void UnusedVariableRemover::visit(InterfaceBlock &iface) 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(; (ksecond.assignments.size() && ksecond.assignments.size()); ++k) - if(i->second.assignments[k]!=j->second.assignments[k]) + for(; (ksecond.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); } } @@ -1354,17 +1353,17 @@ void UnusedVariableRemover::visit(FunctionDeclaration &func) 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::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) + for(const RefPtr &p: func.parameters) { - BlockVariableMap::iterator j = variables.find(i->get()); + auto j = variables.find(p.get()); if(j!=variables.end()) j->second.referenced = true; } @@ -1397,8 +1396,8 @@ void UnusedVariableRemover::visit(Iteration &iter) /* Visit the external references of the loop again to record assignments done in the loop as used. */ - for(vector::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 diff --git a/source/glsl/output.cpp b/source/glsl/output.cpp index 50b60c14..6ba06447 100644 --- a/source/glsl/output.cpp +++ b/source/glsl/output.cpp @@ -59,8 +59,8 @@ string Formatter::apply(Stage &s) 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; } @@ -99,16 +99,16 @@ void Formatter::visit(Block &block) SetForScope set(indent, indent+(indent>0 || use_braces)); string spaces(indent*2, ' '); bool first = true; - for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) + for(const RefPtr &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) @@ -218,7 +218,7 @@ void Formatter::visit(TernaryExpression &ternary) void Formatter::visit(FunctionCall &call) { append(format("%s(", call.name)); - for(NodeArray::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(", "); @@ -246,7 +246,7 @@ void Formatter::visit(Precision &prec) void Formatter::visit(Layout &layout) { append("layout("); - for(vector::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(", "); @@ -340,7 +340,7 @@ void Formatter::visit(InterfaceBlock &iface) void Formatter::visit(FunctionDeclaration &func) { append(format("%s %s(", func.return_type_declaration->name, func.name)); - for(NodeArray::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(", "); diff --git a/source/glsl/parser.cpp b/source/glsl/parser.cpp index 626a6542..cc0233ea 100644 --- a/source/glsl/parser.cpp +++ b/source/glsl/parser.cpp @@ -58,8 +58,8 @@ void Parser::parse_source(const string &name, int index) if(const Stage *builtin = get_builtins(Stage::SHARED)) { - for(map::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); @@ -99,13 +99,13 @@ void Parser::stage_change(Stage::Type stage) cur_stage = &module->stages.back(); stage_types.clear(); - for(vector::const_iterator i=imported_modules.begin(); i!=imported_modules.end(); ++i) + for(const Module *m: imported_modules) { - list::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::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); } } } @@ -369,8 +369,8 @@ RefPtr Parser::parse_import() { const Module &imported_mod = mod_cache->get_module(import->module); imported_modules.push_back(&imported_mod); - for(map::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; diff --git a/source/glsl/reflect.cpp b/source/glsl/reflect.cpp index 91e8fa55..82ad244c 100644 --- a/source/glsl/reflect.cpp +++ b/source/glsl/reflect.cpp @@ -1,3 +1,4 @@ +#include #include "reflect.h" using namespace std; @@ -158,8 +159,8 @@ void TypeComparer::visit(StructDeclaration &strct) else { r_result = true; - NodeList::const_iterator i = strct1->members.body.begin(); - NodeList::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); } @@ -207,10 +208,10 @@ void LocationCounter::visit(ImageTypeDeclaration &) void LocationCounter::visit(StructDeclaration &strct) { unsigned total = 0; - for(NodeList::const_iterator i=strct.members.body.begin(); i!=strct.members.body.end(); ++i) + for(const RefPtr &s: strct.members.body) { r_count = 1; - (*i)->visit(*this); + s->visit(*this); total += r_count; } r_count = total; @@ -256,12 +257,12 @@ void MemoryRequirementsCalculator::visit(StructDeclaration &strct) { unsigned total = 0; unsigned max_align = 1; - for(NodeList::iterator i=strct.members.body.begin(); i!=strct.members.body.end(); ++i) + for(const RefPtr &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; @@ -277,10 +278,9 @@ void MemoryRequirementsCalculator::visit(VariableDeclaration &var) { if(var.layout) { - const vector qualifiers = var.layout->qualifiers; - for(vector::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) diff --git a/source/glsl/resolve.cpp b/source/glsl/resolve.cpp index f2670b53..c12a0f48 100644 --- a/source/glsl/resolve.cpp +++ b/source/glsl/resolve.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include "reflect.h" @@ -34,7 +34,7 @@ bool TypeResolver::apply(Stage &s) TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type) { - map::iterator i = array_types.find(&type); + auto i = array_types.find(&type); if(i!=array_types.end()) return i->second; @@ -52,10 +52,10 @@ TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type) void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array) { TypeDeclaration *resolved = 0; - map::iterator i = stage->types.find(name); + auto i = stage->types.find(name); if(i!=stage->types.end()) { - map::iterator j = alias_map.find(i->second); + auto j = alias_map.find(i->second); resolved = (j!=alias_map.end() ? j->second : i->second); } @@ -68,7 +68,7 @@ void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool void TypeResolver::visit(Block &block) { - for(NodeList::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; @@ -164,8 +164,8 @@ bool VariableResolver::apply(Stage &s) s.interface_blocks.clear(); r_any_resolved = false; s.content.visit(*this); - for(vector::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; } @@ -218,7 +218,7 @@ void VariableResolver::visit(VariableReference &var) one. */ for(Block *block=current_block; (!declaration && block); block=block->parent) { - map::iterator i = block->variables.find(var.name); + auto i = block->variables.find(var.name); if(i!=block->variables.end()) declaration = i->second; } @@ -226,7 +226,7 @@ void VariableResolver::visit(VariableReference &var) if(!declaration) { const map &blocks = stage->interface_blocks; - map::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. @@ -274,7 +274,7 @@ void VariableResolver::visit(VariableReference &var) void VariableResolver::visit(InterfaceBlockReference &iface) { - map::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; @@ -290,12 +290,12 @@ void VariableResolver::visit(MemberAccess &memacc) int index = -1; if(StructDeclaration *strct = dynamic_cast(memacc.left->type)) { - map::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::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) @@ -313,7 +313,7 @@ void VariableResolver::visit(MemberAccess &memacc) bool ok = true; UInt8 components[4] = { }; for(unsigned i=0; (ok && i::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::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); } } @@ -474,18 +471,16 @@ ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTyp BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size, bool sign) { - for(vector::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::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 &expr, BasicTypeDeclaration &type) @@ -549,7 +544,7 @@ void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool l void ExpressionResolver::visit(Block &block) { SetForScope set_block(current_block, &block); - for(NodeList::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); @@ -857,7 +852,7 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) if(call.arguments.empty()) return; - map::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) @@ -872,10 +867,10 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) args.reserve(call.arguments.size()); unsigned arg_component_total = 0; bool has_matrices = false; - for(NodeArray::const_iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j) + for(const RefPtr &a: call.arguments) { ArgumentInfo info; - if(!(info.type=dynamic_cast((*j)->type))) + if(!(info.type=dynamic_cast(a->type))) return; if(is_scalar(*info.type) || info.type->kind==BasicTypeDeclaration::BOOL) info.component_count = 1; @@ -1033,12 +1028,12 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) if(convert_args) { // The argument list may have changed so can't rely on args. - for(NodeArray::iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j) - if(BasicTypeDeclaration *basic_arg = dynamic_cast((*j)->type)) + for(RefPtr &a: call.arguments) + if(BasicTypeDeclaration *basic_arg = dynamic_cast(a->type)) { BasicTypeDeclaration *elem_arg = get_element_type(*basic_arg); if(elem_arg!=elem) - convert_to_element(*j, *elem); + convert_to_element(a, *elem); } } } @@ -1047,16 +1042,17 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) if(call.arguments.size()!=strct->members.body.size()) return; - unsigned k = 0; - for(NodeList::const_iterator j=strct->members.body.begin(); j!=strct->members.body.end(); ++j, ++k) + auto j = call.arguments.begin(); + for(const RefPtr &s: strct->members.body) { - if(VariableDeclaration *var = dynamic_cast(j->get())) + if(VariableDeclaration *var = dynamic_cast(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; } } @@ -1136,7 +1132,7 @@ void FunctionResolver::visit(FunctionCall &call) { string arg_types; bool has_signature = true; - for(NodeArray::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); @@ -1146,7 +1142,7 @@ void FunctionResolver::visit(FunctionCall &call) if(has_signature) { - map::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) @@ -1177,10 +1173,10 @@ void FunctionResolver::visit(FunctionDeclaration &func) if(func.signature.empty()) { string param_types; - for(NodeArray::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) + for(const RefPtr &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; } @@ -1205,11 +1201,11 @@ void FunctionResolver::visit(FunctionDeclaration &func) stage_decl = &func; // Set all previous declarations to use this definition. - for(vector::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 diff --git a/source/glsl/sourcemap.cpp b/source/glsl/sourcemap.cpp index 9eb33b05..b81931e1 100644 --- a/source/glsl/sourcemap.cpp +++ b/source/glsl/sourcemap.cpp @@ -59,9 +59,9 @@ string SourceMap::translate_errors(const string &errors) const static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$"); vector lines = split(errors, '\n'); string translated; - for(vector::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; @@ -82,7 +82,7 @@ string SourceMap::translate_errors(const string &errors) const translated += format("%s:%d: %s", src, line, m[6].str); } else - translated += *i; + translated += l; translated += '\n'; } diff --git a/source/glsl/spirv.cpp b/source/glsl/spirv.cpp index 72f73bd5..1da748a6 100644 --- a/source/glsl/spirv.cpp +++ b/source/glsl/spirv.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "reflect.h" @@ -151,11 +152,11 @@ void SpirVGenerator::apply(Module &module) { use_capability(CAP_SHADER); - for(list::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); @@ -239,7 +240,7 @@ SpirVGenerator::Id SpirVGenerator::get_id(Node &node) const SpirVGenerator::Id SpirVGenerator::allocate_id(Node &node, Id type_id) { - map::iterator i = declared_ids.find(&node); + auto i = declared_ids.find(&node); if(i!=declared_ids.end()) { if(i->second.type_id) @@ -255,7 +256,7 @@ SpirVGenerator::Id SpirVGenerator::allocate_id(Node &node, Id type_id) SpirVGenerator::Id SpirVGenerator::allocate_forward_id(Node &node) { - map::iterator i = declared_ids.find(&node); + auto i = declared_ids.find(&node); if(i!=declared_ids.end()) return i->second.id; @@ -345,7 +346,7 @@ SpirVGenerator::Id SpirVGenerator::get_standard_type_id(BasicTypeDeclaration::Ki bool SpirVGenerator::is_scalar_type(Id type_id, BasicTypeDeclaration::Kind kind) const { - map::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); } @@ -413,7 +414,7 @@ SpirVGenerator::Id SpirVGenerator::get_load_id(VariableDeclaration &var) void SpirVGenerator::prune_loads(Id min_id) { - for(map::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++); @@ -499,8 +500,8 @@ SpirVGenerator::Id SpirVGenerator::write_construct(Id type_id, const Id *elem_id void SpirVGenerator::visit(Block &block) { - for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) - (*i)->visit(*this); + for(const RefPtr &s: block.body) + s->visit(*this); } void SpirVGenerator::visit(Literal &literal) @@ -564,13 +565,13 @@ void SpirVGenerator::generate_composite_access(TypeDeclaration &result_type) throw internal_error("composite access through pointer in constant context"); Id int32_type_id = get_standard_type_id(BasicTypeDeclaration::INT, 1); - for(vector::iterator i=r_composite_chain.begin(); i!=r_composite_chain.end(); ++i) - *i = (*i<0x400000 ? get_constant_id(int32_type_id, static_cast(*i)) : *i&0x3FFFFF); + for(unsigned &i: r_composite_chain) + i = (i<0x400000 ? get_constant_id(int32_type_id, static_cast(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::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"); @@ -582,18 +583,18 @@ void SpirVGenerator::generate_composite_access(TypeDeclaration &result_type) throw internal_error("assignment to temporary composite"); else { - for(vector::iterator i=r_composite_chain.begin(); i!=r_composite_chain.end(); ++i) - for(map::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::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; @@ -1062,9 +1063,9 @@ void SpirVGenerator::visit(FunctionCall &call) vector argument_ids; argument_ids.reserve(call.arguments.size()); bool all_args_const = true; - for(NodeArray::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) + for(const RefPtr &a: call.arguments) { - (*i)->visit(*this); + a->visit(*this); argument_ids.push_back(r_expression_result_id); all_args_const &= r_constant_result; } @@ -1080,8 +1081,8 @@ void SpirVGenerator::visit(FunctionCall &call) else if(call.declaration->source==BUILTIN_SOURCE) { string arg_types; - for(NodeArray::const_iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) - if(BasicTypeDeclaration *basic_arg = dynamic_cast((*i)->type)) + for(const RefPtr &a: call.arguments) + if(BasicTypeDeclaration *basic_arg = dynamic_cast(a->type)) { BasicTypeDeclaration &elem_arg = *get_element_type(*basic_arg); switch(elem_arg.kind) @@ -1137,14 +1138,14 @@ void SpirVGenerator::visit(FunctionCall &call) { 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::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 dependencies = DependencyCollector().apply(*call.declaration->definition); - for(set::const_iterator i=dependencies.begin(); i!=dependencies.end(); ++i) - if(const VariableDeclaration *var = dynamic_cast(*i)) + for(Node *n: dependencies) + if(const VariableDeclaration *var = dynamic_cast(n)) variable_load_ids.erase(var); } } @@ -1405,7 +1406,7 @@ void SpirVGenerator::visit_builtin_interpolate(FunctionCall &call, const vector< writer.write(ext_id); writer.write(opcode); writer.write(get_id(*var->declaration)); - for(vector::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); } @@ -1422,11 +1423,11 @@ void SpirVGenerator::visit(InterfaceLayout &layout) bool SpirVGenerator::check_duplicate_type(TypeDeclaration &type) { - for(map::const_iterator i=declared_ids.begin(); i!=declared_ids.end(); ++i) - if(TypeDeclaration *type2 = dynamic_cast(i->first)) + for(const auto &kvp: declared_ids) + if(TypeDeclaration *type2 = dynamic_cast(kvp.first)) if(TypeComparer().apply(type, *type2)) { - insert_unique(declared_ids, &type, i->second); + insert_unique(declared_ids, &type, kvp.second); return true; } @@ -1527,9 +1528,9 @@ void SpirVGenerator::visit(StructDeclaration &strct) bool builtin = (strct.interface_block && !strct.interface_block->block_name.compare(0, 3, "gl_")); vector member_type_ids; member_type_ids.reserve(strct.members.body.size()); - for(NodeList::const_iterator i=strct.members.body.begin(); i!=strct.members.body.end(); ++i) + for(const RefPtr &s: strct.members.body) { - const VariableDeclaration *var = dynamic_cast(i->get()); + const VariableDeclaration *var = dynamic_cast(s.get()); if(!var) continue; @@ -1547,14 +1548,13 @@ void SpirVGenerator::visit(StructDeclaration &strct) { if(var->layout) { - const vector &qualifiers = var->layout->qualifiers; - for(vector::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); } } @@ -1572,8 +1572,8 @@ void SpirVGenerator::visit(StructDeclaration &strct) writer.begin_op(content.globals, OP_TYPE_STRUCT); writer.write(type_id); - for(vector::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); } @@ -1584,9 +1584,9 @@ void SpirVGenerator::visit(VariableDeclaration &var) int spec_id = -1; if(layout_ql) { - for(vector::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); @@ -1649,14 +1649,14 @@ void SpirVGenerator::visit(VariableDeclaration &var) if(layout_ql) { - for(vector::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); } } @@ -1700,10 +1700,9 @@ void SpirVGenerator::visit(InterfaceBlock &iface) if(iface.layout) { - const vector &qualifiers = iface.layout->qualifiers; - for(vector::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); } } @@ -1721,15 +1720,15 @@ void SpirVGenerator::visit_entry_point(FunctionDeclaration &func, Id func_id) writer.write_string(func.name); set dependencies = DependencyCollector().apply(func); - for(set::const_iterator i=dependencies.begin(); i!=dependencies.end(); ++i) + for(Node *n: dependencies) { - if(const VariableDeclaration *var = dynamic_cast(*i)) + if(const VariableDeclaration *var = dynamic_cast(n)) { if(!var->interface.empty()) - writer.write(get_id(**i)); + writer.write(get_id(*n)); } - else if(dynamic_cast(*i)) - writer.write(get_id(**i)); + else if(dynamic_cast(n)) + writer.write(get_id(*n)); } writer.end_op(OP_ENTRY_POINT); @@ -1739,28 +1738,27 @@ void SpirVGenerator::visit_entry_point(FunctionDeclaration &func, Id func_id) else if(stage->type==Stage::GEOMETRY) use_capability(CAP_GEOMETRY); - for(vector::const_iterator i=interface_layouts.begin(); i!=interface_layouts.end(); ++i) + for(const InterfaceLayout *i: interface_layouts) { - const vector &qualifiers = (*i)->layout.qualifiers; - for(vector::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); } } } @@ -1779,8 +1777,8 @@ void SpirVGenerator::visit(FunctionDeclaration &func) Id return_type_id = get_id(*func.return_type_declaration); vector param_type_ids; param_type_ids.reserve(func.parameters.size()); - for(NodeArray::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) - param_type_ids.push_back(get_variable_type_id(**i)); + for(const RefPtr &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]; @@ -1790,8 +1788,8 @@ void SpirVGenerator::visit(FunctionDeclaration &func) writer.begin_op(content.globals, OP_TYPE_FUNCTION); writer.write(type_id); writer.write(return_type_id); - for(vector::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); diff --git a/source/glsl/syntax.cpp b/source/glsl/syntax.cpp index 4526bf2b..502c2748 100644 --- a/source/glsl/syntax.cpp +++ b/source/glsl/syntax.cpp @@ -1,3 +1,4 @@ +#include #include #include "syntax.h" #include "visitor.h" @@ -69,8 +70,8 @@ template NodeContainer::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(); } @@ -491,10 +492,8 @@ string get_unused_variable_name(const Block &block, const string &base) int get_layout_value(const Layout &layout, const string &name, int def_value) { - for(vector::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) diff --git a/source/glsl/tokenizer.cpp b/source/glsl/tokenizer.cpp index bd455e3c..595a97ab 100644 --- a/source/glsl/tokenizer.cpp +++ b/source/glsl/tokenizer.cpp @@ -95,7 +95,7 @@ void Tokenizer::preprocess() { SetForScope > clear_tokens(next_tokens, deque()); - string::const_iterator line_end = iter; + auto line_end = iter; for(; (line_end!=source_end && *line_end!='\n'); ++line_end) ; SetForScope stop_at_line_end(source_end, line_end); diff --git a/source/glsl/validate.cpp b/source/glsl/validate.cpp index fdff3fe6..ce30c2ab 100644 --- a/source/glsl/validate.cpp +++ b/source/glsl/validate.cpp @@ -61,16 +61,16 @@ const char *DeclarationValidator::describe_variable(ScopeType scope) void DeclarationValidator::visit(Layout &layout) { - for(vector::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; @@ -91,7 +91,7 @@ void DeclarationValidator::visit(Layout &layout) err_descr = "non-uniform interface block"; } } - else if(i->name=="constant_id") + else if(q.name=="constant_id") { allowed = (variable && scope==GLOBAL); if(allowed) @@ -113,35 +113,35 @@ void DeclarationValidator::visit(Layout &layout) } } } - 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) @@ -167,12 +167,12 @@ void DeclarationValidator::visit(Layout &layout) 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)); } } @@ -298,8 +298,8 @@ void DeclarationValidator::visit(InterfaceBlock &iface) void DeclarationValidator::visit(FunctionDeclaration &func) { SetForScope set_scope(scope, FUNCTION_PARAM); - for(NodeArray::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) - (*i)->visit(*this); + for(const RefPtr &p: func.parameters) + p->visit(*this); scope = FUNCTION; func.body.visit(*this); } @@ -318,7 +318,7 @@ void IdentifierValidator::multiple_definition(const string &name, Statement &sta 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]; @@ -362,7 +362,7 @@ void IdentifierValidator::visit(VariableDeclaration &var) void IdentifierValidator::visit(InterfaceBlock &iface) { string key = format("%s %s", iface.interface, iface.block_name); - map::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 @@ -382,16 +382,15 @@ void IdentifierValidator::visit(InterfaceBlock &iface) if(iface.instance_name.empty() && iface.struct_declaration) { // Inject anonymous interface block members into the global scope - const map &iface_vars = iface.struct_declaration->members.variables; - for(map::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::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) @@ -460,7 +459,7 @@ void ReferenceValidator::visit(FunctionCall &call) bool have_declaration = call.constructor; if(!call.constructor) { - map::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); } @@ -468,7 +467,7 @@ void ReferenceValidator::visit(FunctionCall &call) { bool valid_types = true; string signature; - for(NodeArray::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); @@ -705,14 +704,14 @@ FlowControlValidator::FlowControlValidator(): void FlowControlValidator::visit(Block &block) { - for(NodeList::const_iterator i=block.body.begin(); i!=block.body.end(); ++i) + for(const RefPtr &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); } } @@ -747,10 +746,7 @@ void FlowControlValidator::visit(Iteration &iter) int StageInterfaceValidator::get_location(const Layout &layout) { - for(vector::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) @@ -791,7 +787,7 @@ void StageInterfaceValidator::visit(VariableDeclaration &var) unsigned loc_count = LocationCounter().apply(var); for(unsigned i=0; i::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)); @@ -806,16 +802,16 @@ void StageInterfaceValidator::visit(VariableDeclaration &var) void GlobalInterfaceValidator::apply(Module &module) { - for(list::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::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) @@ -845,7 +841,7 @@ void GlobalInterfaceValidator::check_uniform(const Uniform &uni) if(uni.location>=0) { - map::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) @@ -864,7 +860,7 @@ void GlobalInterfaceValidator::check_uniform(const Uniform &uni) if(uni.bind_point>=0) { map &used = used_bindings[uni.desc_set]; - map::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) diff --git a/source/glsl/visitor.cpp b/source/glsl/visitor.cpp index 3e480484..5e56d919 100644 --- a/source/glsl/visitor.cpp +++ b/source/glsl/visitor.cpp @@ -12,8 +12,8 @@ void TraversingVisitor::visit(Block &block) if(&block!=current_block) enter(block); SetForScope set_block(current_block, &block); - for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) - (*i)->visit(*this); + for(const RefPtr &s: block.body) + s->visit(*this); } void TraversingVisitor::visit(RefPtr &expr) @@ -57,8 +57,8 @@ void TraversingVisitor::visit(TernaryExpression &ternary) void TraversingVisitor::visit(FunctionCall &call) { - for(NodeArray::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) - visit(*i); + for(RefPtr &a: call.arguments) + visit(a); } void TraversingVisitor::visit(ExpressionStatement &expr) @@ -98,8 +98,8 @@ void TraversingVisitor::visit(FunctionDeclaration &func) { enter(func.body); SetForScope set_block(current_block, &func.body); - for(NodeArray::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) - (*i)->visit(*this); + for(const RefPtr &p: func.parameters) + p->visit(*this); func.body.visit(*this); } @@ -152,7 +152,7 @@ void NodeRemover::apply(Stage &s, const set &tr) template void NodeRemover::remove_from_map(map &vars, const string &key, T &node) { - typename map::iterator i = vars.find(key); + auto i = vars.find(key); if(i!=vars.end() && i->second==&node) vars.erase(i); } @@ -160,7 +160,7 @@ void NodeRemover::remove_from_map(map &vars, const string &key, T & void NodeRemover::visit(Block &block) { SetForScope set_block(current_block, &block); - for(NodeList::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())) @@ -234,15 +234,12 @@ void NodeReorderer::apply(Stage &stage, Node &before, const set &tr) void NodeReorderer::visit(Block &block) { - NodeList::iterator insert_point = block.body.end(); - for(NodeList::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::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) diff --git a/source/materials/lighting.cpp b/source/materials/lighting.cpp index 699aabc0..91bc4d06 100644 --- a/source/materials/lighting.cpp +++ b/source/materials/lighting.cpp @@ -74,7 +74,7 @@ void Lighting::attach(const Light &l) void Lighting::detach(const Light &l) { - vector::iterator i = find_member(lights, &l, &AttachedLight::light); + auto i = find_member(lights, &l, &AttachedLight::light); if(i!=lights.end()) lights.erase(i); } diff --git a/source/materials/material.cpp b/source/materials/material.cpp index 2d7e4487..d03a72a5 100644 --- a/source/materials/material.cpp +++ b/source/materials/material.cpp @@ -18,12 +18,12 @@ const Program *Material::create_compatible_shader(const map &extra_ map spec_values; fill_program_info(module_name, spec_values); - for(map::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::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)); diff --git a/source/materials/renderpass.cpp b/source/materials/renderpass.cpp index 9a7ceef7..f144a5dd 100644 --- a/source/materials/renderpass.cpp +++ b/source/materials/renderpass.cpp @@ -61,7 +61,7 @@ void RenderPass::set_shader_program(const Program *prog, const ProgramData *data Tag RenderPass::get_slotted_uniform_tag(Tag slot) const { - map::const_iterator i = uniform_slots.find(slot); + auto i = uniform_slots.find(slot); if(i==uniform_slots.end()) return Tag(); return i->second; @@ -76,7 +76,7 @@ void RenderPass::set_material(const Material *mat) void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp) { - vector::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)); @@ -89,7 +89,7 @@ void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp) Tag RenderPass::get_texture_tag(const string &slot) const { - vector::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()); } @@ -99,28 +99,27 @@ void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler * throw invalid_operation("RenderPass::set_texture"); const vector &uniforms = shprog->get_uniforms(); - for(vector::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i) - if(is_image(i->type) && i->binding==static_cast(index)) - return set_texture(i->tag, tex, samp); + for(const Program::UniformInfo &u: uniforms) + if(is_image(u.type) && u.binding==static_cast(index)) + return set_texture(u.tag, tex, samp); if(shdata) { - const vector &tags = shdata->get_uniform_tags(); - for(vector::const_iterator i=tags.begin(); i!=tags.end(); ++i) + for(Tag t: shdata->get_uniform_tags()) { - vector::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(shdata->find_uniform(*i))) + if(const Uniform1i *uni1i = dynamic_cast(shdata->find_uniform(t))) if(uni1i->get()==static_cast(index)) - return set_texture(*i, tex, samp); + return set_texture(t, tex, samp); } } } int RenderPass::get_texture_index(const string &n) const { - vector::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); } @@ -136,8 +135,8 @@ void RenderPass::set_receive_shadows(bool rs) void RenderPass::apply(Renderer &renderer) const { - for(vector::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()); @@ -237,7 +236,7 @@ void RenderPass::Loader::shader(const string &n) void RenderPass::Loader::texture(const string &n) { - vector::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)); @@ -253,16 +252,15 @@ void RenderPass::Loader::texunit(unsigned) string name; if(obj.shprog) { - const vector &uniforms = obj.shprog->get_uniforms(); - for(vector::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; } } diff --git a/source/materials/technique.cpp b/source/materials/technique.cpp index 26a70a01..ef176411 100644 --- a/source/materials/technique.cpp +++ b/source/materials/technique.cpp @@ -31,19 +31,19 @@ const RenderPass &Technique::get_pass(Tag tag) const 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; } } @@ -54,12 +54,12 @@ bool Technique::replace_texture(const string &slot, const Texture &tex) 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; } } @@ -71,24 +71,24 @@ bool Technique::replace_uniforms(const ProgramData &shdata) { bool replaced = false; const vector &uniform_tags = shdata.get_uniform_tags(); - for(PassMap::iterator i=passes.begin(); i!=passes.end(); ++i) + for(auto &kvp: passes) { RefPtr new_shdata; - for(vector::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; @@ -96,8 +96,8 @@ bool Technique::replace_uniforms(const ProgramData &shdata) 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; } @@ -105,8 +105,8 @@ bool Technique::has_shaders() const void Technique::set_debug_name(const std::string &name) { #ifdef DEBUG - for(map::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 @@ -182,9 +182,9 @@ void Technique::InheritLoader::material(const string &slot, const string &name) 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); diff --git a/source/materials/technique.h b/source/materials/technique.h index bfd979aa..74c37fc8 100644 --- a/source/materials/technique.h +++ b/source/materials/technique.h @@ -49,18 +49,14 @@ private: void uniforms(); }; -public: - typedef std::map PassMap; - -private: - PassMap passes; + std::map 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 &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 &); diff --git a/source/render/instancearray.cpp b/source/render/instancearray.cpp index 293fa6cd..a53a9881 100644 --- a/source/render/instancearray.cpp +++ b/source/render/instancearray.cpp @@ -28,10 +28,9 @@ InstanceArray::InstanceArray(const Object &o): 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"); @@ -65,8 +64,8 @@ InstanceArray::InstanceArray(const Object &o): InstanceArray::~InstanceArray() { - for(vector::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; @@ -94,7 +93,7 @@ void InstanceArray::append(ObjectInstance *inst) void InstanceArray::remove(ObjectInstance &inst) { - vector::iterator i = find(instances, &inst); + auto i = find(instances, &inst); if(i==instances.end()) throw key_error(&inst); @@ -141,12 +140,12 @@ void InstanceArray::render(Renderer &renderer, Tag tag) const } else { - for(vector::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); } } } diff --git a/source/render/object.cpp b/source/render/object.cpp index 067686d2..8a3963ee 100644 --- a/source/render/object.cpp +++ b/source/render/object.cpp @@ -76,12 +76,12 @@ void Object::set_mesh(unsigned i, const Mesh *m) void Object::update_bounding_sphere() { vector points; - for(vector::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; diff --git a/source/render/occludedscene.cpp b/source/render/occludedscene.cpp index 7c043fa6..23d79df2 100644 --- a/source/render/occludedscene.cpp +++ b/source/render/occludedscene.cpp @@ -26,8 +26,8 @@ OccludedScene::~OccludedScene() { vector 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]); } @@ -58,9 +58,9 @@ void OccludedScene::populate_cache() const 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; @@ -73,14 +73,14 @@ void OccludedScene::populate_cache() const 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 @@ -93,8 +93,8 @@ 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; } @@ -105,7 +105,7 @@ void OccludedScene::render(Renderer &renderer, Tag tag) const // 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) @@ -128,7 +128,7 @@ void OccludedScene::render(Renderer &renderer, Tag tag) const } // 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; @@ -146,7 +146,7 @@ void OccludedScene::render(Renderer &renderer, Tag tag) const 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); @@ -163,7 +163,7 @@ void OccludedScene::render(Renderer &renderer, Tag tag) const } // 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; diff --git a/source/render/occludedscene.h b/source/render/occludedscene.h index 9140d13b..056acb2e 100644 --- a/source/render/occludedscene.h +++ b/source/render/occludedscene.h @@ -31,14 +31,11 @@ private: OccludedRenderable(); }; - typedef std::set RenderableSet; - typedef std::vector OccludedArray; - const Mesh &bounding_mesh; const Program &bounding_shader; - RenderableSet renderables; + std::set renderables; float occluder_min_size; - mutable OccludedArray occluded_cache; + mutable std::vector occluded_cache; mutable bool cache_dirty; public: diff --git a/source/render/orderedscene.cpp b/source/render/orderedscene.cpp index 4386d3e7..5912e747 100644 --- a/source/render/orderedscene.cpp +++ b/source/render/orderedscene.cpp @@ -1,4 +1,4 @@ -#include +#include #include "orderedscene.h" #include "renderer.h" @@ -12,7 +12,7 @@ void OrderedScene::add(Renderable &r) 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()); } @@ -23,42 +23,41 @@ void OrderedScene::prepend(Renderable &r) 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); } } diff --git a/source/render/orderedscene.h b/source/render/orderedscene.h index 0ca1c465..aa43eb73 100644 --- a/source/render/orderedscene.h +++ b/source/render/orderedscene.h @@ -17,9 +17,7 @@ public: using Scene::Loader; private: - typedef std::list RenderableList; - - RenderableList renderables; + std::list renderables; public: virtual void add(Renderable &); diff --git a/source/render/programdata.cpp b/source/render/programdata.cpp index 79b55901..e8e30e63 100644 --- a/source/render/programdata.cpp +++ b/source/render/programdata.cpp @@ -36,8 +36,8 @@ ProgramData::ProgramData(const ProgramData &other): buffer(0), dirty(0) { - for(vector::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): @@ -48,13 +48,13 @@ ProgramData::ProgramData(const ProgramData &other, const Program *p): { if(tied_program) { - for(vector::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::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) @@ -62,11 +62,11 @@ ProgramData &ProgramData::operator=(const ProgramData &other) tied_program = other.tied_program; uniforms = other.uniforms; - for(vector::iterator i=uniforms.begin(); i!=uniforms.end(); ++i) - i->value = i->value->clone(); + for(TaggedUniform &u: uniforms) + u.value = u.value->clone(); - for(vector::iterator i=blocks.begin(); i!=blocks.end(); ++i) - delete i->block; + for(SharedBlock &b: blocks) + delete b.block; programs.clear(); last_buffer_block = 0; @@ -78,13 +78,13 @@ ProgramData &ProgramData::operator=(const ProgramData &other) ProgramData::~ProgramData() { - for(vector::iterator i=uniforms.begin(); i!=uniforms.end(); ++i) - delete i->value; - for(vector::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; } @@ -181,7 +181,7 @@ void ProgramData::add_uniform(Tag tag, Uniform *uni) throw too_many_uniforms(tag.str()); } - vector::iterator j = lower_bound_member(uniforms, tag, &TaggedUniform::tag); + auto j = lower_bound_member(uniforms, tag, &TaggedUniform::tag); TaggedUniform nu; nu.tag = tag; @@ -431,7 +431,7 @@ void ProgramData::uniform_matrix4_array(Tag tag, unsigned n, const float *v) void ProgramData::remove_uniform(Tag tag) { - vector::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; @@ -445,8 +445,8 @@ vector ProgramData::get_uniform_tags() const { vector tags; tags.reserve(uniforms.size()); - for(vector::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; } @@ -466,14 +466,14 @@ const Uniform *ProgramData::find_uniform(Tag tag) const int ProgramData::find_uniform_index(Tag tag) const { - vector::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::iterator ProgramData::get_program(const Program &prog) const { Program::LayoutHash prog_hash = prog.get_uniform_layout_hash(); - vector::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; @@ -485,8 +485,8 @@ vector::iterator ProgramData::get_program(const Progr the hashes so they can be matched up later. */ vector block_hashes; block_hashes.reserve(programs.size()); - for(vector::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::iterator ProgramData::get_program(const Progr block_hashes[index+1+j] = info.layout_hash; programs[index+1+j].bind_point = info.bind_point; - vector::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)); @@ -508,7 +508,7 @@ vector::iterator ProgramData::get_program(const Progr unsigned hash = block_hashes[j]; if(hash) { - vector::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 @@ -600,7 +600,7 @@ void ProgramData::update_block(SharedBlock &block, const Program::UniformBlockIn vector::const_iterator ProgramData::prepare_program(const Program &prog) const { BufferBackedUniformBlock *old_last_block = last_buffer_block; - vector::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; @@ -611,11 +611,11 @@ vector::const_iterator ProgramData::prepare_program(c program will cause this to happen if there's any dirty uniforms. */ if(affected) { - for(vector::iterator i=blocks.begin(); i!=blocks.end(); ++i) - i->dirty |= (dirty&i->used) | force_dirty; - for(vector::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; } @@ -626,29 +626,31 @@ vector::const_iterator ProgramData::prepare_program(c /* 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::iterator j = prog_begin+1; - for(vector::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::iterator j = prog_begin+1; - for(vector::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; @@ -680,9 +682,9 @@ vector::const_iterator ProgramData::prepare_program(c void ProgramData::apply(const Program &prog, PipelineState &state) const { - vector::const_iterator prog_begin = prepare_program(prog); + auto prog_begin = prepare_program(prog); Program::LayoutHash prog_hash = prog_begin->prog_hash; - for(vector::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) diff --git a/source/render/renderer.cpp b/source/render/renderer.cpp index 5610f30a..6f7e9cba 100644 --- a/source/render/renderer.cpp +++ b/source/render/renderer.cpp @@ -89,7 +89,7 @@ void Renderer::set_texture(Tag tag, const Texture *tex, const Sampler *samp) flush_textures(); } - for(vector::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(); @@ -319,15 +319,15 @@ void Renderer::apply_state() if(state->texture_count::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::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); @@ -335,10 +335,10 @@ void Renderer::apply_state() { if(extra_shdata) shdata_stack.erase(shdata_stack.begin()+state->shdata_count, shdata_stack.end()); - for(vector::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; } diff --git a/source/render/rendertarget.cpp b/source/render/rendertarget.cpp index 6a114285..b144580c 100644 --- a/source/render/rendertarget.cpp +++ b/source/render/rendertarget.cpp @@ -41,8 +41,8 @@ RenderTarget::RenderTarget(unsigned w, unsigned h, const FrameFormat &f): RenderTarget::~RenderTarget() { - for(vector::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 diff --git a/source/render/sequence.cpp b/source/render/sequence.cpp index 3460bc72..e953b4d6 100644 --- a/source/render/sequence.cpp +++ b/source/render/sequence.cpp @@ -57,9 +57,9 @@ void Sequence::init(unsigned w, unsigned h, const FrameFormat &f) Sequence::~Sequence() { - for(vector::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; @@ -94,15 +94,15 @@ void Sequence::add_postprocessor(PostProcessor *pp, bool owned) void Sequence::setup_frame(Renderer &renderer) { - for(vector::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::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(); } @@ -121,20 +121,20 @@ void Sequence::render(Renderer &renderer, Tag tag) const renderer.clear(); } - for(vector::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]) diff --git a/source/render/simplescene.cpp b/source/render/simplescene.cpp index af2b366f..bcc4af21 100644 --- a/source/render/simplescene.cpp +++ b/source/render/simplescene.cpp @@ -29,14 +29,14 @@ void SimpleScene::populate_cache() const 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 @@ -44,14 +44,14 @@ 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); } } diff --git a/source/render/simplescene.h b/source/render/simplescene.h index 2fb89a31..1e90874b 100644 --- a/source/render/simplescene.h +++ b/source/render/simplescene.h @@ -16,11 +16,8 @@ public: using Scene::Loader; private: - typedef std::set RenderableSet; - typedef std::vector RenderableArray; - - RenderableSet renderables; - mutable RenderableArray cache; + std::set renderables; + mutable std::vector cache; public: virtual void add(Renderable &); diff --git a/source/render/zsortedscene.cpp b/source/render/zsortedscene.cpp index 2e57afd5..7b64fb2b 100644 --- a/source/render/zsortedscene.cpp +++ b/source/render/zsortedscene.cpp @@ -46,14 +46,14 @@ void ZSortedScene::populate_cache() const 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 @@ -66,8 +66,8 @@ 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; } @@ -77,25 +77,25 @@ void ZSortedScene::render(Renderer &renderer, Tag tag) const 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 *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 *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) @@ -104,7 +104,7 @@ void ZSortedScene::render(Renderer &renderer, Tag tag) const 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)) { @@ -117,7 +117,7 @@ void ZSortedScene::render(Renderer &renderer, Tag tag) const ++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); } diff --git a/source/render/zsortedscene.h b/source/render/zsortedscene.h index 319cd8c3..9653567c 100644 --- a/source/render/zsortedscene.h +++ b/source/render/zsortedscene.h @@ -42,13 +42,10 @@ private: bool operator<(const SortedRenderable &o) const { return depth RenderableSet; - typedef std::vector SortedArray; - - RenderableSet renderables; + std::set renderables; SortOrder order; DepthReference reference; - mutable SortedArray sorted_cache; + mutable std::vector sorted_cache; public: ZSortedScene(); diff --git a/source/resources/resourcemanager.cpp b/source/resources/resourcemanager.cpp index 30f75013..4bac0aec 100644 --- a/source/resources/resourcemanager.cpp +++ b/source/resources/resourcemanager.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -118,8 +118,7 @@ void ResourceManager::load_resource(Resource &r) 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 @@ -156,15 +155,15 @@ void ResourceManager::remove_resource(Resource &r) 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::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); @@ -202,9 +201,9 @@ void ResourceManager::tick() 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_unloadlast_used+min_retain_framesstate = ManagedResource::NOT_LOADED; + for(ManagedResource *r: queue) + r->state = ManagedResource::NOT_LOADED; queue.clear(); return; } @@ -237,11 +236,11 @@ void ResourceManager::unload_by_age() { 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_usedsecond.unload(); - total_data_size -= i->second.data_size; + kvp.second.unload(); + total_data_size -= kvp.second.data_size; } } @@ -253,13 +252,13 @@ void ResourceManager::unload_by_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_usedsecond.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; } } @@ -336,8 +335,8 @@ void ResourceManager::ManagedResource::finish_loading(bool successful) state = LOADED; data_size = resource->get_data_size(); - for(vector::const_iterator i=observers.begin(); i!=observers.end(); ++i) - (*i)->resource_loaded(*resource); + for(ResourceObserver *o: observers) + o->resource_loaded(*resource); } else { @@ -356,8 +355,8 @@ void ResourceManager::ManagedResource::unload() resource->unload(); state = NOT_LOADED; - for(vector::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) @@ -368,7 +367,7 @@ void ResourceManager::ManagedResource::add_observer(ResourceObserver &w) void ResourceManager::ManagedResource::remove_observer(ResourceObserver &w) { - vector::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()); } @@ -415,7 +414,7 @@ void ResourceManager::LoadingThread::main() } } -ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(LoadQueue &que) +ResourceManager::ManagedResource *ResourceManager::LoadingThread::front(list &que) { MutexLock lock(queue_mutex); if(que.empty()) @@ -459,10 +458,10 @@ bool ResourceManager::LoadingThread::try_remove_resource(ManagedResource &r) { 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); diff --git a/source/resources/resourcemanager.h b/source/resources/resourcemanager.h index ba4f39a9..25969997 100644 --- a/source/resources/resourcemanager.h +++ b/source/resources/resourcemanager.h @@ -76,15 +76,13 @@ private: void remove_observer(ResourceObserver &); }; - typedef std::list LoadQueue; - class LoadingThread: public Thread { private: Semaphore sem; Mutex queue_mutex; - LoadQueue async_queue; - LoadQueue sync_queue; + std::list async_queue; + std::list sync_queue; unsigned capacity; unsigned size; std::list error_queue; @@ -98,7 +96,7 @@ private: private: virtual void main(); - ManagedResource *front(LoadQueue &); + ManagedResource *front(std::list &); public: void add_resource(ManagedResource &); @@ -113,13 +111,11 @@ private: void terminate(); }; - typedef std::map ResourceMap; - LoadingPolicy policy; bool async_loads; mutable Mutex map_mutex; - ResourceMap resources; - LoadQueue queue; + std::map resources; + std::list queue; UInt64 total_data_size; UInt64 size_limit; unsigned frame; diff --git a/tests/glsl/glslcompiler.cpp b/tests/glsl/glslcompiler.cpp index 35bbc8bc..dbac0f51 100644 --- a/tests/glsl/glslcompiler.cpp +++ b/tests/glsl/glslcompiler.cpp @@ -80,8 +80,8 @@ using namespace Msp; void GlslCompilerHelper::load_all_test_cases(const FS::Path &tests_dir) { - list test_files = FS::list_filtered(tests_dir, "\\.glsl$"); - test_files.sort(); + vector test_files = FS::list_filtered(tests_dir, "\\.glsl$"); + sort(test_files); for(const auto &fn: test_files) load_test_case((tests_dir/fn).str()); } -- 2.43.0