LumixEngine/src/renderer/pipeline.cpp

2583 lines
79 KiB
C++
Raw Normal View History

2018-06-30 15:18:27 +02:00
#include "draw2d.h"
#include "ffr/ffr.h"
2018-07-17 01:13:58 +02:00
#include "engine/blob.h"
2016-05-10 08:24:31 +02:00
#include "engine/crc32.h"
2018-06-30 15:18:27 +02:00
#include "engine/engine.h"
2016-05-10 08:24:31 +02:00
#include "engine/fs/disk_file_device.h"
#include "engine/fs/file_system.h"
2018-06-30 15:18:27 +02:00
#include "engine/fs/ifile_device.h"
2016-05-10 08:24:31 +02:00
#include "engine/geometry.h"
2018-07-22 15:22:36 +02:00
#include "engine/job_system.h"
2016-05-10 08:24:31 +02:00
#include "engine/log.h"
#include "engine/lua_wrapper.h"
2018-07-29 12:40:58 +02:00
#include "engine/math_utils.h"
2018-07-15 17:35:41 +02:00
#include "engine/mt/atomic.h"
2018-09-29 15:14:46 +02:00
#include "engine/mt/sync.h"
2018-06-30 15:18:27 +02:00
#include "engine/path.h"
2016-05-10 08:24:31 +02:00
#include "engine/profiler.h"
#include "engine/resource_manager.h"
2018-08-23 22:35:38 +02:00
#include "engine/simd.h"
2018-07-29 18:36:32 +02:00
#include "engine/timer.h"
#include "engine/universe/universe.h"
2018-07-08 00:44:45 +02:00
#include "engine/viewport.h"
2018-06-30 15:18:27 +02:00
#include "font_manager.h"
#include "material.h"
#include "model.h"
2018-08-23 22:35:38 +02:00
#include "particle_system.h"
2018-06-30 15:18:27 +02:00
#include "pipeline.h"
2018-07-17 01:13:58 +02:00
#include "pose.h"
2018-06-30 15:18:27 +02:00
#include "renderer.h"
#include "render_scene.h"
#include "shader.h"
#include "shader_manager.h"
2018-07-22 15:22:36 +02:00
#include "terrain.h"
2018-06-30 15:18:27 +02:00
#include "texture.h"
2018-07-05 15:54:14 +02:00
#include "texture_manager.h"
2018-07-22 15:22:36 +02:00
#include <algorithm>
2018-07-06 21:31:44 +02:00
#include <cmath>
2018-09-29 15:14:46 +02:00
#include <Windows.h> // TODO
2018-07-22 15:22:36 +02:00
2015-07-04 15:22:28 +02:00
namespace Lumix
{
2016-02-14 16:49:37 +01:00
2018-09-29 15:14:46 +02:00
template <typename T>
struct MTBucketArray
{
enum { BUCKET_SIZE = 4096 };
struct Bucket {
T values[BUCKET_SIZE];
int count = 0;
};
struct Accessor
{
MTBucketArray<T>* array;
Bucket* bucket;
LUMIX_FORCE_INLINE void push(const T& value)
{
int c = bucket->count;
if(c == BUCKET_SIZE) {
Bucket* new_bucket = LUMIX_NEW(array->allocator, Bucket);
MT::SpinLock lock(array->mutex);
array->buckets.push(new_bucket);
bucket = new_bucket;
c = 0;
}
bucket->values[c] = value;
++bucket->count;
}
};
MTBucketArray(IAllocator& allocator)
: buckets(allocator)
, allocator(allocator)
, mutex(false)
{
}
~MTBucketArray()
{
for(Bucket* b : buckets) {
LUMIX_DELETE(allocator, b);
}
}
Accessor begin()
{
Bucket* bucket = LUMIX_NEW(allocator, Bucket);
MT::SpinLock lock(mutex);
buckets.push(bucket);
return {this, bucket};
}
void merge(Array<T>& out) const
{
int c = 0;
for(Bucket* b : buckets) {
c += b->count;
}
out.resize(c);
int offset = 0;
for(Bucket* b : buckets) {
memcpy(out.begin() + offset, b->values, sizeof(T) * b->count);
offset += b->count;
}
}
IAllocator& allocator;
MT::SpinMutex mutex;
Array<Bucket*> buckets;
};
2018-07-06 21:31:44 +02:00
static const float SHADOW_CAM_NEAR = 50.0f;
static const float SHADOW_CAM_FAR = 5000.0f;
2018-09-09 17:58:25 +02:00
ResourceType PipelineResource::TYPE("pipeline");
void PipelineResource::unload()
{
content.clear();
}
bool PipelineResource::load(FS::IFile& file)
{
content.resize((int)file.size());
file.read(content.begin(), content.size());
return true;
}
PipelineResourceManager::PipelineResourceManager(IAllocator& allocator)
: ResourceManager(allocator)
, m_allocator(allocator)
{}
Resource* PipelineResourceManager::createResource(const Path& path)
{
return LUMIX_NEW(m_allocator, PipelineResource)(path, *this, m_allocator);
}
void PipelineResourceManager::destroyResource(Resource& resource)
{
return LUMIX_DELETE(m_allocator, static_cast<PipelineResource*>(&resource));
}
PipelineResource::PipelineResource(const Path& path, ResourceManager& owner, IAllocator& allocator)
: Resource(path, owner, allocator)
, content(allocator)
{}
2018-08-19 17:35:37 +02:00
struct PipelineImpl final : Pipeline
2015-07-23 23:17:51 +02:00
{
2018-09-09 17:58:25 +02:00
PipelineImpl(Renderer& renderer, PipelineResource* resource, const char* define, IAllocator& allocator)
2018-07-01 23:37:56 +02:00
: m_allocator(allocator)
, m_renderer(renderer)
2018-09-09 17:58:25 +02:00
, m_resource(resource)
2015-07-23 23:17:51 +02:00
, m_lua_state(nullptr)
2018-06-30 15:18:27 +02:00
, m_custom_commands_handlers(allocator)
2018-07-06 21:31:44 +02:00
, m_define(define)
2016-06-16 19:24:15 +02:00
, m_scene(nullptr)
2018-02-03 19:41:47 +01:00
, m_is_first_render(true)
2018-06-30 15:18:27 +02:00
, m_draw2d(allocator)
2018-07-11 23:35:34 +02:00
, m_output(-1)
2018-06-30 15:18:27 +02:00
, m_renderbuffers(allocator)
2018-07-05 15:54:14 +02:00
, m_shaders(allocator)
2014-05-20 23:35:09 +02:00
{
2018-07-29 18:36:32 +02:00
m_timer = Timer::create(m_allocator);
2018-07-08 18:16:16 +02:00
m_viewport.w = m_viewport.h = 800;
ResourceManagerHub& rm = renderer.getEngine().getResourceManager();
m_draw2d_shader = rm.load<Shader>(Path("pipelines/draw2d.shd"));
m_debug_shape_shader = rm.load<Shader>(Path("pipelines/debug_shape.shd"));
2018-07-05 15:54:14 +02:00
TextureManager& texture_manager = renderer.getTextureManager();
m_default_cubemap = rm.load<Texture>(Path("models/common/default_probe.dds"));
2018-07-01 23:37:56 +02:00
2018-06-30 15:18:27 +02:00
FontAtlas& font_atlas = m_renderer.getFontManager().getFontAtlas();
m_draw2d.FontTexUvWhitePixel = font_atlas.TexUvWhitePixel;
m_draw2d.Clear();
m_draw2d.PushClipRectFullScreen();
m_draw2d.PushTextureID(font_atlas.TexID);
2018-07-15 17:35:41 +02:00
2018-10-06 12:15:05 +02:00
m_position_uniform = ffr::allocUniform("u_position", ffr::UniformType::VEC3, 1);
m_lod_uniform = ffr::allocUniform("u_lod", ffr::UniformType::INT, 1);
2018-09-05 20:45:06 +02:00
m_position_radius_uniform = ffr::allocUniform("u_pos_radius", ffr::UniformType::VEC4, 1);
2018-07-22 15:22:36 +02:00
m_terrain_params_uniform = ffr::allocUniform("u_terrain_params", ffr::UniformType::VEC4, 1);
2018-10-06 12:15:05 +02:00
m_rel_camera_pos_uniform = ffr::allocUniform("u_rel_camera_pos", ffr::UniformType::VEC3, 1);
2018-07-22 15:22:36 +02:00
m_terrain_scale_uniform = ffr::allocUniform("u_terrain_scale", ffr::UniformType::VEC4, 1);
m_terrain_matrix_uniform = ffr::allocUniform("u_terrain_matrix", ffr::UniformType::MAT4, 1);
2018-07-15 17:35:41 +02:00
m_model_uniform = ffr::allocUniform("u_model", ffr::UniformType::MAT4, 1);
2018-07-17 01:13:58 +02:00
m_bones_uniform = ffr::allocUniform("u_bones", ffr::UniformType::MAT4, 196);
2018-07-15 17:35:41 +02:00
m_canvas_size_uniform = ffr::allocUniform("u_canvas_size", ffr::UniformType::VEC2, 1);
m_texture_uniform = ffr::allocUniform("u_texture", ffr::UniformType::INT, 1);
m_irradiance_map_uniform = ffr::allocUniform("u_irradiancemap", ffr::UniformType::INT, 1);
m_radiance_map_uniform = ffr::allocUniform("u_radiancemap", ffr::UniformType::INT, 1);
m_material_params_uniform = ffr::allocUniform("u_material_params", ffr::UniformType::VEC4, 1);
2018-07-29 12:40:58 +02:00
m_material_color_uniform = ffr::allocUniform("u_material_color", ffr::UniformType::VEC4, 1);
2018-09-05 20:45:06 +02:00
float cube_verts[] = {
-1, -1, -1,
1, -1, -1,
1, -1, 1,
-1, -1, 1,
-1, 1, -1,
1, 1, -1,
1, 1, 1,
-1, 1, 1
};
const Renderer::MemRef vb_mem = m_renderer.copy(cube_verts, sizeof(cube_verts));
m_cube_vb = m_renderer.createBuffer(vb_mem);
u16 cube_indices[] = {
0, 1, 2,
0, 2, 3,
4, 6, 5,
4, 7, 6,
0, 4, 5,
0, 5, 1,
2, 6, 7,
2, 7, 3,
0, 3, 7,
0, 7, 4,
2018-09-15 09:55:46 +02:00
1, 6, 2,
1, 5, 6
2018-09-05 20:45:06 +02:00
};
const Renderer::MemRef ib_mem = m_renderer.copy(cube_indices, sizeof(cube_indices));
m_cube_ib = m_renderer.createBuffer(ib_mem);
2018-09-09 17:58:25 +02:00
m_resource->onLoaded<PipelineImpl, &PipelineImpl::onStateChanged>(this);
2016-01-28 15:19:56 +01:00
}
2018-06-30 15:18:27 +02:00
~PipelineImpl()
2016-01-28 15:19:56 +01:00
{
2018-06-30 15:18:27 +02:00
m_draw2d_shader->getResourceManager().unload(*m_draw2d_shader);
2018-07-01 23:37:56 +02:00
m_debug_shape_shader->getResourceManager().unload(*m_debug_shape_shader);
2018-07-05 15:54:14 +02:00
m_default_cubemap->getResourceManager().unload(*m_default_cubemap);
for(ShaderRef& shader : m_shaders) {
shader.res->getResourceManager().unload(*shader.res);
}
2018-07-29 18:36:32 +02:00
Timer::destroy(m_timer);
2015-07-23 23:17:51 +02:00
}
2014-05-20 23:35:09 +02:00
2018-06-30 15:18:27 +02:00
void callInitScene()
2015-07-23 23:17:51 +02:00
{
2018-06-30 15:18:27 +02:00
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
lua_getfield(m_lua_state, -1, "initScene");
if (lua_type(m_lua_state, -1) == LUA_TFUNCTION)
2014-05-20 23:35:09 +02:00
{
2018-06-30 15:18:27 +02:00
lua_pushlightuserdata(m_lua_state, this);
if (lua_pcall(m_lua_state, 1, 0, 0) != 0)
2014-05-20 23:35:09 +02:00
{
2018-06-30 15:18:27 +02:00
g_log_error.log("lua") << lua_tostring(m_lua_state, -1);
lua_pop(m_lua_state, 1);
2014-05-20 23:35:09 +02:00
}
2018-06-30 15:18:27 +02:00
}
else
{
lua_pop(m_lua_state, 1);
2014-05-20 23:35:09 +02:00
}
2015-07-23 23:17:51 +02:00
}
2014-05-20 23:35:09 +02:00
2015-05-16 13:17:20 +02:00
2018-06-30 15:18:27 +02:00
2016-02-22 22:56:46 +01:00
void cleanup()
2015-07-23 23:17:51 +02:00
{
if (m_lua_state)
{
luaL_unref(m_renderer.getEngine().getState(), LUA_REGISTRYINDEX, m_lua_thread_ref);
2016-02-24 01:10:34 +01:00
luaL_unref(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
2015-07-23 23:17:51 +02:00
m_lua_state = nullptr;
}
2016-02-22 22:56:46 +01:00
}
2017-10-03 22:08:41 +02:00
void setDefine()
{
2018-07-06 21:31:44 +02:00
if (m_define == "") return;
StaticString<256> tmp(m_define, " = true");
2017-10-03 22:08:41 +02:00
2018-09-09 17:58:25 +02:00
bool errors = luaL_loadbuffer(m_lua_state, tmp, stringLength(tmp.data), m_resource->getPath().c_str()) != 0;
2017-10-03 22:08:41 +02:00
if (errors)
{
2018-09-09 17:58:25 +02:00
g_log_error.log("Renderer") << m_resource->getPath().c_str() << ": " << lua_tostring(m_lua_state, -1);
2017-10-03 22:08:41 +02:00
lua_pop(m_lua_state, 1);
return;
}
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
2018-06-30 15:18:27 +02:00
lua_setfenv(m_lua_state, -2);
errors = lua_pcall(m_lua_state, 0, 0, 0) != 0;
2017-10-03 22:08:41 +02:00
if (errors)
{
2018-09-09 17:58:25 +02:00
g_log_error.log("Renderer") << m_resource->getPath().c_str() << ": " << lua_tostring(m_lua_state, -1);
2017-10-03 22:08:41 +02:00
lua_pop(m_lua_state, 1);
}
}
2018-06-30 15:18:27 +02:00
void executeCustomCommand(const char* name)
{
u32 name_hash = crc32(name);
for(CustomCommandHandler& handler : m_custom_commands_handlers)
{
if(handler.hash == name_hash)
{
handler.callback.invoke();
break;
}
}
}
void exposeCustomCommandToLua(const CustomCommandHandler& handler)
{
if (!m_lua_state) return;
char tmp[1024];
copyString(tmp, "function ");
catString(tmp, handler.name);
2018-07-05 15:54:14 +02:00
catString(tmp, "() executeCustomCommand(\"");
2018-06-30 15:18:27 +02:00
catString(tmp, handler.name);
catString(tmp, "\") end");
2018-07-05 15:54:14 +02:00
bool errors = luaL_loadbuffer(m_lua_state, tmp, stringLength(tmp), "exposeCustomCommandToLua") != 0;
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
lua_setfenv(m_lua_state, -2);
2018-06-30 15:18:27 +02:00
errors = errors || lua_pcall(m_lua_state, 0, 0, 0) != 0;
if (errors)
{
g_log_error.log("Renderer") << lua_tostring(m_lua_state, -1);
lua_pop(m_lua_state, 1);
}
}
2018-09-09 17:58:25 +02:00
void onStateChanged(Resource::State, Resource::State new_state, Resource&)
2016-02-22 22:56:46 +01:00
{
2018-09-09 17:58:25 +02:00
if (new_state != Resource::State::READY) return;
2016-02-22 22:56:46 +01:00
cleanup();
2016-02-24 01:10:34 +01:00
m_lua_state = lua_newthread(m_renderer.getEngine().getState());
m_lua_thread_ref = luaL_ref(m_renderer.getEngine().getState(), LUA_REGISTRYINDEX);
2016-02-24 01:10:34 +01:00
lua_newtable(m_lua_state);
lua_pushvalue(m_lua_state, -1);
m_lua_env = luaL_ref(m_lua_state, LUA_REGISTRYINDEX);
lua_pushvalue(m_lua_state, -1);
lua_setmetatable(m_lua_state, -2);
2018-06-30 15:18:27 +02:00
lua_pushvalue(m_lua_state, LUA_GLOBALSINDEX);
2016-02-24 01:10:34 +01:00
lua_setfield(m_lua_state, -2, "__index");
2016-03-31 16:58:42 +02:00
if (m_renderer.getEngine().getDiskFileDevice())
{
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
lua_pushstring(m_lua_state, m_renderer.getEngine().getDiskFileDevice()->getBasePath());
lua_setfield(m_lua_state, -2, "LUA_PATH");
}
2016-02-24 01:10:34 +01:00
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
2016-02-15 15:50:24 +01:00
lua_pushlightuserdata(m_lua_state, this);
2016-02-24 01:10:34 +01:00
lua_setfield(m_lua_state, -2, "this");
2018-07-05 15:54:14 +02:00
registerLuaAPI(m_lua_state);
2018-06-30 15:18:27 +02:00
for (auto& handler : m_custom_commands_handlers)
2015-07-23 23:17:51 +02:00
{
2018-06-30 15:18:27 +02:00
exposeCustomCommandToLua(handler);
2015-07-23 23:17:51 +02:00
}
2015-02-14 00:13:34 +01:00
2018-06-30 15:18:27 +02:00
setDefine();
2014-05-20 23:35:09 +02:00
2018-09-09 17:58:25 +02:00
const char* content = m_resource->content.begin();
const int content_size = m_resource->content.size();
2018-06-30 15:18:27 +02:00
bool errors =
2018-09-09 17:58:25 +02:00
luaL_loadbuffer(m_lua_state, content, content_size, m_resource->getPath().c_str()) != 0;
2018-06-30 15:18:27 +02:00
if (errors)
2015-05-16 13:17:20 +02:00
{
2018-09-09 17:58:25 +02:00
g_log_error.log("Renderer") << m_resource->getPath().c_str() << ": " << lua_tostring(m_lua_state, -1);
2018-06-30 15:18:27 +02:00
lua_pop(m_lua_state, 1);
return;
2018-03-13 14:56:33 +01:00
}
2018-06-30 15:18:27 +02:00
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
lua_setfenv(m_lua_state, -2);
errors = lua_pcall(m_lua_state, 0, 0, 0) != 0;
if (errors)
2018-03-13 14:56:33 +01:00
{
2018-09-09 17:58:25 +02:00
g_log_error.log("Renderer") << m_resource->getPath().c_str() << ": " << lua_tostring(m_lua_state, -1);
2018-06-30 15:18:27 +02:00
lua_pop(m_lua_state, 1);
return;
}
2018-07-08 18:16:16 +02:00
m_viewport.w = m_viewport.h = 800;
2018-06-30 15:18:27 +02:00
if (m_scene) callInitScene();
2018-04-24 00:55:10 +02:00
}
2018-06-30 15:18:27 +02:00
void clearBuffers()
2015-07-23 23:17:51 +02:00
{
2018-06-30 15:18:27 +02:00
for (Renderbuffer& rb : m_renderbuffers) {
++rb.frame_counter;
}
2015-07-04 15:22:28 +02:00
2018-06-30 15:18:27 +02:00
for(int i = m_renderbuffers.size() - 1; i >= 0; --i) {
2018-08-19 22:01:20 +02:00
if (m_renderbuffers[i].frame_counter > 1) {
2018-07-14 10:03:38 +02:00
m_renderer.destroy(m_renderbuffers[i].handle);
2018-06-30 15:18:27 +02:00
m_renderbuffers.eraseFast(i);
}
2018-07-14 10:03:38 +02:00
}
2016-10-22 18:08:53 +02:00
}
2018-08-19 22:01:20 +02:00
virtual void setViewport(const Viewport& viewport) override
{
m_viewport = viewport;
}
2018-07-08 00:44:45 +02:00
2018-08-22 19:52:08 +02:00
void resetState()
{
struct Cmd : Renderer::RenderCommandBase {
void setup() override {}
void execute() override {
ffr::setStencil(0xff, ffr::StencilFuncs::DISABLE, 0, 0, ffr::StencilOps::KEEP, ffr::StencilOps::KEEP, ffr::StencilOps::KEEP);
}
};
Cmd* cmd = LUMIX_NEW(m_allocator, Cmd);
m_renderer.push(cmd);
}
2018-07-08 00:44:45 +02:00
2018-06-30 15:18:27 +02:00
bool render() override
{
2015-07-23 23:17:51 +02:00
PROFILE_FUNCTION();
2015-07-04 15:22:28 +02:00
2018-10-07 18:00:48 +02:00
if (!isReady() || !m_scene || m_viewport.w <= 0 || m_viewport.h <= 0) {
2018-07-06 21:31:44 +02:00
m_is_first_render = true;
return false;
}
2018-06-30 15:18:27 +02:00
if (m_is_first_render) {
2018-02-03 19:41:47 +01:00
// m_draw2d might accumulate too much data to render while pipeline was not ready
// so we clear it on the first frame
m_is_first_render = false;
m_draw2d.Clear();
}
2015-05-16 13:17:20 +02:00
2016-01-28 15:19:56 +01:00
m_stats = {};
2018-08-19 22:01:20 +02:00
clearBuffers();
2018-07-11 23:35:34 +02:00
for(Renderbuffer& rb : m_renderbuffers) {
if(!rb.use_realtive_size) continue;
const uint w = uint(rb.relative_size.x * m_viewport.w + 0.5f);
const uint h = uint(rb.relative_size.y * m_viewport.h + 0.5f);
if(rb.width != w || rb.height != h) {
rb.width = w;
rb.height = h;
m_renderer.destroy(rb.handle);
rb.handle = m_renderer.createTexture(w, h, rb.format, 0, {0, 0});
}
}
Renderer::GlobalState state;
2018-07-22 15:22:36 +02:00
const Matrix view = m_viewport.getViewRotation();
2018-07-11 23:35:34 +02:00
const Matrix projection = m_viewport.getProjection(ffr::isHomogenousDepth());
state.camera_projection = projection;
2018-07-29 18:36:32 +02:00
state.camera_inv_projection = projection;
state.camera_inv_projection.inverse();
2018-07-11 23:35:34 +02:00
state.camera_view = view;
2018-07-26 23:53:14 +02:00
state.camera_inv_view = view.fastInverted();
2018-07-11 23:35:34 +02:00
state.camera_view_projection = projection * view;
state.camera_inv_view_projection = state.camera_view_projection;
state.camera_inv_view_projection.inverse();
2018-07-29 18:36:32 +02:00
state.time = m_timer->getTimeSinceStart();
2018-09-05 20:45:06 +02:00
state.framebuffer_size.x = m_viewport.w;
state.framebuffer_size.y = m_viewport.h;
2018-07-11 23:35:34 +02:00
2018-08-19 17:35:37 +02:00
const EntityPtr global_light = m_scene->getActiveGlobalLight();
2018-07-11 23:35:34 +02:00
if(global_light.isValid()) {
2018-08-19 17:35:37 +02:00
EntityRef gl = (EntityRef)global_light;
state.light_direction = Vec4(m_scene->getUniverse().getRotation(gl).rotate(Vec3(0, 0, -1)), 456);
state.light_color = m_scene->getGlobalLightColor(gl);
state.light_intensity = m_scene->getGlobalLightIntensity(gl);
state.light_indirect_intensity = m_scene->getGlobalLightIndirectIntensity(gl);
2018-07-11 23:35:34 +02:00
}
2018-08-22 19:52:08 +02:00
resetState();
2018-07-11 23:35:34 +02:00
m_renderer.setGlobalState(state);
2018-07-14 10:03:38 +02:00
2018-09-03 16:04:47 +02:00
LuaWrapper::DebugGuard lua_debug_guard(m_lua_state);
2018-07-14 10:03:38 +02:00
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
lua_getfield(m_lua_state, -1, "main");
2018-08-29 12:36:19 +02:00
if (lua_type(m_lua_state, -1) != LUA_TFUNCTION) {
lua_pop(m_lua_state, 2);
return false;
2018-07-11 23:35:34 +02:00
}
2018-08-29 12:36:19 +02:00
LuaWrapper::pcall(m_lua_state, 0);
lua_pop(m_lua_state, 1);
2018-07-08 18:16:16 +02:00
return true;
2015-07-23 23:17:51 +02:00
}
2015-05-15 22:38:34 +02:00
2015-07-04 15:22:28 +02:00
2018-07-01 23:37:56 +02:00
void renderDebugLines()
{
2018-07-14 17:52:06 +02:00
struct Cmd : Renderer::RenderCommandBase
{
Cmd(IAllocator& allocator) : lines(allocator) {}
2018-07-01 23:37:56 +02:00
2018-07-14 17:52:06 +02:00
void setup() override
{
const Array<DebugLine>& src_lines = pipeline->m_scene->getDebugLines();
lines.resize(src_lines.size());
copyMemory(&lines[0], &src_lines[0], lines.size() * sizeof(lines[0]));
2018-08-09 22:35:00 +02:00
render_data = pipeline->m_debug_shape_shader->m_render_data;
2018-07-14 17:52:06 +02:00
}
2018-07-01 23:37:56 +02:00
2018-07-14 17:52:06 +02:00
void execute() override {
2018-08-09 22:35:00 +02:00
ffr::pushDebugGroup("debug lines");
const Shader::Program& shader = Shader::getProgram(render_data, 0);
2018-07-14 17:52:06 +02:00
struct BaseVertex {
Vec3 pos;
u32 color;
};
2018-07-01 23:37:56 +02:00
2018-07-14 17:52:06 +02:00
Array<BaseVertex> vertices(pipeline->m_allocator);
vertices.resize(lines.size() * 2);
for (int j = 0, n = lines.size(); j < n; ++j) {
const DebugLine& line = lines[j];
2018-07-01 23:37:56 +02:00
2018-07-14 17:52:06 +02:00
vertices[j * 2].color = line.color;
2018-09-16 18:35:57 +02:00
vertices[j * 2].pos = (line.from - viewport_pos).toFloat();
2018-07-01 23:37:56 +02:00
2018-07-14 17:52:06 +02:00
vertices[j * 2 + 1].color = line.color;
2018-09-16 18:35:57 +02:00
vertices[j * 2 + 1].pos = (line.to - viewport_pos).toFloat();
2018-07-14 17:52:06 +02:00
}
2018-07-01 23:37:56 +02:00
2018-07-14 17:52:06 +02:00
ffr::VertexDecl vertex_decl;
vertex_decl.addAttribute(3, ffr::AttributeType::FLOAT, false, false);
vertex_decl.addAttribute(4, ffr::AttributeType::U8, true, false);
2018-07-15 17:35:41 +02:00
ffr::setUniformMatrix4f(pipeline->m_model_uniform, &Matrix::IDENTITY.m11);
2018-07-01 23:37:56 +02:00
2018-07-15 17:35:41 +02:00
ffr::setState(0);
ffr::useProgram(shader.handle);
2018-07-22 22:57:29 +02:00
const Renderer::TransientSlice vb = pipeline->m_renderer.allocTransient(vertices.byte_size());
ffr::update(vb.buffer, vertices.begin(), vb.offset, vb.size);
ffr::setVertexBuffer(&vertex_decl, vb.buffer, vb.offset, nullptr);
2018-07-15 17:35:41 +02:00
ffr::setIndexBuffer(ffr::INVALID_BUFFER);
ffr::drawArrays(0, lines.size() * 2, ffr::PrimitiveType::LINES);
2018-08-09 22:35:00 +02:00
ffr::popDebugGroup();
2018-07-14 17:52:06 +02:00
}
Array<DebugLine> lines;
PipelineImpl* pipeline;
2018-09-16 18:35:57 +02:00
DVec3 viewport_pos;
ShaderRenderData* render_data;
2018-07-14 17:52:06 +02:00
};
const Array<DebugLine>& lines = m_scene->getDebugLines();
if (lines.empty() || !m_debug_shape_shader->isReady()) return;
IAllocator& allocator = m_renderer.getAllocator();
Cmd* cmd = LUMIX_NEW(allocator, Cmd)(allocator);
cmd->pipeline = this;
2018-08-09 22:35:00 +02:00
cmd->viewport_pos = m_viewport.pos;
2018-07-14 17:52:06 +02:00
m_renderer.push(cmd);
2018-07-01 23:37:56 +02:00
}
void renderDebugShapes()
{
renderDebugLines();
/*renderDebugTriangles();
renderDebugPoints();*/
}
2018-06-30 15:18:27 +02:00
void render2D()
{
2018-07-14 21:55:32 +02:00
auto resetDraw2D = [this](){
2018-06-30 15:18:27 +02:00
m_draw2d.Clear();
m_draw2d.PushClipRectFullScreen();
FontAtlas& atlas = m_renderer.getFontManager().getFontAtlas();
m_draw2d.FontTexUvWhitePixel = atlas.TexUvWhitePixel;
m_draw2d.PushTextureID(atlas.TexID);
};
2016-01-12 13:52:14 +01:00
2018-06-30 15:18:27 +02:00
if (!m_draw2d_shader->isReady()) {
resetDraw2D();
return;
}
2018-07-14 21:55:32 +02:00
if (m_draw2d.IdxBuffer.size() == 0) {
2018-06-30 15:18:27 +02:00
resetDraw2D();
return;
}
2018-07-14 21:55:32 +02:00
struct Cmd : Renderer::RenderCommandBase
{
Renderer::MemRef idx_buffer_mem;
Renderer::MemRef vtx_buffer_mem;
int num_indices;
int num_vertices;
Array<Draw2D::DrawCmd> cmd_buffer;
Cmd(IAllocator& allocator) : cmd_buffer(allocator) {}
void setup()
{
size.set((float)pipeline->m_viewport.w, (float)pipeline->m_viewport.h);
Draw2D& draw2d = pipeline->m_draw2d;
2018-07-14 21:55:32 +02:00
num_indices = draw2d.IdxBuffer.size();
num_vertices = draw2d.VtxBuffer.size();
2016-01-15 00:05:53 +01:00
2018-07-14 21:55:32 +02:00
idx_buffer_mem = pipeline->m_renderer.copy(&draw2d.IdxBuffer[0], num_indices * sizeof(ImDrawIdx));
vtx_buffer_mem = pipeline->m_renderer.copy(&draw2d.VtxBuffer[0], num_vertices * sizeof(ImDrawVert));
cmd_buffer.resize(draw2d.CmdBuffer.size());
copyMemory(&cmd_buffer[0], draw2d.CmdBuffer.begin(), sizeof(cmd_buffer[0]) * cmd_buffer.size());
2016-01-15 00:05:53 +01:00
2018-07-14 21:55:32 +02:00
draw2d.Clear();
draw2d.PushClipRectFullScreen();
FontAtlas& atlas = pipeline->m_renderer.getFontManager().getFontAtlas();
draw2d.FontTexUvWhitePixel = atlas.TexUvWhitePixel;
draw2d.PushTextureID(atlas.TexID);
2018-08-09 22:35:00 +02:00
shader = pipeline->m_draw2d_shader->m_render_data;
2018-07-14 21:55:32 +02:00
}
void execute()
{
ffr::VertexDecl vertex_decl;
vertex_decl.addAttribute(2, ffr::AttributeType::FLOAT, false, false);
vertex_decl.addAttribute(2, ffr::AttributeType::FLOAT, false, false);
vertex_decl.addAttribute(4, ffr::AttributeType::U8, true, false);
2018-07-15 17:35:41 +02:00
ffr::BufferHandle vb = ffr::allocBufferHandle();
ffr::BufferHandle ib = ffr::allocBufferHandle();
ffr::createBuffer(vb, vtx_buffer_mem.size, vtx_buffer_mem.data);
ffr::createBuffer(ib, idx_buffer_mem.size, idx_buffer_mem.data);
2018-07-14 21:55:32 +02:00
pipeline->m_renderer.free(idx_buffer_mem);
pipeline->m_renderer.free(vtx_buffer_mem);
ffr::pushDebugGroup("draw2d");
2018-08-09 22:35:00 +02:00
ffr::ProgramHandle prg = Shader::getProgram(shader, 0).handle;
2018-07-15 17:35:41 +02:00
ffr::setUniform2f(pipeline->m_canvas_size_uniform, &size.x);
ffr::setVertexBuffer(&vertex_decl, vb, 0, nullptr);
ffr::setIndexBuffer(ib);
2018-07-14 21:55:32 +02:00
u32 elem_offset = 0;
const Draw2D::DrawCmd* pcmd_begin = cmd_buffer.begin();
const Draw2D::DrawCmd* pcmd_end = cmd_buffer.end();
2018-07-15 17:35:41 +02:00
ffr::setState(0);
ffr::setUniform1i(pipeline->m_texture_uniform, 0);
ffr::useProgram(prg);
2018-07-14 21:55:32 +02:00
ASSERT(pcmd_begin <= pcmd_end - 1); // TODO compute correct offsets
for (const Draw2D::DrawCmd* pcmd = pcmd_begin; pcmd != pcmd_end; pcmd++) {
if (0 == pcmd->ElemCount) continue;
2018-06-30 15:18:27 +02:00
2018-07-14 21:55:32 +02:00
ffr::scissor(uint(Math::maximum(pcmd->ClipRect.x, 0.0f)),
uint(Math::maximum(pcmd->ClipRect.y, 0.0f)),
uint(Math::minimum(pcmd->ClipRect.z, 65535.0f) - Math::maximum(pcmd->ClipRect.x, 0.0f)),
uint(Math::minimum(pcmd->ClipRect.w, 65535.0f) - Math::maximum(pcmd->ClipRect.y, 0.0f)));
2018-06-30 15:18:27 +02:00
2018-07-14 21:55:32 +02:00
const Texture* atlas_texture = pipeline->m_renderer.getFontManager().getAtlasTexture();
ffr::TextureHandle texture_id = atlas_texture->handle;
if (pcmd->TextureId) texture_id = *(ffr::TextureHandle*)pcmd->TextureId;
if(!texture_id.isValid()) texture_id = atlas_texture->handle;
2018-06-30 15:18:27 +02:00
2018-07-15 17:35:41 +02:00
ffr::bindTexture(0, texture_id);
2018-07-14 21:55:32 +02:00
ffr::blending(1);
2018-07-15 17:35:41 +02:00
ffr::drawTriangles(num_indices);
2018-07-14 21:55:32 +02:00
elem_offset += pcmd->ElemCount;
}
ffr::popDebugGroup();
2018-07-15 17:35:41 +02:00
ffr::destroy(vb);
ffr::destroy(ib);
2018-07-14 21:55:32 +02:00
}
2018-07-14 21:55:32 +02:00
Vec2 size;
PipelineImpl* pipeline;
ShaderRenderData* shader;
2018-07-14 21:55:32 +02:00
};
IAllocator& allocator = m_renderer.getAllocator();
Cmd* cmd = LUMIX_NEW(allocator, Cmd)(allocator);
cmd->pipeline = this;
m_renderer.push(cmd);
2016-01-15 00:05:53 +01:00
}
2018-06-30 15:18:27 +02:00
void setScene(RenderScene* scene) override
2016-01-15 00:05:53 +01:00
{
2018-06-30 15:18:27 +02:00
m_scene = scene;
if (m_lua_state && m_scene) callInitScene();
2016-01-15 00:05:53 +01:00
}
2018-06-30 15:18:27 +02:00
RenderScene* getScene() const override { return m_scene; }
2016-01-15 00:05:53 +01:00
2018-06-30 15:18:27 +02:00
CustomCommandHandler& addCustomCommandHandler(const char* name) override
2016-01-15 00:05:53 +01:00
{
2018-06-30 15:18:27 +02:00
auto& handler = m_custom_commands_handlers.emplace();
copyString(handler.name, name);
handler.hash = crc32(name);
exposeCustomCommandToLua(handler);
return handler;
2016-01-15 00:05:53 +01:00
}
2018-06-30 15:18:27 +02:00
static ffr::TextureFormat getFormat(const char* name)
{
static const struct
{
const char* name;
ffr::TextureFormat value;
} FORMATS[] = {
{"depth32", ffr::TextureFormat::D32},
{"depth24", ffr::TextureFormat::D24},
{"depth24stencil8", ffr::TextureFormat::D24S8},
{"rgba8", ffr::TextureFormat::RGBA8},
2018-07-05 15:54:14 +02:00
{"srgba", ffr::TextureFormat::SRGBA},
{"srgb", ffr::TextureFormat::SRGB},
{"rgba16", ffr::TextureFormat::RGBA16},
2018-06-30 15:18:27 +02:00
{"rgba16f", ffr::TextureFormat::RGBA16F},
{"r16f", ffr::TextureFormat::R16F},
{"r16", ffr::TextureFormat::R16},
{"r32f", ffr::TextureFormat::R32F},
};
2016-01-15 00:05:53 +01:00
2018-06-30 15:18:27 +02:00
for (auto& i : FORMATS)
{
if (equalStrings(i.name, name)) return i.value;
}
g_log_error.log("Renderer") << "Uknown texture format " << name;
return ffr::TextureFormat::RGBA8;
2016-02-14 16:49:37 +01:00
}
2018-06-30 15:18:27 +02:00
int createRenderbuffer(float w, float h, bool relative, const char* format_str)
2016-02-15 15:50:24 +01:00
{
2018-07-11 23:35:34 +02:00
const uint rb_w = uint(relative ? w * m_viewport.w + 0.5f : w);
const uint rb_h = uint(relative ? h * m_viewport.h + 0.5f : h);
2018-06-30 15:18:27 +02:00
const ffr::TextureFormat format = getFormat(format_str);
2016-02-15 15:50:24 +01:00
2018-06-30 15:18:27 +02:00
for (int i = 0, n = m_renderbuffers.size(); i < n; ++i)
{
Renderbuffer& rb = m_renderbuffers[i];
if (rb.frame_counter == 0) continue;
if (rb.width != rb_w) continue;
if (rb.height != rb_h) continue;
if (rb.format != format) continue;
2016-02-15 15:50:24 +01:00
2018-06-30 15:18:27 +02:00
rb.frame_counter = 0;
return i;
}
2018-06-30 15:18:27 +02:00
Renderbuffer& rb = m_renderbuffers.emplace();
2018-07-11 23:35:34 +02:00
rb.use_realtive_size = relative;
rb.relative_size.set(w, h);
2018-06-30 15:18:27 +02:00
rb.frame_counter = 0;
rb.width = rb_w;
rb.height = rb_h;
rb.format = format;
2018-07-08 18:16:16 +02:00
rb.handle = m_renderer.createTexture(rb_w, rb_h, format, 0, {0, 0});
2015-07-23 23:17:51 +02:00
2018-06-30 15:18:27 +02:00
return m_renderbuffers.size() - 1;
}
2017-10-04 13:23:34 +02:00
2015-10-24 15:27:48 +02:00
2018-07-22 15:22:36 +02:00
static int renderTerrains(lua_State* L)
{
PROFILE_FUNCTION();
const int pipeline_idx = lua_upvalueindex(1);
if (lua_type(L, pipeline_idx) != LUA_TLIGHTUSERDATA) {
LuaWrapper::argError<PipelineImpl*>(L, pipeline_idx);
}
PipelineImpl* pipeline = LuaWrapper::toType<PipelineImpl*>(L, pipeline_idx);
const char* define = LuaWrapper::checkArg<const char*>(L, 1);
2018-08-23 22:35:38 +02:00
const CameraParams cp = checkCameraParams(L, 2);
2018-07-22 15:22:36 +02:00
IAllocator& allocator = pipeline->m_renderer.getAllocator();
RenderTerrainsCommand* cmd = LUMIX_NEW(allocator, RenderTerrainsCommand)(allocator);
if (lua_gettop(L) > 3 && lua_istable(L, 3)) {
lua_pushnil(L);
while (lua_next(L, 3) != 0) {
if(lua_type(L, -1) != LUA_TNUMBER) {
g_log_error.log("Renderer") << "Incorrect global textures arguments of renderTerrains";
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
lua_pop(L, 2);
return 0;
}
if(lua_type(L, -2) != LUA_TSTRING) {
g_log_error.log("Renderer") << "Incorrect global textures arguments of renderTerrains";
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
lua_pop(L, 2);
return 0;
}
if (cmd->m_global_textures_count > lengthOf(cmd->m_global_textures)) {
g_log_error.log("Renderer") << "Too many textures in renderTerrains call";
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
lua_pop(L, 2);
return 0;
}
const char* uniform = lua_tostring(L, -2);
const int rb_idx = (int)lua_tointeger(L, -1);
auto& t = cmd->m_global_textures[cmd->m_global_textures_count];
t.texture = pipeline->m_renderbuffers[rb_idx].handle;
t.uniform = ffr::allocUniform(uniform, ffr::UniformType::INT, 1);
++cmd->m_global_textures_count;
lua_pop(L, 1);
}
}
cmd->m_pipeline = pipeline;
cmd->m_camera_params = cp;
cmd->m_shader_define = define;
pipeline->m_renderer.push(cmd);
return 0;
}
2018-08-23 22:35:38 +02:00
static int renderParticles(lua_State* L)
2018-07-14 10:03:38 +02:00
{
const int pipeline_idx = lua_upvalueindex(1);
if (lua_type(L, pipeline_idx) != LUA_TLIGHTUSERDATA) {
2018-07-22 15:22:36 +02:00
LuaWrapper::argError<PipelineImpl*>(L, pipeline_idx);
2018-07-14 10:03:38 +02:00
}
2018-08-29 12:36:19 +02:00
const CameraParams cp = checkCameraParams(L ,1);
2018-07-14 10:03:38 +02:00
PipelineImpl* pipeline = LuaWrapper::toType<PipelineImpl*>(L, pipeline_idx);
2018-08-29 12:36:19 +02:00
PROFILE_FUNCTION();
2018-08-23 22:35:38 +02:00
struct Cmd : Renderer::RenderCommandBase
{
Cmd(IAllocator& allocator)
: m_data(allocator)
{}
void setup() override
{
const auto& emitters = m_pipeline->m_scene->getParticleEmitters();
2018-07-14 10:03:38 +02:00
2018-08-23 22:35:38 +02:00
int byte_size = 0;
for (ParticleEmitter* emitter : emitters) {
byte_size += emitter->getInstanceDataSizeBytes();
}
m_data.reserve(sizeof(int) * emitters.size() + byte_size);
for (ParticleEmitter* emitter : emitters) {
if (!emitter->getResource() || !emitter->getResource()->isReady()) continue;
2018-08-29 12:36:19 +02:00
const int size = emitter->getInstanceDataSizeBytes();
if (size == 0) continue;
2018-08-26 18:19:50 +02:00
2018-08-23 22:35:38 +02:00
const Material* material = emitter->getResource()->getMaterial();
m_data.write(material->getShader()->m_render_data);
2018-08-29 12:36:19 +02:00
m_data.write(size);
m_data.write(emitter->getInstancesCount());
2018-08-26 18:19:50 +02:00
float* instance_data = (float*)m_data.skip(size);
emitter->fillInstanceData(m_camera_params.pos, instance_data);
2018-08-23 22:35:38 +02:00
}
}
void execute() override
{
2018-08-26 18:19:50 +02:00
ffr::pushDebugGroup("particles");
2018-08-23 22:35:38 +02:00
InputBlob blob(m_data);
ffr::VertexDecl instance_decl;
2018-08-26 18:19:50 +02:00
instance_decl.addAttribute(3, ffr::AttributeType::FLOAT, false, false);
2018-08-23 22:35:38 +02:00
while(blob.getPosition() < blob.getSize()) {
ShaderRenderData* shader_data = blob.read<ShaderRenderData*>();
2018-08-23 22:35:38 +02:00
const int byte_size = blob.read<int>();
const int instances_count = blob.read<int>();
const Renderer::TransientSlice transient = m_pipeline->m_renderer.allocTransient(byte_size);
2018-08-26 18:19:50 +02:00
if ((int)transient.size < byte_size) {
g_log_warning.log("Renderer") << "Not enough memory reserved to render all particles.";
break;
}
2018-08-23 22:35:38 +02:00
const void* mem = blob.skip(byte_size);
ffr::update(transient.buffer, mem, transient.offset, byte_size);
const Shader::Program& prog = Shader::getProgram(shader_data, 0);
2018-08-26 18:19:50 +02:00
ffr::blending(0);
2018-08-23 22:35:38 +02:00
ffr::useProgram(prog.handle);
ffr::setInstanceBuffer(instance_decl, transient.buffer, transient.offset, 0);
2018-08-26 18:19:50 +02:00
ffr::drawTriangleStripArraysInstanced(0, 4, instances_count);
2018-08-23 22:35:38 +02:00
}
2018-08-26 18:19:50 +02:00
ffr::popDebugGroup();
2018-08-23 22:35:38 +02:00
}
OutputBlob m_data;
PipelineImpl* m_pipeline;
2018-08-26 18:19:50 +02:00
CameraParams m_camera_params;
2018-08-23 22:35:38 +02:00
};
Cmd* cmd = LUMIX_NEW(pipeline->m_allocator, Cmd)(pipeline->m_allocator);
cmd->m_pipeline = pipeline;
2018-08-26 18:19:50 +02:00
cmd->m_camera_params = cp;
2018-08-23 22:35:38 +02:00
pipeline->m_renderer.push(cmd);
return 0;
}
struct CameraParams
{
2018-09-16 18:35:57 +02:00
ShiftedFrustum frustum;
DVec3 pos;
2018-08-23 22:35:38 +02:00
float lod_multiplier;
};
static CameraParams checkCameraParams(lua_State* L, int idx)
{
2018-07-14 10:03:38 +02:00
CameraParams cp;
2018-08-23 22:35:38 +02:00
lua_getfield(L, idx, "frustum");
2018-07-14 10:03:38 +02:00
if (!lua_istable(L, -1)) {
lua_pop(L, 1);
luaL_error(L, "Frustum is not a table");
}
float* points = cp.frustum.xs;
2018-09-16 18:35:57 +02:00
if(!LuaWrapper::checkField(L, -1, "origin", &cp.frustum.origin)) {
lua_pop(L, 1);
luaL_error(L, "Frustum without origin");
}
2018-07-14 10:03:38 +02:00
for (int i = 0; i < 32 + 24; ++i) {
lua_rawgeti(L, -1, i + 1);
if(!LuaWrapper::isType<float>(L, -1)) {
lua_pop(L, 2);
luaL_error(L, "Frustum must contain exactly 24 floats");
}
points[i] = LuaWrapper::toType<float>(L, -1);
lua_pop(L, 1);
}
cp.frustum.setPlanesFromPoints();
2018-08-23 22:35:38 +02:00
if(!LuaWrapper::checkField(L, idx, "lod_multiplier", &cp.lod_multiplier)) {
2018-07-14 10:03:38 +02:00
luaL_error(L, "Missing lod_multiplier in camera params");
}
2018-08-23 22:35:38 +02:00
if(!LuaWrapper::checkField(L, idx, "position", &cp.pos)) {
2018-07-14 10:03:38 +02:00
luaL_error(L, "Missing position in camera params");
}
2018-08-23 22:35:38 +02:00
return cp;
}
2018-09-05 20:45:06 +02:00
static int bindTextures(lua_State* L)
{
struct Cmd : Renderer::RenderCommandBase {
void setup() override {}
void execute() override
{
for(int i = 0; i < m_textures_count; ++i) {
ffr::bindTexture(m_offset + i, m_textures[i].handle);
ffr::setUniform1i(m_textures[i].uniform, i + m_offset);
}
}
struct {
ffr::TextureHandle handle;
ffr::UniformHandle uniform;
} m_textures[16];
int m_offset = 0;
int m_textures_count = 0;
};
const int pipeline_idx = lua_upvalueindex(1);
if (lua_type(L, pipeline_idx) != LUA_TLIGHTUSERDATA) {
LuaWrapper::argError<PipelineImpl*>(L, pipeline_idx);
}
PipelineImpl* pipeline = LuaWrapper::toType<PipelineImpl*>(L, pipeline_idx);
LuaWrapper::checkTableArg(L, 1);
const int offset = lua_gettop(L) > 1 ? LuaWrapper::checkArg<int>(L, 2) : 0;
Cmd* cmd = LUMIX_NEW(pipeline->m_renderer.getAllocator(), Cmd);
cmd->m_offset = offset;
lua_pushnil(L);
while (lua_next(L, 1) != 0) {
if(lua_type(L, -1) != LUA_TNUMBER) {
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
return luaL_error(L, "%s", "Incorrect texture arguments of bindTextures");
}
if(lua_type(L, -2) != LUA_TSTRING) {
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
return luaL_error(L, "%s", "Incorrect texture arguments of bindTextures");
}
if (cmd->m_textures_count > lengthOf(cmd->m_textures)) {
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
return luaL_error(L, "%s", "Too many texture in bindTextures call");
}
const char* uniform_name = lua_tostring(L, -2);
cmd->m_textures[cmd->m_textures_count].uniform = ffr::allocUniform(uniform_name, ffr::UniformType::INT, 1);
const int rb_idx = (int)lua_tointeger(L, -1);
cmd->m_textures[cmd->m_textures_count].handle = pipeline->m_renderbuffers[rb_idx].handle;
++cmd->m_textures_count;
lua_pop(L, 1);
}
pipeline->m_renderer.push(cmd);
return 0;
};
static int renderEnvProbeVolumes(lua_State* L)
{
const int pipeline_idx = lua_upvalueindex(1);
if (lua_type(L, pipeline_idx) != LUA_TLIGHTUSERDATA) {
LuaWrapper::argError<PipelineImpl*>(L, pipeline_idx);
}
PipelineImpl* pipeline = LuaWrapper::toType<PipelineImpl*>(L, pipeline_idx);
const int shader_id = LuaWrapper::checkArg<int>(L, 1);
const Shader* shader = [&] {
for (const ShaderRef& s : pipeline->m_shaders) {
if(s.id == shader_id) {
return s.res;
}
}
return (Shader*)nullptr;
}();
if (!shader) {
return luaL_error(L, "Unknown shader id %d in renderEnvProbeVolumes.", shader_id);
}
const CameraParams cp = checkCameraParams(L, 2);
struct Cmd : public Renderer::RenderCommandBase
{
struct Probe
{
Vec3 pos;
ffr::TextureHandle texture;
};
Cmd(IAllocator& allocator) : m_probes(allocator) {}
void setup() override
{
m_pipeline->getScene()->getEnvironmentProbes(m_probes);
}
void execute() override
{
PROFILE_FUNCTION();
if(m_probes.empty()) return;
ffr::pushDebugGroup("environment");
const Shader::Program& prog = Shader::getProgram(m_shader, 0);
if(!prog.handle.isValid()) return;
const int pos_radius_uniform_loc = ffr::getUniformLocation(prog.handle, m_pos_radius_uniform);
ffr::VertexDecl decl;
decl.addAttribute(3, ffr::AttributeType::FLOAT, false, false);
ffr::setVertexBuffer(&decl, m_vb, 0, nullptr);
ffr::setIndexBuffer(m_ib);
ffr::useProgram(prog.handle);
2018-09-15 09:55:46 +02:00
ffr::setState(u64(ffr::StateFlags::DEPTH_TEST) | u64(ffr::StateFlags::CULL_BACK));
ffr::blending(2);
2018-09-05 20:45:06 +02:00
const int irradiance_map_loc = ffr::getUniformLocation(prog.handle, m_irradiance_map_uniform);
const int radiance_map_loc = ffr::getUniformLocation(prog.handle, m_radiance_map_uniform);
2018-09-16 18:35:57 +02:00
const DVec3 cam_pos = m_camera_params.pos;
2018-09-05 20:45:06 +02:00
for (const EnvProbeInfo& probe : m_probes) {
2018-09-16 18:35:57 +02:00
const Vec4 pos_radius((probe.position - cam_pos).toFloat(), probe.radius);
2018-09-05 20:45:06 +02:00
ffr::bindTexture(0, probe.radiance);
ffr::applyUniform1i(irradiance_map_loc, 0);
ffr::bindTexture(1, probe.radiance);
ffr::applyUniform1i(radiance_map_loc, 1);
ffr::applyUniform4f(pos_radius_uniform_loc, &pos_radius.x);
ffr::drawTriangles(36);
}
2018-09-15 09:55:46 +02:00
ffr::blending(0);
2018-09-05 20:45:06 +02:00
ffr::popDebugGroup();
}
ffr::BufferHandle m_ib;
ffr::BufferHandle m_vb;
ffr::UniformHandle m_pos_radius_uniform;
ffr::UniformHandle m_irradiance_map_uniform;
ffr::UniformHandle m_radiance_map_uniform;
CameraParams m_camera_params;
PipelineImpl* m_pipeline;
Array<EnvProbeInfo> m_probes;
ShaderRenderData* m_shader;
2018-09-05 20:45:06 +02:00
};
if(shader->isReady()) {
IAllocator& allocator = pipeline->m_renderer.getAllocator();
Cmd* cmd = LUMIX_NEW(allocator, Cmd)(allocator);
cmd->m_pipeline = pipeline;
cmd->m_shader = shader->m_render_data;
cmd->m_ib = pipeline->m_cube_ib;
cmd->m_vb = pipeline->m_cube_vb;
cmd->m_camera_params = cp;
cmd->m_irradiance_map_uniform = pipeline->m_irradiance_map_uniform;
cmd->m_radiance_map_uniform = pipeline->m_radiance_map_uniform;
cmd->m_pos_radius_uniform = pipeline->m_position_radius_uniform;
pipeline->m_renderer.push(cmd);
}
return 0;
}
2018-08-23 22:35:38 +02:00
static int renderMeshes(lua_State* L)
{
PROFILE_FUNCTION();
const int pipeline_idx = lua_upvalueindex(1);
if (lua_type(L, pipeline_idx) != LUA_TLIGHTUSERDATA) {
LuaWrapper::argError<PipelineImpl*>(L, pipeline_idx);
}
PipelineImpl* pipeline = LuaWrapper::toType<PipelineImpl*>(L, pipeline_idx);
const char* define = LuaWrapper::checkArg<const char*>(L, 1);
LuaWrapper::checkTableArg(L, 2);
2018-08-23 22:35:38 +02:00
const CameraParams cp = checkCameraParams(L ,2);
2018-08-23 22:35:38 +02:00
2018-07-28 14:51:09 +02:00
IAllocator& allocator = pipeline->m_renderer.getAllocator();
RenderMeshesCommand* cmd = LUMIX_NEW(allocator, RenderMeshesCommand)(allocator);
2018-07-14 10:03:38 +02:00
if (lua_gettop(L) > 4 && lua_istable(L, 4)) {
lua_pushnil(L);
while (lua_next(L, 4) != 0) {
if(lua_type(L, -1) != LUA_TNUMBER) {
g_log_error.log("Renderer") << "Incorrect global textures arguments of renderMeshes";
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
lua_pop(L, 2);
return 0;
}
if(lua_type(L, -2) != LUA_TSTRING) {
g_log_error.log("Renderer") << "Incorrect global textures arguments of renderMeshes";
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
lua_pop(L, 2);
return 0;
}
2018-07-29 10:53:13 +02:00
if (cmd->m_global_textures_count > lengthOf(cmd->m_global_textures)) {
2018-07-14 10:03:38 +02:00
g_log_error.log("Renderer") << "Too many textures in renderMeshes call";
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
lua_pop(L, 2);
return 0;
}
const char* uniform = lua_tostring(L, -2);
const int rb_idx = (int)lua_tointeger(L, -1);
2018-07-29 10:53:13 +02:00
auto& t = cmd->m_global_textures[cmd->m_global_textures_count];
2018-07-15 17:35:41 +02:00
t.texture = pipeline->m_renderbuffers[rb_idx].handle;
t.uniform = ffr::allocUniform(uniform, ffr::UniformType::INT, 1);
2018-07-29 10:53:13 +02:00
++cmd->m_global_textures_count;
2018-07-14 10:03:38 +02:00
lua_pop(L, 1);
}
}
2018-07-29 10:53:13 +02:00
cmd->m_camera_params = cp;
cmd->m_pipeline = pipeline;
2018-09-25 18:50:23 +02:00
cmd->m_define_mask = 1 << pipeline->m_renderer.getShaderDefineIdx(define);
2018-07-14 10:03:38 +02:00
pipeline->m_renderer.push(cmd);
return 0;
}
2018-07-06 21:31:44 +02:00
2018-07-05 15:54:14 +02:00
static int drawArray(lua_State* L)
{
2018-07-14 10:03:38 +02:00
struct Cmd : Renderer::RenderCommandBase {
2018-08-09 22:35:00 +02:00
void setup() override { m_render_data = m_shader->isReady() ? m_shader->m_render_data : nullptr; }
2018-07-14 10:03:38 +02:00
void execute() override
{
2018-08-09 22:35:00 +02:00
if (!m_render_data) return;
ffr::ProgramHandle prg = Shader::getProgram(m_render_data, m_define_mask).handle;
2018-07-14 10:03:38 +02:00
for(int i = 0; i < m_textures_count; ++i) {
2018-07-15 17:35:41 +02:00
ffr::bindTexture(i, m_textures[i].handle);
ffr::setUniform1i(m_textures[i].uniform, i);
2018-07-14 10:03:38 +02:00
}
for(int i = 0; i < m_uniforms_count; ++i) {
2018-07-15 17:35:41 +02:00
ffr::setUniform4f(m_uniforms[i].handle, &m_uniforms[i].value.x);
2018-07-14 10:03:38 +02:00
}
2018-07-15 17:35:41 +02:00
ffr::setVertexBuffer(nullptr, ffr::INVALID_BUFFER, 0, nullptr);
ffr::useProgram(prg);
ffr::setState(0);
ffr::setIndexBuffer(ffr::INVALID_BUFFER);
ffr::drawArrays(m_indices_offset, m_indices_count, ffr::PrimitiveType::TRIANGLE_STRIP);
2018-07-14 10:03:38 +02:00
}
struct {
ffr::TextureHandle handle;
2018-07-15 17:35:41 +02:00
ffr::UniformHandle uniform;
2018-07-14 10:03:38 +02:00
} m_textures[16];
int m_textures_count = 0;
struct {
Vec4 value;
2018-07-15 17:35:41 +02:00
ffr::UniformHandle handle;
2018-07-14 10:03:38 +02:00
} m_uniforms[16];
int m_uniforms_count = 0;
Shader* m_shader;
int m_indices_count;
int m_indices_offset;
2018-07-29 18:36:32 +02:00
u32 m_define_mask = 0;
ShaderRenderData* m_render_data = nullptr;
2018-08-09 22:35:00 +02:00
2018-07-14 10:03:38 +02:00
};
const int pipeline_idx = lua_upvalueindex(1);
2018-07-05 15:54:14 +02:00
if (lua_type(L, pipeline_idx) != LUA_TLIGHTUSERDATA) {
2018-07-22 15:22:36 +02:00
LuaWrapper::argError<PipelineImpl*>(L, pipeline_idx);
2018-07-05 15:54:14 +02:00
}
PipelineImpl* pipeline = LuaWrapper::toType<PipelineImpl*>(L, pipeline_idx);
const int indices_offset = LuaWrapper::checkArg<int>(L, 1);
const int indices_count = LuaWrapper::checkArg<int>(L, 2);
int shader_id = LuaWrapper::checkArg<int>(L, 3);
2018-07-22 22:57:29 +02:00
if(lua_gettop(L) > 3) {
LuaWrapper::checkTableArg(L, 4);
}
if(lua_gettop(L) > 4) {
LuaWrapper::checkTableArg(L, 5);
}
2018-07-05 15:54:14 +02:00
Shader* shader = nullptr;
for (const ShaderRef& s : pipeline->m_shaders) {
if(s.id == shader_id) {
shader = s.res;
break;
}
}
if (!shader) {
2018-08-05 18:00:30 +02:00
return luaL_error(L, "Unknown shader id %d in drawArrays.", shader_id);
2018-07-05 15:54:14 +02:00
}
if (shader->isFailure()) {
2018-08-05 18:00:30 +02:00
return luaL_error(L, "Shader %s failed to load. `drawArrays` has no effect.", shader->getPath().c_str());
2018-07-05 15:54:14 +02:00
}
if (!shader->isReady()) return 0;
2018-07-14 10:03:38 +02:00
Cmd* cmd = LUMIX_NEW(pipeline->m_renderer.getAllocator(), Cmd);
2018-07-22 22:57:29 +02:00
if(lua_gettop(L) > 3) {
2018-07-05 15:54:14 +02:00
lua_pushnil(L);
2018-07-22 22:57:29 +02:00
while (lua_next(L, 4) != 0) {
if(lua_type(L, -1) != LUA_TNUMBER) {
2018-07-14 10:03:38 +02:00
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
2018-08-05 18:00:30 +02:00
return luaL_error(L, "%s", "Incorrect texture arguments of drawArrays");
2018-07-05 15:54:14 +02:00
}
if(lua_type(L, -2) != LUA_TSTRING) {
2018-07-22 22:57:29 +02:00
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
2018-08-05 18:00:30 +02:00
return luaL_error(L, "%s", "Incorrect texture arguments of drawArrays");
2018-07-22 22:57:29 +02:00
}
if (cmd->m_textures_count > lengthOf(cmd->m_textures)) {
2018-07-14 10:03:38 +02:00
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
2018-08-05 18:00:30 +02:00
return luaL_error(L, "%s", "Too many texture in drawArray call");
2018-07-05 15:54:14 +02:00
}
2018-07-15 17:35:41 +02:00
const char* uniform_name = lua_tostring(L, -2);
2018-07-22 22:57:29 +02:00
cmd->m_textures[cmd->m_textures_count].uniform = ffr::allocUniform(uniform_name, ffr::UniformType::INT, 1);
const int rb_idx = (int)lua_tointeger(L, -1);
cmd->m_textures[cmd->m_textures_count].handle = pipeline->m_renderbuffers[rb_idx].handle;
++cmd->m_textures_count;
lua_pop(L, 1);
}
if (lua_istable(L, 5)) {
lua_pushnil(L);
while (lua_next(L, 5) != 0) {
if(lua_type(L, -1) != LUA_TTABLE) {
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
2018-08-05 18:00:30 +02:00
return luaL_error(L, "%s", "Incorrect uniform arguments of drawArrays");
2018-07-22 22:57:29 +02:00
}
if(lua_type(L, -2) != LUA_TSTRING) {
2018-07-14 10:03:38 +02:00
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
2018-08-05 18:00:30 +02:00
return luaL_error(L, "%s", "Incorrect uniform arguments of drawArrays");
2018-07-05 15:54:14 +02:00
}
2018-07-22 22:57:29 +02:00
const char* uniform_name = lua_tostring(L, -2);
cmd->m_uniforms[cmd->m_uniforms_count].handle = ffr::allocUniform(uniform_name, ffr::UniformType::VEC4, 1);
float* value = &cmd->m_uniforms[cmd->m_uniforms_count].value.x;
for(int i = 0; i < 4; ++i) {
lua_rawgeti(L, -1, 1 + i);
if (lua_type(L, -1) != LUA_TNUMBER) {
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
2018-08-05 18:00:30 +02:00
return luaL_error(L, "%s", "Incorrect uniform arguments of drawArrays. Uniforms can only be Vec4.");
2018-07-22 22:57:29 +02:00
}
value[i] = (float)lua_tonumber(L, -1);
lua_pop(L, 1);
}
++cmd->m_uniforms_count;
2018-07-05 15:54:14 +02:00
lua_pop(L, 1);
}
}
2018-07-29 18:36:32 +02:00
if (lua_isstring(L, 6)) {
const char* define = lua_tostring(L, 6);
cmd->m_define_mask = 1 << pipeline->m_renderer.getShaderDefineIdx(define);
}
else if (lua_istable(L, 6)) {
lua_pushnil(L);
while (lua_next(L, 6) != 0) {
if(lua_type(L, -1) != LUA_TSTRING) {
LUMIX_DELETE(pipeline->m_renderer.getAllocator(), cmd);
2018-08-05 18:00:30 +02:00
return luaL_error(L, "%s", "Incorrect uniform arguments of drawArrays");
2018-07-29 18:36:32 +02:00
}
const char* define = lua_tostring(L, -1);
cmd->m_define_mask |= 1 << pipeline->m_renderer.getShaderDefineIdx(define);
lua_pop(L, 1);
}
}
2018-07-05 15:54:14 +02:00
}
2018-09-16 18:35:57 +02:00
2018-07-14 10:03:38 +02:00
cmd->m_shader = shader;
cmd->m_indices_count = indices_count;
cmd->m_indices_offset = indices_offset;
pipeline->m_renderer.push(cmd);
2018-07-05 15:54:14 +02:00
return 0;
}
2018-07-06 21:31:44 +02:00
static void pushCameraParams(lua_State* L, const CameraParams& params)
{
lua_createtable(L, 0, 4);
lua_createtable(L, 32+24, 0);
const float* frustum = params.frustum.xs;
for(int i = 0; i < 32+24; ++i) {
LuaWrapper::push(L, frustum[i]);
lua_rawseti(L, -2, i + 1);
}
2018-09-16 18:35:57 +02:00
LuaWrapper::push(L, params.frustum.origin);
lua_setfield(L, -2, "origin");
2018-07-06 21:31:44 +02:00
lua_setfield(L, -2, "frustum");
LuaWrapper::setField(L, -2, "position", params.pos);
LuaWrapper::setField(L, -2, "lod_multiplier", params.lod_multiplier);
}
static int getCameraParams(lua_State* L)
{
const int pipeline_idx = lua_upvalueindex(1);
if (lua_type(L, pipeline_idx) != LUA_TLIGHTUSERDATA) {
2018-07-22 15:22:36 +02:00
LuaWrapper::argError<PipelineImpl*>(L, pipeline_idx);
2018-07-06 21:31:44 +02:00
}
PipelineImpl* pipeline = LuaWrapper::toType<PipelineImpl*>(L, pipeline_idx);
RenderScene* scene = pipeline->m_scene;
CameraParams cp;
2018-07-08 00:44:45 +02:00
cp.pos = pipeline->m_viewport.pos;
cp.frustum = pipeline->m_viewport.getFrustum();
cp.lod_multiplier = scene->getCameraLODMultiplier(pipeline->m_viewport.fov, pipeline->m_viewport.is_ortho);
2018-07-06 21:31:44 +02:00
pushCameraParams(L, cp);
return 1;
}
2018-09-16 18:35:57 +02:00
static void findExtraShadowcasterPlanes(const Vec3& light_forward, const Frustum& camera_frustum, const DVec3& camera_position, Frustum* shadow_camera_frustum)
2018-07-06 21:31:44 +02:00
{
2018-09-16 18:35:57 +02:00
/*static const Frustum::Planes planes[] = {
2018-07-06 21:31:44 +02:00
Frustum::Planes::LEFT, Frustum::Planes::TOP, Frustum::Planes::RIGHT, Frustum::Planes::BOTTOM };
bool prev_side = dotProduct(light_forward, camera_frustum.getNormal(planes[lengthOf(planes) - 1])) < 0;
int out_plane = (int)Frustum::Planes::EXTRA0;
Vec3 camera_frustum_center = camera_frustum.computeBoundingSphere().position;
for (int i = 0; i < lengthOf(planes); ++i)
{
bool side = dotProduct(light_forward, camera_frustum.getNormal(planes[i])) < 0;
if (prev_side != side)
{
Vec3 n0 = camera_frustum.getNormal(planes[i]);
Vec3 n1 = camera_frustum.getNormal(planes[(i + lengthOf(planes) - 1) % lengthOf(planes)]);
Vec3 line_dir = crossProduct(n1, n0);
Vec3 n = crossProduct(light_forward, line_dir);
float d = -dotProduct(camera_position, n);
if (dotProduct(camera_frustum_center, n) + d < 0)
{
n = -n;
d = -dotProduct(camera_position, n);
}
shadow_camera_frustum->setPlane((Frustum::Planes)out_plane, n, d);
++out_plane;
if (out_plane >(int)Frustum::Planes::EXTRA1) break;
}
prev_side = side;
2018-09-16 18:35:57 +02:00
}*/
// TODO
ASSERT(false);
2018-07-06 21:31:44 +02:00
}
static Vec3 shadowmapTexelAlign(const Vec3& shadow_cam_pos,
float shadowmap_width,
float frustum_radius,
const Matrix& light_mtx)
{
Matrix inv = light_mtx;
inv.fastInverse();
Vec3 out = inv.transformPoint(shadow_cam_pos);
float align = 2 * frustum_radius / (shadowmap_width * 0.5f - 2);
out.x -= fmodf(out.x, align);
out.y -= fmodf(out.y, align);
out = light_mtx.transformPoint(out);
return out;
}
static int getShadowCameraParams(lua_State* L)
{
2018-09-16 18:35:57 +02:00
/*const int pipeline_idx = lua_upvalueindex(1);
2018-07-06 21:31:44 +02:00
if (lua_type(L, pipeline_idx) != LUA_TLIGHTUSERDATA) {
2018-07-22 15:22:36 +02:00
LuaWrapper::argError<PipelineImpl*>(L, pipeline_idx);
2018-07-06 21:31:44 +02:00
}
PipelineImpl* pipeline = LuaWrapper::toType<PipelineImpl*>(L, pipeline_idx);
const int slice = LuaWrapper::checkArg<int>(L, 1);
const int shadowmap_width = LuaWrapper::checkArg<int>(L, 2);
RenderScene* scene = pipeline->m_scene;
const Universe& universe = scene->getUniverse();
2018-08-19 17:35:37 +02:00
const EntityPtr light = scene->getActiveGlobalLight();
const Vec4 cascades = light.isValid() ? scene->getShadowmapCascades((EntityRef)light) : Vec4(3, 10, 60, 150);
const Matrix light_mtx = light.isValid() ? universe.getMatrix((EntityRef)light) : Matrix::IDENTITY;
2018-07-06 21:31:44 +02:00
2018-07-08 00:44:45 +02:00
const float camera_height = (float)pipeline->m_viewport.h;
const float camera_fov = pipeline->m_viewport.fov;
const float camera_ratio = pipeline->m_viewport.w / camera_height;
2018-07-06 21:31:44 +02:00
const float split_distances[] = {0.1f, cascades.x, cascades.y, cascades.z, cascades.w};
Frustum camera_frustum;
2018-07-08 00:44:45 +02:00
camera_frustum.computePerspective(pipeline->m_viewport.pos,
pipeline->m_viewport.rot * Vec3(0, 0, -1),
pipeline->m_viewport.rot * Vec3(0, 1, 0),
2018-07-06 21:31:44 +02:00
camera_fov,
camera_ratio,
split_distances[slice],
split_distances[slice + 1]);
const Sphere frustum_bounding_sphere = camera_frustum.computeBoundingSphere();
const float bb_size = frustum_bounding_sphere.radius;
2018-07-08 00:44:45 +02:00
const Vec3 light_forward = light_mtx.getZVector();
2018-07-06 21:31:44 +02:00
Vec3 shadow_cam_pos = frustum_bounding_sphere.position;
shadow_cam_pos = shadowmapTexelAlign(shadow_cam_pos, 0.5f * shadowmap_width - 2, bb_size, light_mtx);
2018-07-14 10:03:38 +02:00
Renderer::GlobalState global_state = pipeline->m_renderer.getGlobalState();
2018-07-06 21:31:44 +02:00
Matrix projection_matrix;
projection_matrix.setOrtho(-bb_size, bb_size, -bb_size, bb_size, SHADOW_CAM_NEAR, SHADOW_CAM_FAR, ffr::isHomogenousDepth(), true);
shadow_cam_pos -= light_forward * SHADOW_CAM_FAR * 0.5f;
Matrix view_matrix;
view_matrix.lookAt(shadow_cam_pos, shadow_cam_pos + light_forward, light_mtx.getYVector());
const float ymul = ffr::isOriginBottomLeft() ? 0.5f : -0.5f;
const Matrix bias_matrix(
0.5, 0.0, 0.0, 0.0,
0.0, ymul, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.0, 1.0);
global_state.shadowmap_matrices[slice] = bias_matrix * projection_matrix * view_matrix;
global_state.shadow_view_projection = projection_matrix * view_matrix;
2018-07-14 10:03:38 +02:00
pipeline->m_renderer.setGlobalState(global_state);
2018-07-05 15:54:14 +02:00
2018-07-06 21:31:44 +02:00
CameraParams cp;
cp.lod_multiplier = 1;
2018-07-08 00:44:45 +02:00
cp.pos = pipeline->m_viewport.pos;
2018-07-06 21:31:44 +02:00
cp.frustum.computeOrtho(shadow_cam_pos
, -light_forward
, light_mtx.getYVector()
, bb_size
, bb_size
, SHADOW_CAM_NEAR
, SHADOW_CAM_FAR);
2018-07-05 15:54:14 +02:00
2018-07-08 00:44:45 +02:00
findExtraShadowcasterPlanes(light_forward, camera_frustum, pipeline->m_viewport.pos, &cp.frustum);
2018-07-06 21:31:44 +02:00
pushCameraParams(L, cp);
2018-09-16 18:35:57 +02:00
return 1;*/
// TODO
ASSERT(false);
return 0;
2018-07-06 21:31:44 +02:00
}
2018-07-15 17:35:41 +02:00
2018-07-08 18:16:16 +02:00
2018-07-06 21:31:44 +02:00
static int setRenderTargets(lua_State* L)
2018-06-30 15:18:27 +02:00
{
2018-07-05 15:54:14 +02:00
const int pipeline_idx = lua_upvalueindex(1);
if (lua_type(L, pipeline_idx) != LUA_TLIGHTUSERDATA) {
2018-07-22 15:22:36 +02:00
LuaWrapper::argError<PipelineImpl*>(L, pipeline_idx );
2018-07-05 15:54:14 +02:00
}
PipelineImpl* pipeline = LuaWrapper::toType<PipelineImpl*>(L, pipeline_idx);
2015-07-23 23:17:51 +02:00
2018-09-15 09:55:46 +02:00
const int rb_count = lua_gettop(L);
2018-07-08 18:16:16 +02:00
int rbs[16];
2018-06-30 15:18:27 +02:00
if(rb_count > lengthOf(rbs)) {
2018-07-05 15:54:14 +02:00
g_log_error.log("Renderer") << "Too many render buffers in " << pipeline->getPath();
return 0;
}
if(rb_count <= 0) {
g_log_error.log("Renderer") << "createFramebuffer without arguments in " << pipeline->getPath();
2018-06-30 15:18:27 +02:00
return 0;
}
2015-07-23 23:17:51 +02:00
2018-07-15 17:35:41 +02:00
struct Cmd : Renderer::RenderCommandBase
{
void setup() override { }
void execute() override
{
PROFILE_FUNCTION();
const ffr::FramebufferHandle fb = pipeline->m_renderer.getFramebuffer();
ffr::update(fb, count, rbs);
ffr::setFramebuffer(fb, true);
ffr::viewport(0, 0, w, h);
}
PipelineImpl* pipeline;
ffr::TextureHandle rbs[16];
uint count;
uint w;
uint h;
};
Cmd* cmd = LUMIX_NEW(pipeline->m_renderer.getAllocator(), Cmd);
2018-06-30 15:18:27 +02:00
for(int i = 0; i < rb_count; ++i) {
2018-09-15 09:55:46 +02:00
const int rb_idx = LuaWrapper::checkArg<int>(L, i + 1);
2018-07-14 10:03:38 +02:00
cmd->rbs[i] = pipeline->m_renderbuffers[rb_idx].handle;
2018-06-30 15:18:27 +02:00
}
2018-07-14 10:03:38 +02:00
cmd->pipeline = pipeline;
cmd->count = rb_count;
cmd->w = pipeline->m_viewport.w;
cmd->h = pipeline->m_viewport.h;
pipeline->m_renderer.push(cmd);
2018-07-06 21:31:44 +02:00
2018-07-22 22:57:29 +02:00
return 0;
2018-06-30 15:18:27 +02:00
}
2015-07-04 15:22:28 +02:00
2018-06-30 15:18:27 +02:00
void renderModel(Model& model, const Matrix& mtx) override
{
2018-07-14 16:24:17 +02:00
for(int i = 0; i < model.getMeshCount(); ++i) {
2016-01-24 17:20:03 +01:00
2018-06-30 15:18:27 +02:00
const Mesh& mesh = model.getMesh(i);
const Material* material = mesh.material;
ShaderRenderData* shader_rd = material->getShader()->m_render_data;
const Shader::Program& prog = Shader::getProgram(shader_rd, 0); // TODO define
2018-06-30 15:18:27 +02:00
const int textures_count = material->getTextureCount();
2016-01-24 17:20:03 +01:00
2018-07-01 18:13:44 +02:00
if(!prog.handle.isValid()) continue;
2017-11-07 16:49:18 +01:00
2018-06-30 15:18:27 +02:00
for(int i = 0; i < textures_count; ++i) {
2018-07-15 17:35:41 +02:00
ffr::bindTexture(i, material->getTexture(i)->handle);
ffr::setUniform1i(material->getTextureUniform(i), i);
2018-06-30 15:18:27 +02:00
}
2017-11-07 16:49:18 +01:00
2018-07-01 18:13:44 +02:00
int attribute_map[16];
const Mesh::RenderData* rd = mesh.render_data;
for(uint i = 0; i < rd->vertex_decl.attributes_count; ++i) {
attribute_map[i] = prog.attribute_by_semantics[(int)rd->attributes_semantic[i]];
2018-07-01 18:13:44 +02:00
}
2018-07-15 17:35:41 +02:00
ffr::setUniformMatrix4f(m_model_uniform, &mtx.m11);
ffr::useProgram(prog.handle);
ffr::setVertexBuffer(&rd->vertex_decl, rd->vertex_buffer_handle, 0, prog.use_semantics ? attribute_map : nullptr);
ffr::setIndexBuffer(rd->index_buffer_handle);
2018-09-15 09:55:46 +02:00
ffr::setState(u64(ffr::StateFlags::DEPTH_TEST) | u64(ffr::StateFlags::DEPTH_WRITE) | material->getRenderStates());
ffr::drawTriangles(rd->indices_count);
2018-07-14 16:24:17 +02:00
}
}
2017-09-27 15:38:37 +02:00
2018-07-22 15:22:36 +02:00
struct RenderTerrainsCommand : Renderer::RenderCommandBase
2018-07-06 21:31:44 +02:00
{
2018-07-22 15:22:36 +02:00
RenderTerrainsCommand(IAllocator& allocator)
: m_allocator(allocator)
, m_instance_data(allocator)
, m_batches(allocator)
{
}
void setup() override
{
2018-09-16 18:35:57 +02:00
/*PROFILE_FUNCTION();
2018-07-22 15:22:36 +02:00
Array<TerrainInfo> infos(m_allocator);
m_pipeline->m_scene->getTerrainInfos(m_camera_params.frustum, m_camera_params.pos, infos);
if (infos.empty()) return;
m_define_mask = m_shader_define.empty()
? 0
: 1 << m_pipeline->m_renderer.getShaderDefineIdx(m_shader_define);
std::sort(infos.begin(), infos.end(), [](const TerrainInfo& a, const TerrainInfo& b) {
2018-09-16 18:35:57 +02:00
if (a.terrain == b.terrain) return a.index < b.index;
return a.terrain < b.terrain;
2018-07-22 15:22:36 +02:00
});
m_instance_data.resize(infos.size());
Terrain* prev_terrain = nullptr;
int prev_idx = -1;
int prev_submesh = -1;
for (int i = 0, c = infos.size(); i < c; ++i) {
const TerrainInfo& info = infos[i];
2018-09-16 18:35:57 +02:00
if (info.terrain != prev_terrain || prev_submesh != info.index) {
2018-07-22 15:22:36 +02:00
if (prev_terrain) {
Batch& b = m_batches.emplace();
b.terrain = prev_terrain;
2018-09-16 18:35:57 +02:00
b.shader = infos[prev_idx].shader->m_render_data;
b.matrix = infos[prev_idx].rot.toMatrix();
b.matrix.setTranslation((infos[prev_idx].position - m_camera_params.pos).toFloat());
b.submesh = infos[prev_idx].index;
2018-07-22 15:22:36 +02:00
b.from = prev_idx;
b.to = i - 1;
}
prev_idx = i;
2018-09-16 18:35:57 +02:00
prev_terrain = info.terrain;
prev_submesh = info.index;
2018-07-22 15:22:36 +02:00
}
2018-09-16 18:35:57 +02:00
m_instance_data[i].size = info.size;
m_instance_data[i].quad_min = info.min;
m_instance_data[i].morph_consts = info.morph_const;
2018-07-22 15:22:36 +02:00
}
Batch& b = m_batches.emplace();
b.terrain = prev_terrain;
2018-09-16 18:35:57 +02:00
b.shader = infos[prev_idx].shader->m_render_data;
b.matrix = infos[prev_idx].rot.toMatrix();
b.matrix.setTranslation((infos[prev_idx].position - m_camera_params.pos).toFloat());
b.submesh = infos[prev_idx].index;
2018-07-22 15:22:36 +02:00
b.from = prev_idx;
2018-09-16 18:35:57 +02:00
b.to = infos.size() - 1;*/
// TODO
//ASSERT(false);
2018-07-22 15:22:36 +02:00
}
void execute() override
{
2018-10-06 12:15:05 +02:00
Array<TerrainInfo> infos(m_allocator);
m_pipeline->m_scene->getTerrainInfos(m_camera_params.frustum, m_camera_params.pos, infos);
if(infos.empty()) return;
auto* rd = infos[0].shader->m_render_data;
const u32 deferred_define_mask = 1 << m_pipeline->m_renderer.getShaderDefineIdx("DEFERRED");
const u8 edge_define_idx = m_pipeline->m_renderer.getShaderDefineIdx("EDGE");
auto& p = Shader::getProgram(rd, deferred_define_mask);
auto& p_edge = Shader::getProgram(rd, deferred_define_mask | (1 << edge_define_idx));
const Vec3 pos = (infos[0].position - m_camera_params.pos).toFloat();
Vec3 lpos = (m_camera_params.pos - infos[0].position).toFloat();
lpos = infos[0].rot.conjugated().rotate(lpos);
ffr::setUniform3f(m_pipeline->m_position_uniform, &pos.x);
ffr::setUniform3f(m_pipeline->m_rel_camera_pos_uniform, &lpos.x);
ffr::blending(0);
for(int i = 0; i < infos[0].terrain->m_material->getTextureCount(); ++i) {
Texture* t = infos[0].terrain->m_material->getTexture(i);
if (t) {
ffr::UniformHandle uniform = infos[0].terrain->m_material->getTextureUniform(i);
ffr::bindTexture(i, t->handle);
ffr::setUniform1i(uniform, i);
}
}
static u64 state = (u64)ffr::StateFlags::DEPTH_TEST | (u64)ffr::StateFlags::DEPTH_WRITE;
if(p.handle.isValid()) {
ffr::useProgram(p.handle);
ffr::setState(state);
const int loc = ffr::getUniformLocation(p.handle, m_pipeline->m_lod_uniform);
for(int i = 0; i < 6; ++i) {
ffr::applyUniform1i(loc, i);
ffr::drawArrays(0, 126 * 126, ffr::PrimitiveType::POINTS);
}
}
if (p_edge.handle.isValid()) {
ffr::useProgram(p_edge.handle);
ffr::setState(state);
const int loc = ffr::getUniformLocation(p_edge.handle, m_pipeline->m_lod_uniform);
for(int i = 0; i < 6; ++i) {
ffr::applyUniform1i(loc, i);
ffr::drawArrays(0, 64, ffr::PrimitiveType::POINTS);
}
}
2018-09-16 18:35:57 +02:00
/*if(m_instance_data.empty()) return;
2018-07-22 15:22:36 +02:00
ffr::pushDebugGroup("terrains");
Renderer::TransientSlice instance_buffer = m_pipeline->m_renderer.allocTransient(m_instance_data.byte_size());
ffr::update(instance_buffer.buffer, m_instance_data.begin(), 0, m_instance_data.byte_size());
ffr::VertexDecl decl;
decl.addAttribute(3, ffr::AttributeType::FLOAT, false, false);
decl.addAttribute(2, ffr::AttributeType::FLOAT, false, false);
ffr::VertexDecl instance_decl;
instance_decl.addAttribute(3, ffr::AttributeType::FLOAT, false, false);
instance_decl.addAttribute(1, ffr::AttributeType::FLOAT, false, false);
instance_decl.addAttribute(3, ffr::AttributeType::FLOAT, false, false);
2018-09-16 18:35:57 +02:00
const DVec3 camera_pos = m_camera_params.pos;
2018-07-22 15:22:36 +02:00
for (const Batch& batch : m_batches) {
Texture* detail_texture = batch.terrain->getDetailTexture();
if (!detail_texture) continue;
Texture* splat_texture = batch.terrain->getSplatmap();
if (!splat_texture) continue;
const Matrix inv_world_matrix = batch.matrix.fastInverted();
const Vec4 rel_cam_pos(inv_world_matrix.transformPoint(camera_pos) / batch.terrain->getXZScale(), 1);
const Vec4 terrain_scale(batch.terrain->getScale(), 0);
const Vec4 terrain_params(batch.terrain->getRootSize()
, (float)detail_texture->width
, (float)splat_texture->width
, 0);
ffr::setUniform4f(m_pipeline->m_terrain_params_uniform, &terrain_params.x);
ffr::setUniform4f(m_pipeline->m_rel_camera_pos_uniform, &rel_cam_pos.x);
ffr::setUniform4f(m_pipeline->m_terrain_scale_uniform, &terrain_scale.x);
ffr::setUniformMatrix4f(m_pipeline->m_terrain_matrix_uniform, &batch.matrix.m11);
2018-08-09 22:35:00 +02:00
const ffr::ProgramHandle prg = Shader::getProgram(batch.shader, m_define_mask).handle;
2018-07-22 15:22:36 +02:00
ffr::useProgram(prg);
/*
for (int i = 0; i < m_global_textures_count; ++i) {
const auto& t = m_global_textures[i];
ffr::bindTexture(i, t.texture);
ffr::setUniform1i(t.uniform, i);
}
2018-09-16 18:35:57 +02:00
*//*
2018-07-22 15:22:36 +02:00
const Material* material = batch.terrain->m_material;
const int textures_count = material->getTextureCount();
for (int i = 0; i < textures_count; ++i) {
ffr::bindTexture(i + 0, material->getTexture(i)->handle);
ffr::setUniform1i(material->getTextureUniform(i), i + 0);
}
const Mesh& mesh = *batch.terrain->getMesh();
ffr::setVertexBuffer(&decl, mesh.vertex_buffer_handle, 0, nullptr);
ffr::setInstanceBuffer(instance_decl, instance_buffer.buffer, instance_buffer.offset + batch.from * sizeof(m_instance_data[0]), 2);
ffr::setIndexBuffer(mesh.index_buffer_handle);
2018-09-15 09:55:46 +02:00
ffr::setState(u64(ffr::StateFlags::DEPTH_WRITE) | u64(ffr::StateFlags::DEPTH_TEST) | batch.terrain->m_material->getRenderStates());
2018-07-22 15:22:36 +02:00
const int submesh_indices_count = mesh.indices_count / 4;
ffr::drawTrianglesInstanced(batch.submesh * submesh_indices_count * sizeof(u16), submesh_indices_count , 1 + batch.to - batch.from);
}
2018-09-16 18:35:57 +02:00
ffr::popDebugGroup();*/
// TODO
//ASSERT(false);
2018-07-22 15:22:36 +02:00
}
struct InstanceData
{
Vec3 quad_min;
float size;
Vec3 morph_consts;
};
struct Batch
{
Terrain* terrain;
ShaderRenderData* shader;
2018-07-22 15:22:36 +02:00
Matrix matrix;
uint submesh;
uint from;
uint to;
2018-07-17 01:13:58 +02:00
};
2018-07-22 15:22:36 +02:00
IAllocator& m_allocator;
PipelineImpl* m_pipeline;
CameraParams m_camera_params;
StaticString<32> m_shader_define;
u32 m_define_mask;
Array<InstanceData> m_instance_data;
Array<Batch> m_batches;
struct {
ffr::TextureHandle texture;
ffr::UniformHandle uniform;
} m_global_textures[16];
int m_global_textures_count = 0;
};
2018-07-17 01:13:58 +02:00
2018-07-22 15:22:36 +02:00
struct RenderMeshesCommand : Renderer::RenderCommandBase
{
2018-07-28 14:51:09 +02:00
RenderMeshesCommand(IAllocator& allocator)
2018-07-29 10:53:13 +02:00
: m_allocator(allocator)
2018-09-23 18:18:18 +02:00
, m_cmds(allocator)
2018-07-28 14:51:09 +02:00
{}
2018-07-29 10:53:13 +02:00
2018-09-29 15:14:46 +02:00
void radixSort(u64* _keys, u64* _values, int size)
2018-07-29 10:53:13 +02:00
{
2018-09-15 15:06:37 +02:00
PROFILE_FUNCTION();
2018-09-23 18:18:18 +02:00
PROFILE_INT("count", size);
if(size == 0) return;
2018-07-29 10:53:13 +02:00
// from https://github.com/bkaradzic/bx
enum {
RADIXSORT_BITS = 11,
RADIXSORT_HISTOGRAM_SIZE = 1 << RADIXSORT_BITS,
RADIXSORT_BIT_MASK = RADIXSORT_HISTOGRAM_SIZE - 1
};
Array<u64> tmp_keys(m_allocator); // TODO more suitable allocator
2018-09-29 15:14:46 +02:00
Array<u64> tmp_values(m_allocator);
2018-07-29 10:53:13 +02:00
tmp_keys.resize(size);
tmp_values.resize(size);
u64* keys = _keys;
u64* tempKeys = tmp_keys.begin();
2018-09-29 15:14:46 +02:00
u64* values = _values;
u64* tempValues = tmp_values.begin();
2018-07-29 10:53:13 +02:00
u32 histogram[RADIXSORT_HISTOGRAM_SIZE];
u16 shift = 0;
2018-09-15 15:06:37 +02:00
for (int pass = 0; pass < 5; ++pass) {
2018-07-29 10:53:13 +02:00
memset(histogram, 0, sizeof(u32) * RADIXSORT_HISTOGRAM_SIZE);
bool sorted = true;
u64 key = keys[0];
u64 prevKey = key;
for (int i = 0; i < size; ++i, prevKey = key) {
key = keys[i];
const u16 index = (key >> shift) & RADIXSORT_BIT_MASK;
++histogram[index];
sorted &= prevKey <= key;
}
if (sorted) {
if (pass & 1) {
// Odd number of passes needs to do copy to the destination.
memcpy(_keys, tmp_keys.begin(), tmp_keys.byte_size());
memcpy(_values, tmp_values.begin(), tmp_values.byte_size());
}
return;
}
u32 offset = 0;
for (int i = 0; i < RADIXSORT_HISTOGRAM_SIZE; ++i) {
const u32 count = histogram[i];
histogram[i] = offset;
offset += count;
}
for (int i = 0; i < size; ++i) {
const u64 key = keys[i];
const u16 index = (key >> shift) & RADIXSORT_BIT_MASK;
const u32 dest = histogram[index]++;
tempKeys[dest] = key;
tempValues[dest] = values[i];
}
u64* const swapKeys = tempKeys;
tempKeys = keys;
keys = swapKeys;
2018-09-29 15:14:46 +02:00
u64* const swapValues = tempValues;
2018-07-29 10:53:13 +02:00
tempValues = values;
values = swapValues;
shift += RADIXSORT_BITS;
}
}
2018-09-26 20:21:04 +02:00
struct CreateSortKeys
2018-09-23 18:18:18 +02:00
{
2018-10-07 18:00:48 +02:00
static void execute(void* data)
2018-09-26 20:21:04 +02:00
{
2018-10-07 18:00:48 +02:00
PROFILE_BLOCK("sort_keys");
CreateSortKeys* ctx = (CreateSortKeys*)data;
PROFILE_INT("num", ctx->count);
RenderScene* scene = ctx->cmd->m_pipeline->m_scene;
const ModelInstance* model_instances = scene->getModelInstances();
const u32* renderables = ctx->renderables;
MTBucketArray<u64>::Accessor sort_keys = ctx->sort_keys;
MTBucketArray<u64>::Accessor subrenderables = ctx->subrenderables;
const Universe::EntityData* entity_data = scene->getUniverse().getEntityData();
const DVec3 camera_pos = ctx->camera_pos;
for (int i = 0, c = ctx->count; i < c; ++i) {
const EntityRef e = {int(renderables[i] & 0x00ffFFff)};
const DVec3 pos = entity_data[e.index].transform.pos;
const float squared_length = float((pos - camera_pos).squaredLength());
const ModelInstance& mi = model_instances[e.index];
const RenderableTypes type = RenderableTypes(renderables[i] >> 24);
switch (type) {
case RenderableTypes::MESH:
sort_keys.push(mi.meshes[0].sort_key);
subrenderables.push(renderables[i]);
break;
case RenderableTypes::SKINNED:
case RenderableTypes::MESH_GROUP:
LODMeshIndices lod = mi.model->getLODMeshIndices(squared_length);
for(int mesh_idx = lod.from; mesh_idx <= lod.to; ++mesh_idx) {
sort_keys.push(mi.meshes[mesh_idx].sort_key);
subrenderables.push(renderables[i] | ((u64)mesh_idx << 32));
}
break;
default: ASSERT(false); break;
2018-09-26 20:21:04 +02:00
}
2018-10-07 18:00:48 +02:00
}
2018-09-23 22:59:47 +02:00
}
2018-09-29 15:14:46 +02:00
MTBucketArray<u64>::Accessor sort_keys;
MTBucketArray<u64>::Accessor subrenderables;
DVec3 camera_pos;
2018-09-26 20:21:04 +02:00
u32* renderables;
int count;
RenderMeshesCommand* cmd;
};
2018-09-23 22:59:47 +02:00
2018-09-26 20:21:04 +02:00
struct CreateCommands
{
2018-10-07 18:00:48 +02:00
static void execute(void* data)
2018-09-23 22:59:47 +02:00
{
2018-10-07 18:00:48 +02:00
PROFILE_BLOCK("create cmds");
CreateCommands* ctx = (CreateCommands*)data;
PROFILE_INT("num", ctx->count);
const Universe& universe = ctx->cmd->m_pipeline->m_scene->getUniverse();
ctx->output->resize(ctx->count * (sizeof(RenderableTypes) + sizeof(Matrix) + sizeof(Mesh*) + sizeof(Material) + sizeof(u16)));
u8* out = ctx->output->begin();
const u64* LUMIX_RESTRICT renderables = ctx->renderables;
const u64* LUMIX_RESTRICT sort_keys = ctx->sort_keys;
const ModelInstance* LUMIX_RESTRICT model_instances = ctx->cmd->m_pipeline->m_scene->getModelInstances();
const Universe::EntityData* LUMIX_RESTRICT entity_data = universe.getEntityData();
const DVec3 camera_pos = ctx->camera_pos;
for (int i = 0, c = ctx->count; i < c; ++i) {
const EntityRef e = {int(renderables[i] & 0x00ffFFff)};
const RenderableTypes type = RenderableTypes((renderables[i] >> 24) & 0xff);
*(RenderableTypes*)out = type;
out += sizeof(type);
switch(type) {
case RenderableTypes::MESH_GROUP:
case RenderableTypes::MESH: {
const u32 mesh_idx = renderables[i] >> 32;
const ModelInstance* LUMIX_RESTRICT mi = &model_instances[e.index];
*(Mesh::RenderData**)out = mi->meshes[mesh_idx].render_data;
out += sizeof(Mesh::RenderData*);
*(Material::RenderData**)out = mi->meshes[mesh_idx].material->getRenderData();
out += sizeof(Material::RenderData*);
u16* instance_count = (u16*)out;
int start_i = i;
out += sizeof(*instance_count);
const u64 key = sort_keys[i];
while (i < c && sort_keys[i] == key) {
2018-09-29 15:14:46 +02:00
const EntityRef e = {int(renderables[i] & 0x00ffFFff)};
const Transform& tr = entity_data[e.index].transform;
Matrix mtx = tr.rot.toMatrix();
mtx.multiply3x3(tr.scale);
mtx.setTranslation((tr.pos - camera_pos).toFloat());
memcpy(out, &mtx, sizeof(mtx));
out += sizeof(mtx);
2018-10-07 18:00:48 +02:00
++i;
}
*instance_count = u16(i - start_i);
--i;
break;
}
case RenderableTypes::SKINNED: {
const u32 mesh_idx = renderables[i] >> 32;
const ModelInstance* LUMIX_RESTRICT mi = &model_instances[e.index];
*(Mesh::RenderData**)out = mi->meshes[mesh_idx].render_data;
out += sizeof(Mesh::RenderData*);
*(Material::RenderData**)out = mi->meshes[mesh_idx].material->getRenderData();
out += sizeof(Material::RenderData*);
const EntityRef e = {int(renderables[i] & 0x00ffFFff)};
const Transform& tr = entity_data[e.index].transform;
Matrix mtx = tr.rot.toMatrix();
mtx.multiply3x3(tr.scale);
mtx.setTranslation((tr.pos - camera_pos).toFloat());
memcpy(out, &mtx, sizeof(mtx));
out += sizeof(mtx);
*(int*)out = mi->pose->count;
out += sizeof(int);
const uint out_offset = uint(out - ctx->output->begin());
ctx->output->resize(ctx->output->size() + mi->pose->count * sizeof(Matrix));
out = ctx->output->begin() + out_offset;
const Quat* rotations = mi->pose->rotations;
const Vec3* positions = mi->pose->positions;
Model& model = *mi->model;
for(int j = 0, c = mi->pose->count; j < c; ++j) {
const Model::Bone& bone = model.getBone(j);
const LocalRigidTransform tmp = {positions[j], rotations[j]};
Matrix m = (tmp * bone.inv_bind_transform).toMatrix();
memcpy(out, &m, sizeof(m));
out += sizeof(m);
2018-09-29 15:14:46 +02:00
}
2018-10-07 18:00:48 +02:00
break;
2018-09-26 20:21:04 +02:00
}
2018-10-07 18:00:48 +02:00
default: ASSERT(false); break;
2018-09-23 22:59:47 +02:00
}
2018-10-07 18:00:48 +02:00
}
ctx->output->resize(int(out - ctx->output->begin()));
2018-09-26 20:21:04 +02:00
}
2018-09-23 22:59:47 +02:00
2018-09-29 15:14:46 +02:00
u64* renderables;
2018-09-26 20:21:04 +02:00
u64* sort_keys;
Array<u8>* output;
DVec3 camera_pos;
int count;
RenderMeshesCommand* cmd;
};
2018-09-23 22:59:47 +02:00
2018-09-29 15:14:46 +02:00
void createSortKeys(const Array<Array<u32>>& renderables, MTBucketArray<u64>& sort_keys, MTBucketArray<u64>& subrenderables)
2018-09-26 20:21:04 +02:00
{
Array<CreateSortKeys> create_sort_keys(m_allocator);
create_sort_keys.reserve(renderables.size());
for(int i = 0; i < renderables.size(); ++i) {
2018-10-07 18:00:48 +02:00
if (renderables[i].empty()) continue;
2018-09-26 20:21:04 +02:00
CreateSortKeys& ctx = create_sort_keys.emplace();
ctx.renderables = renderables[i].begin();
2018-09-29 15:14:46 +02:00
ctx.sort_keys = sort_keys.begin();
ctx.subrenderables = subrenderables.begin();
2018-09-26 20:21:04 +02:00
ctx.count = renderables[i].size();
2018-09-29 15:14:46 +02:00
ctx.camera_pos = m_pipeline->m_viewport.pos;
2018-09-26 20:21:04 +02:00
ctx.cmd = this;
2018-09-23 18:18:18 +02:00
}
2018-10-07 18:00:48 +02:00
JobSystem::CounterHandle counter = JobSystem::runMany(create_sort_keys.begin(), &CreateSortKeys::execute, create_sort_keys.size(), sizeof(CreateSortKeys));
JobSystem::wait(counter);
2018-09-23 18:18:18 +02:00
}
2018-09-26 20:21:04 +02:00
2018-07-14 10:03:38 +02:00
void setup() override
2018-07-08 18:16:16 +02:00
{
2018-10-06 12:15:05 +02:00
PROFILE_FUNCTION();
2018-07-29 10:53:13 +02:00
if(!m_pipeline->m_scene) return;
2018-07-11 23:35:34 +02:00
2018-07-29 10:53:13 +02:00
Renderer& renderer = m_pipeline->m_renderer;
const RenderScene* scene = m_pipeline->getScene();
2018-09-23 18:18:18 +02:00
Array<Array<u32>> renderables(renderer.getAllocator());
scene->getRenderables(m_camera_params.frustum, renderables);
if (renderables.empty()) return;
2018-07-29 10:53:13 +02:00
2018-09-29 15:14:46 +02:00
MTBucketArray<u64> sort_keys(m_allocator);
MTBucketArray<u64> subrenderables(m_allocator);
Array<u64> sort_keys_linear(m_allocator);
Array<u64> subrenderables_linear(m_allocator);
createSortKeys(renderables, sort_keys, subrenderables);
2018-10-06 12:15:05 +02:00
{
PROFILE_BLOCK("merge");
sort_keys.merge(sort_keys_linear);
subrenderables.merge(subrenderables_linear);
}
2018-09-29 15:14:46 +02:00
if (!subrenderables_linear.empty()) {
radixSort(sort_keys_linear.begin(), subrenderables_linear.begin(), sort_keys_linear.size());
createCommands(subrenderables_linear, sort_keys_linear);
2018-07-08 18:16:16 +02:00
}
2018-09-26 20:21:04 +02:00
}
2018-09-29 15:14:46 +02:00
void createCommands(const Array<u64>& renderables, const Array<u64>& sort_keys)
2018-09-26 20:21:04 +02:00
{
Array<CreateCommands> create_commands(m_allocator);
const int job_count = Math::minimum(renderables.size(), 16);
create_commands.reserve(job_count);
int offset = 0;
const int step = (renderables.size() + job_count - 1) / job_count;
m_cmds.reserve(job_count);
for(int i = 0; i < job_count; ++i) {
CreateCommands& ctx = create_commands.emplace();
ctx.renderables = renderables.begin() + offset;
ctx.sort_keys = sort_keys.begin() + offset;
ctx.count = Math::minimum(step, renderables.size() - offset);
ctx.cmd = this;
ctx.camera_pos = m_camera_params.pos;
ctx.output = &m_cmds.emplace(m_allocator);
offset += step;
}
2018-10-07 18:00:48 +02:00
JobSystem::CounterHandle counter = JobSystem::runMany(create_commands.begin(), &CreateCommands::execute, create_commands.size(), sizeof(CreateCommands));
JobSystem::wait(counter);
2018-07-08 18:16:16 +02:00
}
2018-07-14 10:03:38 +02:00
void execute() override
2018-07-08 18:16:16 +02:00
{
2018-07-11 23:35:34 +02:00
PROFILE_FUNCTION();
2018-09-23 18:18:18 +02:00
if(m_cmds.empty()) return;
2018-07-29 10:53:13 +02:00
for (int i = 0; i < m_global_textures_count; ++i) {
const auto& t = m_global_textures[i];
2018-09-26 20:21:04 +02:00
ffr::bindTexture(i, t.texture);
ffr::setUniform1i(t.uniform, i);
2018-07-15 17:35:41 +02:00
}
2018-07-17 01:13:58 +02:00
ffr::VertexDecl instance_decl;
instance_decl.addAttribute(4, ffr::AttributeType::FLOAT, false, false);
instance_decl.addAttribute(4, ffr::AttributeType::FLOAT, false, false);
instance_decl.addAttribute(4, ffr::AttributeType::FLOAT, false, false);
instance_decl.addAttribute(4, ffr::AttributeType::FLOAT, false, false);
2018-07-29 10:53:13 +02:00
Renderer& renderer = m_pipeline->m_renderer;
2018-07-17 01:13:58 +02:00
2018-07-29 10:53:13 +02:00
int drawcalls_count = 0;
2018-09-06 21:29:58 +02:00
2018-09-29 15:14:46 +02:00
const u32 instanced_mask = m_define_mask | (1 << m_pipeline->m_renderer.getShaderDefineIdx("INSTANCED"));
const u32 skinned_mask = m_define_mask | (1 << m_pipeline->m_renderer.getShaderDefineIdx("SKINNED"));
2018-09-25 18:50:23 +02:00
2018-09-23 22:59:47 +02:00
for (Array<u8>& cmds : m_cmds) {
const u8* cmd = cmds.begin();
2018-09-26 20:21:04 +02:00
const u8* cmd_end = cmds.end();
while (cmd != cmd_end) {
2018-09-29 15:14:46 +02:00
const RenderableTypes type = *(RenderableTypes*)cmd;
cmd += sizeof(type);
switch(type) {
case RenderableTypes::MESH:
case RenderableTypes::MESH_GROUP: {
const Mesh::RenderData* mesh = *(const Mesh::RenderData**)cmd;
cmd += sizeof(mesh);
const Material::RenderData* material = *(const Material::RenderData**)cmd;
cmd += sizeof(mesh);
const u16 instances_count = *(u16*)cmd;
cmd += sizeof(instances_count);
const float* matrices = (const float*)cmd;
cmd += sizeof(Matrix) * instances_count;
ShaderRenderData* shader = material->shader;
for (int i = 0; i < material->textures_count; ++i) {
const ffr::TextureHandle handle = material->textures[i];
const ffr::UniformHandle uniform = shader->texture_uniforms[i];
ffr::bindTexture(i + 2 + m_global_textures_count, handle);
ffr::setUniform1i(uniform, i + 2 + m_global_textures_count);
}
const Shader::Program& prog = Shader::getProgram(shader, instanced_mask);
if(prog.handle.isValid()) {
ffr::setState(material->render_states | u64(ffr::StateFlags::DEPTH_TEST) | u64(ffr::StateFlags::DEPTH_WRITE));
const Vec4 params(material->roughness, material->metallic, material->emission, 0);
ffr::setUniform4f(m_pipeline->m_material_params_uniform, &params.x);
ffr::setUniform4f(m_pipeline->m_material_color_uniform, &material->color.x);
ffr::useProgram(prog.handle);
int attribute_map[16];
for (uint i = 0; i < mesh->vertex_decl.attributes_count; ++i) {
attribute_map[i] = prog.attribute_by_semantics[(int)mesh->attributes_semantic[i]];
}
ffr::setVertexBuffer(&mesh->vertex_decl, mesh->vertex_buffer_handle, 0, prog.use_semantics ? attribute_map : nullptr);
ffr::setIndexBuffer(mesh->index_buffer_handle);
const Renderer::TransientSlice instance_buffer = m_pipeline->m_renderer.allocTransient(instances_count * sizeof(Matrix));
2018-09-23 22:59:47 +02:00
2018-09-29 15:14:46 +02:00
ffr::update(instance_buffer.buffer, matrices, instance_buffer.offset, instance_buffer.size);
ffr::setInstanceBuffer(instance_decl, instance_buffer.buffer, instance_buffer.offset, mesh->vertex_decl.attributes_count);
ffr::drawTrianglesInstanced(0, mesh->indices_count, instances_count);
}
break;
}
case RenderableTypes::SKINNED: {
const Mesh::RenderData* mesh = *(const Mesh::RenderData**)cmd;
cmd += sizeof(mesh);
const Material::RenderData* material = *(const Material::RenderData**)cmd;
cmd += sizeof(mesh);
const float* model_mtx = (const float*)cmd;
cmd += sizeof(Matrix);
const int bones_count = *(const int*)cmd;
cmd += sizeof(int);
const float* bones = (const float*)cmd;
cmd += sizeof(Matrix) * bones_count;
ShaderRenderData* shader = material->shader;
for (int i = 0; i < material->textures_count; ++i) {
const ffr::TextureHandle handle = material->textures[i];
const ffr::UniformHandle uniform = shader->texture_uniforms[i];
ffr::bindTexture(i + 2 + m_global_textures_count, handle);
ffr::setUniform1i(uniform, i + 2 + m_global_textures_count);
}
const Shader::Program& prog = Shader::getProgram(shader, skinned_mask);
if(prog.handle.isValid()) {
ffr::setState(material->render_states | u64(ffr::StateFlags::DEPTH_TEST) | u64(ffr::StateFlags::DEPTH_WRITE));
const Vec4 params(material->roughness, material->metallic, material->emission, 0);
ffr::setUniform4f(m_pipeline->m_material_params_uniform, &params.x);
ffr::setUniform4f(m_pipeline->m_material_color_uniform, &material->color.x);
ffr::setUniformMatrix4f(m_pipeline->m_model_uniform, model_mtx);
ffr::useProgram(prog.handle);
const int loc = ffr::getUniformLocation(prog.handle, m_pipeline->m_bones_uniform);
if (loc >= 0) ffr::applyUniformMatrix4fv(loc, bones_count, bones);
int attribute_map[16];
for (uint i = 0; i < mesh->vertex_decl.attributes_count; ++i) {
attribute_map[i] = prog.attribute_by_semantics[(int)mesh->attributes_semantic[i]];
}
ffr::setVertexBuffer(&mesh->vertex_decl, mesh->vertex_buffer_handle, 0, prog.use_semantics ? attribute_map : nullptr);
ffr::setIndexBuffer(mesh->index_buffer_handle);
ffr::drawTriangles(mesh->indices_count);
}
break;
}
default: ASSERT(false); break;
2018-09-26 20:21:04 +02:00
}
2018-07-28 14:51:09 +02:00
}
}
2018-06-30 15:18:27 +02:00
}
2016-10-22 18:08:53 +02:00
2018-07-08 18:16:16 +02:00
2018-07-29 10:53:13 +02:00
IAllocator& m_allocator;
CameraParams m_camera_params;
PipelineImpl* m_pipeline;
2018-09-23 18:18:18 +02:00
Array<Array<u8>> m_cmds;
2018-09-25 18:50:23 +02:00
u32 m_define_mask;
2018-07-14 10:03:38 +02:00
struct {
2018-07-15 17:35:41 +02:00
ffr::TextureHandle texture;
ffr::UniformHandle uniform;
2018-07-29 10:53:13 +02:00
} m_global_textures[16];
int m_global_textures_count = 0;
2018-07-08 18:16:16 +02:00
};
2018-07-01 18:13:44 +02:00
void blending(const char* mode)
{
2018-07-29 12:40:58 +02:00
struct Cmd : Renderer::RenderCommandBase
{
void setup() override {}
void execute() override
{
ffr::blending(mode);
}
int mode;
};
Cmd* cmd = LUMIX_NEW(m_renderer.getAllocator(), Cmd);
cmd->mode = mode[0] ? 1 : 0;
m_renderer.push(cmd);
2018-07-01 18:13:44 +02:00
}
2018-07-06 21:31:44 +02:00
void clear(u32 flags, float r, float g, float b, float a, float depth)
2016-02-15 15:50:24 +01:00
{
2018-09-15 09:55:46 +02:00
struct Cmd : Renderer::RenderCommandBase {
void setup() override {}
void execute() override {
ffr::clear(flags, &color.x, depth);
}
Vec4 color;
float depth;
u32 flags;
};
Cmd* cmd = LUMIX_NEW(m_renderer.getAllocator(), Cmd);
cmd->color.set(r, g, b, a);
cmd->flags = flags;
cmd->depth = depth;
m_renderer.push(cmd);
2016-02-15 15:50:24 +01:00
}
2017-02-18 00:26:56 +01:00
2018-06-30 15:18:27 +02:00
2018-07-06 21:31:44 +02:00
void viewport(int x, int y, int w, int h)
{
2018-07-14 10:03:38 +02:00
struct Cmd : Renderer::RenderCommandBase {
void setup() override {}
void execute() override { ffr::viewport(x, y, w, h); }
int x, y, w, h;
};
Cmd* cmd = LUMIX_NEW(m_renderer.getAllocator(), Cmd);
cmd->x = x;
cmd->y = y;
cmd->w = w;
cmd->h = h;
m_renderer.push(cmd);
2018-07-06 21:31:44 +02:00
}
2018-07-14 10:03:38 +02:00
void beginBlock(const char* name)
2018-07-06 21:31:44 +02:00
{
2018-07-14 10:03:38 +02:00
struct Cmd : Renderer::RenderCommandBase
{
void setup() override {}
void execute() override
{
ffr::pushDebugGroup(name);
renderer->beginProfileBlock(name);
}
StaticString<32> name;
Renderer* renderer;
};
Cmd* cmd = LUMIX_NEW(m_renderer.getAllocator(), Cmd);
cmd->name = name;
cmd->renderer = &m_renderer;
m_renderer.push(cmd);
}
void endBlock()
{
struct Cmd : Renderer::RenderCommandBase
{
void setup() override {}
void execute() override
{
renderer->endProfileBlock();
ffr::popDebugGroup();
}
Renderer* renderer;
};
Cmd* cmd = LUMIX_NEW(m_renderer.getAllocator(), Cmd);
cmd->renderer = &m_renderer;
m_renderer.push(cmd);
2018-07-06 21:31:44 +02:00
}
2018-07-22 22:57:29 +02:00
void setStencil(uint write_mask, uint func, int ref, uint mask, uint sfail, uint zfail, uint zpass)
{
struct Cmd : Renderer::RenderCommandBase
{
void setup() {}
void execute() override
{
ffr::setStencil(write_mask, (ffr::StencilFuncs)func, ref, mask, (ffr::StencilOps)sfail, (ffr::StencilOps)zfail, (ffr::StencilOps)zpass);
}
uint write_mask;
uint func;
int ref;
uint mask;
uint sfail;
uint zfail;
uint zpass;
};
IAllocator& allocator = m_renderer.getAllocator();
Cmd* cmd = LUMIX_NEW(allocator, Cmd);
cmd->write_mask = write_mask;
cmd->func = func;
cmd->ref = ref;
cmd->mask = mask;
cmd->sfail = sfail;
cmd->zfail = zfail;
cmd->zpass = zpass;
m_renderer.push(cmd);
}
2018-06-30 15:18:27 +02:00
void setOutput(int rb_index)
{
2018-07-11 23:35:34 +02:00
m_output = rb_index;
}
2016-02-18 01:47:49 +01:00
2017-10-10 13:21:45 +02:00
2018-07-05 15:54:14 +02:00
int preloadShader(const char* path)
{
ResourceManagerHub& rm = m_renderer.getEngine().getResourceManager();
2018-07-05 15:54:14 +02:00
ShaderRef s;
s.res = rm.load<Shader>(Path(path));
2018-07-05 15:54:14 +02:00
s.id = 0;
for(ShaderRef& i : m_shaders) {
if(i.id >= s.id) {
s.id = i.id + 1;
}
}
m_shaders.push(s);
return s.id;
}
2018-06-30 15:18:27 +02:00
void setWindowHandle(void* data) override { ASSERT(false); } // TODO
2014-05-20 23:35:09 +02:00
2018-06-30 15:18:27 +02:00
void callLuaFunction(const char* function) override
{
if (!m_lua_state) return;
2018-06-30 15:18:27 +02:00
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
lua_getfield(m_lua_state, -1, function);
if (lua_type(m_lua_state, -1) != LUA_TFUNCTION)
{
lua_pop(m_lua_state, 2);
return;
}
2018-06-30 15:18:27 +02:00
if (lua_pcall(m_lua_state, 0, 0, 0) != 0)
{
2018-06-30 15:18:27 +02:00
g_log_warning.log("Renderer") << lua_tostring(m_lua_state, -1);
lua_pop(m_lua_state, 1);
}
2018-06-30 15:18:27 +02:00
lua_pop(m_lua_state, 1);
}
2018-06-30 15:18:27 +02:00
2018-07-05 15:54:14 +02:00
void registerLuaAPI(lua_State* L)
{
lua_rawgeti(m_lua_state, LUA_REGISTRYINDEX, m_lua_env);
auto registerCFunction = [L, this](const char* name, lua_CFunction function) {
lua_pushlightuserdata(L, this);
lua_pushcclosure(L, function, 1);
lua_setfield(L, -3, name);
};
auto registerConst = [L](const char* name, u32 value)
{
lua_pushinteger(L, value);
lua_setfield(L, -2, name);
};
#define REGISTER_FUNCTION(name) \
do {\
auto f = &LuaWrapper::wrapMethodClosure<PipelineImpl, decltype(&PipelineImpl::name), &PipelineImpl::name>; \
registerCFunction(#name, f); \
} while(false) \
2018-07-14 10:03:38 +02:00
REGISTER_FUNCTION(beginBlock);
2018-07-05 15:54:14 +02:00
REGISTER_FUNCTION(blending);
2018-07-06 21:31:44 +02:00
REGISTER_FUNCTION(clear);
2018-07-05 15:54:14 +02:00
REGISTER_FUNCTION(createRenderbuffer);
2018-07-14 10:03:38 +02:00
REGISTER_FUNCTION(endBlock);
2018-07-05 15:54:14 +02:00
REGISTER_FUNCTION(executeCustomCommand);
REGISTER_FUNCTION(preloadShader);
REGISTER_FUNCTION(render2D);
REGISTER_FUNCTION(renderDebugShapes);
REGISTER_FUNCTION(setOutput);
2018-07-22 22:57:29 +02:00
REGISTER_FUNCTION(setStencil);
2018-07-06 21:31:44 +02:00
REGISTER_FUNCTION(viewport);
2018-07-05 15:54:14 +02:00
2018-07-08 00:44:45 +02:00
registerConst("CLEAR_DEPTH", (uint)ffr::ClearFlags::DEPTH);
2018-07-22 22:57:29 +02:00
registerConst("CLEAR_COLOR", (uint)ffr::ClearFlags::COLOR);
registerConst("CLEAR_ALL", (uint)ffr::ClearFlags::COLOR | (uint)ffr::ClearFlags::DEPTH | (uint)ffr::ClearFlags::STENCIL);
registerConst("STENCIL_ALWAYS", (uint)ffr::StencilFuncs::ALWAYS);
registerConst("STENCIL_EQUAL", (uint)ffr::StencilFuncs::EQUAL);
registerConst("STENCIL_NOT_EQUAL", (uint)ffr::StencilFuncs::NOT_EQUAL);
registerConst("STENCIL_DISABLE", (uint)ffr::StencilFuncs::DISABLE);
registerConst("STENCIL_KEEP", (uint)ffr::StencilOps::KEEP);
registerConst("STENCIL_REPLACE", (uint)ffr::StencilOps::REPLACE);
2018-07-05 15:54:14 +02:00
2018-09-05 20:45:06 +02:00
registerCFunction("bindTextures", PipelineImpl::bindTextures);
2018-07-05 15:54:14 +02:00
registerCFunction("drawArray", PipelineImpl::drawArray);
2018-07-06 21:31:44 +02:00
registerCFunction("getCameraParams", PipelineImpl::getCameraParams);
registerCFunction("getShadowCameraParams", PipelineImpl::getShadowCameraParams);
2018-09-05 20:45:06 +02:00
registerCFunction("renderEnvProbeVolumes", PipelineImpl::renderEnvProbeVolumes);
2018-07-14 10:03:38 +02:00
registerCFunction("renderMeshes", PipelineImpl::renderMeshes);
2018-08-23 22:35:38 +02:00
registerCFunction("renderParticles", PipelineImpl::renderParticles);
2018-07-22 15:22:36 +02:00
registerCFunction("renderTerrains", PipelineImpl::renderTerrains);
2018-07-06 21:31:44 +02:00
registerCFunction("setRenderTargets", PipelineImpl::setRenderTargets);
2018-07-05 15:54:14 +02:00
lua_pop(L, 1); // pop env
#undef REGISTER_FUNCTION
}
2018-09-09 17:58:25 +02:00
bool isReady() const override { return m_resource->isReady(); }
2018-06-30 15:18:27 +02:00
const Stats& getStats() const override { return m_stats; }
2018-09-09 17:58:25 +02:00
const Path& getPath() override { return m_resource->getPath(); }
2018-06-30 15:18:27 +02:00
Draw2D& getDraw2D() override { return m_draw2d; }
2018-07-11 23:35:34 +02:00
ffr::TextureHandle getOutput() override {
2018-07-29 18:36:32 +02:00
if (m_output < 0 || m_output >= m_renderbuffers.size()) return ffr::INVALID_TEXTURE;
2018-07-11 23:35:34 +02:00
return m_renderbuffers[m_output].handle;
}
2018-06-30 15:18:27 +02:00
struct Renderbuffer {
uint width;
uint height;
2018-07-11 23:35:34 +02:00
bool use_realtive_size;
Vec2 relative_size;
2018-06-30 15:18:27 +02:00
ffr::TextureFormat format;
2018-07-11 23:35:34 +02:00
ffr::TextureHandle handle;
2018-06-30 15:18:27 +02:00
int frame_counter;
};
2018-07-05 15:54:14 +02:00
struct ShaderRef {
Lumix::Shader* res;
int id;
};
2018-07-01 23:37:56 +02:00
IAllocator& m_allocator;
2018-06-30 15:18:27 +02:00
Renderer& m_renderer;
2018-09-09 17:58:25 +02:00
PipelineResource* m_resource;
2018-06-30 15:18:27 +02:00
lua_State* m_lua_state;
int m_lua_thread_ref;
int m_lua_env;
bool m_is_first_render;
2018-07-06 21:31:44 +02:00
StaticString<32> m_define;
2018-06-30 15:18:27 +02:00
RenderScene* m_scene;
Draw2D m_draw2d;
Shader* m_draw2d_shader;
Stats m_stats;
2018-07-08 00:44:45 +02:00
Viewport m_viewport;
2018-07-11 23:35:34 +02:00
int m_output;
2018-07-01 23:37:56 +02:00
Shader* m_debug_shape_shader;
2018-07-05 15:54:14 +02:00
Texture* m_default_cubemap;
2018-06-30 15:18:27 +02:00
Array<CustomCommandHandler> m_custom_commands_handlers;
Array<Renderbuffer> m_renderbuffers;
2018-07-05 15:54:14 +02:00
Array<ShaderRef> m_shaders;
2018-07-29 18:36:32 +02:00
Timer* m_timer;
2018-07-15 17:35:41 +02:00
2018-09-05 20:45:06 +02:00
ffr::UniformHandle m_position_radius_uniform;
2018-10-06 12:15:05 +02:00
ffr::UniformHandle m_position_uniform;
ffr::UniformHandle m_lod_uniform;
2018-07-22 15:22:36 +02:00
ffr::UniformHandle m_terrain_params_uniform;
ffr::UniformHandle m_rel_camera_pos_uniform;
ffr::UniformHandle m_terrain_scale_uniform;
ffr::UniformHandle m_terrain_matrix_uniform;
2018-07-15 17:35:41 +02:00
ffr::UniformHandle m_model_uniform;
2018-07-17 01:13:58 +02:00
ffr::UniformHandle m_bones_uniform;
2018-07-15 17:35:41 +02:00
ffr::UniformHandle m_canvas_size_uniform;
ffr::UniformHandle m_texture_uniform;
ffr::UniformHandle m_irradiance_map_uniform;
ffr::UniformHandle m_radiance_map_uniform;
ffr::UniformHandle m_material_params_uniform;
2018-07-29 12:40:58 +02:00
ffr::UniformHandle m_material_color_uniform;
2018-09-05 20:45:06 +02:00
ffr::BufferHandle m_cube_vb;
ffr::BufferHandle m_cube_ib;
2018-06-30 15:18:27 +02:00
};
2017-10-06 17:52:00 +02:00
2018-09-09 17:58:25 +02:00
Pipeline* Pipeline::create(Renderer& renderer, PipelineResource* resource, const char* define, IAllocator& allocator)
2015-09-02 11:14:42 +02:00
{
2018-09-09 17:58:25 +02:00
return LUMIX_NEW(allocator, PipelineImpl)(renderer, resource, define, allocator);
2015-09-02 11:14:42 +02:00
}
2018-06-30 15:18:27 +02:00
void Pipeline::destroy(Pipeline* pipeline)
2015-07-23 23:17:51 +02:00
{
2018-07-14 17:52:06 +02:00
PipelineImpl* p = (PipelineImpl*)pipeline;
LUMIX_DELETE(p->m_allocator, p);
2015-07-23 23:17:51 +02:00
}
2018-06-30 15:18:27 +02:00
} // namespace Lumix