Extra2D/include/renderer/shader.h

156 lines
3.8 KiB
C
Raw Normal View History

#pragma once
#include <types/ptr/ref_counted.h>
#include <types/ptr/intrusive_ptr.h>
#include <types/base/types.h>
#include <string>
#include <unordered_map>
// 前向声明 OpenGL 类型
typedef unsigned int GLuint;
typedef int GLint;
typedef unsigned int GLenum;
namespace extra2d {
/**
* @brief
*
* OpenGL 使
*
*/
class Shader : public RefCounted {
public:
/**
* @brief
*/
Shader();
/**
* @brief
*/
~Shader() override;
/**
* @brief
* @param vsPath
* @param fsPath
* @return
*/
bool loadFromFile(const std::string& vsPath, const std::string& fsPath);
/**
* @brief
* @param vsSource
* @param fsSource
* @return
*/
bool loadFromSource(const std::string& vsSource, const std::string& fsSource);
/**
* @brief
*/
void bind() const;
/**
* @brief
*/
void unbind() const;
/**
* @brief Uniform Block
* @param name Uniform Block
* @param binding
*/
void setUniformBlock(const std::string& name, uint32_t binding);
/**
* @brief int uniform
* @param name uniform
* @param value
*/
void setInt(const std::string& name, int value);
/**
* @brief float uniform
* @param name uniform
* @param value
*/
void setFloat(const std::string& name, float value);
/**
* @brief vec2 uniform
* @param name uniform
* @param x X
* @param y Y
*/
void setVec2(const std::string& name, float x, float y);
/**
* @brief vec4 uniform
* @param name uniform
* @param x X
* @param y Y
* @param z Z
* @param w W
*/
void setVec4(const std::string& name, float x, float y, float z, float w);
/**
* @brief mat4 uniform
* @param name uniform
* @param value 16float
*/
void setMat4(const std::string& name, const float* value);
/**
* @brief ID
* @return OpenGL ID
*/
GLuint getProgram() const { return program_; }
/**
* @brief
* @return
*/
bool isLoaded() const { return program_ != 0; }
private:
/**
* @brief
* @param type
* @param source
* @return 0
*/
GLuint compileShader(GLenum type, const std::string& source);
/**
* @brief
* @param vertexShader
* @param fragmentShader
* @return
*/
bool linkProgram(GLuint vertexShader, GLuint fragmentShader);
/**
* @brief uniform
* @param name uniform
* @return uniform -1
*/
GLint getUniformLocation(const std::string& name);
/**
* @brief
* @param source
* @param type
* @return
*/
std::string addVersionIfNeeded(const std::string& source, GLenum type);
private:
GLuint program_ = 0; // OpenGL 程序对象
std::unordered_map<std::string, GLint> uniformCache_; // Uniform 位置缓存
};
} // namespace extra2d