|
|
@@ -31,95 +31,15 @@ OTHER DEALINGS IN THE SOFTWARE.
|
|
|
#include "device.h"
|
|
|
#include "resource_manager.h"
|
|
|
#include "texture_resource.h"
|
|
|
+#include "material_manager.h"
|
|
|
+#include "log.h"
|
|
|
+#include "shader.h"
|
|
|
#include <bgfx.h>
|
|
|
|
|
|
-static const uint8_t vs_h[358] =
|
|
|
-{
|
|
|
- 0x56, 0x53, 0x48, 0x02, 0x8c, 0xd4, 0x10, 0xbd, 0x01, 0x00, 0x0f, 0x75, 0x5f, 0x6d, 0x6f, 0x64, // VSH........u_mod
|
|
|
- 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x09, 0x01, 0x00, 0x00, 0x01, 0x00, // elViewProj......
|
|
|
- 0x41, 0x01, 0x00, 0x00, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x6d, 0x65, // A...attribute me
|
|
|
- 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, 0x5f, 0x70, 0x6f, 0x73, // diump vec2 a_pos
|
|
|
- 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, // ition;.attribute
|
|
|
- 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, 0x5f, // mediump vec2 a_
|
|
|
- 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, 0x3b, 0x0a, 0x76, 0x61, 0x72, 0x79, 0x69, // texcoord0;.varyi
|
|
|
- 0x6e, 0x67, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, // ng mediump vec2
|
|
|
- 0x76, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, 0x3b, 0x0a, 0x75, 0x6e, 0x69, // v_texcoord0;.uni
|
|
|
- 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x6d, 0x61, 0x74, // form mediump mat
|
|
|
- 0x34, 0x20, 0x75, 0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, // 4 u_modelViewPro
|
|
|
- 0x6a, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, // j;.void main ().
|
|
|
- 0x7b, 0x0a, 0x20, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, // {. mediump vec4
|
|
|
- 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, // tmpvar_1;. tmp
|
|
|
- 0x76, 0x61, 0x72, 0x5f, 0x31, 0x2e, 0x7a, 0x77, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x32, 0x28, // var_1.zw = vec2(
|
|
|
- 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, // 0.0, 1.0);. tmp
|
|
|
- 0x76, 0x61, 0x72, 0x5f, 0x31, 0x2e, 0x78, 0x79, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x70, 0x6f, 0x73, // var_1.xy = a_pos
|
|
|
- 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69, // ition;. gl_Posi
|
|
|
- 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x28, 0x75, 0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, // tion = (u_modelV
|
|
|
- 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x20, 0x2a, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, // iewProj * tmpvar
|
|
|
- 0x5f, 0x31, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x76, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, // _1);. v_texcoor
|
|
|
- 0x64, 0x30, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, // d0 = a_texcoord0
|
|
|
- 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00, // ;.}...
|
|
|
-};
|
|
|
-
|
|
|
-static const uint8_t fs_h[207] =
|
|
|
-{
|
|
|
- 0x46, 0x53, 0x48, 0x02, 0x8c, 0xd4, 0x10, 0xbd, 0x01, 0x00, 0x08, 0x75, 0x5f, 0x61, 0x6c, 0x62, // FSH........u_alb
|
|
|
- 0x65, 0x64, 0x6f, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0xb1, 0x00, 0x00, 0x00, 0x76, 0x61, 0x72, // edo..........var
|
|
|
- 0x79, 0x69, 0x6e, 0x67, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, // ying mediump vec
|
|
|
- 0x32, 0x20, 0x76, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, 0x3b, 0x0a, 0x75, // 2 v_texcoord0;.u
|
|
|
- 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x73, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x72, 0x32, 0x44, // niform sampler2D
|
|
|
- 0x20, 0x75, 0x5f, 0x61, 0x6c, 0x62, 0x65, 0x64, 0x6f, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, // u_albedo;.void
|
|
|
- 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x6c, 0x6f, 0x77, 0x70, // main ().{. lowp
|
|
|
- 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x3b, 0x0a, // vec4 tmpvar_1;.
|
|
|
- 0x20, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x20, 0x3d, 0x20, 0x74, 0x65, 0x78, // tmpvar_1 = tex
|
|
|
- 0x74, 0x75, 0x72, 0x65, 0x32, 0x44, 0x20, 0x28, 0x75, 0x5f, 0x61, 0x6c, 0x62, 0x65, 0x64, 0x6f, // ture2D (u_albedo
|
|
|
- 0x2c, 0x20, 0x76, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, 0x29, 0x3b, 0x0a, // , v_texcoord0);.
|
|
|
- 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x46, 0x72, 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, // gl_FragColor =
|
|
|
- 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00, // tmpvar_1;.}...
|
|
|
-};
|
|
|
-
|
|
|
-static const uint8_t vsc_h[358] =
|
|
|
-{
|
|
|
- 0x56, 0x53, 0x48, 0x02, 0x8c, 0xd4, 0x10, 0xbd, 0x01, 0x00, 0x0f, 0x75, 0x5f, 0x6d, 0x6f, 0x64, // VSH........u_mod
|
|
|
- 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x09, 0x01, 0x00, 0x00, 0x01, 0x00, // elViewProj......
|
|
|
- 0x41, 0x01, 0x00, 0x00, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x20, 0x6d, 0x65, // A...attribute me
|
|
|
- 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, 0x5f, 0x70, 0x6f, 0x73, // diump vec2 a_pos
|
|
|
- 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, // ition;.attribute
|
|
|
- 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x61, 0x5f, // mediump vec2 a_
|
|
|
- 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, 0x3b, 0x0a, 0x76, 0x61, 0x72, 0x79, 0x69, // texcoord0;.varyi
|
|
|
- 0x6e, 0x67, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, // ng mediump vec2
|
|
|
- 0x76, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, 0x3b, 0x0a, 0x75, 0x6e, 0x69, // v_texcoord0;.uni
|
|
|
- 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x6d, 0x61, 0x74, // form mediump mat
|
|
|
- 0x34, 0x20, 0x75, 0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, // 4 u_modelViewPro
|
|
|
- 0x6a, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, // j;.void main ().
|
|
|
- 0x7b, 0x0a, 0x20, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, // {. mediump vec4
|
|
|
- 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, 0x5f, 0x31, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, // tmpvar_1;. tmp
|
|
|
- 0x76, 0x61, 0x72, 0x5f, 0x31, 0x2e, 0x7a, 0x77, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x32, 0x28, // var_1.zw = vec2(
|
|
|
- 0x30, 0x2e, 0x30, 0x2c, 0x20, 0x31, 0x2e, 0x30, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x74, 0x6d, 0x70, // 0.0, 1.0);. tmp
|
|
|
- 0x76, 0x61, 0x72, 0x5f, 0x31, 0x2e, 0x78, 0x79, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x70, 0x6f, 0x73, // var_1.xy = a_pos
|
|
|
- 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x3b, 0x0a, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x50, 0x6f, 0x73, 0x69, // ition;. gl_Posi
|
|
|
- 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x3d, 0x20, 0x28, 0x75, 0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x6c, 0x56, // tion = (u_modelV
|
|
|
- 0x69, 0x65, 0x77, 0x50, 0x72, 0x6f, 0x6a, 0x20, 0x2a, 0x20, 0x74, 0x6d, 0x70, 0x76, 0x61, 0x72, // iewProj * tmpvar
|
|
|
- 0x5f, 0x31, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x76, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, // _1);. v_texcoor
|
|
|
- 0x64, 0x30, 0x20, 0x3d, 0x20, 0x61, 0x5f, 0x74, 0x65, 0x78, 0x63, 0x6f, 0x6f, 0x72, 0x64, 0x30, // d0 = a_texcoord0
|
|
|
- 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00, // ;.}...
|
|
|
-};
|
|
|
-
|
|
|
-static const uint8_t fsc_h[103] =
|
|
|
-{
|
|
|
- 0x46, 0x53, 0x48, 0x02, 0x8c, 0xd4, 0x10, 0xbd, 0x01, 0x00, 0x07, 0x75, 0x5f, 0x63, 0x6f, 0x6c, // FSH........u_col
|
|
|
- 0x6f, 0x72, 0x07, 0x01, 0x00, 0x00, 0x01, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x75, 0x6e, 0x69, 0x66, // or......J...unif
|
|
|
- 0x6f, 0x72, 0x6d, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x75, 0x6d, 0x70, 0x20, 0x76, 0x65, 0x63, 0x34, // orm mediump vec4
|
|
|
- 0x20, 0x75, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x6d, // u_color;.void m
|
|
|
- 0x61, 0x69, 0x6e, 0x20, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20, 0x67, 0x6c, 0x5f, 0x46, 0x72, // ain ().{. gl_Fr
|
|
|
- 0x61, 0x67, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x75, 0x5f, 0x63, 0x6f, 0x6c, 0x6f, // agColor = u_colo
|
|
|
- 0x72, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x00, // r;.}...
|
|
|
-};
|
|
|
-
|
|
|
-
|
|
|
namespace crown
|
|
|
{
|
|
|
|
|
|
-void Material::create(const MaterialResource* mr)
|
|
|
+void Material::create(const MaterialResource* mr, MaterialManager& mm)
|
|
|
{
|
|
|
uint32_t size = mr->dynamic_data_size();
|
|
|
uint32_t offt = mr->dynamic_data_offset();
|
|
|
@@ -128,10 +48,6 @@ void Material::create(const MaterialResource* mr)
|
|
|
data = (char*) default_allocator().allocate(size);
|
|
|
memcpy(data, base, size);
|
|
|
|
|
|
- bgfx::ShaderHandle vs = bgfx::createShader(bgfx::makeRef(vs_h, sizeof(vs_h)));
|
|
|
- bgfx::ShaderHandle fs = bgfx::createShader(bgfx::makeRef(fs_h, sizeof(fs_h)));
|
|
|
- program = bgfx::createProgram(vs, fs, true);
|
|
|
-
|
|
|
for (uint32_t i = 0; i < mr->num_textures(); i++)
|
|
|
{
|
|
|
TextureData* ud = mr->get_texture_data(i);
|
|
|
@@ -142,9 +58,8 @@ void Material::create(const MaterialResource* mr)
|
|
|
ResourceId texid;
|
|
|
texid.type = TEXTURE_TYPE;
|
|
|
texid.name = ud->id;
|
|
|
- TextureResource* tex = (TextureResource*) device()->resource_manager()->get(texid);
|
|
|
-
|
|
|
- th->texture_handle = bgfx::createTexture(bgfx::copy(tex->data(), tex->size())).idx;
|
|
|
+ TextureImage* teximg = (TextureImage*) device()->resource_manager()->get(texid);
|
|
|
+ th->texture_handle = teximg->handle.idx;
|
|
|
}
|
|
|
|
|
|
for (uint32_t i = 0; i < mr->num_uniforms(); i++)
|
|
|
@@ -158,10 +73,20 @@ void Material::create(const MaterialResource* mr)
|
|
|
resource = mr;
|
|
|
}
|
|
|
|
|
|
-void Material::destroy() const
|
|
|
+void Material::clone(const Material& m)
|
|
|
{
|
|
|
- bgfx::destroyProgram(program);
|
|
|
+ const MaterialResource* mr = m.resource;
|
|
|
+ uint32_t size = mr->dynamic_data_size();
|
|
|
+ char* base = m.data;
|
|
|
|
|
|
+ data = (char*) default_allocator().allocate(size);
|
|
|
+ memcpy(data, base, size);
|
|
|
+
|
|
|
+ resource = mr;
|
|
|
+}
|
|
|
+
|
|
|
+void Material::destroy() const
|
|
|
+{
|
|
|
for (uint32_t i = 0; i < resource->num_textures(); i++)
|
|
|
{
|
|
|
TextureHandle* th = resource->get_texture_handle(i, data);
|
|
|
@@ -169,10 +94,6 @@ void Material::destroy() const
|
|
|
bgfx::UniformHandle sh;
|
|
|
sh.idx = th->sampler_handle;
|
|
|
bgfx::destroyUniform(sh);
|
|
|
-
|
|
|
- bgfx::TextureHandle bgfx_th;
|
|
|
- bgfx_th.idx = th->texture_handle;
|
|
|
- bgfx::destroyTexture(bgfx_th);
|
|
|
}
|
|
|
|
|
|
for (uint32_t i = 0; i < resource->num_uniforms(); i++)
|
|
|
@@ -189,7 +110,12 @@ void Material::destroy() const
|
|
|
|
|
|
void Material::bind() const
|
|
|
{
|
|
|
- bgfx::setProgram(program);
|
|
|
+ // bgfx::setProgram(program);
|
|
|
+ ResourceId shader_id;
|
|
|
+ shader_id.type = SHADER_TYPE;
|
|
|
+ shader_id.name = resource->shader();
|
|
|
+ Shader* shader = (Shader*) device()->resource_manager()->get(shader_id);
|
|
|
+ bgfx::setProgram(shader->program);
|
|
|
|
|
|
// Set samplers
|
|
|
for (uint32_t i = 0; i < resource->num_textures(); i++)
|
|
|
@@ -215,4 +141,23 @@ void Material::bind() const
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void Material::set_float(const char* name, float val)
|
|
|
+{
|
|
|
+ char* p = (char*) resource->get_uniform_handle_by_string(name, data);
|
|
|
+ *((float*)(p + sizeof(uint32_t))) = val;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+void Material::set_vector2(const char* name, const Vector2& val)
|
|
|
+{
|
|
|
+ char* p = (char*) resource->get_uniform_handle_by_string(name, data);
|
|
|
+ *((Vector2*)(p + sizeof(uint32_t))) = val;
|
|
|
+}
|
|
|
+
|
|
|
+void Material::set_vector3(const char* name, const Vector3& val)
|
|
|
+{
|
|
|
+ char* p = (char*) resource->get_uniform_handle_by_string(name, data);
|
|
|
+ *((Vector3*)(p + sizeof(uint32_t))) = val;
|
|
|
+}
|
|
|
+
|
|
|
} // namespace crown
|