LumixEngine/src/renderer/material.cpp

730 lines
17 KiB
C++
Raw Normal View History

2015-08-17 23:45:26 +02:00
#include "renderer/material.h"
2016-05-10 08:24:31 +02:00
#include "engine/crc32.h"
2019-06-11 22:39:39 +02:00
#include "engine/file_system.h"
2016-05-10 08:24:31 +02:00
#include "engine/log.h"
2018-06-30 15:18:27 +02:00
#include "engine/lua_wrapper.h"
2016-05-10 08:24:31 +02:00
#include "engine/path_utils.h"
#include "engine/profiler.h"
#include "engine/resource_manager.h"
2019-06-11 01:09:14 +02:00
#include "engine/stream.h"
2016-02-14 16:49:37 +01:00
#include "renderer/pipeline.h"
2015-09-05 13:08:47 +02:00
#include "renderer/renderer.h"
2015-08-17 23:45:26 +02:00
#include "renderer/shader.h"
#include "renderer/texture.h"
2018-07-15 17:35:41 +02:00
#include "ffr/ffr.h"
2014-06-16 21:18:15 +02:00
namespace Lumix
{
2014-09-07 01:10:24 +02:00
static const float DEFAULT_ALPHA_REF_VALUE = 0.3f;
2016-05-17 07:10:20 +02:00
2016-03-14 00:59:28 +01:00
static struct CustomFlags
{
char flags[32][32];
int count;
} s_custom_flags = {};
2014-09-07 01:10:24 +02:00
2018-01-11 21:13:59 +01:00
const ResourceType Material::TYPE("material");
Material::Material(const Path& path, ResourceManager& resource_manager, Renderer& renderer, IAllocator& allocator)
2015-09-04 14:00:28 +02:00
: Resource(path, resource_manager, allocator)
, m_shader(nullptr)
, m_uniforms(allocator)
, m_allocator(allocator)
, m_texture_count(0)
2018-07-01 18:13:44 +02:00
, m_renderer(renderer)
, m_render_states(u64(ffr::StateFlags::CULL_BACK))
2017-11-07 12:03:51 +01:00
, m_color(1, 1, 1, 1)
, m_metallic(1.f)
, m_roughness(1.f)
2018-02-21 17:15:47 +01:00
, m_emission(0.0f)
2016-02-08 00:18:19 +01:00
, m_define_mask(0)
2016-03-14 00:59:28 +01:00
, m_custom_flags(0)
, m_render_data(nullptr)
2015-09-04 14:00:28 +02:00
{
2018-10-13 23:24:44 +02:00
static u32 last_sort_key = 0;
m_sort_key = ++last_sort_key;
2018-10-13 15:08:58 +02:00
m_layer = m_renderer.getLayerIdx("default");
setAlphaRef(DEFAULT_ALPHA_REF_VALUE);
2015-09-04 14:00:28 +02:00
for (int i = 0; i < MAX_TEXTURE_COUNT; ++i)
{
m_textures[i] = nullptr;
}
setShader(nullptr);
2015-09-04 14:00:28 +02:00
}
2014-06-16 21:18:15 +02:00
Material::~Material()
{
ASSERT(isEmpty());
}
2015-06-18 01:33:49 +02:00
2016-03-14 00:59:28 +01:00
const char* Material::getCustomFlagName(int index)
{
return s_custom_flags.flags[index];
}
int Material::getCustomFlagCount()
{
return s_custom_flags.count;
}
2016-11-20 17:27:41 +01:00
u32 Material::getCustomFlag(const char* flag_name)
2016-03-14 00:59:28 +01:00
{
for (int i = 0; i < s_custom_flags.count; ++i)
{
2016-05-12 09:56:07 +02:00
if (equalStrings(s_custom_flags.flags[i], flag_name)) return 1 << i;
2016-03-14 00:59:28 +01:00
}
if (s_custom_flags.count >= lengthOf(s_custom_flags.flags))
{
ASSERT(false);
return 0;
}
copyString(s_custom_flags.flags[s_custom_flags.count], flag_name);
++s_custom_flags.count;
return 1 << (s_custom_flags.count - 1);
}
2016-11-20 17:27:41 +01:00
bool Material::isDefined(u8 define_idx) const
2015-09-04 14:00:28 +02:00
{
2016-02-08 00:18:19 +01:00
return (m_define_mask & (1 << define_idx)) != 0;
2015-09-05 13:08:47 +02:00
}
2016-11-20 17:27:41 +01:00
void Material::setDefine(u8 define_idx, bool enabled)
2015-09-05 20:54:26 +02:00
{
2019-07-23 00:08:28 +02:00
u32 old_mask = m_define_mask;
2018-07-01 18:13:44 +02:00
if (enabled) {
2016-02-08 00:18:19 +01:00
m_define_mask |= 1 << define_idx;
2015-09-04 14:00:28 +02:00
}
2018-07-01 18:13:44 +02:00
else {
2016-02-08 00:18:19 +01:00
m_define_mask &= ~(1 << define_idx);
2015-09-04 14:00:28 +02:00
}
2019-07-23 00:08:28 +02:00
if(old_mask != m_define_mask) updateRenderData(false);
2014-10-06 23:58:33 +02:00
}
2019-08-12 18:10:01 +02:00
Material::Uniform* Material::findUniform(u32 name_hash) {
for (Uniform& u : m_uniforms) {
if (u.name_hash == name_hash) return &u;
}
return nullptr;
}
2017-11-28 19:37:15 +01:00
void Material::unload()
2014-06-16 21:18:15 +02:00
{
2016-02-09 19:21:00 +01:00
m_uniforms.clear();
2018-07-01 18:13:44 +02:00
for (int i = 0; i < m_texture_count; i++) {
2019-07-23 18:24:13 +02:00
if (m_textures[i]) {
2015-05-31 15:48:35 +02:00
removeDependency(*m_textures[i]);
2019-07-23 18:24:13 +02:00
m_textures[i]->getResourceManager().unload(*m_textures[i]);
2015-02-28 13:05:01 +01:00
}
2014-06-16 21:18:15 +02:00
}
2018-07-01 18:13:44 +02:00
m_texture_count = 0;
2018-07-11 23:35:34 +02:00
for(Texture*& tex : m_textures ) {
tex = nullptr;
}
2018-07-01 18:13:44 +02:00
m_renderer.runInRenderThread(m_render_data, [](Renderer& renderer, void* ptr){
LUMIX_DELETE(renderer.getAllocator(), (RenderData*)ptr);
});
m_render_data = nullptr;
2018-04-19 20:49:20 +02:00
setShader(nullptr);
2017-09-05 22:32:28 +02:00
m_alpha_ref = 0.3f;
2017-11-07 12:03:51 +01:00
m_color.set(1, 1, 1, 1);
2017-09-05 22:32:28 +02:00
m_custom_flags = 0;
m_define_mask = 0;
m_metallic = 1.0f;
2017-09-05 22:32:28 +02:00
m_roughness = 1.0f;
2018-02-21 17:15:47 +01:00
m_emission = 0.0f;
2018-07-01 18:13:44 +02:00
m_render_states = u64(ffr::StateFlags::CULL_BACK);
2014-06-16 21:18:15 +02:00
}
2019-06-11 01:09:14 +02:00
bool Material::save(IOutputStream& file)
2014-06-16 21:18:15 +02:00
{
2016-02-09 19:21:00 +01:00
if(!isReady()) return false;
if(!m_shader) return false;
2018-08-05 18:00:30 +02:00
2018-12-24 16:15:22 +01:00
file << "shader \"" << m_shader->getPath().c_str() << "\"\n";
2018-09-09 15:31:09 +02:00
file << "backface_culling(" << (isBackfaceCulling() ? "true" : "false") << ")\n";
2018-10-27 21:05:52 +02:00
file << "layer \"" << m_renderer.getLayerName(m_layer) << "\"\n";
2016-02-09 19:21:00 +01:00
2019-08-12 18:10:01 +02:00
file << "emission(" << m_emission << ")\n";
file << "metallic(" << m_metallic << ")\n";
file << "roughness(" << m_roughness << ")\n";
file << "alpha_ref(" << m_alpha_ref << ")\n";
2016-02-08 00:18:19 +01:00
2018-08-05 18:00:30 +02:00
file << "defines {";
for (int i = 0; i < sizeof(m_define_mask) * 8; ++i) {
if ((m_define_mask & (1 << i)) == 0) continue;
2018-10-27 21:05:52 +02:00
const char* def = m_renderer.getShaderDefine(i);
2018-12-24 16:15:22 +01:00
if (i > 0) file << ", ";
2018-08-05 18:00:30 +02:00
file << "\"" << def << "\"";
}
file << "}\n";
2019-08-12 18:10:01 +02:00
file << "color { " << m_color.x << ", " << m_color.y << ", " << m_color.z << ", " << m_color.w << " }\n";
2018-09-08 14:41:18 +02:00
2018-08-05 18:00:30 +02:00
for (int i = 0; i < m_texture_count; ++i) {
char path[MAX_PATH_LENGTH];
2018-08-05 18:00:30 +02:00
if (m_textures[i] && m_textures[i] != m_shader->m_texture_slots[i].default_texture) {
2019-07-25 18:50:31 +02:00
copyString(Span(path), m_textures[i]->getPath().c_str());
2015-05-31 15:48:35 +02:00
}
2018-08-05 18:00:30 +02:00
else {
2015-05-31 15:48:35 +02:00
path[0] = '\0';
2014-09-28 17:29:39 +02:00
}
2018-08-05 18:00:30 +02:00
file << "texture \"/" << path << "\"\n";
2014-09-28 17:29:39 +02:00
}
2016-02-08 00:18:19 +01:00
2019-08-12 18:10:01 +02:00
file << "layer \"" << m_renderer.getLayerName(m_layer) << "\"\n";
if (m_custom_flags != 0) {
2016-03-14 00:59:28 +01:00
for (int i = 0; i < 32; ++i)
{
2019-08-12 18:10:01 +02:00
if (m_custom_flags & (1 << i)) {
file << "custom_flag \"" << s_custom_flags.flags[i] << "\"\n";
}
2016-03-14 00:59:28 +01:00
}
}
2019-08-12 18:10:01 +02:00
auto writeArray = [&file](const float* value, u32 num) {
file << "{ ";
for (u32 i = 0; i < num; ++i) {
if (i > 0) file << ", ";
file << value[i];
}
file << " }";
};
for (const Shader::Uniform& su : m_shader->m_uniforms) {
for (const Uniform& mu : m_uniforms) {
if(mu.name_hash == su.name_hash) {
file << "uniform(\"" << su.name << "\", ";
switch(su.type) {
case Shader::Uniform::FLOAT: file << mu.float_value; break;
case Shader::Uniform::INT: file << *(int*)&mu.float_value; break;
case Shader::Uniform::COLOR:
case Shader::Uniform::VEC4: writeArray(mu.vec4, 4); break;
case Shader::Uniform::VEC3: writeArray(mu.vec4, 3); break;
case Shader::Uniform::VEC2: writeArray(mu.vec4, 2); break;
case Shader::Uniform::MATRIX4: writeArray(mu.matrix, 16); break;
default: ASSERT(false); break;
2014-09-28 17:29:39 +02:00
}
2019-08-12 18:10:01 +02:00
file << ")\n";
2014-09-28 17:29:39 +02:00
break;
2019-08-12 18:10:01 +02:00
}
2014-09-28 17:29:39 +02:00
}
2019-08-12 18:10:01 +02:00
}
2015-09-25 23:03:10 +02:00
return true;
2014-06-16 21:18:15 +02:00
}
2019-08-12 18:10:01 +02:00
int Material::uniform(lua_State* L) {
const char* name = LuaWrapper::checkArg<const char*>(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
Uniform u;
u.name_hash = crc32(name);
switch (lua_type(L, 2)) {
case LUA_TNUMBER: u.float_value = LuaWrapper::toType<float>(L, 2); break;
case LUA_TTABLE:
switch (lua_objlen(L, 2)) {
case 2: *(Vec2*)u.vec2 = LuaWrapper::toType<Vec2>(L, 2);
case 3: *(Vec3*)u.vec3 = LuaWrapper::toType<Vec3>(L, 2);
case 4: *(Vec4*)u.vec4 = LuaWrapper::toType<Vec4>(L, 2);
case 16: *(Matrix*)u.vec4 = LuaWrapper::toType<Matrix>(L, 2);
default: luaL_error(L, "Uniform %s has unsupported type", name); break;
2014-06-16 21:18:15 +02:00
}
2019-08-12 18:10:01 +02:00
break;
default: luaL_error(L, "Uniform %s has unsupported type", name); break;
2014-06-16 21:18:15 +02:00
}
2019-08-12 18:10:01 +02:00
material->m_uniforms.push(u);
return 0;
2014-06-16 21:18:15 +02:00
}
2015-05-28 23:30:50 +02:00
2015-05-31 15:48:35 +02:00
void Material::setTexturePath(int i, const Path& path)
2014-06-16 21:18:15 +02:00
{
2015-05-31 15:48:35 +02:00
if (path.length() == 0)
2014-06-16 21:18:15 +02:00
{
2015-05-31 15:48:35 +02:00
setTexture(i, nullptr);
2014-06-16 21:18:15 +02:00
}
2015-05-31 15:48:35 +02:00
else
2014-09-27 22:15:14 +02:00
{
Texture* texture = m_resource_manager.getOwner().load<Texture>(path);
2015-05-31 15:48:35 +02:00
setTexture(i, texture);
2014-09-27 22:15:14 +02:00
}
2014-12-12 00:48:06 +01:00
}
2014-06-16 21:18:15 +02:00
void Material::setTexture(int i, Texture* texture)
2016-02-10 15:25:28 +01:00
{
2015-06-01 23:35:57 +02:00
Texture* old_texture = i < m_texture_count ? m_textures[i] : nullptr;
2018-08-19 22:01:20 +02:00
if (!texture && m_shader && m_shader->isReady() && m_shader->m_texture_slots[i].default_texture)
2018-04-19 20:49:20 +02:00
{
texture = m_shader->m_texture_slots[i].default_texture;
2019-07-23 18:24:13 +02:00
texture->getResourceManager().load(*texture);
2018-04-19 20:49:20 +02:00
}
2019-07-23 18:24:13 +02:00
if (texture) addDependency(*texture);
2015-05-31 15:48:35 +02:00
m_textures[i] = texture;
if (i >= m_texture_count) m_texture_count = i + 1;
2019-07-23 18:24:13 +02:00
if (old_texture) {
2015-05-28 23:30:50 +02:00
removeDependency(*old_texture);
2019-07-23 18:24:13 +02:00
old_texture->getResourceManager().unload(*old_texture);
2015-05-28 23:30:50 +02:00
}
2015-09-05 20:54:26 +02:00
if (isReady() && m_shader)
2014-06-16 21:18:15 +02:00
{
2016-05-07 12:59:47 +02:00
int define_idx = m_shader->m_texture_slots[i].define_idx;
2016-02-10 15:25:28 +01:00
if(define_idx >= 0)
2015-09-05 20:54:26 +02:00
{
2016-02-10 15:25:28 +01:00
if(m_textures[i])
{
m_define_mask |= 1 << define_idx;
}
else
{
m_define_mask &= ~(1 << define_idx);
}
2015-09-05 20:54:26 +02:00
}
2014-06-16 21:18:15 +02:00
}
updateRenderData(false);
2014-10-06 23:58:33 +02:00
}
2014-12-12 00:48:06 +01:00
void Material::setShader(const Path& path)
{
Shader* shader = m_resource_manager.getOwner().load<Shader>(path);
2014-12-12 00:48:06 +01:00
setShader(shader);
}
2015-10-03 01:14:38 +02:00
void Material::onBeforeReady()
2015-05-31 15:48:35 +02:00
{
2015-10-03 01:14:38 +02:00
if (!m_shader) return;
2016-02-09 19:21:00 +01:00
2019-07-23 18:24:13 +02:00
for(int i = 0; i < m_shader->m_texture_slot_count; ++i) {
if (!m_textures[i] && m_shader->m_texture_slots[i].default_texture) {
m_textures[i] = m_shader->m_texture_slots[i].default_texture;
if (i >= m_texture_count) m_texture_count = i + 1;
m_textures[i]->getResourceManager().load(*m_textures[i]);
addDependency(*m_textures[i]);
return;
}
}
2016-05-07 12:59:47 +02:00
for(int i = 0; i < m_shader->m_uniforms.size(); ++i)
2016-02-09 19:21:00 +01:00
{
2019-08-12 18:10:01 +02:00
const Shader::Uniform& shader_uniform = m_shader->m_uniforms[i];
2016-02-09 19:21:00 +01:00
bool found = false;
for(int j = i; j < m_uniforms.size(); ++j)
{
if(m_uniforms[j].name_hash == shader_uniform.name_hash)
{
auto tmp = m_uniforms[i];
m_uniforms[i] = m_uniforms[j];
m_uniforms[j] = tmp;
found = true;
break;
}
}
if(found) continue;
if(i < m_uniforms.size())
{
m_uniforms.emplace(m_uniforms[i]);
}
else
{
m_uniforms.emplace();
}
m_uniforms[i].name_hash = shader_uniform.name_hash;
}
2018-07-01 18:13:44 +02:00
for(int i = 0; i < m_shader->m_texture_slot_count; ++i) {
const int define_idx = m_shader->m_texture_slots[i].define_idx;
if(define_idx >= 0) {
if(m_textures[i]) {
2016-02-10 15:25:28 +01:00
m_define_mask |= 1 << define_idx;
}
2018-07-01 18:13:44 +02:00
else {
2016-02-10 15:25:28 +01:00
m_define_mask &= ~(1 << define_idx);
}
}
}
2018-08-19 22:01:20 +02:00
for (int i = m_shader->m_texture_slot_count; i < m_texture_count; ++i) {
setTexture(i, nullptr);
}
2019-06-13 17:26:52 +02:00
m_texture_count = minimum(m_texture_count, m_shader->m_texture_slot_count);
2018-10-28 17:38:15 +01:00
2019-06-24 21:17:23 +02:00
updateRenderData(true);
}
void Material::updateRenderData(bool on_before_ready)
{
if (!m_shader) return;
if (!on_before_ready && !isReady()) return;
if(m_render_data) {
2019-08-07 21:21:31 +02:00
m_renderer.destroyMaterialConstants(m_render_data->material_constants);
2019-06-24 21:17:23 +02:00
m_renderer.runInRenderThread(m_render_data, [](Renderer& renderer, void* ptr){
LUMIX_DELETE(renderer.getAllocator(), (RenderData*)ptr);
});
}
2019-06-13 00:01:11 +02:00
m_render_data = LUMIX_NEW(m_renderer.getAllocator(), RenderData);
m_render_data->define_mask = m_define_mask;
2019-06-13 00:01:11 +02:00
m_render_data->render_states = m_render_states;
m_render_data->shader = m_shader->m_render_data;
m_render_data->textures_count = m_texture_count;
2019-08-12 18:10:01 +02:00
MaterialConsts cs;
static_assert(sizeof(cs) == 256, "Renderer::MaterialConstants must have 256B");
2019-08-07 21:21:31 +02:00
cs.color = m_color;
cs.emission = m_emission;
cs.metallic = m_metallic;
cs.roughness = m_roughness;
2019-08-12 18:10:01 +02:00
setMemory(cs.custom, 0, sizeof(cs.custom));
for (const Shader::Uniform& shader_uniform : m_shader->m_uniforms) {
for (Uniform& mat_uniform : m_uniforms) {
if (shader_uniform.name_hash == mat_uniform.name_hash) {
const u32 size = shader_uniform.size();
copyMemory((u8*)cs.custom + shader_uniform.offset, mat_uniform.matrix, size);
break;
}
}
}
2019-08-07 21:21:31 +02:00
m_render_data->material_constants = m_renderer.createMaterialConstants(cs);
2019-06-13 00:01:11 +02:00
for(int i = 0; i < m_texture_count; ++i) {
m_render_data->textures[i] = m_textures[i] ? m_textures[i]->handle : ffr::INVALID_TEXTURE;
2018-10-28 17:38:15 +01:00
}
2015-05-31 15:48:35 +02:00
}
2014-06-16 21:18:15 +02:00
void Material::setShader(Shader* shader)
{
2018-07-01 18:13:44 +02:00
if (m_shader) {
Shader* shader = m_shader;
m_shader = nullptr;
removeDependency(*shader);
2019-07-23 18:24:13 +02:00
shader->getResourceManager().unload(*shader);
2014-06-16 21:18:15 +02:00
}
m_shader = shader;
2018-07-01 18:13:44 +02:00
if (m_shader) {
2014-06-16 21:18:15 +02:00
addDependency(*m_shader);
2015-05-31 15:48:35 +02:00
}
updateRenderData(false);
2015-05-31 15:48:35 +02:00
}
2015-06-18 01:33:49 +02:00
Texture* Material::getTextureByName(const char* name) const
2015-05-31 15:48:35 +02:00
{
2016-08-04 20:53:20 +02:00
if (!m_shader) return nullptr;
2015-08-07 02:40:57 +02:00
2018-07-01 18:13:44 +02:00
for (int i = 0, c = m_shader->m_texture_slot_count; i < c; ++i) {
if (equalStrings(m_shader->m_texture_slots[i].name, name)) {
2015-05-31 15:48:35 +02:00
return m_textures[i];
}
2018-07-01 18:13:44 +02:00
}
2015-05-31 15:48:35 +02:00
return nullptr;
2014-06-16 21:18:15 +02:00
}
2016-08-04 20:53:20 +02:00
2016-11-20 17:27:41 +01:00
bool Material::isTextureDefine(u8 define_idx) const
2016-08-04 20:53:20 +02:00
{
if (!m_shader) return false;
2018-07-01 18:13:44 +02:00
for (int i = 0, c = m_shader->m_texture_slot_count; i < c; ++i) {
if (m_shader->m_texture_slots[i].define_idx == define_idx) {
2016-08-04 20:53:20 +02:00
return true;
}
2014-09-27 22:15:14 +02:00
}
2018-07-01 18:13:44 +02:00
return false;
2014-09-27 22:15:14 +02:00
}
2015-06-07 18:17:25 +02:00
2016-06-08 17:31:31 +02:00
void Material::enableBackfaceCulling(bool enable)
{
2018-07-15 17:35:41 +02:00
if (enable) {
m_render_states |= (u64)ffr::StateFlags::CULL_BACK;
}
else {
m_render_states &= ~(u64)ffr::StateFlags::CULL_BACK;
2016-06-08 17:31:31 +02:00
}
}
bool Material::isBackfaceCulling() const
{
2018-07-15 17:35:41 +02:00
return (m_render_states & (u64)ffr::StateFlags::CULL_BACK) != 0;
2018-06-30 15:18:27 +02:00
}
namespace LuaAPI
{
2018-10-13 15:08:58 +02:00
int layer(lua_State* L)
{
const char* layer_name = LuaWrapper::checkArg<const char*>(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
const int layer = material->getRenderer().getLayerIdx(layer_name);
material->setLayer(layer);
return 0;
}
2018-08-05 18:00:30 +02:00
int roughness(lua_State* L)
{
const float r = LuaWrapper::checkArg<float>(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
material->setRoughness(r);
return 0;
}
2018-08-22 19:52:08 +02:00
int alpha_ref(lua_State* L)
{
const float r = LuaWrapper::checkArg<float>(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
material->setAlphaRef(r);
return 0;
}
2018-09-09 15:31:09 +02:00
int backface_culling(lua_State* L)
{
const bool enable = LuaWrapper::checkArg<bool>(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
material->enableBackfaceCulling(enable);
return 0;
}
2018-08-22 19:52:08 +02:00
int color(lua_State* L)
{
const Vec4 c = LuaWrapper::checkArg<Vec4>(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
material->setColor(c);
return 0;
}
int custom_flag(lua_State* L)
{
const float m = LuaWrapper::checkArg<float>(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
const char* flag_name = LuaWrapper::checkArg<const char*>(L, 1);
const u32 flag = material->getCustomFlag(flag_name);
material->setCustomFlag(flag);
return 0;
}
2018-08-05 18:00:30 +02:00
int metallic(lua_State* L)
{
const float m = LuaWrapper::checkArg<float>(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
material->setMetallic(m);
return 0;
}
2018-08-22 19:52:08 +02:00
int emission(lua_State* L)
{
const float m = LuaWrapper::checkArg<float>(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
material->setEmission(m);
return 0;
}
2018-07-01 18:13:44 +02:00
int defines(lua_State* L)
{
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
2018-11-11 13:46:42 +01:00
LuaWrapper::forEachArrayItem<const char*>(L, 1, "array of strings expected", [&](const char* v){
material->setDefine(material->getRenderer().getShaderDefineIdx(v), true);
});
2018-07-01 18:13:44 +02:00
return 0;
}
2018-06-30 15:18:27 +02:00
int shader(lua_State* L)
{
const char* path = LuaWrapper::checkArg<const char*>(L, 1);
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
material->setShader(Path(path));
return 0;
}
int texture(lua_State* L)
{
lua_getfield(L, LUA_GLOBALSINDEX, "this");
Material* material = (Material*)lua_touserdata(L, -1);
lua_pop(L, 1);
2018-07-11 23:35:34 +02:00
char material_dir[MAX_PATH_LENGTH];
2019-07-25 18:50:31 +02:00
PathUtils::getDir(Span(material_dir), material->getPath().c_str());
2018-07-05 15:54:14 +02:00
if (lua_istable(L, 1)) {
lua_getfield(L, 1, "source");
if (lua_isstring(L, -1)) {
const char* path = lua_tostring(L, -1);
const int idx = material->getTextureCount();
2018-07-11 23:35:34 +02:00
char texture_path[MAX_PATH_LENGTH];
if (path[0] != '/' && path[0] != '\\' && path[0] != '\0')
{
copyString(texture_path, material_dir);
catString(texture_path, path);
}
else
{
copyString(texture_path, path);
}
material->setTexturePath(idx, Path(texture_path));
2018-07-05 15:54:14 +02:00
}
else {
2019-06-21 17:14:06 +02:00
logError("Renderer") << material->getPath() << " texture's source is not a string.";
2018-07-05 15:54:14 +02:00
lua_pop(L, 1);
return 0;
}
lua_pop(L, 1);
2018-07-22 15:22:36 +02:00
Texture* texture = material->getTexture(material->getTextureCount() - 1);
bool keep_data = false;
LuaWrapper::getOptionalField(L, 1, "keep_data", &keep_data);
if (keep_data) texture->addDataReference();
2018-07-05 15:54:14 +02:00
return 0;
}
const char* path = LuaWrapper::checkArg<const char*>(L, 1);
2018-06-30 15:18:27 +02:00
const int idx = material->getTextureCount();
2018-07-11 23:35:34 +02:00
char texture_path[MAX_PATH_LENGTH];
if (path[0] != '/' && path[0] != '\\' && path[0] != '\0')
{
copyString(texture_path, material_dir);
catString(texture_path, path);
}
else
{
copyString(texture_path, path);
}
material->setTexturePath(idx, Path(texture_path));
2018-06-30 15:18:27 +02:00
return 0;
2016-06-08 17:31:31 +02:00
}
2018-06-30 15:18:27 +02:00
} // namespace LuaAPI
2019-06-11 01:09:14 +02:00
bool Material::load(u64 size, const u8* mem)
2014-06-16 21:18:15 +02:00
{
PROFILE_FUNCTION();
2015-08-15 09:56:37 +02:00
2018-08-05 18:00:30 +02:00
// TODO reuse state
2018-06-30 15:18:27 +02:00
lua_State* L = luaL_newstate();
lua_pushlightuserdata(L, this);
lua_setfield(L, LUA_GLOBALSINDEX, "this");
2019-08-12 18:10:01 +02:00
m_uniforms.clear();
m_render_states = u64(ffr::StateFlags::CULL_BACK);
2018-08-22 19:52:08 +02:00
#define DEFINE_LUA_FUNC(func) \
lua_pushcclosure(L, LuaAPI::func, 0); \
lua_setfield(L, LUA_GLOBALSINDEX, #func);
2018-10-13 15:08:58 +02:00
DEFINE_LUA_FUNC(alpha_ref);
DEFINE_LUA_FUNC(backface_culling);
DEFINE_LUA_FUNC(color);
DEFINE_LUA_FUNC(custom_flag);
2018-08-22 19:52:08 +02:00
DEFINE_LUA_FUNC(defines);
DEFINE_LUA_FUNC(emission);
2018-10-13 15:08:58 +02:00
DEFINE_LUA_FUNC(layer);
DEFINE_LUA_FUNC(metallic);
DEFINE_LUA_FUNC(roughness);
DEFINE_LUA_FUNC(shader);
DEFINE_LUA_FUNC(texture);
2019-08-12 18:10:01 +02:00
lua_pushcfunction(L, &Material::uniform);
lua_setfield(L, LUA_GLOBALSINDEX, "uniform");
2018-08-22 19:52:08 +02:00
#undef DEFINE_LUA_FUNC
setAlphaRef(DEFAULT_ALPHA_REF_VALUE);
2018-06-30 15:18:27 +02:00
2018-08-22 19:52:08 +02:00
m_custom_flags = 0;
2019-07-25 19:14:12 +02:00
const Span<const char> content((const char*)mem, (int)size);
2018-06-30 15:18:27 +02:00
if (!LuaWrapper::execute(L, content, getPath().c_str(), 0)) {
lua_close(L);
return false;
}
lua_close(L);
2019-08-12 18:10:01 +02:00
if (!m_shader) {
2019-06-21 17:14:06 +02:00
logError("Renderer") << "Material " << getPath() << " without a shader";
2015-10-03 01:14:38 +02:00
return false;
2014-06-16 21:18:15 +02:00
}
2015-08-15 09:56:37 +02:00
2019-06-11 01:09:14 +02:00
m_size = size;
2015-10-03 01:14:38 +02:00
return true;
2014-06-16 21:18:15 +02:00
}
2018-02-12 17:02:10 +01:00
} // namespace Lumix