summaryrefslogtreecommitdiff
path: root/Runtime/Shaders/MaterialProperties.h
blob: 5c1c0bd20fd275ef0f1838bcd220fe16365a0b31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef MATERIAL_PROPERTIES_H
#define MATERIAL_PROPERTIES_H

#include "Runtime/Utilities/dynamic_array.h"
#include "Runtime/Modules/ExportModules.h"
#include "Runtime/GfxDevice/GfxDeviceTypes.h"

class Vector4f;
class Matrix4x4f;
class ColorRGBAf;
namespace Unity { class Material; }
namespace ShaderLab { struct FastPropertyName; }

// Tightly packed buffer of material properties
class EXPORT_COREMODULE MaterialPropertyBlock
{
public:
	struct Property
	{
		int		nameIndex;
		UInt8	rows;
		UInt8	cols;
		UInt8	texDim; // if texDim==None, this is a value property
		// These should not be size_t, as the GfxDevice may run across processes of different
		// bitness, and the data serialized in the command buffer must match.
		UInt32	arraySize;
		UInt32	offset;
	};

	MaterialPropertyBlock() {}

	// Does not copy data!
	MaterialPropertyBlock(Property* props, size_t propCount, float* buffer, size_t bufSize);

	// Clear all properties
	void Clear();

	// Add Properties without checking if another property with the same name exists
	void AddProperty(const ShaderLab::FastPropertyName& name, const float* data, UInt8 rows, UInt8 cols, size_t arraySize);
	void AddPropertyFloat(const ShaderLab::FastPropertyName& name, float val);
	void AddPropertyVector(const ShaderLab::FastPropertyName& name, const Vector4f& vec);
	void AddPropertyColor(const ShaderLab::FastPropertyName& name, const ColorRGBAf& col);
	void AddPropertyMatrix(const ShaderLab::FastPropertyName& name, const Matrix4x4f& mat);
	void AddPropertyTexture(const ShaderLab::FastPropertyName& name, TextureDimension dim, TextureID tid);

	// Replace properties
	void ReplacePropertyFloat(const ShaderLab::FastPropertyName& name, float data);
	void ReplacePropertyVector(const ShaderLab::FastPropertyName& name, const Vector4f& col);
	void ReplacePropertyColor(const ShaderLab::FastPropertyName& name, const ColorRGBAf& col);
	void ReplacePropertyTexture(const ShaderLab::FastPropertyName& name, TextureDimension dim, TextureID tid);
	
	// Replaces a single float property on either a float1 or one component of a float4.
	/// If other components on a float4 are not yet defined, they will be initialized to zero
	void ReplacePartialFloatProperty(const ShaderLab::FastPropertyName& name, float data, UInt8 cols, UInt8 colIndex);
	void ReplacePartialFloatColorProperty(const ShaderLab::FastPropertyName& name, float data, UInt8 cols, UInt8 colIndex);


	const float* FindFloat(const ShaderLab::FastPropertyName& name) const;
	const Vector4f* FindVector(const ShaderLab::FastPropertyName& name) const;
	bool GetColor(const ShaderLab::FastPropertyName& name, ColorRGBAf& outColor) const;
	const Matrix4x4f* FindMatrix(const ShaderLab::FastPropertyName& name) const;
	const TextureID FindTexture(const ShaderLab::FastPropertyName& name) const;
	
	const Property*	GetPropertiesBegin() const	{ return m_Properties.begin(); }
	const Property*	GetPropertiesEnd() const	{ return m_Properties.end(); }
	const float*	GetBufferBegin() const		{ return m_Buffer.begin(); }
	const float*	GetBufferEnd() const		{ return m_Buffer.end(); }

	
	const void* Find(const ShaderLab::FastPropertyName& name, UInt8 rows, UInt8 cols, size_t arraySize) const;
	int GetPropertyIndex (const ShaderLab::FastPropertyName& name) const;

private:
	dynamic_array<Property> m_Properties;
	dynamic_array<float> m_Buffer;
};

inline void MaterialPropertyBlock::Clear()
{
	m_Properties.resize_uninitialized(0);
	m_Buffer.resize_uninitialized(0);
}


#endif