From 2e7f19b895424c3a77940e648639f8df2b395d0f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 6 Sep 2011 23:46:52 +0300 Subject: [PATCH] Rework exceptions --- source/batch.cpp | 15 ++++++------ source/bloom.cpp | 4 ++-- source/buffer.cpp | 5 +++- source/error.h | 47 +++++++++++++++++++++++++++++++++++++ source/except.h | 33 -------------------------- source/extension.cpp | 6 ++--- source/extension.h | 2 +- source/font.cpp | 2 +- source/framebuffer.cpp | 9 +++---- source/immediate.cpp | 3 ++- source/light.cpp | 4 ++-- source/matrix.cpp | 12 +++++----- source/object.cpp | 5 ++-- source/pixelformat.cpp | 4 ++-- source/primitivebuilder.cpp | 15 +++++++----- source/program.cpp | 6 ++--- source/renderable.cpp | 4 ++-- source/renderer.cpp | 8 +++---- source/renderpass.cpp | 5 ++-- source/shader.cpp | 4 ++-- source/texture.cpp | 8 +++---- source/texture2d.cpp | 21 +++++++---------- source/texture2d.h | 1 - source/texture3d.cpp | 25 +++++++++----------- source/texture3d.h | 1 - source/texturing.cpp | 4 +++- source/texunit.cpp | 3 ++- source/vertexarray.cpp | 3 ++- source/vertexformat.cpp | 17 +++++++------- source/windingtest.cpp | 3 ++- 30 files changed, 149 insertions(+), 130 deletions(-) create mode 100644 source/error.h delete mode 100644 source/except.h diff --git a/source/batch.cpp b/source/batch.cpp index 1260c6ab..b4b88176 100644 --- a/source/batch.cpp +++ b/source/batch.cpp @@ -1,6 +1,7 @@ #include "batch.h" #include "bindable.h" #include "buffer.h" +#include "error.h" #include "extension.h" #include "nv_primitive_restart.h" #include "vertexarray.h" @@ -33,11 +34,11 @@ Batch::~Batch() void Batch::set_data_type(DataType t) { if(t!=UNSIGNED_BYTE && t!=UNSIGNED_SHORT && t!=UNSIGNED_INT) - throw InvalidParameterValue("Batch data type must be an unsigned integer"); + throw invalid_argument("Batch::set_data_type"); if(t==UNSIGNED_BYTE && max_index>0xFE) - throw InvalidState("UNSIGNED_BYTE can't hold all indices in Batch"); + throw invalid_operation("Batch::set_data_type"); else if(t==UNSIGNED_SHORT && max_index>0xFFFE) - throw InvalidState("UNSIGNED_SHORT can't hold all indices in Batch"); + throw invalid_operation("Batch::set_data_type"); if(data_type==UNSIGNED_BYTE && t==UNSIGNED_SHORT) expand_data(); @@ -60,7 +61,7 @@ void Batch::set_data_type(DataType t) void Batch::use_index_buffer(Buffer *buf, Batch *prev) { if(buf && prev && prev->ibuf!=buf) - throw InvalidParameterValue("Previous batch is not in the same buffer"); + throw invalid_argument("Batch::use_index_buffer"); if(!buf) { @@ -153,11 +154,11 @@ void Batch::append(const vector &ind) void Batch::append(const Batch &other) { if(other.prim_type!=prim_type) - throw InvalidParameterValue("Can't concatenate batches with different primitive types"); + throw invalid_argument("Batch::append"); if(prim_type==LINE_STRIP || prim_type==LINE_LOOP) - throw InvalidState("Can't concatenate line strips or loops"); + throw incompatible_data("Batch::append"); else if(prim_type==POLYGON) - throw InvalidState("Can't concatenate polygons"); + throw incompatible_data("Batch::append"); else if(prim_type==TRIANGLE_FAN) static RequireExtension _ext("GL_NV_primitive_restart"); diff --git a/source/bloom.cpp b/source/bloom.cpp index f1b570bf..76460b99 100644 --- a/source/bloom.cpp +++ b/source/bloom.cpp @@ -95,7 +95,7 @@ Bloom::Bloom(unsigned w, unsigned h): void Bloom::set_radius(float r) { if(r<=0.0f) - throw InvalidParameterValue("Radius must be positive"); + throw out_of_range("Bloom::set_radius"); int size = min(static_cast(r*3.0f), 9); blur_shdata_common.uniform("size", size); @@ -113,7 +113,7 @@ void Bloom::set_radius(float r) void Bloom::set_strength(float s) { if(s<0.0f || s>1.0f) - throw InvalidParameterValue("Strength must be in the range [0.0, 1.0]"); + throw out_of_range("Bloom::set_strength"); combine_shdata.uniform("strength", s); } diff --git a/source/buffer.cpp b/source/buffer.cpp index 395057cd..fbb1e159 100644 --- a/source/buffer.cpp +++ b/source/buffer.cpp @@ -1,7 +1,10 @@ +#include #include "arb_vertex_buffer_object.h" #include "extension.h" #include "buffer.h" +using namespace std; + namespace Msp { namespace GL { @@ -70,7 +73,7 @@ const Buffer *&Buffer::binding(BufferType type) case ELEMENT_ARRAY_BUFFER: return bound[1]; case PIXEL_PACK_BUFFER: return bound[2]; case PIXEL_UNPACK_BUFFER: return bound[3]; - default: throw InvalidParameterValue("Invalid buffer type"); + default: throw invalid_argument("Buffer::binding"); } } diff --git a/source/error.h b/source/error.h new file mode 100644 index 00000000..bb714506 --- /dev/null +++ b/source/error.h @@ -0,0 +1,47 @@ +#ifndef MSP_GL_ERROR_H_ +#define MSP_GL_ERROR_H_ + +#include + +namespace Msp { +namespace GL { + +class unsupported_extension: public std::runtime_error +{ +public: + unsupported_extension(const std::string &w): std::runtime_error(w) { } + virtual ~unsupported_extension() throw() { } +}; + +class invalid_operation: public std::logic_error +{ +public: + invalid_operation(const std::string &w): std::logic_error(w) { } + virtual ~invalid_operation() throw() { } +}; + +class stack_underflow: public std::logic_error +{ +public: + stack_underflow(const std::string &w): std::logic_error(w) { } + virtual ~stack_underflow() throw() { } +}; + +class incompatible_data: public std::logic_error +{ +public: + incompatible_data(const std::string &w): std::logic_error(w) { } + virtual ~incompatible_data() throw() { } +}; + +class compile_error: public std::runtime_error +{ +public: + compile_error(const std::string &w): std::runtime_error(w) { } + virtual ~compile_error() throw() { } +}; + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/except.h b/source/except.h deleted file mode 100644 index 74467e40..00000000 --- a/source/except.h +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef MSP_GL_EXCEPT_H_ -#define MSP_GL_EXCEPT_H_ - -#include - -namespace Msp { -namespace GL { - -class IncompatibleData: public Exception -{ -public: - IncompatibleData(const std::string &w_): Exception(w_) { } - ~IncompatibleData() throw() { } -}; - -class UnsupportedExtension: public Exception -{ -public: - UnsupportedExtension(const std::string &w_): Exception(w_+" is not supported") { } - ~UnsupportedExtension() throw() { } -}; - -class CompileError: public Exception -{ -public: - CompileError(const std::string &w_): Exception(w_) { } - ~CompileError() throw() { } -}; - -} // namespace GL -} // namespace Msp - -#endif diff --git a/source/extension.cpp b/source/extension.cpp index e47bbe19..ef05e39b 100644 --- a/source/extension.cpp +++ b/source/extension.cpp @@ -9,10 +9,10 @@ #include "arb_vertex_buffer_object.h" #include "arb_vertex_program.h" #include "arb_vertex_shader.h" +#include "error.h" #include "ext_framebuffer_blit.h" #include "ext_framebuffer_multisample.h" #include "ext_framebuffer_object.h" -#include "except.h" #include "extension.h" #include "gl.h" #include "nv_primitive_restart.h" @@ -65,7 +65,7 @@ bool is_supported(const string &ext) void require_extension(const string &ext) { if(!is_supported(ext)) - throw UnsupportedExtension(ext); + throw unsupported_extension(ext); } const Version &get_gl_version() @@ -101,7 +101,7 @@ bool is_version_at_least(unsigned a, unsigned b) void require_version(unsigned a, unsigned b) { if(!is_version_at_least(a, b)) - throw UnsupportedExtension(format("OpenGL %d.%d", a, b)); + throw unsupported_extension(format("OpenGL %d.%d", a, b)); } ExtFunc *get_proc_address(const string &name) diff --git a/source/extension.h b/source/extension.h index 69308241..4072869c 100644 --- a/source/extension.h +++ b/source/extension.h @@ -1,7 +1,7 @@ #ifndef MSP_GL_EXTENSION_H_ #define MSP_GL_EXTENSION_H_ -#include +#include namespace Msp { namespace GL { diff --git a/source/font.cpp b/source/font.cpp index 88780331..33c1584b 100644 --- a/source/font.cpp +++ b/source/font.cpp @@ -26,7 +26,7 @@ void Font::set_texture(const Texture2D &t) const Texture2D &Font::get_texture() const { if(!texture) - throw InvalidState("No texture"); + throw logic_error("No texture"); return *texture; } diff --git a/source/framebuffer.cpp b/source/framebuffer.cpp index 3ac742bd..6877f480 100644 --- a/source/framebuffer.cpp +++ b/source/framebuffer.cpp @@ -1,3 +1,4 @@ +#include "error.h" #include "extension.h" #include "ext_framebuffer_blit.h" #include "ext_framebuffer_object.h" @@ -16,7 +17,7 @@ Framebuffer::Framebuffer(unsigned i): dirty(0) { if(id) - throw InvalidParameterValue("System framebuffer must have id 0"); + throw invalid_argument("System framebuffer must have id 0"); int viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); @@ -98,7 +99,7 @@ void Framebuffer::check_size() void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf) { if(!id) - throw InvalidState("Can't attach to system framebuffer"); + throw invalid_operation("Framebuffer::attach"); unsigned i = get_attachment_index(attch); attachments[i].set(rbuf); @@ -109,7 +110,7 @@ void Framebuffer::attach(FramebufferAttachment attch, Renderbuffer &rbuf) void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned level) { if(!id) - throw InvalidState("Can't attach to system framebuffer"); + throw invalid_operation("Framebuffer::attach"); unsigned i = get_attachment_index(attch); attachments[i].set(tex, level); @@ -120,7 +121,7 @@ void Framebuffer::attach(FramebufferAttachment attch, Texture2D &tex, unsigned l void Framebuffer::detach(FramebufferAttachment attch) { if(!id) - throw InvalidState("Can't detach from system framebuffer"); + throw invalid_operation("Framebuffer::detach"); unsigned i = get_attachment_index(attch); attachments[i].clear(); diff --git a/source/immediate.cpp b/source/immediate.cpp index 252eed33..cbcc37b9 100644 --- a/source/immediate.cpp +++ b/source/immediate.cpp @@ -1,4 +1,5 @@ #include "batch.h" +#include "error.h" #include "immediate.h" namespace Msp { @@ -14,7 +15,7 @@ Immediate::Immediate(VertexFormat f): void Immediate::reset() { if(in_batch) - throw InvalidState("Can't reset Immediate between begin() and end()"); + throw invalid_operation("Immediate::reset"); array.clear(); } diff --git a/source/light.cpp b/source/light.cpp index 39693c40..067cc3d1 100644 --- a/source/light.cpp +++ b/source/light.cpp @@ -1,4 +1,4 @@ -#include "except.h" +#include #include "light.h" #include "misc.h" @@ -94,7 +94,7 @@ void Light::activate(unsigned i) static unsigned max_lights = get_i(GL_MAX_LIGHTS); if(i>=max_lights) - throw InvalidParameterValue("Light unit index out of range"); + throw out_of_range("Light::activate"); if(i>=current_lights.size()) current_lights.resize(i+1); diff --git a/source/matrix.cpp b/source/matrix.cpp index f5089285..549fe8b6 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include "error.h" #include "matrix.h" using namespace std; @@ -149,7 +149,7 @@ Vector4 Matrix::operator*(const Vector4 &vec) const double Matrix::operator[](unsigned i) const { if(i>=16) - throw InvalidParameterValue("Matrix element index out of range"); + throw out_of_range("Matrix::operator[]"); return matrix[i]; } @@ -215,7 +215,7 @@ Matrix Matrix::scaling(double x, double y, double z) Matrix Matrix::ortho(double l, double r, double b, double t, double n, double f) { if(l==r || b==t || n==f) - throw InvalidParameterValue("Orthogonal projection can't have zero dimension"); + throw invalid_argument("Matrix::ortho"); Matrix result; result.matrix[0] = 2/(r-l); @@ -246,7 +246,7 @@ Matrix Matrix::ortho_topleft(double w, double h) Matrix Matrix::frustum(double l, double r, double b, double t, double n, double f) { if(l==r || b==t || n<=0 || f<=n) - throw InvalidParameterValue("Invalid frustum parameters"); + throw invalid_argument("Matrix::frustum"); Matrix result; result.matrix[0] = 2*n/(r-l); @@ -312,7 +312,7 @@ void MatrixStack::push() void MatrixStack::pop() { if(matrices.size()==1) - throw InvalidState("Can't pop the last matrix"); + throw stack_underflow("MatrixStack::pop()"); matrices.pop_back(); update(); @@ -368,7 +368,7 @@ void matrix_mode(MatrixMode m) else if(m==PROJECTION) active_stack = &MatrixStack::projection(); else - throw InvalidParameterValue("Texture matrices are not supported"); + throw invalid_argument("matrix_mode"); } void load_identity() diff --git a/source/object.cpp b/source/object.cpp index 70c2aeee..1e94d7cc 100644 --- a/source/object.cpp +++ b/source/object.cpp @@ -1,6 +1,5 @@ #include #include -#include "except.h" #include "material.h" #include "mesh.h" #include "object.h" @@ -27,7 +26,7 @@ Object::~Object() void Object::set_mesh(unsigned i, const Mesh *m) { if(i>meshes.size()) - throw InvalidParameterValue("LODs must be continuous"); + throw invalid_argument("Object::set_mesh"); if(i==meshes.size()) meshes.push_back(m); @@ -141,7 +140,7 @@ void Object::Loader::mesh_inline() void Object::Loader::mesh_inline_lod(unsigned l) { if(l>obj.meshes.size()) - throw InvalidParameterValue("LODs must be continuous"); + throw invalid_argument("Object::Loader::mesh_inline_lod"); RefPtr msh = new Mesh; load_sub(*msh); diff --git a/source/pixelformat.cpp b/source/pixelformat.cpp index 4744ebbd..8f742d83 100644 --- a/source/pixelformat.cpp +++ b/source/pixelformat.cpp @@ -1,4 +1,4 @@ -#include "except.h" +#include #include "pixelformat.h" using namespace std; @@ -54,7 +54,7 @@ PixelFormat pixelformat_from_graphics(Graphics::PixelFormat pf) case Graphics::RGBA: return RGBA; case Graphics::BGR: return BGR; case Graphics::BGRA: return BGRA; - default: throw InvalidParameterValue("Unknown Graphics::PixelFormat"); + default: throw invalid_argument("pixelformat_from_graphics"); } } diff --git a/source/primitivebuilder.cpp b/source/primitivebuilder.cpp index 046cc5b2..dd869908 100644 --- a/source/primitivebuilder.cpp +++ b/source/primitivebuilder.cpp @@ -1,5 +1,8 @@ +#include "error.h" #include "primitivebuilder.h" +using namespace std; + namespace Msp { namespace GL { @@ -13,7 +16,7 @@ PrimitiveBuilder::PrimitiveBuilder(VertexArray &a): void PrimitiveBuilder::begin(PrimitiveType t) { if(in_batch) - throw InvalidState("begin() already called"); + throw invalid_operation("PrimitiveBuilder::begin"); type = t; in_batch = true; @@ -24,7 +27,7 @@ void PrimitiveBuilder::begin(PrimitiveType t) void PrimitiveBuilder::end() { if(!in_batch) - throw InvalidState("end() called without begin()"); + throw invalid_operation("PrimitiveBuilder::end"); in_batch = false; @@ -34,23 +37,23 @@ void PrimitiveBuilder::end() void PrimitiveBuilder::offset(unsigned o) { if(o>array.size()) - throw InvalidParameterValue("Element offset out of range"); + throw out_of_range("PrimitiveBuilder::offset"); offs = o; } void PrimitiveBuilder::element(unsigned i) { if(!in_batch) - throw InvalidState("Element specification not between begin() and end()"); + throw invalid_operation("PrimitiveBuilder::element"); if(offs+i>=array.size()) - throw InvalidParameterValue("Element index out of range"); + throw out_of_range("PrimitiveBuilder::element"); element_(offs+i); } PrimitiveType PrimitiveBuilder::get_type() const { if(!in_batch) - throw InvalidState("Not between begin() and end()"); + throw invalid_operation("PrimitiveBuilder::get_type"); return type; } diff --git a/source/program.cpp b/source/program.cpp index 3c3a78b7..6fa14953 100644 --- a/source/program.cpp +++ b/source/program.cpp @@ -1,7 +1,7 @@ #include #include "arb_shader_objects.h" #include "arb_vertex_shader.h" -#include "except.h" +#include "error.h" #include "extension.h" #include "program.h" #include "shader.h" @@ -214,7 +214,7 @@ void Program::link() int value; glGetObjectParameterivARB(id, GL_OBJECT_LINK_STATUS_ARB, &value); if(!(linked = value)) - throw CompileError(get_info_log()); + throw compile_error(get_info_log()); glGetObjectParameterivARB(id, GL_OBJECT_ACTIVE_UNIFORMS_ARB, &value); for(int i=0; i +#include "error.h" #include "renderable.h" #include "renderer.h" @@ -7,7 +7,7 @@ namespace GL { void Renderable::render(const Tag &) const { - throw Exception("This Renderable doesn't support rendering without a Renderer"); + throw invalid_operation("Renderable::render"); } void Renderable::render(Renderer &renderer, const Tag &tag) const diff --git a/source/renderer.cpp b/source/renderer.cpp index 72043cd1..03042e67 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -1,7 +1,7 @@ -#include #include "batch.h" #include "buffer.h" #include "camera.h" +#include "error.h" #include "material.h" #include "program.h" #include "programdata.h" @@ -80,7 +80,7 @@ void Renderer::set_shader(const Program *p, const ProgramData *d) void Renderer::add_shader_data(const ProgramData *d) { if(!state->shprog) - throw InvalidState("No shader program"); + throw invalid_operation("Renderer::add_shader_data"); state->shdata.push_back(d); } @@ -111,7 +111,7 @@ void Renderer::push_state() void Renderer::pop_state() { if(state_stack.size()==1) - throw InvalidState("Can't pop the last state"); + throw stack_underflow("Renderer::pop_state"); state_stack.pop_back(); state = &state_stack.back(); @@ -128,7 +128,7 @@ void Renderer::escape() void Renderer::draw(const Batch &batch) { if(!vertex_array) - throw InvalidState("Can't draw without a vertex array"); + throw invalid_operation("Renderer::draw"); apply_state(); diff --git a/source/renderpass.cpp b/source/renderpass.cpp index 82ca7a39..93538d48 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "error.h" #include "material.h" #include "renderpass.h" #include "program.h" @@ -113,7 +114,7 @@ void RenderPass::Loader::texunit_named(unsigned i, const string &n) void RenderPass::Loader::uniforms() { if(!obj.shprog) - throw InvalidState("Can't load uniforms without a shader program"); + throw invalid_operation("RenderPass::Loader::uniforms"); if(!obj.shdata) obj.shdata = new ProgramData(*obj.shprog); load_sub(*obj.shdata); @@ -143,7 +144,7 @@ void RenderPass::TextureLoader::finish() void RenderPass::TextureLoader::texenv() { - throw Exception("TexEnvs can't be loaded yet"); + throw runtime_error("TexEnvs can't be loaded yet"); /*env = new TexEnv; load_sub(*env);*/ } diff --git a/source/shader.cpp b/source/shader.cpp index 967ac8a6..0f60edcd 100644 --- a/source/shader.cpp +++ b/source/shader.cpp @@ -1,5 +1,5 @@ #include "arb_shader_objects.h" -#include "except.h" +#include "error.h" #include "extension.h" #include "shader.h" @@ -59,7 +59,7 @@ void Shader::compile() int value = 0; glGetObjectParameterivARB(id, GL_OBJECT_COMPILE_STATUS_ARB, &value); if(!(compiled = value)) - throw CompileError(get_info_log()); + throw compile_error(get_info_log()); } string Shader::get_info_log() const diff --git a/source/texture.cpp b/source/texture.cpp index c2ea8ff7..d22f189b 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -1,4 +1,5 @@ -#include "except.h" +#include +#include "error.h" #include "texture.h" #include "texunit.h" @@ -40,7 +41,7 @@ void operator>>(const LexicalConverter &c, TextureWrap &tw) else if(c.get()=="MIRRORED_REPEAT") tw = MIRRORED_REPEAT; else - throw LexicalError("Invalid input in TextureWrap conversion"); + throw lexical_error(format("conversion of '%s' to TextureWrap", c.get())); } @@ -146,9 +147,6 @@ void Texture::set_compare_func(Predicate f) void Texture::bind() const { - if(!target) - throw InvalidState("Attempt to bind a texture without target (should never happen)"); - const Texture *cur = TexUnit::current().get_texture(); if(cur!=this) { diff --git a/source/texture2d.cpp b/source/texture2d.cpp index b6b59548..be4e1a11 100644 --- a/source/texture2d.cpp +++ b/source/texture2d.cpp @@ -1,5 +1,5 @@ #include "bindable.h" -#include "except.h" +#include "error.h" #include "texture2d.h" using namespace std; @@ -17,9 +17,9 @@ Texture2D::Texture2D(): void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht) { if(width>0) - throw InvalidState("Texture storage may only be specified once"); + throw invalid_operation("Texture2D::storage"); if(wd==0 || ht==0) - throw InvalidParameterValue("Invalid texture dimensions"); + throw invalid_argument("Texture2D::storage"); ifmt = fmt; width = wd; @@ -36,7 +36,8 @@ void Texture2D::allocate(unsigned level) void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data) { - require_storage(); + if(width==0 || height==0) + throw invalid_operation("Texture2D::image"); unsigned w = width; unsigned h = height; @@ -55,7 +56,9 @@ void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data) { - require_storage(); + if(width==0 || height==0) + throw invalid_operation("Texture2D::sub_image"); + allocate(level); Bind _bind(this, true); @@ -78,17 +81,11 @@ void Texture2D::image(const Graphics::Image &img) if(width==0) storage(fmt, w, h); else if(w!=width || h!=height) - throw IncompatibleData("Image does not match texture storage"); + throw incompatible_data("Texture2D::image"); image(0, fmt, UNSIGNED_BYTE, img.get_data()); } -void Texture2D::require_storage() -{ - if(width==0 || height==0) - throw InvalidState("Texture storage has not been specified"); -} - void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) { w >>= level; diff --git a/source/texture2d.h b/source/texture2d.h index 05fbbce2..5f4a7f92 100644 --- a/source/texture2d.h +++ b/source/texture2d.h @@ -68,7 +68,6 @@ public: private: void image(const Graphics::Image &); - void require_storage(); void get_level_size(unsigned, unsigned &, unsigned &); }; diff --git a/source/texture3d.cpp b/source/texture3d.cpp index e716c901..4aac49ed 100644 --- a/source/texture3d.cpp +++ b/source/texture3d.cpp @@ -1,7 +1,7 @@ #include #include #include "bindable.h" -#include "except.h" +#include "error.h" #include "extension.h" #include "texture3d.h" #include "version_1_2.h" @@ -24,9 +24,9 @@ Texture3D::Texture3D(): void Texture3D::storage(PixelFormat f, unsigned w, unsigned h, unsigned d) { if(width>0) - throw InvalidState("Textures storage may only be specified once"); + throw invalid_operation("Texture3D::storage"); if(w==0 || h==0 || d==0) - throw InvalidParameterValue("Invalid texture dimensions"); + throw invalid_argument("Texture3D::storage"); width = w; height = h; @@ -44,7 +44,8 @@ void Texture3D::allocate(unsigned level) void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data) { - require_storage(); + if(width==0 || height==0 || depth==0) + throw invalid_operation("Texture3D::image"); unsigned w = width; unsigned h = height; @@ -64,7 +65,9 @@ void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelFormat fmt, DataType type, const void *data) { - require_storage(); + if(width==0 || height==0 || depth==0) + throw invalid_operation("Texture3D::image"); + allocate(level); Bind _bind(this, true); @@ -83,7 +86,7 @@ void Texture3D::load_image(const string &fn, int dp) if(dp==-1) { if(h%w) - throw IncompatibleData("Image height is not divisible by its width"); + throw incompatible_data("Texture3D::load_image"); d = h/w; h = w; } @@ -92,7 +95,7 @@ void Texture3D::load_image(const string &fn, int dp) for(d=h; d*d>h; d>>=2) ; for(; d*d0) @@ -102,17 +105,11 @@ void Texture3D::load_image(const string &fn, int dp) if(width==0) storage(fmt, w, h, d); else if(w!=width || h!=height || d!=depth) - throw IncompatibleData("Image does not match texture storage"); + throw incompatible_data("Texture3D::load_image"); image(0, fmt, UNSIGNED_INT, img.get_data()); } -void Texture3D::require_storage() -{ - if(width==0 || height==0 || depth==0) - throw InvalidState("Texture storage has not been specified"); -} - void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) { w >>= level; diff --git a/source/texture3d.h b/source/texture3d.h index a0e96f42..12bd4a4b 100644 --- a/source/texture3d.h +++ b/source/texture3d.h @@ -29,7 +29,6 @@ public: unsigned get_height() const { return height; } unsigned get_depth() const { return depth; } private: - void require_storage(); void get_level_size(unsigned, unsigned &, unsigned &, unsigned &); }; diff --git a/source/texturing.cpp b/source/texturing.cpp index 425c710c..880f3f07 100644 --- a/source/texturing.cpp +++ b/source/texturing.cpp @@ -3,6 +3,8 @@ #include "texturing.h" #include "texunit.h" +using namespace std; + namespace Msp { namespace GL { @@ -30,7 +32,7 @@ void Texturing::detach(unsigned attch) void Texturing::set_attachment(unsigned attch, const Texture *tex, const TexEnv *env) { if(attch>=TexUnit::get_n_units()) - throw InvalidParameterValue("Invalid texture attachment"); + throw out_of_range("Texturing::set_attachment"); if(attachments.size()<=attch) attachments.resize(attch+1); diff --git a/source/texunit.cpp b/source/texunit.cpp index dde3d406..4d71e3b9 100644 --- a/source/texunit.cpp +++ b/source/texunit.cpp @@ -1,3 +1,4 @@ +#include #include "extension.h" #include "gl.h" #include "texunit.h" @@ -48,7 +49,7 @@ unsigned TexUnit::get_n_units() TexUnit &TexUnit::activate(unsigned n) { if(n>=get_n_units()) - throw InvalidParameterValue("Invalid texture unit number"); + throw out_of_range("TexUnit::activate"); if(units.size()<=n) units.resize(n+1); diff --git a/source/vertexarray.cpp b/source/vertexarray.cpp index f3476268..7716c5b2 100644 --- a/source/vertexarray.cpp +++ b/source/vertexarray.cpp @@ -1,5 +1,6 @@ #include "arb_vertex_program.h" #include "buffer.h" +#include "error.h" #include "extension.h" #include "gl.h" #include "version_1_2.h" @@ -75,7 +76,7 @@ void VertexArray::reset(const VertexFormat &f) void VertexArray::apply() const { if(format.empty()) - throw InvalidState("Trying to apply a vertex array with no data"); + throw invalid_operation("VertexArray::apply"); if(vbuf) { diff --git a/source/vertexformat.cpp b/source/vertexformat.cpp index f5e17571..ed2b04b8 100644 --- a/source/vertexformat.cpp +++ b/source/vertexformat.cpp @@ -1,7 +1,8 @@ #include +#include #include #include -#include "except.h" +#include "error.h" #include "vertexformat.h" using namespace std; @@ -60,7 +61,7 @@ unsigned VertexFormat::stride() const int VertexFormat::offset(VertexComponent comp, unsigned index) const { if((comp0) || (comp=8) || index>=53) - throw InvalidParameterValue("Vertex component index out of range"); + throw out_of_range("VertexFormat::offset"); unsigned type = (comp>>2)+index; unsigned size = comp&3; @@ -109,11 +110,11 @@ VertexFormat operator,(const VertexFormat &f, VertexComponent c) VertexFormat operator,(const VertexFormat &f, unsigned i) { if(!f.data) - throw InvalidState("VertexFormat has no components"); + throw invalid_operation("VertexFormat::operator,"); VertexFormat r = f; unsigned char *c = r.data+r.data[0]; if((*c0) || (*c=8) || i>=53) - throw InvalidParameterValue("Vertex component index out of range"); + throw out_of_range("VertexFormat::operator,"); *c += i*4; return r; @@ -137,7 +138,7 @@ void operator>>(const LexicalConverter &conv, VertexComponent &c) else if(str.size()==11 && str[9]=='_' && str[10]>='0' && str[10]<='7') c = static_cast(TEXCOORD1+(str[8]-'1')+(str[10]-'0')*4); else - throw LexicalError("Invalid texture unit in VertexComponent conversion"); + throw lexical_error(format("conversion of '%s' to VertexComponent", str)); } else if(str.size()>=9 && !str.compare(0, 6, "ATTRIB") && str[6]>='1' && str[6]<='4' && str[7]=='_') { @@ -146,14 +147,14 @@ void operator>>(const LexicalConverter &conv, VertexComponent &c) { n = lexical_cast(str.substr(8)); } - catch(const LexicalError &) + catch(const lexical_error &) { - throw LexicalError("Invalid attribute in VertexComponent conversion"); + throw lexical_error(format("conversion of '%s' to VertexComponent", str)); } c = static_cast(ATTRIB1+(str[6]-'1')+n*4); } else - throw LexicalError("Invalid input in VertexComponent conversion"); + throw lexical_error(format("conversion of '%s' to VertexComponent", str)); } // XXX This will go away eventually diff --git a/source/windingtest.cpp b/source/windingtest.cpp index 401d6803..de9e0a25 100644 --- a/source/windingtest.cpp +++ b/source/windingtest.cpp @@ -1,3 +1,4 @@ +#include #include "windingtest.h" namespace Msp { @@ -10,7 +11,7 @@ void operator>>(const LexicalConverter &conv, FaceWinding &winding) else if(conv.get()=="COUNTERCLOCKWISE") winding = COUNTERCLOCKWISE; else - throw InvalidParameterValue("Invalid FaceWinding"); + throw lexical_error(format("conversion of '%s' to FaceWinding", conv.get())); } WindingTest::WindingTest(): -- 2.45.2