|
|
@@ -27,12 +27,10 @@ OTHER DEALINGS IN THE SOFTWARE.
|
|
|
#include "material.h"
|
|
|
#include "material_resource.h"
|
|
|
#include "memory.h"
|
|
|
-#include "random.h"
|
|
|
#include "device.h"
|
|
|
#include "resource_manager.h"
|
|
|
#include "texture_resource.h"
|
|
|
#include "material_manager.h"
|
|
|
-#include "log.h"
|
|
|
#include "shader.h"
|
|
|
#include <bgfx.h>
|
|
|
|
|
|
@@ -44,73 +42,18 @@ void Material::create(const MaterialResource* mr, MaterialManager& mm)
|
|
|
uint32_t size = mr->dynamic_data_size();
|
|
|
uint32_t offt = mr->dynamic_data_offset();
|
|
|
char* base = (char*) mr + offt;
|
|
|
-
|
|
|
- data = (char*) default_allocator().allocate(size);
|
|
|
- memcpy(data, base, size);
|
|
|
-
|
|
|
- for (uint32_t i = 0; i < mr->num_textures(); i++)
|
|
|
- {
|
|
|
- TextureData* ud = mr->get_texture_data(i);
|
|
|
-
|
|
|
- TextureHandle* th = mr->get_texture_handle(i, data);
|
|
|
- th->sampler_handle = bgfx::createUniform(ud->sampler_name, bgfx::UniformType::Uniform1iv).idx;
|
|
|
-
|
|
|
- ResourceId texid;
|
|
|
- texid.type = TEXTURE_TYPE;
|
|
|
- texid.name = ud->id;
|
|
|
- TextureImage* teximg = (TextureImage*) device()->resource_manager()->get(texid);
|
|
|
- th->texture_handle = teximg->handle.idx;
|
|
|
- }
|
|
|
-
|
|
|
- for (uint32_t i = 0; i < mr->num_uniforms(); i++)
|
|
|
- {
|
|
|
- UniformData* ud = mr->get_uniform_data(i);
|
|
|
-
|
|
|
- UniformHandle* uh = mr->get_uniform_handle(i, data);
|
|
|
- uh->uniform_handle = bgfx::createUniform(ud->name, bgfx::UniformType::Uniform4fv).idx;
|
|
|
- }
|
|
|
-
|
|
|
- resource = mr;
|
|
|
-}
|
|
|
-
|
|
|
-void Material::clone(const Material& m)
|
|
|
-{
|
|
|
- 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);
|
|
|
-
|
|
|
- bgfx::UniformHandle sh;
|
|
|
- sh.idx = th->sampler_handle;
|
|
|
- bgfx::destroyUniform(sh);
|
|
|
- }
|
|
|
-
|
|
|
- for (uint32_t i = 0; i < resource->num_uniforms(); i++)
|
|
|
- {
|
|
|
- UniformHandle* uh = resource->get_uniform_handle(i, data);
|
|
|
-
|
|
|
- bgfx::UniformHandle bgfx_uh;
|
|
|
- bgfx_uh.idx = uh->uniform_handle;
|
|
|
- bgfx::destroyUniform(bgfx_uh);
|
|
|
- }
|
|
|
-
|
|
|
default_allocator().deallocate(data);
|
|
|
}
|
|
|
|
|
|
void Material::bind() const
|
|
|
{
|
|
|
- // bgfx::setProgram(program);
|
|
|
ResourceId shader_id;
|
|
|
shader_id.type = SHADER_TYPE;
|
|
|
shader_id.name = resource->shader();
|
|
|
@@ -120,12 +63,18 @@ void Material::bind() const
|
|
|
// Set samplers
|
|
|
for (uint32_t i = 0; i < resource->num_textures(); i++)
|
|
|
{
|
|
|
+ TextureData* td = resource->get_texture_data(i);
|
|
|
TextureHandle* th = resource->get_texture_handle(i, data);
|
|
|
|
|
|
bgfx::UniformHandle sampler;
|
|
|
bgfx::TextureHandle texture;
|
|
|
sampler.idx = th->sampler_handle;
|
|
|
- texture.idx = th->texture_handle;
|
|
|
+
|
|
|
+ ResourceId texid;
|
|
|
+ texid.type = TEXTURE_TYPE;
|
|
|
+ texid.name = td->id;
|
|
|
+ TextureImage* teximg = (TextureImage*) device()->resource_manager()->get(texid);
|
|
|
+ texture.idx = teximg->handle.idx;
|
|
|
|
|
|
bgfx::setTexture(i, sampler, texture);
|
|
|
}
|
|
|
@@ -145,7 +94,6 @@ 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)
|