summaryrefslogtreecommitdiff
path: root/Runtime/Shaders/MaterialProperties.cpp
blob: 357a73fe2b828da91bc88d8c0908b74dc6900d57 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "UnityPrefix.h"
#include "MaterialProperties.h"
#include "Material.h"
#include "External/shaderlab/Library/properties.h"
#include "Runtime/Math/Matrix4x4.h"

inline size_t CalculateSizeFromProperty (const MaterialPropertyBlock::Property& prop)
{
	return prop.rows * prop.cols * prop.arraySize;
}

MaterialPropertyBlock::MaterialPropertyBlock(Property* props, size_t propCount, float* buffer, size_t bufSize)
{
	m_Properties.assign_external(props, props + propCount);
	m_Buffer.assign_external(buffer, buffer + bufSize);
}

void MaterialPropertyBlock::AddProperty(const ShaderLab::FastPropertyName& name, const float* data, UInt8 rows, UInt8 cols, size_t arraySize)
{
	size_t offset = m_Buffer.size();
	Property prop = { name.index, rows, cols, kTexDimNone, arraySize, offset };
	m_Properties.push_back(prop);
	size_t size = rows * cols * arraySize;
	m_Buffer.resize_uninitialized(offset + size);
	memcpy(&m_Buffer[offset], data, size * sizeof(float));
}

void MaterialPropertyBlock::AddPropertyTexture(const ShaderLab::FastPropertyName& name, TextureDimension dim, TextureID tid)
{
	AddProperty (name, reinterpret_cast<float*> (&tid.m_ID), 1, 1, 1);
	m_Properties.back().texDim = dim;
}

void MaterialPropertyBlock::AddPropertyFloat(const ShaderLab::FastPropertyName& name, float val)
{
	AddProperty(name, &val, 1, 1, 1);
}

void MaterialPropertyBlock::AddPropertyVector(const ShaderLab::FastPropertyName& name, const Vector4f& vec)
{
	AddProperty(name, vec.GetPtr(), 1, 4, 1);
}

void MaterialPropertyBlock::AddPropertyColor(const ShaderLab::FastPropertyName& name, const ColorRGBAf& col)
{
	ColorRGBAf converted = GammaToActiveColorSpace (col);
	AddProperty(name, converted.GetPtr(), 1, 4, 1);
}

void MaterialPropertyBlock::AddPropertyMatrix(const ShaderLab::FastPropertyName& name, const Matrix4x4f& mat)
{
	AddProperty(name, mat.GetPtr(), 4, 4, 1);
}

void MaterialPropertyBlock::ReplacePropertyTexture(const ShaderLab::FastPropertyName& name, TextureDimension dim, TextureID tid)
{
	int index = GetPropertyIndex(name);
	if (index == -1)
		AddPropertyTexture(name, dim, tid);
	else
	{
		Property& prop = m_Properties[index];
		if (prop.rows == 1 && prop.cols == 1 && prop.arraySize == 1)
		{
			TextureID* buf = reinterpret_cast<TextureID*> (&m_Buffer[prop.offset]);
			*buf = tid;
			prop.texDim = dim;
		}
		else
		{
			ErrorString("The material property is different from already stored property.");
		}
	}	
}

void MaterialPropertyBlock::ReplacePropertyColor(const ShaderLab::FastPropertyName& name, const ColorRGBAf& col)
{
	ColorRGBAf activeColor = GammaToActiveColorSpace (col);
	ReplacePropertyVector (name, *reinterpret_cast<const Vector4f*> (&activeColor));
}

void MaterialPropertyBlock::ReplacePropertyVector(const ShaderLab::FastPropertyName& name, const Vector4f& vec)
{
	int index = GetPropertyIndex(name);
	if (index == -1)
		AddPropertyVector(name, vec);
	else
	{
		const Property& prop = m_Properties[index];
		if (prop.rows == 1 && prop.cols == 4 && prop.arraySize == 1)
		{
			Vector4f* buf = reinterpret_cast<Vector4f*> (&m_Buffer[prop.offset]);
			*buf = vec;
		}
		else
		{
			ErrorString("The material property is different from already stored property.");
		}
	}
}

void MaterialPropertyBlock::ReplacePropertyFloat(const ShaderLab::FastPropertyName& name, float data)
{
	ReplacePartialFloatProperty(name, data, 1, 0);
}

void MaterialPropertyBlock::ReplacePartialFloatColorProperty(const ShaderLab::FastPropertyName& name, float data, UInt8 cols, UInt8 colIndex)
{
	ReplacePartialFloatProperty(name, GammaToActiveColorSpace(data), cols, colIndex);
}

void MaterialPropertyBlock::ReplacePartialFloatProperty(const ShaderLab::FastPropertyName& name, float data, UInt8 cols, UInt8 colIndex)
{
	int index = GetPropertyIndex(name);
	if (index == -1)
	{
		float prop[4] = { 0.0F, 0.0F, 0.0F, 0.0F };
		prop[colIndex] = data;
		
		AddProperty(name, prop, 1, cols, 1);
	}
	else
	{
		const Property& prop = m_Properties[index];
		if (prop.rows == 1 && prop.cols == cols && prop.arraySize == 1)
		{
			float* buffer = reinterpret_cast<float*> (&m_Buffer[prop.offset]);
			buffer[colIndex] = data;
		}
		else
		{
			ErrorString("The material property is different from already stored property.");
		}
	}
}

int MaterialPropertyBlock::GetPropertyIndex (const ShaderLab::FastPropertyName& name) const
{
	for (int i=0;i<m_Properties.size();i++)
	{
		if (m_Properties[i].nameIndex == name.index)
			return i;
	}
	return -1;
}

const void* MaterialPropertyBlock::Find(const ShaderLab::FastPropertyName& name, UInt8 rows, UInt8 cols, size_t arraySize) const
{
	for (size_t i = 0, n = m_Properties.size(); i != n; ++i)
	{
		const Property& prop = m_Properties[i];
		if (name.index == prop.nameIndex && prop.cols == cols && prop.rows == rows)
			return &m_Buffer[prop.offset];
	}
	return NULL;
}


const float* MaterialPropertyBlock::FindFloat(const ShaderLab::FastPropertyName& name) const
{
	return static_cast<const float*> (Find(name, 1, 1, 1));
}

const Vector4f* MaterialPropertyBlock::FindVector(const ShaderLab::FastPropertyName& name) const
{
	return static_cast<const Vector4f*> (Find(name, 1, 4, 1));
}

const Matrix4x4f* MaterialPropertyBlock::FindMatrix(const ShaderLab::FastPropertyName& name) const
{
	return static_cast<const Matrix4x4f*> (Find(name, 4, 4, 1));
}

bool MaterialPropertyBlock::GetColor(const ShaderLab::FastPropertyName& name, ColorRGBAf& outColor) const
{
	const ColorRGBAf* color = static_cast<const ColorRGBAf*> (Find(name, 1, 4, 1));
	if (color != NULL)
	{
		outColor = ActiveToGammaColorSpace(*color);
		return true;
	}
	else
		return false;
}

const TextureID MaterialPropertyBlock::FindTexture(const ShaderLab::FastPropertyName& name) const
{
	for (size_t i = 0, n = m_Properties.size(); i != n; ++i)
	{
		const Property& prop = m_Properties[i];
		if (name.index == prop.nameIndex && prop.texDim != kTexDimNone)
			return TextureID(*(const int*)&m_Buffer[prop.offset]);
	}
	return TextureID();
}