-/* $Id$
-
-This file is part of libmspgl
-Copyright © 2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
#ifndef MSP_GL_PROGRAM_H_
#define MSP_GL_PROGRAM_H_
-#include <list>
#include <string>
-#include <GL/gl.h>
-#include "shader.h"
-#include "types.h"
+#include <vector>
+#include <msp/datafile/objectloader.h>
+#include "bindable.h"
+#include "gl.h"
+#include "programbuilder.h"
+#include "vertexformat.h"
namespace Msp {
namespace GL {
class Shader;
-class Program
+/**
+A complete shader program. Programs can be assembled of individual Shaders or
+generated with a set of standard features.
+*/
+class Program: public Bindable<Program>
{
public:
+ class Loader: public DataFile::ObjectLoader<Program>
+ {
+ public:
+ Loader(Program &);
+
+ private:
+ virtual void finish();
+
+ void attribute(unsigned, const std::string &);
+ void fragment_shader(const std::string &);
+ void geometry_shader(const std::string &);
+ void standard();
+ void vertex_shader(const std::string &);
+ };
+
+ typedef unsigned LayoutHash;
+ struct UniformBlockInfo;
+
+ struct UniformInfo
+ {
+ std::string name;
+ const UniformBlockInfo *block;
+ unsigned location;
+ unsigned size;
+ unsigned array_stride;
+ unsigned matrix_stride;
+ GLenum type;
+ };
+
+ struct UniformBlockInfo
+ {
+ std::string name;
+ unsigned data_size;
+ int bind_point;
+ std::vector<const UniformInfo *> uniforms;
+ LayoutHash layout_hash;
+ };
+
+ struct AttributeInfo
+ {
+ std::string name;
+ unsigned location;
+ unsigned size;
+ GLenum type;
+ };
+
+ typedef std::vector<Shader *> ShaderList;
+ typedef std::map<std::string, UniformInfo> UniformMap;
+ typedef std::map<std::string, UniformBlockInfo> UniformBlockMap;
+ typedef std::map<std::string, AttributeInfo> AttributeMap;
+
+private:
+ unsigned id;
+ ShaderList shaders;
+ ShaderList owned_data;
+ bool linked;
+ UniformBlockMap uniform_blocks;
+ UniformMap uniforms;
+ LayoutHash uniform_layout_hash;
+ AttributeMap attributes;
+
+public:
+ /// Constructs an empty Program with no Shaders attached.
Program();
- ~Program();
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
+ /// Constructs a Program with standard features.
+ DEPRECATED Program(const ProgramBuilder::StandardFeatures &);
+#pragma GCC diagnostic pop
+
+ /// Constructs a Program from unified source code using ProgramCompiler.
+ Program(const std::string &);
+
+ /// Constructs a Program from vertex and fragment shader source code.
+ Program(const std::string &, const std::string &);
+
+private:
+ void init();
+public:
+ virtual ~Program();
void attach_shader(Shader &shader);
+ void attach_shader_owned(Shader *shader);
void detach_shader(Shader &shader);
- bool link();
- int get_param(GLenum param) const;
- std::string get_info_log() const;
- void bind();
+ const ShaderList &get_attached_shaders() const { return shaders; }
- static void unbind();
+ void bind_attribute(unsigned, const std::string &);
+ void bind_attribute(VertexComponent, const std::string &);
+ void bind_fragment_data(unsigned, const std::string &);
+
+ void link();
private:
- uint id;
- std::list<Shader *> shaders;
- bool linked;
+ static void require_type(GLenum);
+ void query_uniforms();
+ void query_uniform_blocks(const std::vector<UniformInfo *> &);
+ void query_attributes();
+ static LayoutHash compute_layout_hash(const std::vector<const UniformInfo *> &);
+ static bool uniform_location_compare(const UniformInfo *, const UniformInfo *);
+public:
+ bool is_linked() const { return linked; }
+ std::string get_info_log() const;
+
+ LayoutHash get_uniform_layout_hash() const { return uniform_layout_hash; }
+ const UniformBlockMap &get_uniform_blocks() const { return uniform_blocks; }
+ const UniformBlockInfo &get_uniform_block_info(const std::string &) const;
+ const UniformMap &get_uniforms() const { return uniforms; }
+ const UniformInfo &get_uniform_info(const std::string &) const;
+ int get_uniform_location(const std::string &) const;
+ const AttributeMap &get_attributes() const { return attributes; }
+ const AttributeInfo &get_attribute_info(const std::string &) const;
+ int get_attribute_location(const std::string &) const;
- static Program *cur_prog;
+ void bind() const;
+ static void unbind();
};
} // namespace GL