diff options
author | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2020-02-06 15:05:45 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2020-02-06 15:36:39 +0300 |
commit | 62ca9bcd0a7ff6fe52e359521369fc8b063d082a (patch) | |
tree | 7aae08f2a67b3ca12e01e524e3759ae226adf936 /source/blender | |
parent | dd00e8ecd642cafc6294ec7f0f31c5e0e0fe9870 (diff) |
Cleanup: refactor default materials and shader nodes
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenkernel/BKE_material.h | 19 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/gpencil.c | 2 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/material.c | 156 | ||||
-rw-r--r-- | source/blender/draw/engines/eevee/eevee_materials.c | 4 | ||||
-rw-r--r-- | source/blender/editors/gpencil/drawgpencil.c | 4 | ||||
-rw-r--r-- | source/blender/editors/space_node/node_edit.c | 115 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_codegen.c | 8 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_material.c | 4 |
8 files changed, 193 insertions, 119 deletions
diff --git a/source/blender/blenkernel/BKE_material.h b/source/blender/blenkernel/BKE_material.h index 0fd08dc3246..4bec0b1983e 100644 --- a/source/blender/blenkernel/BKE_material.h +++ b/source/blender/blenkernel/BKE_material.h @@ -35,10 +35,13 @@ struct Object; struct Scene; struct bNode; -/* materials */ +/* Module */ void BKE_materials_init(void); void BKE_materials_exit(void); + +/* Materials */ + void BKE_material_free(struct Material *ma); void BKE_object_materials_test(struct Main *bmain, struct Object *ob, struct ID *id); void BKE_objects_materials_test_all(struct Main *bmain, struct ID *id); @@ -47,7 +50,6 @@ void BKE_material_resize_object(struct Main *bmain, const short totcol, bool do_id_user); void BKE_material_init(struct Material *ma); -void BKE_material_gpencil_init(struct Material *ma); void BKE_material_remap_object(struct Object *ob, const unsigned int *remap); void BKE_material_remap_object_calc(struct Object *ob_dst, struct Object *ob_src, @@ -97,7 +99,6 @@ bool BKE_object_material_slot_remove(struct Main *bmain, struct Object *ob); bool BKE_object_material_slot_used(struct ID *id, short actcol); struct Material *BKE_gpencil_material(struct Object *ob, short act); -struct Material *BKE_gpencil_material_default(void); struct MaterialGPencilStyle *BKE_gpencil_material_settings(struct Object *ob, short act); void BKE_texpaint_slot_refresh_cache(struct Scene *scene, struct Material *ma); @@ -122,15 +123,21 @@ void BKE_material_copybuf_free(void); void BKE_material_copybuf_copy(struct Main *bmain, struct Material *ma); void BKE_material_copybuf_paste(struct Main *bmain, struct Material *ma); +/* Default Materials */ + +struct Material *BKE_material_default_empty(void); +struct Material *BKE_material_default_surface(void); +struct Material *BKE_material_default_volume(void); +struct Material *BKE_material_default_gpencil(void); + +void BKE_material_defaults_free_gpu(void); + /* Dependency graph evaluation. */ struct Depsgraph; void BKE_material_eval(struct Depsgraph *depsgraph, struct Material *material); -extern struct Material defmaterial; -extern struct Material defgpencil_material; - #ifdef __cplusplus } #endif diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c index e7242ca11a4..db137d26005 100644 --- a/source/blender/blenkernel/intern/gpencil.c +++ b/source/blender/blenkernel/intern/gpencil.c @@ -1233,7 +1233,7 @@ Material *BKE_gpencil_object_material_ensure_from_active_input_material(Object * return ma; } - return &defgpencil_material; + return BKE_material_default_gpencil(); } /* Get active color, and add all default settings if we don't find anything */ diff --git a/source/blender/blenkernel/intern/material.c b/source/blender/blenkernel/intern/material.c index 8159cc03414..84b3ea27025 100644 --- a/source/blender/blenkernel/intern/material.c +++ b/source/blender/blenkernel/intern/material.c @@ -70,25 +70,10 @@ #include "GPU_material.h" -/* used in UI and render */ -Material defmaterial; -Material defgpencil_material; +#include "NOD_shader.h" static CLG_LogRef LOG = {"bke.material"}; -/* called on startup, creator.c */ -void BKE_materials_init(void) -{ - BKE_material_init(&defmaterial); - BKE_material_gpencil_init(&defgpencil_material); -} - -/* Free the GPencil data of the default material, creator.c */ -void BKE_materials_exit(void) -{ - MEM_SAFE_FREE(defgpencil_material.gp_style); -} - /** Free (or release) any data used by this material (does not free the material itself). */ void BKE_material_free(Material *ma) { @@ -140,16 +125,6 @@ void BKE_material_init(Material *ma) MEMCPY_STRUCT_AFTER(ma, DNA_struct_default_get(Material), id); } -void BKE_material_gpencil_init(Material *ma) -{ - BKE_material_init(ma); - - /* grease pencil settings */ - strcpy(ma->id.name, "MADefault GPencil"); - BKE_gpencil_material_attr_init(ma); - add_v3_fl(&ma->gp_style->stroke_rgba[0], 0.6f); -} - Material *BKE_material_add(Main *bmain, const char *name) { Material *ma; @@ -594,15 +569,10 @@ Material *BKE_gpencil_material(Object *ob, short act) return ma; } else { - return &defgpencil_material; + return BKE_material_default_gpencil(); } } -struct Material *BKE_gpencil_material_default(void) -{ - return &defgpencil_material; -} - MaterialGPencilStyle *BKE_gpencil_material_settings(Object *ob, short act) { Material *ma = BKE_object_material_get(ob, act); @@ -614,7 +584,7 @@ MaterialGPencilStyle *BKE_gpencil_material_settings(Object *ob, short act) return ma->gp_style; } else { - return defgpencil_material.gp_style; + return BKE_material_default_gpencil()->gp_style; } } @@ -1610,3 +1580,123 @@ void BKE_material_eval(struct Depsgraph *depsgraph, Material *material) DEG_debug_print_eval(depsgraph, __func__, material->id.name, material); GPU_material_free(&material->gpumaterial); } + +/* Default Materials + * + * Used for rendering when objects have no materials assigned, and initializing + * default shader nodes. */ + +static Material default_material_empty; +static Material default_material_surface; +static Material default_material_volume; +static Material default_material_gpencil; + +static Material *default_materials[] = {&default_material_empty, + &default_material_surface, + &default_material_volume, + &default_material_gpencil, + NULL}; + +static void material_default_gpencil_init(Material *ma) +{ + strcpy(ma->id.name, "MADefault GPencil"); + BKE_gpencil_material_attr_init(ma); + add_v3_fl(&ma->gp_style->stroke_rgba[0], 0.6f); +} + +static void material_default_surface_init(Material *ma) +{ + bNodeTree *ntree = ntreeAddTree(NULL, "Shader Nodetree", ntreeType_Shader->idname); + ma->nodetree = ntree; + + bNode *principled = nodeAddStaticNode(NULL, ntree, SH_NODE_BSDF_PRINCIPLED); + bNodeSocket *base_color = nodeFindSocket(principled, SOCK_IN, "Base Color"); + copy_v3_v3(((bNodeSocketValueRGBA *)base_color->default_value)->value, &ma->r); + + bNode *output = nodeAddStaticNode(NULL, ntree, SH_NODE_OUTPUT_MATERIAL); + + nodeAddLink(ntree, + principled, + nodeFindSocket(principled, SOCK_OUT, "BSDF"), + output, + nodeFindSocket(output, SOCK_IN, "Surface")); + + principled->locx = 10.0f; + principled->locy = 300.0f; + output->locx = 300.0f; + output->locy = 300.0f; + + nodeSetActive(ntree, output); +} + +static void material_default_volume_init(Material *ma) +{ + bNodeTree *ntree = ntreeAddTree(NULL, "Shader Nodetree", ntreeType_Shader->idname); + ma->nodetree = ntree; + + bNode *principled = nodeAddStaticNode(NULL, ntree, SH_NODE_VOLUME_PRINCIPLED); + bNode *output = nodeAddStaticNode(NULL, ntree, SH_NODE_OUTPUT_MATERIAL); + + nodeAddLink(ntree, + principled, + nodeFindSocket(principled, SOCK_OUT, "Volume"), + output, + nodeFindSocket(output, SOCK_IN, "Volume")); + + principled->locx = 10.0f; + principled->locy = 300.0f; + output->locx = 300.0f; + output->locy = 300.0f; + + nodeSetActive(ntree, output); +} + +Material *BKE_material_default_empty(void) +{ + return &default_material_empty; +} + +Material *BKE_material_default_surface(void) +{ + return &default_material_surface; +} + +Material *BKE_material_default_volume(void) +{ + return &default_material_volume; +} + +Material *BKE_material_default_gpencil(void) +{ + return &default_material_gpencil; +} + +void BKE_material_defaults_free_gpu(void) +{ + for (int i = 0; default_materials[i]; i++) { + Material *ma = default_materials[i]; + if (ma->gpumaterial.first) { + GPU_material_free(&ma->gpumaterial); + } + } +} + +/* Module functions called on startup and exit. */ + +void BKE_materials_init(void) +{ + for (int i = 0; default_materials[i]; i++) { + BKE_material_init(default_materials[i]); + } + + material_default_surface_init(&default_material_surface); + material_default_volume_init(&default_material_volume); + material_default_gpencil_init(&default_material_gpencil); +} + +void BKE_materials_exit(void) +{ + for (int i = 0; default_materials[i]; i++) { + BKE_material_free(default_materials[i]); + } +} diff --git a/source/blender/draw/engines/eevee/eevee_materials.c b/source/blender/draw/engines/eevee/eevee_materials.c index 6da7621c6c7..c114ca57f05 100644 --- a/source/blender/draw/engines/eevee/eevee_materials.c +++ b/source/blender/draw/engines/eevee/eevee_materials.c @@ -1430,12 +1430,12 @@ static void material_transparent(Material *ma, DRW_shgroup_state_enable(*shgrp, cur_state); } -/* Return correct material or &defmaterial if slot is empty. */ +/* Return correct material or empty default material if slot is empty. */ BLI_INLINE Material *eevee_object_material_get(Object *ob, int slot) { Material *ma = BKE_object_material_get(ob, slot + 1); if (ma == NULL) { - ma = &defmaterial; + ma = BKE_material_default_empty(); } return ma; } diff --git a/source/blender/editors/gpencil/drawgpencil.c b/source/blender/editors/gpencil/drawgpencil.c index 3fb2e1135e8..9fef1e4948b 100644 --- a/source/blender/editors/gpencil/drawgpencil.c +++ b/source/blender/editors/gpencil/drawgpencil.c @@ -430,7 +430,7 @@ static void gp_draw_stroke_fill(bGPdata *gpd, BLI_assert(gps->totpoints >= 3); const bool use_mat = (gpd->mat != NULL); - Material *ma = (use_mat) ? gpd->mat[gps->mat_nr] : BKE_gpencil_material_default(); + Material *ma = (use_mat) ? gpd->mat[gps->mat_nr] : BKE_material_default_gpencil(); MaterialGPencilStyle *gp_style = (ma) ? ma->gp_style : NULL; /* Calculate triangles cache for filling area (must be done only after changes) */ @@ -884,7 +884,7 @@ static void gp_draw_strokes(tGPDdraw *tgpw) continue; } /* check if the color is visible */ - Material *ma = (use_mat) ? tgpw->gpd->mat[gps->mat_nr] : BKE_gpencil_material_default(); + Material *ma = (use_mat) ? tgpw->gpd->mat[gps->mat_nr] : BKE_material_default_gpencil(); MaterialGPencilStyle *gp_style = (ma) ? ma->gp_style : NULL; if ((gp_style == NULL) || (gp_style->flag & GP_STYLE_COLOR_HIDE) || diff --git a/source/blender/editors/space_node/node_edit.c b/source/blender/editors/space_node/node_edit.c index 9623d89e030..045e4d28df6 100644 --- a/source/blender/editors/space_node/node_edit.c +++ b/source/blender/editors/space_node/node_edit.c @@ -37,6 +37,7 @@ #include "BKE_image.h" #include "BKE_library.h" #include "BKE_main.h" +#include "BKE_material.h" #include "BKE_node.h" #include "BKE_report.h" #include "BKE_scene.h" @@ -441,81 +442,59 @@ bool ED_node_is_texture(struct SpaceNode *snode) /* called from shading buttons or header */ void ED_node_shader_default(const bContext *C, ID *id) { - bNode *in, *out; - bNodeSocket *fromsock, *tosock, *sock; - bNodeTree *ntree; - int output_type, shader_type; - float color[4] = {0.0f, 0.0f, 0.0f, 1.0f}, strength = 1.0f; - - ntree = ntreeAddTree(NULL, "Shader Nodetree", ntreeType_Shader->idname); - - switch (GS(id->name)) { - case ID_MA: { - Material *ma = (Material *)id; - ma->nodetree = ntree; - - output_type = SH_NODE_OUTPUT_MATERIAL; - shader_type = SH_NODE_BSDF_PRINCIPLED; + Main *bmain = CTX_data_main(C); - copy_v3_v3(color, &ma->r); - strength = 0.0f; - break; + if (GS(id->name) == ID_MA) { + /* Materials */ + Material *ma = (Material *)id; + Material *ma_default = BKE_material_default_surface(); + ma->nodetree = ntreeCopyTree(bmain, ma_default->nodetree); + ntreeUpdateTree(bmain, ma->nodetree); + } + else if (ELEM(GS(id->name), ID_WO, ID_LA)) { + /* Emission */ + bNodeTree *ntree = ntreeAddTree(NULL, "Shader Nodetree", ntreeType_Shader->idname); + bNode *shader, *output; + + if (GS(id->name) == ID_WO) { + World *world = (World *)id; + world->nodetree = ntree; + + shader = nodeAddStaticNode(NULL, ntree, SH_NODE_BACKGROUND); + output = nodeAddStaticNode(NULL, ntree, SH_NODE_OUTPUT_WORLD); + nodeAddLink(ntree, + shader, + nodeFindSocket(shader, SOCK_OUT, "Background"), + output, + nodeFindSocket(output, SOCK_IN, "Surface")); + + bNodeSocket *color_sock = nodeFindSocket(shader, SOCK_IN, "Color"); + copy_v3_v3(((bNodeSocketValueRGBA *)color_sock->default_value)->value, &world->horr); } - case ID_WO: { - World *wo = (World *)id; - wo->nodetree = ntree; - - output_type = SH_NODE_OUTPUT_WORLD; - shader_type = SH_NODE_BACKGROUND; + else { + Light *light = (Light *)id; + light->nodetree = ntree; - copy_v3_v3(color, &wo->horr); - strength = 1.0f; - break; + shader = nodeAddStaticNode(NULL, ntree, SH_NODE_EMISSION); + output = nodeAddStaticNode(NULL, ntree, SH_NODE_OUTPUT_LIGHT); + nodeAddLink(ntree, + shader, + nodeFindSocket(shader, SOCK_OUT, "Emission"), + output, + nodeFindSocket(output, SOCK_IN, "Surface")); } - case ID_LA: { - Light *la = (Light *)id; - la->nodetree = ntree; - output_type = SH_NODE_OUTPUT_LIGHT; - shader_type = SH_NODE_EMISSION; - - copy_v3_fl3(color, 1.0f, 1.0f, 1.0f); - strength = 1.0f; - break; - } - default: - printf("ED_node_shader_default called on wrong ID type.\n"); - return; + shader->locx = 10.0f; + shader->locy = 300.0f; + output->locx = 300.0f; + output->locy = 300.0f; + nodeSetActive(ntree, output); + ntreeUpdateTree(bmain, ntree); } - - out = nodeAddStaticNode(C, ntree, output_type); - out->locx = 300.0f; - out->locy = 300.0f; - - in = nodeAddStaticNode(C, ntree, shader_type); - in->locx = 10.0f; - in->locy = 300.0f; - nodeSetActive(ntree, in); - - /* only a link from color to color */ - fromsock = in->outputs.first; - tosock = out->inputs.first; - nodeAddLink(ntree, in, fromsock, out, tosock); - - /* default values */ - PointerRNA sockptr; - sock = in->inputs.first; - RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock, &sockptr); - - RNA_float_set_array(&sockptr, "default_value", color); - - if (strength != 0.0f) { - sock = in->inputs.last; - RNA_pointer_create((ID *)ntree, &RNA_NodeSocket, sock, &sockptr); - RNA_float_set(&sockptr, "default_value", strength); + else { + printf("ED_node_shader_default called on wrong ID type.\n"); + return; } - - ntreeUpdateTree(CTX_data_main(C), ntree); } /* assumes nothing being done in ntree yet, sets the default in/out node */ diff --git a/source/blender/gpu/intern/gpu_codegen.c b/source/blender/gpu/intern/gpu_codegen.c index a7dd34a5f96..3f7a5962efc 100644 --- a/source/blender/gpu/intern/gpu_codegen.c +++ b/source/blender/gpu/intern/gpu_codegen.c @@ -38,6 +38,8 @@ #include "BLI_ghash.h" #include "BLI_threads.h" +#include "BKE_material.h" + #include "PIL_time.h" #include "GPU_extensions.h" @@ -384,11 +386,7 @@ void gpu_codegen_init(void) void gpu_codegen_exit(void) { - extern Material defmaterial; /* render module abuse... */ - - if (defmaterial.gpumaterial.first) { - GPU_material_free(&defmaterial.gpumaterial); - } + BKE_material_defaults_free_gpu(); if (FUNCTION_HASH) { BLI_ghash_free(FUNCTION_HASH, NULL, MEM_freeN); diff --git a/source/blender/gpu/intern/gpu_material.c b/source/blender/gpu/intern/gpu_material.c index 2e3a0a8df27..2f52aea2c91 100644 --- a/source/blender/gpu/intern/gpu_material.c +++ b/source/blender/gpu/intern/gpu_material.c @@ -40,6 +40,7 @@ #include "BLI_ghash.h" #include "BKE_main.h" +#include "BKE_material.h" #include "BKE_node.h" #include "BKE_scene.h" @@ -778,7 +779,6 @@ void GPU_materials_free(Main *bmain) { Material *ma; World *wo; - extern Material defmaterial; for (ma = bmain->materials.first; ma; ma = ma->id.next) { GPU_material_free(&ma->gpumaterial); @@ -788,5 +788,5 @@ void GPU_materials_free(Main *bmain) GPU_material_free(&wo->gpumaterial); } - GPU_material_free(&defmaterial.gpumaterial); + BKE_material_defaults_free_gpu(); } |