Extra2D/include/renderer/material.h

248 lines
5.9 KiB
C
Raw Normal View History

#pragma once
#include <types/ptr/ref_counted.h>
#include <types/ptr/intrusive_ptr.h>
#include <types/math/vec2.h>
#include <types/math/color.h>
#include <renderer/render_types.h>
#include <renderer/shader.h>
#include <renderer/uniform_buffer.h>
#include <string>
#include <unordered_map>
#include <vector>
namespace extra2d {
// 前向声明
class Material;
class Texture;
/**
* @brief
*/
struct MaterialParamInfo {
MaterialParamType type;
uint32_t offset;
uint32_t size;
};
/**
* @brief
*/
struct TextureSlot {
Handle<Texture> handle;
uint32_t slot;
std::string uniformName;
};
/**
* @brief
*
*
*/
class MaterialLayout : public RefCounted {
public:
/**
* @brief
*/
MaterialLayout();
/**
* @brief
* @param name
* @param type
*/
void addParam(const std::string& name, MaterialParamType type);
/**
* @brief
*/
void finalize();
/**
* @brief
* @param name
* @return nullptr
*/
const MaterialParamInfo* getParam(const std::string& name) const;
/**
* @brief
* @return
*/
uint32_t getBufferSize() const { return bufferSize_; }
/**
* @brief
* @return
*/
bool isFinalized() const { return finalized_; }
private:
std::unordered_map<std::string, MaterialParamInfo> params_;
uint32_t bufferSize_ = 0;
bool finalized_ = false;
};
/**
* @brief
*
*
* -
* - UBO
* -
*
* 使
* @code
* // 创建自定义着色器
* auto shader = makePtr<Shader>();
* shader->loadFromFile("custom.vert", "custom.frag");
*
* // 创建材质布局
* auto layout = makePtr<MaterialLayout>();
* layout->addParam("uTintColor", MaterialParamType::Color);
* layout->addParam("uOpacity", MaterialParamType::Float);
* layout->finalize();
*
* // 创建材质
* auto material = makePtr<Material>();
* material->setShader(shader);
* material->setLayout(layout);
* material->setColor("uTintColor", Color::White);
* material->setFloat("uOpacity", 1.0f);
*
* // 设置纹理
* material->setTexture("uTexture", textureHandle, 0);
*
* // 注册到渲染器
* MaterialHandle handle = renderer->registerMaterial(material);
* @endcode
*/
class Material : public RefCounted {
public:
/**
* @brief
*/
Material();
// ========================================
// 着色器和布局设置
// ========================================
/**
* @brief
* @param layout
*/
void setLayout(Ptr<MaterialLayout> layout);
/**
* @brief
* @param shader
*/
void setShader(Ptr<Shader> shader);
/**
* @brief
* @return
*/
Ptr<Shader> getShader() const { return shader_; }
// ========================================
// 参数设置
// ========================================
/**
* @brief float
* @param name
* @param value
*/
void setFloat(const std::string& name, float value);
/**
* @brief vec2
* @param name
* @param value
*/
void setVec2(const std::string& name, const Vec2& value);
/**
* @brief vec4
* @param name
* @param value
*/
void setVec4(const std::string& name, float x, float y, float z, float w);
/**
* @brief
* @param name
* @param value
*/
void setColor(const std::string& name, const Color& value);
/**
* @brief mat4
* @param name
* @param value
*/
void setMat4(const std::string& name, const float* value);
// ========================================
// 纹理管理
// ========================================
/**
* @brief
* @param uniformName uniform
* @param texture
* @param slot 0-15
*/
void setTexture(const std::string& uniformName, Handle<Texture> texture, uint32_t slot);
/**
* @brief
* @return
*/
const std::vector<TextureSlot>& getTextures() const { return textures_; }
/**
* @brief
*/
void clearTextures();
// ========================================
// 数据访问
// ========================================
/**
* @brief
* @return
*/
const void* getData() const { return data_.data(); }
/**
* @brief
* @return
*/
uint32_t getDataSize() const { return static_cast<uint32_t>(data_.size()); }
// ========================================
// 应用材质(渲染时调用)
// ========================================
/**
* @brief
*
* UBO
* @param uboManager UBO
*/
void apply(UniformBufferManager& uboManager);
private:
Ptr<MaterialLayout> layout_; // 材质布局
Ptr<Shader> shader_; // 着色器
std::vector<uint8_t> data_; // 材质数据UBO 缓冲)
std::vector<TextureSlot> textures_; // 纹理槽位列表
};
} // namespace extra2d