Extra2D/include/renderer/material.h

179 lines
4.1 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;
/**
* @brief
*/
struct MaterialParamInfo {
MaterialParamType type;
uint32_t offset;
uint32_t size;
};
/**
* @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 GPU
*/
class Material : public RefCounted {
public:
/**
* @brief
*/
Material();
/**
* @brief
* @param layout
*/
void setLayout(Ptr<MaterialLayout> layout);
/**
* @brief
* @param shader
*/
void setShader(Ptr<Shader> 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 name
* @param texture
* @param slot
*/
void setTexture(const std::string& name, TextureHandle texture, uint32_t slot);
/**
* @brief
*
* UBO
* @param uboManager UBO
*/
void apply(UniformBufferManager& uboManager);
/**
* @brief
* @return
*/
Ptr<Shader> getShader() const { return shader_; }
/**
* @brief
* @return
*/
const void* getData() const { return data_.data(); }
/**
* @brief
* @return
*/
uint32_t getDataSize() const { return static_cast<uint32_t>(data_.size()); }
private:
Ptr<MaterialLayout> layout_; // 材质布局
Ptr<Shader> shader_; // 着色器
std::vector<uint8_t> data_; // 材质数据
std::vector<std::pair<TextureHandle, uint32_t>> textures_; // 纹理列表
};
} // namespace extra2d