2017-03-17 00:00:46 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2016, Blender Foundation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Contributor(s): Blender Institute
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** \file eevee.c
|
|
|
|
* \ingroup DNA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "DRW_render.h"
|
|
|
|
|
2017-03-28 00:09:45 +02:00
|
|
|
#include "BLI_dynstr.h"
|
2017-04-18 12:50:09 +02:00
|
|
|
#include "BLI_rand.h"
|
|
|
|
#include "GPU_glew.h"
|
2017-03-28 00:09:45 +02:00
|
|
|
|
2017-03-17 00:00:46 +01:00
|
|
|
#include "eevee.h"
|
|
|
|
#include "eevee_private.h"
|
2017-04-03 11:04:42 +02:00
|
|
|
#include "eevee_lut.h"
|
2017-03-17 00:00:46 +01:00
|
|
|
|
|
|
|
#define EEVEE_ENGINE "BLENDER_EEVEE"
|
|
|
|
|
|
|
|
/* *********** STATIC *********** */
|
|
|
|
static struct {
|
|
|
|
struct GPUShader *default_lit;
|
2017-03-18 01:55:41 +01:00
|
|
|
struct GPUShader *depth_sh;
|
2017-03-17 00:00:46 +01:00
|
|
|
struct GPUShader *tonemap;
|
2017-04-10 12:06:17 +02:00
|
|
|
struct GPUShader *shadow_sh;
|
2017-04-18 12:50:09 +02:00
|
|
|
/* Temp : use world shader */
|
|
|
|
struct GPUShader *probe_sh;
|
|
|
|
struct GPUShader *probe_filter_sh;
|
2017-04-18 21:02:02 +02:00
|
|
|
struct GPUShader *probe_spherical_harmonic_sh;
|
2017-04-18 12:50:09 +02:00
|
|
|
|
2017-04-03 11:04:42 +02:00
|
|
|
struct GPUTexture *ltc_mat;
|
2017-04-18 12:50:09 +02:00
|
|
|
struct GPUTexture *brdf_lut;
|
|
|
|
struct GPUTexture *hammersley;
|
|
|
|
struct GPUTexture *jitter;
|
2017-04-10 12:06:17 +02:00
|
|
|
|
2017-03-29 23:45:07 +02:00
|
|
|
float camera_pos[3];
|
2017-03-17 00:00:46 +01:00
|
|
|
} e_data = {NULL}; /* Engine data */
|
|
|
|
|
2017-04-03 11:04:42 +02:00
|
|
|
extern char datatoc_ltc_lib_glsl[];
|
2017-04-18 12:50:09 +02:00
|
|
|
extern char datatoc_bsdf_lut_frag_glsl[];
|
2017-03-28 00:09:45 +02:00
|
|
|
extern char datatoc_bsdf_common_lib_glsl[];
|
|
|
|
extern char datatoc_bsdf_direct_lib_glsl[];
|
2017-04-18 12:50:09 +02:00
|
|
|
extern char datatoc_bsdf_sampling_lib_glsl[];
|
2017-03-17 00:00:46 +01:00
|
|
|
extern char datatoc_lit_surface_frag_glsl[];
|
|
|
|
extern char datatoc_lit_surface_vert_glsl[];
|
|
|
|
extern char datatoc_tonemap_frag_glsl[];
|
2017-04-10 12:06:17 +02:00
|
|
|
extern char datatoc_shadow_frag_glsl[];
|
|
|
|
extern char datatoc_shadow_geom_glsl[];
|
|
|
|
extern char datatoc_shadow_vert_glsl[];
|
2017-04-18 12:50:09 +02:00
|
|
|
extern char datatoc_probe_filter_frag_glsl[];
|
2017-04-18 21:02:02 +02:00
|
|
|
extern char datatoc_probe_sh_frag_glsl[];
|
2017-04-18 12:50:09 +02:00
|
|
|
extern char datatoc_probe_frag_glsl[];
|
|
|
|
extern char datatoc_probe_geom_glsl[];
|
|
|
|
extern char datatoc_probe_vert_glsl[];
|
|
|
|
|
|
|
|
/* Van der Corput sequence */
|
|
|
|
/* From http://holger.dammertz.org/stuff/notes_HammersleyOnHemisphere.html */
|
|
|
|
static float radical_inverse(int i) {
|
|
|
|
unsigned int bits = (unsigned int)i;
|
|
|
|
bits = (bits << 16u) | (bits >> 16u);
|
|
|
|
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
|
|
|
|
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
|
|
|
|
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
|
|
|
|
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
|
|
|
|
return (float)bits * 2.3283064365386963e-10f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct GPUTexture *create_hammersley_sample_texture(int samples)
|
|
|
|
{
|
|
|
|
struct GPUTexture *tex;
|
|
|
|
float (*texels)[2] = MEM_mallocN(sizeof(float[2]) * samples, "hammersley_tex");
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < samples; i++) {
|
|
|
|
float phi = radical_inverse(i) * 2.0f * M_PI;
|
|
|
|
texels[i][0] = cos(phi);
|
|
|
|
texels[i][1] = sinf(phi);
|
|
|
|
}
|
|
|
|
|
|
|
|
tex = DRW_texture_create_1D(samples, DRW_TEX_RG_16, DRW_TEX_WRAP, (float *)texels);
|
|
|
|
MEM_freeN(texels);
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct GPUTexture *create_jitter_texture(int w, int h)
|
|
|
|
{
|
|
|
|
struct GPUTexture *tex;
|
|
|
|
float (*texels)[2] = MEM_mallocN(sizeof(float[2]) * w * h, "jitter_tex");
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* TODO replace by something more evenly distributed like blue noise */
|
|
|
|
for (i = 0; i < w * h; i++) {
|
|
|
|
texels[i][0] = 2.0f * BLI_frand() - 1.0f;
|
|
|
|
texels[i][1] = 2.0f * BLI_frand() - 1.0f;
|
|
|
|
normalize_v2(texels[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
tex = DRW_texture_create_2D(w, h, DRW_TEX_RG_16, DRW_TEX_WRAP, (float *)texels);
|
|
|
|
MEM_freeN(texels);
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
2017-04-18 15:22:54 +02:00
|
|
|
static struct GPUTexture *create_ggx_lut_texture(int UNUSED(w), int UNUSED(h))
|
2017-04-18 12:50:09 +02:00
|
|
|
{
|
|
|
|
struct GPUTexture *tex;
|
|
|
|
#if 0 /* Used only to generate the LUT values */
|
|
|
|
struct GPUFrameBuffer *fb = NULL;
|
|
|
|
static float samples_ct = 8192.0f;
|
|
|
|
static float inv_samples_ct = 1.0f / 8192.0f;
|
|
|
|
|
|
|
|
char *lib_str = NULL;
|
|
|
|
|
|
|
|
DynStr *ds_vert = BLI_dynstr_new();
|
|
|
|
BLI_dynstr_append(ds_vert, datatoc_bsdf_common_lib_glsl);
|
|
|
|
BLI_dynstr_append(ds_vert, datatoc_bsdf_sampling_lib_glsl);
|
|
|
|
lib_str = BLI_dynstr_get_cstring(ds_vert);
|
|
|
|
BLI_dynstr_free(ds_vert);
|
|
|
|
|
|
|
|
struct GPUShader *sh = DRW_shader_create_with_lib(datatoc_probe_vert_glsl, datatoc_probe_geom_glsl, datatoc_bsdf_lut_frag_glsl, lib_str,
|
|
|
|
"#define HAMMERSLEY_SIZE 8192\n"
|
|
|
|
"#define BRDF_LUT_SIZE 64\n"
|
|
|
|
"#define NOISE_SIZE 64\n");
|
|
|
|
|
|
|
|
DRWPass *pass = DRW_pass_create("Probe Filtering", DRW_STATE_WRITE_COLOR);
|
|
|
|
DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
|
|
|
|
DRW_shgroup_uniform_float(grp, "sampleCount", &samples_ct, 1);
|
|
|
|
DRW_shgroup_uniform_float(grp, "invSampleCount", &inv_samples_ct, 1);
|
|
|
|
DRW_shgroup_uniform_texture(grp, "texHammersley", e_data.hammersley, 0);
|
|
|
|
DRW_shgroup_uniform_texture(grp, "texJitter", e_data.jitter, 1);
|
|
|
|
|
|
|
|
struct Batch *geom = DRW_cache_fullscreen_quad_get();
|
|
|
|
DRW_shgroup_call_add(grp, geom, NULL);
|
|
|
|
|
|
|
|
float *texels = MEM_mallocN(sizeof(float[2]) * w * h, "lut");
|
|
|
|
|
|
|
|
tex = DRW_texture_create_2D(w, h, DRW_TEX_RG_16, DRW_TEX_FILTER, (float *)texels);
|
|
|
|
|
|
|
|
DRWFboTexture tex_filter = {&tex, DRW_BUF_RG_16, DRW_TEX_FILTER};
|
|
|
|
DRW_framebuffer_init(&fb, w, h, &tex_filter, 1);
|
|
|
|
|
|
|
|
DRW_framebuffer_bind(fb);
|
|
|
|
DRW_draw_pass(pass);
|
|
|
|
|
|
|
|
float *data = MEM_mallocN(sizeof(float[3]) * w * h, "lut");
|
|
|
|
glReadBuffer(GL_COLOR_ATTACHMENT0);
|
|
|
|
glReadPixels(0, 0, w, h, GL_RGB, GL_FLOAT, data);
|
|
|
|
|
|
|
|
printf("{");
|
|
|
|
for (int i = 0; i < w*h * 3; i+=3) {
|
|
|
|
printf("%ff, %ff, ", data[i], data[i+1]); i+=3;
|
|
|
|
printf("%ff, %ff, ", data[i], data[i+1]); i+=3;
|
|
|
|
printf("%ff, %ff, ", data[i], data[i+1]); i+=3;
|
|
|
|
printf("%ff, %ff, \n", data[i], data[i+1]);
|
|
|
|
}
|
|
|
|
printf("}");
|
|
|
|
|
|
|
|
MEM_freeN(texels);
|
|
|
|
MEM_freeN(data);
|
|
|
|
#else
|
|
|
|
float (*texels)[3] = MEM_mallocN(sizeof(float[3]) * 64 * 64, "bsdf lut texels");
|
|
|
|
|
|
|
|
for (int i = 0; i < 64 * 64; i++) {
|
|
|
|
texels[i][0] = bsdf_split_sum_ggx[i*2 + 0];
|
|
|
|
texels[i][1] = bsdf_split_sum_ggx[i*2 + 1];
|
|
|
|
texels[i][2] = ltc_mag_ggx[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
tex = DRW_texture_create_2D(64, 64, DRW_TEX_RGB_16, DRW_TEX_FILTER, (float *)texels);
|
|
|
|
MEM_freeN(texels);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
|
2017-03-17 00:00:46 +01:00
|
|
|
|
|
|
|
/* *********** FUNCTIONS *********** */
|
|
|
|
|
2017-04-18 12:50:09 +02:00
|
|
|
static void EEVEE_engine_init(void *ved)
|
2017-03-17 00:00:46 +01:00
|
|
|
{
|
2017-04-18 12:50:09 +02:00
|
|
|
EEVEE_Data *vedata = (EEVEE_Data *)ved;
|
|
|
|
EEVEE_TextureList *txl = vedata->txl;
|
|
|
|
EEVEE_FramebufferList *fbl = vedata->fbl;
|
|
|
|
EEVEE_StorageList *stl = vedata->stl;
|
2017-03-17 00:00:46 +01:00
|
|
|
|
2017-04-05 12:57:32 +02:00
|
|
|
DRWFboTexture tex = {&txl->color, DRW_BUF_RGBA_16, DRW_TEX_FILTER};
|
2017-03-17 00:00:46 +01:00
|
|
|
|
2017-04-12 12:10:01 +10:00
|
|
|
const float *viewport_size = DRW_viewport_size_get();
|
2017-03-17 00:00:46 +01:00
|
|
|
DRW_framebuffer_init(&fbl->main,
|
|
|
|
(int)viewport_size[0], (int)viewport_size[1],
|
|
|
|
&tex, 1);
|
|
|
|
|
2017-03-18 01:55:41 +01:00
|
|
|
if (!e_data.default_lit) {
|
|
|
|
e_data.depth_sh = DRW_shader_create_3D_depth_only();
|
|
|
|
}
|
|
|
|
|
2017-03-17 00:00:46 +01:00
|
|
|
if (!e_data.default_lit) {
|
2017-03-28 00:09:45 +02:00
|
|
|
char *lib_str = NULL;
|
|
|
|
|
|
|
|
DynStr *ds_vert = BLI_dynstr_new();
|
|
|
|
BLI_dynstr_append(ds_vert, datatoc_bsdf_common_lib_glsl);
|
2017-04-03 11:04:42 +02:00
|
|
|
BLI_dynstr_append(ds_vert, datatoc_ltc_lib_glsl);
|
2017-03-28 00:09:45 +02:00
|
|
|
BLI_dynstr_append(ds_vert, datatoc_bsdf_direct_lib_glsl);
|
|
|
|
lib_str = BLI_dynstr_get_cstring(ds_vert);
|
|
|
|
BLI_dynstr_free(ds_vert);
|
|
|
|
|
2017-04-10 12:06:17 +02:00
|
|
|
e_data.default_lit = DRW_shader_create_with_lib(datatoc_lit_surface_vert_glsl, NULL, datatoc_lit_surface_frag_glsl, lib_str,
|
|
|
|
"#define MAX_LIGHT 128\n"
|
|
|
|
"#define MAX_SHADOW_CUBE 42\n"
|
|
|
|
"#define MAX_SHADOW_MAP 64\n"
|
|
|
|
"#define MAX_SHADOW_CASCADE 8\n"
|
|
|
|
"#define MAX_CASCADE_NUM 4\n");
|
2017-03-28 00:09:45 +02:00
|
|
|
|
|
|
|
MEM_freeN(lib_str);
|
2017-03-17 00:00:46 +01:00
|
|
|
}
|
|
|
|
|
2017-04-10 12:06:17 +02:00
|
|
|
if (!e_data.shadow_sh) {
|
|
|
|
e_data.shadow_sh = DRW_shader_create(datatoc_shadow_vert_glsl, datatoc_shadow_geom_glsl, datatoc_shadow_frag_glsl, NULL);
|
|
|
|
}
|
|
|
|
|
2017-04-18 12:50:09 +02:00
|
|
|
if (!e_data.probe_sh) {
|
|
|
|
e_data.probe_sh = DRW_shader_create(datatoc_probe_vert_glsl, datatoc_probe_geom_glsl, datatoc_probe_frag_glsl, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!e_data.probe_filter_sh) {
|
2017-04-18 21:02:02 +02:00
|
|
|
char *shader_str = NULL;
|
2017-04-18 12:50:09 +02:00
|
|
|
|
2017-04-18 21:02:02 +02:00
|
|
|
DynStr *ds_frag = BLI_dynstr_new();
|
|
|
|
BLI_dynstr_append(ds_frag, datatoc_bsdf_common_lib_glsl);
|
|
|
|
BLI_dynstr_append(ds_frag, datatoc_bsdf_sampling_lib_glsl);
|
|
|
|
BLI_dynstr_append(ds_frag, datatoc_probe_filter_frag_glsl);
|
|
|
|
shader_str = BLI_dynstr_get_cstring(ds_frag);
|
|
|
|
BLI_dynstr_free(ds_frag);
|
2017-04-18 12:50:09 +02:00
|
|
|
|
2017-04-18 21:02:02 +02:00
|
|
|
e_data.probe_filter_sh = DRW_shader_create(datatoc_probe_vert_glsl, datatoc_probe_geom_glsl, shader_str,
|
|
|
|
"#define HAMMERSLEY_SIZE 8192\n"
|
|
|
|
"#define NOISE_SIZE 64\n");
|
2017-04-18 12:50:09 +02:00
|
|
|
|
2017-04-18 21:02:02 +02:00
|
|
|
MEM_freeN(shader_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!e_data.probe_spherical_harmonic_sh) {
|
|
|
|
e_data.probe_spherical_harmonic_sh = DRW_shader_create_fullscreen(datatoc_probe_sh_frag_glsl, NULL);
|
2017-04-18 12:50:09 +02:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:00:46 +01:00
|
|
|
if (!e_data.tonemap) {
|
|
|
|
e_data.tonemap = DRW_shader_create_fullscreen(datatoc_tonemap_frag_glsl, NULL);
|
|
|
|
}
|
|
|
|
|
2017-04-03 11:04:42 +02:00
|
|
|
if (!e_data.ltc_mat) {
|
|
|
|
e_data.ltc_mat = DRW_texture_create_2D(64, 64, DRW_TEX_RGBA_16, DRW_TEX_FILTER, ltc_mat_ggx);
|
|
|
|
}
|
|
|
|
|
2017-04-18 12:50:09 +02:00
|
|
|
if (!e_data.hammersley) {
|
|
|
|
e_data.hammersley = create_hammersley_sample_texture(8192);
|
2017-04-03 11:04:42 +02:00
|
|
|
}
|
|
|
|
|
2017-04-18 12:50:09 +02:00
|
|
|
if (!e_data.jitter) {
|
|
|
|
e_data.jitter = create_jitter_texture(64, 64);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!e_data.brdf_lut) {
|
|
|
|
e_data.brdf_lut = create_ggx_lut_texture(64, 64);
|
2017-04-10 12:06:17 +02:00
|
|
|
}
|
2017-03-17 00:00:46 +01:00
|
|
|
|
2017-03-29 23:45:07 +02:00
|
|
|
{
|
|
|
|
float viewinvmat[4][4];
|
|
|
|
DRW_viewport_matrix_get(viewinvmat, DRW_MAT_VIEWINV);
|
|
|
|
|
|
|
|
copy_v3_v3(e_data.camera_pos, viewinvmat[3]);
|
|
|
|
}
|
2017-04-18 12:50:09 +02:00
|
|
|
|
|
|
|
EEVEE_lights_init(stl);
|
|
|
|
|
|
|
|
EEVEE_probes_init(vedata);
|
|
|
|
|
|
|
|
// EEVEE_lights_update(stl);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DRWShadingGroup *eevee_cube_shgroup(struct GPUShader *sh, DRWPass *pass, struct Batch *geom)
|
|
|
|
{
|
|
|
|
DRWShadingGroup *grp = DRW_shgroup_instance_create(sh, pass, geom);
|
|
|
|
|
|
|
|
for (int i = 0; i < 6; ++i)
|
2017-04-18 22:34:44 +02:00
|
|
|
DRW_shgroup_dynamic_call_add_empty(grp);
|
2017-04-18 12:50:09 +02:00
|
|
|
|
|
|
|
return grp;
|
2017-03-17 00:00:46 +01:00
|
|
|
}
|
|
|
|
|
2017-04-19 22:07:53 +02:00
|
|
|
static DRWShadingGroup *eevee_cube_shadow_shgroup(EEVEE_PassList *psl, EEVEE_StorageList *stl, struct Batch *geom, float (*obmat)[4])
|
|
|
|
{
|
|
|
|
DRWShadingGroup *grp = DRW_shgroup_instance_create(e_data.shadow_sh, psl->shadow_cube_pass, geom);
|
|
|
|
DRW_shgroup_uniform_block(grp, "shadow_render_block", stl->shadow_render_ubo, 0);
|
|
|
|
DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat);
|
|
|
|
|
|
|
|
for (int i = 0; i < 6; ++i)
|
|
|
|
DRW_shgroup_dynamic_call_add_empty(grp);
|
|
|
|
|
|
|
|
return grp;
|
|
|
|
}
|
|
|
|
|
2017-04-20 13:07:24 +02:00
|
|
|
static DRWShadingGroup *eevee_cascade_shadow_shgroup(EEVEE_PassList *psl, EEVEE_StorageList *stl, struct Batch *geom, float (*obmat)[4])
|
|
|
|
{
|
|
|
|
DRWShadingGroup *grp = DRW_shgroup_instance_create(e_data.shadow_sh, psl->shadow_cascade_pass, geom);
|
|
|
|
DRW_shgroup_uniform_block(grp, "shadow_render_block", stl->shadow_render_ubo, 0);
|
|
|
|
DRW_shgroup_uniform_mat4(grp, "ShadowModelMatrix", (float *)obmat);
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_CASCADE_NUM; ++i)
|
|
|
|
DRW_shgroup_dynamic_call_add_empty(grp);
|
|
|
|
|
|
|
|
return grp;
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:10:53 +02:00
|
|
|
static void EEVEE_cache_init(void *vedata)
|
2017-03-17 00:00:46 +01:00
|
|
|
{
|
2017-03-26 19:10:53 +02:00
|
|
|
EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
|
|
|
|
EEVEE_TextureList *txl = ((EEVEE_Data *)vedata)->txl;
|
|
|
|
EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
|
2017-03-17 00:00:46 +01:00
|
|
|
|
2017-03-26 20:13:34 +02:00
|
|
|
if (!stl->g_data) {
|
|
|
|
/* Alloc transient pointers */
|
|
|
|
stl->g_data = MEM_mallocN(sizeof(g_data), "g_data");
|
|
|
|
}
|
|
|
|
|
2017-04-10 12:06:17 +02:00
|
|
|
{
|
2017-04-19 22:07:53 +02:00
|
|
|
psl->shadow_cube_pass = DRW_pass_create("Shadow Cube Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
|
|
|
|
}
|
|
|
|
|
2017-04-20 13:07:24 +02:00
|
|
|
{
|
|
|
|
psl->shadow_cascade_pass = DRW_pass_create("Shadow Cascade Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
|
|
|
|
}
|
|
|
|
|
2017-04-19 22:07:53 +02:00
|
|
|
{
|
|
|
|
// psl->shadow_pass = DRW_pass_create("Shadow Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
|
|
|
|
// stl->g_data->shadow_shgrp = DRW_shgroup_create(e_data.shadow_sh, psl->shadow_pass);
|
|
|
|
// DRW_shgroup_uniform_mat4(stl->g_data->shadow_shgrp, "ShadowMatrix", (float *)stl->lamps->shadowmat);
|
|
|
|
// DRW_shgroup_uniform_int(stl->g_data->shadow_shgrp, "Layer", &stl->lamps->layer, 1);
|
2017-04-10 12:06:17 +02:00
|
|
|
}
|
|
|
|
|
2017-04-18 12:50:09 +02:00
|
|
|
{
|
|
|
|
psl->probe_background = DRW_pass_create("Probe Background Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_WRITE_COLOR);
|
|
|
|
|
|
|
|
struct Batch *geom = DRW_cache_fullscreen_quad_get();
|
|
|
|
DRWShadingGroup *grp = eevee_cube_shgroup(e_data.probe_sh, psl->probe_background, geom);
|
|
|
|
DRW_shgroup_uniform_int(grp, "Layer", &stl->probes->layer, 1);
|
|
|
|
DRW_shgroup_uniform_buffer(grp, "probeLatLong", &stl->probes->backgroundtex, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
psl->probe_prefilter = DRW_pass_create("Probe Filtering", DRW_STATE_WRITE_COLOR);
|
|
|
|
|
|
|
|
struct Batch *geom = DRW_cache_fullscreen_quad_get();
|
|
|
|
DRWShadingGroup *grp = eevee_cube_shgroup(e_data.probe_filter_sh, psl->probe_prefilter, geom);
|
|
|
|
DRW_shgroup_uniform_float(grp, "sampleCount", &stl->probes->samples_ct, 1);
|
|
|
|
DRW_shgroup_uniform_float(grp, "invSampleCount", &stl->probes->invsamples_ct, 1);
|
|
|
|
DRW_shgroup_uniform_float(grp, "roughnessSquared", &stl->probes->roughness, 1);
|
|
|
|
DRW_shgroup_uniform_float(grp, "lodFactor", &stl->probes->lodfactor, 1);
|
|
|
|
DRW_shgroup_uniform_float(grp, "lodMax", &stl->probes->lodmax, 1);
|
|
|
|
DRW_shgroup_uniform_int(grp, "Layer", &stl->probes->layer, 1);
|
|
|
|
DRW_shgroup_uniform_texture(grp, "texHammersley", e_data.hammersley, 0);
|
|
|
|
// DRW_shgroup_uniform_texture(grp, "texJitter", e_data.jitter, 1);
|
|
|
|
DRW_shgroup_uniform_texture(grp, "probeHdr", txl->probe_rt, 3);
|
|
|
|
}
|
|
|
|
|
2017-04-18 21:02:02 +02:00
|
|
|
{
|
|
|
|
psl->probe_sh_compute = DRW_pass_create("Probe SH Compute", DRW_STATE_WRITE_COLOR);
|
|
|
|
|
|
|
|
DRWShadingGroup *grp = DRW_shgroup_create(e_data.probe_spherical_harmonic_sh, psl->probe_sh_compute);
|
|
|
|
DRW_shgroup_uniform_int(grp, "probeSize", &stl->probes->shres, 1);
|
2017-04-18 21:25:57 +02:00
|
|
|
DRW_shgroup_uniform_float(grp, "lodBias", &stl->probes->lodfactor, 1);
|
2017-04-18 21:02:02 +02:00
|
|
|
DRW_shgroup_uniform_texture(grp, "probeHdr", txl->probe_rt, 0);
|
|
|
|
|
|
|
|
struct Batch *geom = DRW_cache_fullscreen_quad_get();
|
|
|
|
DRW_shgroup_call_add(grp, geom, NULL);
|
|
|
|
}
|
|
|
|
|
2017-03-17 00:00:46 +01:00
|
|
|
{
|
2017-03-18 01:55:41 +01:00
|
|
|
psl->depth_pass = DRW_pass_create("Depth Pass", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS);
|
2017-03-26 20:13:34 +02:00
|
|
|
stl->g_data->depth_shgrp = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass);
|
2017-03-18 01:55:41 +01:00
|
|
|
|
|
|
|
psl->depth_pass_cull = DRW_pass_create("Depth Pass Cull", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_LESS | DRW_STATE_CULL_BACK);
|
2017-03-26 20:13:34 +02:00
|
|
|
stl->g_data->depth_shgrp_cull = DRW_shgroup_create(e_data.depth_sh, psl->depth_pass_cull);
|
2017-03-18 01:55:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_EQUAL;
|
2017-03-17 00:00:46 +01:00
|
|
|
psl->pass = DRW_pass_create("Default Light Pass", state);
|
|
|
|
|
2017-03-26 20:13:34 +02:00
|
|
|
stl->g_data->default_lit_grp = DRW_shgroup_create(e_data.default_lit, psl->pass);
|
2017-04-10 12:06:17 +02:00
|
|
|
DRW_shgroup_uniform_block(stl->g_data->default_lit_grp, "light_block", stl->light_ubo, 0);
|
|
|
|
DRW_shgroup_uniform_block(stl->g_data->default_lit_grp, "shadow_block", stl->shadow_ubo, 1);
|
|
|
|
DRW_shgroup_uniform_int(stl->g_data->default_lit_grp, "light_count", &stl->lamps->num_light, 1);
|
2017-04-18 12:50:09 +02:00
|
|
|
DRW_shgroup_uniform_float(stl->g_data->default_lit_grp, "lodMax", &stl->probes->lodmax, 1);
|
2017-04-18 21:02:02 +02:00
|
|
|
DRW_shgroup_uniform_vec3(stl->g_data->default_lit_grp, "shCoefs[0]", (float *)stl->probes->shcoefs, 9);
|
2017-03-29 23:45:07 +02:00
|
|
|
DRW_shgroup_uniform_vec3(stl->g_data->default_lit_grp, "cameraPos", e_data.camera_pos, 1);
|
2017-04-03 11:04:42 +02:00
|
|
|
DRW_shgroup_uniform_texture(stl->g_data->default_lit_grp, "ltcMat", e_data.ltc_mat, 0);
|
2017-04-18 12:50:09 +02:00
|
|
|
DRW_shgroup_uniform_texture(stl->g_data->default_lit_grp, "brdfLut", e_data.brdf_lut, 1);
|
|
|
|
DRW_shgroup_uniform_texture(stl->g_data->default_lit_grp, "probeFiltered", txl->probe_pool, 2);
|
2017-04-10 12:06:17 +02:00
|
|
|
/* NOTE : Adding Shadow Map textures uniform in EEVEE_cache_finish */
|
2017-03-17 00:00:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Final pass : Map HDR color to LDR color.
|
|
|
|
* Write result to the default color buffer */
|
|
|
|
psl->tonemap = DRW_pass_create("Tone Mapping", DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND);
|
|
|
|
|
|
|
|
DRWShadingGroup *grp = DRW_shgroup_create(e_data.tonemap, psl->tonemap);
|
|
|
|
DRW_shgroup_uniform_buffer(grp, "hdrColorBuf", &txl->color, 0);
|
|
|
|
|
|
|
|
struct Batch *geom = DRW_cache_fullscreen_quad_get();
|
|
|
|
DRW_shgroup_call_add(grp, geom, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
EEVEE_lights_cache_init(stl);
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:10:53 +02:00
|
|
|
static void EEVEE_cache_populate(void *vedata, Object *ob)
|
2017-03-17 00:00:46 +01:00
|
|
|
{
|
2017-03-26 19:10:53 +02:00
|
|
|
EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
|
2017-04-19 22:07:53 +02:00
|
|
|
EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
|
2017-03-17 00:00:46 +01:00
|
|
|
|
|
|
|
if (ob->type == OB_MESH) {
|
2017-03-30 17:01:23 +02:00
|
|
|
IDProperty *ces_mode_ob = BKE_object_collection_engine_get(ob, COLLECTION_MODE_OBJECT, "");
|
2017-03-18 01:55:41 +01:00
|
|
|
bool do_cull = BKE_collection_engine_property_value_get_bool(ces_mode_ob, "show_backface_culling");
|
2017-04-12 18:08:07 +10:00
|
|
|
struct Batch *geom = DRW_cache_mesh_surface_get(ob);
|
2017-03-17 00:00:46 +01:00
|
|
|
|
2017-03-18 01:55:41 +01:00
|
|
|
/* Depth Prepass */
|
2017-04-03 11:53:09 +02:00
|
|
|
DRW_shgroup_call_add((do_cull) ? stl->g_data->depth_shgrp_cull : stl->g_data->depth_shgrp, geom, ob->obmat);
|
2017-03-18 01:55:41 +01:00
|
|
|
|
2017-03-26 20:13:34 +02:00
|
|
|
DRW_shgroup_call_add(stl->g_data->default_lit_grp, geom, ob->obmat);
|
2017-04-19 22:07:53 +02:00
|
|
|
// DRW_shgroup_call_add(stl->g_data->shadow_shgrp, geom, ob->obmat);
|
2017-04-20 13:07:24 +02:00
|
|
|
eevee_cascade_shadow_shgroup(psl, stl, geom, ob->obmat);
|
2017-04-19 22:07:53 +02:00
|
|
|
eevee_cube_shadow_shgroup(psl, stl, geom, ob->obmat);
|
2017-03-17 00:00:46 +01:00
|
|
|
}
|
|
|
|
else if (ob->type == OB_LAMP) {
|
|
|
|
EEVEE_lights_cache_add(stl, ob);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-26 19:10:53 +02:00
|
|
|
static void EEVEE_cache_finish(void *vedata)
|
2017-03-17 00:00:46 +01:00
|
|
|
{
|
2017-03-26 19:10:53 +02:00
|
|
|
EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
|
2017-04-10 12:06:17 +02:00
|
|
|
EEVEE_TextureList *txl = ((EEVEE_Data *)vedata)->txl;
|
|
|
|
EEVEE_FramebufferList *fbl = ((EEVEE_Data *)vedata)->fbl;
|
2017-03-17 00:00:46 +01:00
|
|
|
|
2017-04-10 12:06:17 +02:00
|
|
|
EEVEE_lights_cache_finish(stl, txl, fbl);
|
|
|
|
|
|
|
|
/* Shadows binding */
|
2017-04-18 12:50:09 +02:00
|
|
|
DRW_shgroup_uniform_texture(stl->g_data->default_lit_grp, "shadowMaps", txl->shadow_depth_map_pool, 4);
|
|
|
|
DRW_shgroup_uniform_texture(stl->g_data->default_lit_grp, "shadowCubes", txl->shadow_depth_cube_pool, 5);
|
2017-04-20 13:07:24 +02:00
|
|
|
DRW_shgroup_uniform_texture(stl->g_data->default_lit_grp, "shadowCascades", txl->shadow_depth_cascade_pool, 6);
|
2017-03-17 00:00:46 +01:00
|
|
|
}
|
|
|
|
|
2017-03-26 19:10:53 +02:00
|
|
|
static void EEVEE_draw_scene(void *vedata)
|
2017-03-17 00:00:46 +01:00
|
|
|
{
|
2017-03-26 19:10:53 +02:00
|
|
|
EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
|
|
|
|
EEVEE_FramebufferList *fbl = ((EEVEE_Data *)vedata)->fbl;
|
2017-03-17 00:00:46 +01:00
|
|
|
|
|
|
|
/* Default framebuffer and texture */
|
|
|
|
DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
|
|
|
|
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
|
|
|
|
|
2017-04-18 12:50:09 +02:00
|
|
|
/* Refresh Probes */
|
|
|
|
EEVEE_refresh_probe((EEVEE_Data *)vedata);
|
|
|
|
|
2017-04-10 12:06:17 +02:00
|
|
|
/* Refresh shadows */
|
|
|
|
EEVEE_draw_shadows((EEVEE_Data *)vedata);
|
|
|
|
|
2017-03-17 00:00:46 +01:00
|
|
|
/* Attach depth to the hdr buffer and bind it */
|
|
|
|
DRW_framebuffer_texture_detach(dtxl->depth);
|
2017-04-18 11:28:11 +02:00
|
|
|
DRW_framebuffer_texture_attach(fbl->main, dtxl->depth, 0, 0);
|
2017-03-17 00:00:46 +01:00
|
|
|
DRW_framebuffer_bind(fbl->main);
|
|
|
|
|
|
|
|
/* Clear Depth */
|
|
|
|
/* TODO do background */
|
2017-04-18 23:44:59 +02:00
|
|
|
// float clearcol[4] = {0.0f, 0.0f, 0.0f, 1.0f};
|
|
|
|
// DRW_framebuffer_clear(true, true, false, clearcol, 1.0f);
|
|
|
|
DRW_draw_background();
|
2017-03-17 00:00:46 +01:00
|
|
|
|
2017-03-18 01:55:41 +01:00
|
|
|
DRW_draw_pass(psl->depth_pass);
|
|
|
|
DRW_draw_pass(psl->depth_pass_cull);
|
2017-03-17 00:00:46 +01:00
|
|
|
DRW_draw_pass(psl->pass);
|
|
|
|
|
|
|
|
/* Restore default framebuffer */
|
|
|
|
DRW_framebuffer_texture_detach(dtxl->depth);
|
2017-04-18 11:28:11 +02:00
|
|
|
DRW_framebuffer_texture_attach(dfbl->default_fb, dtxl->depth, 0, 0);
|
2017-03-17 00:00:46 +01:00
|
|
|
DRW_framebuffer_bind(dfbl->default_fb);
|
|
|
|
|
|
|
|
DRW_draw_pass(psl->tonemap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EEVEE_engine_free(void)
|
|
|
|
{
|
2017-04-13 13:30:53 +10:00
|
|
|
DRW_SHADER_FREE_SAFE(e_data.default_lit);
|
|
|
|
DRW_SHADER_FREE_SAFE(e_data.shadow_sh);
|
2017-04-18 12:50:09 +02:00
|
|
|
DRW_SHADER_FREE_SAFE(e_data.probe_sh);
|
|
|
|
DRW_SHADER_FREE_SAFE(e_data.probe_filter_sh);
|
2017-04-18 21:02:02 +02:00
|
|
|
DRW_SHADER_FREE_SAFE(e_data.probe_spherical_harmonic_sh);
|
2017-04-13 13:30:53 +10:00
|
|
|
DRW_SHADER_FREE_SAFE(e_data.tonemap);
|
|
|
|
DRW_TEXTURE_FREE_SAFE(e_data.ltc_mat);
|
2017-04-18 12:50:09 +02:00
|
|
|
DRW_TEXTURE_FREE_SAFE(e_data.brdf_lut);
|
|
|
|
DRW_TEXTURE_FREE_SAFE(e_data.hammersley);
|
|
|
|
DRW_TEXTURE_FREE_SAFE(e_data.jitter);
|
2017-03-17 00:00:46 +01:00
|
|
|
}
|
|
|
|
|
2017-03-30 17:01:23 +02:00
|
|
|
static void EEVEE_collection_settings_create(RenderEngine *UNUSED(engine), IDProperty *props)
|
2017-03-17 00:00:46 +01:00
|
|
|
{
|
2017-03-30 17:01:23 +02:00
|
|
|
BLI_assert(props &&
|
|
|
|
props->type == IDP_GROUP &&
|
|
|
|
props->subtype == IDP_GROUP_SUB_ENGINE_RENDER);
|
|
|
|
// BKE_collection_engine_property_add_int(props, "high_quality_sphere_lamps", false);
|
2017-03-17 00:00:46 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 19:49:19 +10:00
|
|
|
static const DrawEngineDataSize EEVEE_data_size = DRW_VIEWPORT_DATA_SIZE(EEVEE_Data);
|
|
|
|
|
2017-03-17 00:00:46 +01:00
|
|
|
DrawEngineType draw_engine_eevee_type = {
|
|
|
|
NULL, NULL,
|
2017-03-26 19:10:53 +02:00
|
|
|
N_("Eevee"),
|
2017-04-12 19:49:19 +10:00
|
|
|
&EEVEE_data_size,
|
2017-03-17 00:00:46 +01:00
|
|
|
&EEVEE_engine_init,
|
|
|
|
&EEVEE_engine_free,
|
|
|
|
&EEVEE_cache_init,
|
|
|
|
&EEVEE_cache_populate,
|
|
|
|
&EEVEE_cache_finish,
|
|
|
|
&EEVEE_draw_scene,
|
|
|
|
NULL//&EEVEE_draw_scene
|
|
|
|
};
|
|
|
|
|
|
|
|
RenderEngineType viewport_eevee_type = {
|
|
|
|
NULL, NULL,
|
|
|
|
EEVEE_ENGINE, N_("Eevee"), RE_INTERNAL | RE_USE_OGL_PIPELINE,
|
|
|
|
NULL, NULL, NULL, NULL, NULL, NULL, &EEVEE_collection_settings_create,
|
|
|
|
&draw_engine_eevee_type,
|
|
|
|
{NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#undef EEVEE_ENGINE
|