增加材质和纹理

This commit is contained in:
2025-10-26 20:07:32 +08:00
parent f847154ba2
commit 8db6f7b115
45 changed files with 1124 additions and 281 deletions
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include "dvc_config.h"
#include "dvc_pre_declare.h"
#include "math/dvc_vector2.h"
#include "math/dvc_vector4.h"
DV_CORE_BEGIN_NAMESPACE
class DV_CORE_API Image
{
public:
int32_t getID(void) const { return m_id; }
int32_t getWidth(void) const { return m_width; }
int32_t getHeight(void) const { return m_height; }
PixelFormat getPixelFormat(void) const { return m_pixelFormat; }
DeviceBufferConstPtr getPixelBuffer(void) const { return m_pixelBuffer; }
fVector4 getPixel(int32_t u, int32_t v) const;
void clear();
bool initWithPixelData(Device& device, int32_t width, int32_t height,
PixelFormat pixelFormat, const uint8_t* data, size_t dataSize);
protected:
int32_t m_id;
int32_t m_width;
int32_t m_height;
PixelFormat m_pixelFormat;
size_t m_pixelByteSize;
DeviceBufferPtr m_pixelBuffer;
public:
Image(int32_t id);
~Image();
};
DV_CORE_END_NAMESPACE
+113
View File
@@ -0,0 +1,113 @@
#pragma once
#include "dvc_config.h"
#include "dvc_pre_declare.h"
#include "math/dvc_vector4.h"
DV_CORE_BEGIN_NAMESPACE
class DV_CORE_API Material
{
public:
struct TextureInfo
{
TextureConstPtr texture;
int32_t texCoord = 0;
};
public:
int32_t getID(void) const { return m_id; }
void setBaseColorTexture(const TextureConstPtr& texture, int32_t texCoord, const fVector4& baseColorfactor) {
m_baseColorTexture.texture = texture;
m_baseColorTexture.texCoord = texCoord;
m_baseColorFactor = baseColorfactor;
}
const TextureInfo& getBaseColorTexture() const {
return m_baseColorTexture;
}
const fVector4& getBaseColorFactor() const {
return m_baseColorFactor;
}
void setMetallicRoughnessTexture(const TextureConstPtr& texture, int32_t texCoord, float32_t metallicFactor=1.f, float32_t roughnessFactor=1.f ) {
m_metallicRoughnessTexture.texture = texture;
m_metallicRoughnessTexture.texCoord = texCoord;
m_metallicFactor = metallicFactor;
m_roughnessFactor = roughnessFactor;
}
const TextureInfo& getMetallicRoughnessTexture() const {
return m_metallicRoughnessTexture;
}
float32_t getMetallicFactor() const {
return m_metallicFactor;
}
float32_t getRoughnessFactor() const {
return m_roughnessFactor;
}
void setNormalTexture(const TextureConstPtr& texture, int32_t texCoord, float32_t scale=1.f) {
m_normalTexture.texture = texture;
m_normalTexture.texCoord = texCoord;
m_normalScale = scale;
}
const TextureInfo& getNormalTexture() const {
return m_normalTexture;
}
float32_t getNormalScale() const {
return m_normalScale;
}
VertexShaderInterface* getVertexShader() const {
return m_vertexShader;
}
PixelShaderInterface* getPixelShader() const {
return m_pixelShader;
}
public:
bool loadShader();
protected:
int32_t m_id;
/*The base color texture.
* The first three components (RGB) MUST be encoded with the sRGB transfer function.
*/
TextureInfo m_baseColorTexture;
fVector4 m_baseColorFactor = fVector4::ONE;
/*The metallic - roughness texture.
* The metalness values are sampled from the B channel.
* The roughness values are sampled from the G channel.
*/
TextureInfo m_metallicRoughnessTexture;
float32_t m_metallicFactor = 1.0f;
float32_t m_roughnessFactor = 1.0f;
/*The normal texture.
*/
TextureInfo m_normalTexture;
/*The scalar parameter applied to each normal vector of the texture.
* This value scales the normal vector in X and Y directions using the formula:
* scaledNormal = (normalize<sampled normal texture value> * 2.0 - 1.0) * vec3(<normal scale>, <normal scale>, 1.0).
*/
float32_t m_normalScale = 1.0f;
protected:
VertexShaderInterface* m_vertexShader = nullptr;
PixelShaderInterface* m_pixelShader = nullptr;
public:
Material(int32_t id);
~Material();
};
DV_CORE_END_NAMESPACE
+5 -1
View File
@@ -14,12 +14,15 @@ public:
PrimitiveType type;
PrimitiveAttributes attributes;
DeviceBufferAccessorConstPtr indices;
MaterialPtr material;
};
typedef std::shared_ptr<Primitive> PrimitivePtr;
typedef std::shared_ptr<const Primitive> PrimitiveConstPtr;
typedef std::vector<PrimitivePtr> PrimitiveArray;
public:
int32_t getID(void) const { return m_id; }
const std::string& getName(void) const {
return m_name;
}
@@ -31,11 +34,12 @@ public:
PrimitiveConstPtr getPrimitive(int32_t index) const;
private:
int32_t m_id;
std::string m_name;
PrimitiveArray m_primitiveArray;
public:
Mesh(const std::string& name);
Mesh(int32_t id, const std::string& name);
~Mesh();
};
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include "dvc_config.h"
#include "dvc_pre_declare.h"
#include "math/dvc_vector2.h"
#include "math/dvc_vector4.h"
DV_CORE_BEGIN_NAMESPACE
class DV_CORE_API Texture
{
public:
int32_t getID(void) const { return m_id; }
ImagePtr getImage(void) const { return m_image; }
void setImage(const ImagePtr& image) { m_image = image; }
fVector4 sample(const fVector2& uv) const;
private:
int32_t m_id;
ImagePtr m_image;
public:
Texture(int32_t id);
~Texture();
};
DV_CORE_END_NAMESPACE
+3 -2
View File
@@ -10,6 +10,7 @@ public:
void init(size_t _length);
void clear(void);
int32_t getID(void) const { return m_id; }
size_t length(void) const { return m_length; }
const uint8_t* ptr(size_t offseet) const {
@@ -20,12 +21,12 @@ public:
return m_ptr + offseet;
}
private:
int32_t m_id;
uint8_t* m_ptr;
size_t m_length;
public:
DeviceBuffer();
DeviceBuffer(size_t _length);
DeviceBuffer(int32_t id, size_t _length);
~DeviceBuffer();
};
@@ -7,6 +7,8 @@ DV_CORE_BEGIN_NAMESPACE
class DV_CORE_API DeviceBufferAccessor
{
public:
int32_t getID(void) const { return m_id; }
const DeviceBufferViewConstPtr getBufferView(void) const {
return m_bufferView;
}
@@ -39,6 +41,7 @@ public:
private:
int32_t m_id;
DeviceBufferViewConstPtr m_bufferView;
size_t m_offset;
size_t m_count;
@@ -48,7 +51,7 @@ private:
size_t m_elementSize;
public:
DeviceBufferAccessor(DeviceBufferViewConstPtr bufferView, size_t offset, size_t count, ComponentType componentType, DataType dataType);
DeviceBufferAccessor(int32_t id, DeviceBufferViewConstPtr bufferView, size_t offset, size_t count, ComponentType componentType, DataType dataType);
~DeviceBufferAccessor();
};
+4 -1
View File
@@ -8,6 +8,8 @@ DV_CORE_BEGIN_NAMESPACE
class DV_CORE_API DeviceBufferView
{
public:
int32_t getID(void) const { return m_id; }
const DeviceBufferConstPtr getBuffer(void) const {
return m_buffer;
}
@@ -25,13 +27,14 @@ public:
}
private:
int32_t m_id;
DeviceBufferConstPtr m_buffer;
size_t m_offset;
size_t m_length;
size_t m_stride;
public:
DeviceBufferView(DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride);
DeviceBufferView(int32_t id, DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride);
~DeviceBufferView();
};
+69
View File
@@ -0,0 +1,69 @@
#pragma once
#include "dvc_config.h"
#include "dvc_pre_declare.h"
DV_CORE_BEGIN_NAMESPACE
class DV_CORE_API Device
{
public:
void clear();
public:
DeviceBufferPtr newDeviceBuffer(size_t length=0);
DeviceBufferConstPtr getDeviceBuffer(int32_t id) const;
DeviceBufferViewPtr newDeviceBufferView(DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride);
DeviceBufferViewPtr getDeviceBufferView(int32_t id) const;
DeviceBufferAccessorPtr newDeviceBufferAccessor(DeviceBufferViewConstPtr bufferView, size_t offset,
size_t count, ComponentType componentType, DataType dataType);
DeviceBufferAccessorPtr getDeviceBufferAccessor(int32_t id) const;
MeshPtr newMeshAsset(const std::string& name);
MeshPtr getMesh(int32_t id) const;
ImagePtr newImageAsset();
ImagePtr getImage(int32_t id) const;
TexturePtr newTextureAsset();
TexturePtr getTexture(int32_t id) const;
MaterialPtr newMaterialAsset();
MaterialPtr getMaterial(int32_t id) const;
private:
int32_t nextID() {
if (++m_currentID < 0) m_currentID = 0;
return m_currentID;
}
private:
int32_t m_currentID = 0;
//device buffer
std::map<int32_t, DeviceBufferPtr> m_deviceBufferMap;
//device buffer view
std::map<int32_t, DeviceBufferViewPtr> m_bufferViewMap;
//device buffer accessor
std::map<int32_t, DeviceBufferAccessorPtr> m_bufferAccessorMap;
//mesh asset resources
std::map<int32_t, MeshPtr> m_meshAssetMap;
//image asset resources
std::map<int32_t, ImagePtr> m_imageAssetMap;
//texture asset resources
std::map<int32_t, TexturePtr> m_textureAssetMap;
//material asset resources
std::map<int32_t, MaterialPtr> m_materialAssetMap;
public:
Device();
~Device();
};
DV_CORE_END_NAMESPACE
+11 -9
View File
@@ -89,17 +89,19 @@ enum PrimitiveType
enum PixelFormat
{
//96-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue
PF_FLOAT32_RGB,
PF_UNKNOWN,
/// 128-bit pixel format, 32 bits (float) for red, 32 bits (float) for green, 32 bits (float) for blue, 32 bits (float) for alpha
PF_FLOAT32_RGBA,
// 32-bit pixel format, 32 bits (float) for red
PF_FLOAT32_R,
/// 132-bit pixel format, 8 bits (float) for red, 8 bits (float) for green, 8 bits (float) for blue, 8 bits (float) for alpha
PF_UINT8_ALPHA,
PF_UINT8_RGB,
PF_UINT8_RGBA,
PF_UINT16_ALPHA,
PF_UINT16_RGB,
PF_UINT16_RGBA,
PF_FLOAT32_ALPHA,
PF_FLOAT32_RGB,
PF_FLOAT32_RGBA,
};
enum AntiAliasingTech
+15
View File
@@ -36,8 +36,22 @@ class Mesh;
typedef std::shared_ptr<Mesh> MeshPtr;
typedef std::shared_ptr<const Mesh> MeshConstPtr;
class Image;
typedef std::shared_ptr<Image> ImagePtr;
typedef std::shared_ptr<const Image> ImageConstPtr;
class Texture;
typedef std::shared_ptr<Texture> TexturePtr;
typedef std::shared_ptr<const Texture> TextureConstPtr;
class Material;
typedef std::shared_ptr<Material> MaterialPtr;
typedef std::shared_ptr<const Material> MaterialConstPtr;
class Scene;
class Device;
class SceneNode;
typedef std::shared_ptr<SceneNode> SceneNodePtr;
typedef std::shared_ptr<const SceneNode> SceneNodeConstPtr;
@@ -68,6 +82,7 @@ typedef std::shared_ptr<const Renderable> RenderableConstPtr;
typedef std::vector<RenderableConstPtr> RenderableArray;
class VertexShaderInterface;
class PixelShaderInterface;
class UniformBuffer;
+1
View File
@@ -10,6 +10,7 @@ class MathUtil
public:
static DV_CORE_API size_t ComponentSize(ComponentType ct);
static DV_CORE_API size_t DataTypeSize(DataType dt);
static DV_CORE_API size_t PixelSize(PixelFormat pixelFormat);
//count the number of bits set in v
static DV_CORE_API uint32_t popcount(uint8_t v);
+5
View File
@@ -235,6 +235,11 @@ public:
public:
// special points
static DV_CORE_API const TVector4 ZERO;
static DV_CORE_API const TVector4 ONE;
static DV_CORE_API const TVector4 UNIT_X;
static DV_CORE_API const TVector4 UNIT_Y;
static DV_CORE_API const TVector4 UNIT_Z;
static DV_CORE_API const TVector4 UNIT_W;
static DV_CORE_API const TVector4 WHITE;
static DV_CORE_API const TVector4 BLACK;
static DV_CORE_API const TVector4 RED;
+13 -2
View File
@@ -1,6 +1,7 @@
#pragma once
#include "dvc_config.h"
#include "device/dvc_device.h"
#include "scene/dvc_scene.h"
#include "device/dvc_uniform_buffer.h"
#include "device/dvc_render_target.h"
@@ -10,9 +11,18 @@ DV_CORE_BEGIN_NAMESPACE
class DV_CORE_API RenderPipe
{
public:
Device& getDevice() {
return m_device;
}
const Device& getDevice() const {
return m_device;
}
UniformBuffer& getUniformBuffer() {
return m_uniformBuffer;
}
const UniformBuffer& getUniformBuffer() const {
return m_uniformBuffer;
}
AntiAliasingTech getAntiAliasingTech() const {
return m_aaTech;
}
@@ -24,12 +34,13 @@ public:
void process(RenderTarget& renderTarget);
protected:
AntiAliasingTech m_aaTech;
Device& m_device;
AntiAliasingTech m_aaTech = AntiAliasingTech::AA_None;
UniformBuffer m_uniformBuffer;
RenderableArray m_renderableQueue;
public:
RenderPipe();
RenderPipe(Device& device);
~RenderPipe();
};
@@ -17,7 +17,7 @@ public:
PrimitiveType primitiveType;
PrimitiveAttributes vertexData;
DeviceBufferAccessorConstPtr indices;
VertexShaderInterface* vs;
MaterialConstPtr material;
};
typedef std::vector<Node> NodeArray;
@@ -12,6 +12,7 @@ class VertexShaderStep
public:
struct Node
{
MaterialConstPtr material;
DeviceBufferPtr vertexBuf;
VertexDesc vertexDesc;
size_t vertexCounts;
@@ -21,7 +22,7 @@ public:
typedef std::vector<Node> NodeArray;
public:
void process(UniformBuffer& uniformBuffer, const InputAssemberStep::NodeArray& assembedNodeArray);
void process(RenderPipe& pipe, const InputAssemberStep::NodeArray& assembedNodeArray);
public:
NodeArray m_nodes;
@@ -20,7 +20,7 @@ public:
typedef std::vector<Fragement> FragementArray;
public:
void process(const VertexShaderStep::NodeArray& vsNodeArray, AntiAliasingTech aaTech, int32_t canvasWidth, int32_t canvasHeight);
void process(RenderPipe& pipe, const VertexShaderStep::NodeArray& vsNodeArray, AntiAliasingTech aaTech, int32_t canvasWidth, int32_t canvasHeight);
public:
StartOffsetArray m_startOffsetArray;
-34
View File
@@ -11,32 +11,6 @@ public:
void init(void);
void clear(void);
public:
DeviceBufferPtr newDeviceBuffer(void);
int32_t getDeviceBufferCounts(void) const {
return (int32_t)m_deviceBufferArray.size();
}
DeviceBufferConstPtr getDeviceBuffer(int32_t index) const;
DeviceBufferViewPtr newDeviceBufferView(DeviceBufferConstPtr buffer, size_t offset, size_t length, size_t stride);
int32_t getDeviceBufferViewCounts(void) const {
return (int32_t)m_bufferViewArray.size();
}
DeviceBufferViewPtr getDeviceBufferView(int32_t index) const;
DeviceBufferAccessorPtr newDeviceBufferAccessor(DeviceBufferViewConstPtr bufferView, size_t offset,
size_t count, ComponentType componentType, DataType dataType);
int32_t getDeviceBufferAccessorCounts(void) const {
return (int32_t)m_bufferAccessorArray.size();
}
DeviceBufferAccessorPtr getDeviceBufferAccessor(int32_t index) const;
int32_t pushNewMeshAsset(MeshPtr mesh);
int32_t getMeshCounts(void) const {
return (int32_t)m_meshArray.size();
}
MeshConstPtr getMesh(int32_t index) const;
public:
SceneNodePtr newSceneNode(const std::string& name);
SceneNodePtr getRootSceneNode() const;
@@ -80,14 +54,6 @@ public:
protected:
SceneNodePtr m_rootSceneNode;
//device resources
std::vector<DeviceBufferPtr> m_deviceBufferArray;
std::vector<DeviceBufferViewPtr> m_bufferViewArray;
std::vector<DeviceBufferAccessorPtr> m_bufferAccessorArray;
//asset resources
std::vector<MeshPtr> m_meshArray;
public:
Scene();
~Scene();
@@ -0,0 +1,36 @@
#pragma once
#include "dvc_config.h"
#include "dvc_pre_declare.h"
#include "math/dvc_vector2.h"
#include "math/dvc_vector4.h"
DV_CORE_BEGIN_NAMESPACE
class shader
{
public:
template<typename T>
static inline int32_t step(const T& edge, const T& x) {
return (x >= edge) ? 1 : 0;
}
template<typename T>
static inline T max(const T& a, const T& b) {
return (a > b) ? a : b;
}
template<typename T>
static inline T pow(const T& base, const T& exponent) {
return std::pow(base, exponent);
}
template<typename T>
static inline T min(const T& a, const T& b) {
return (a < b) ? a : b;
}
static DV_CORE_API fVector4 texture2D(const RenderPipe& pipe, const char* textName, const fVector2& uv, const fVector4& defaultValue);
};
DV_CORE_END_NAMESPACE
@@ -2,6 +2,7 @@
#include "dvc_config.h"
#include "dvc_pre_declare.h"
#include "math/dvc_vector2.h"
#include "math/dvc_vector3.h"
#include "math/dvc_vector4.h"
#include "device/dvc_vertex_desc.h"
@@ -15,7 +16,7 @@ public:
virtual const VertexDesc& getOutputVertexDesc() const = 0;
typedef std::function<DeviceBufferAccessorConstPtr(VertexElementType)> SetInputAttributeFunc;
virtual void prepare(const UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc) = 0;
virtual void prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) = 0;
virtual void process(size_t index, void* outputVertex, fVector3& vertexWorldPos) = 0;
};
@@ -26,7 +27,7 @@ public:
virtual const char* getName(void) const = 0;
typedef std::function<size_t(const std::string&)> SetInputAttributeFunc;
virtual void prepare(const UniformBuffer* uniformBuffer, SetInputAttributeFunc setInputAttributeFunc) = 0;
virtual void prepare(const RenderPipe* pipe, SetInputAttributeFunc setInputAttributeFunc) = 0;
virtual void process(const VertexDesc& veretexDesc, const float32_t* pixelData, fVector4& color) = 0;
};