Changeset View
Changeset View
Standalone View
Standalone View
intern/cycles/kernel/osl/osl_shader.cpp
| Show All 11 Lines | |||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| * See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | ||||
| * limitations under the License. | * limitations under the License. | ||||
| */ | */ | ||||
| #include <OSL/oslexec.h> | #include <OSL/oslexec.h> | ||||
| // clang-format off | // clang-format off | ||||
| #include "kernel/kernel_compat_cpu.h" | #include "kernel/device/cpu/compat.h" | ||||
| #include "kernel/device/cpu/globals.h" | |||||
| #include "kernel/kernel_montecarlo.h" | #include "kernel/kernel_montecarlo.h" | ||||
| #include "kernel/kernel_types.h" | #include "kernel/kernel_types.h" | ||||
| #include "kernel/split/kernel_split_data_types.h" | |||||
| #include "kernel/kernel_globals.h" | |||||
| #include "kernel/geom/geom_object.h" | #include "kernel/geom/geom_object.h" | ||||
| #include "kernel/integrator/integrator_state.h" | |||||
| #include "kernel/osl/osl_closures.h" | #include "kernel/osl/osl_closures.h" | ||||
| #include "kernel/osl/osl_globals.h" | #include "kernel/osl/osl_globals.h" | ||||
| #include "kernel/osl/osl_services.h" | #include "kernel/osl/osl_services.h" | ||||
| #include "kernel/osl/osl_shader.h" | #include "kernel/osl/osl_shader.h" | ||||
| // clang-format on | // clang-format on | ||||
| #include "util/util_foreach.h" | #include "util/util_foreach.h" | ||||
| #include "render/attribute.h" | #include "render/attribute.h" | ||||
| CCL_NAMESPACE_BEGIN | CCL_NAMESPACE_BEGIN | ||||
| /* Threads */ | /* Threads */ | ||||
| void OSLShader::thread_init(KernelGlobals *kg, | void OSLShader::thread_init(KernelGlobals *kg, OSLGlobals *osl_globals) | ||||
| KernelGlobals *kernel_globals, | |||||
| OSLGlobals *osl_globals) | |||||
| { | { | ||||
| /* no osl used? */ | /* no osl used? */ | ||||
| if (!osl_globals->use) { | if (!osl_globals->use) { | ||||
| kg->osl = NULL; | kg->osl = NULL; | ||||
| return; | return; | ||||
| } | } | ||||
| /* Per thread kernel data init. */ | /* Per thread kernel data init. */ | ||||
| Show All 29 Lines | void OSLShader::thread_free(KernelGlobals *kg) | ||||
| kg->osl = NULL; | kg->osl = NULL; | ||||
| kg->osl_ss = NULL; | kg->osl_ss = NULL; | ||||
| kg->osl_tdata = NULL; | kg->osl_tdata = NULL; | ||||
| } | } | ||||
| /* Globals */ | /* Globals */ | ||||
| static void shaderdata_to_shaderglobals( | static void shaderdata_to_shaderglobals(const KernelGlobals *kg, | ||||
| KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag, OSLThreadData *tdata) | ShaderData *sd, | ||||
| const IntegratorState *state, | |||||
| int path_flag, | |||||
| OSLThreadData *tdata) | |||||
| { | { | ||||
| OSL::ShaderGlobals *globals = &tdata->globals; | OSL::ShaderGlobals *globals = &tdata->globals; | ||||
| /* copy from shader data to shader globals */ | /* copy from shader data to shader globals */ | ||||
| globals->P = TO_VEC3(sd->P); | globals->P = TO_VEC3(sd->P); | ||||
| globals->dPdx = TO_VEC3(sd->dP.dx); | globals->dPdx = TO_VEC3(sd->dP.dx); | ||||
| globals->dPdy = TO_VEC3(sd->dP.dy); | globals->dPdy = TO_VEC3(sd->dP.dy); | ||||
| globals->I = TO_VEC3(sd->I); | globals->I = TO_VEC3(sd->I); | ||||
| Show All 26 Lines | static void shaderdata_to_shaderglobals(const KernelGlobals *kg, | ||||
| /* must be set to NULL before execute */ | /* must be set to NULL before execute */ | ||||
| globals->Ci = NULL; | globals->Ci = NULL; | ||||
| /* clear trace data */ | /* clear trace data */ | ||||
| tdata->tracedata.init = false; | tdata->tracedata.init = false; | ||||
| /* Used by render-services. */ | /* Used by render-services. */ | ||||
| sd->osl_globals = kg; | sd->osl_globals = kg; | ||||
| /* TODO */ | |||||
| #if 0 | |||||
| sd->osl_path_state = state; | sd->osl_path_state = state; | ||||
| #endif | |||||
| } | } | ||||
| /* Surface */ | /* Surface */ | ||||
| static void flatten_surface_closure_tree(ShaderData *sd, | static void flatten_surface_closure_tree(ShaderData *sd, | ||||
| int path_flag, | int path_flag, | ||||
| const OSL::ClosureColor *closure, | const OSL::ClosureColor *closure, | ||||
| float3 weight = make_float3(1.0f, 1.0f, 1.0f)) | float3 weight = make_float3(1.0f, 1.0f, 1.0f)) | ||||
| Show All 23 Lines | |||||
| #endif | #endif | ||||
| prim->setup(sd, path_flag, weight); | prim->setup(sd, path_flag, weight); | ||||
| } | } | ||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| void OSLShader::eval_surface(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag) | void OSLShader::eval_surface(const KernelGlobals *kg, | ||||
| const IntegratorState *state, | |||||
| ShaderData *sd, | |||||
| int path_flag) | |||||
| { | { | ||||
| /* setup shader globals from shader data */ | /* setup shader globals from shader data */ | ||||
| OSLThreadData *tdata = kg->osl_tdata; | OSLThreadData *tdata = kg->osl_tdata; | ||||
| shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata); | shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata); | ||||
| /* execute shader for this point */ | /* execute shader for this point */ | ||||
| OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss; | OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss; | ||||
| OSL::ShaderGlobals *globals = &tdata->globals; | OSL::ShaderGlobals *globals = &tdata->globals; | ||||
| ▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | |||||
| #endif | #endif | ||||
| prim->setup(sd, 0, weight); | prim->setup(sd, 0, weight); | ||||
| } | } | ||||
| break; | break; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| void OSLShader::eval_background(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag) | void OSLShader::eval_background(const KernelGlobals *kg, | ||||
| const IntegratorState *state, | |||||
| ShaderData *sd, | |||||
| int path_flag) | |||||
| { | { | ||||
| /* setup shader globals from shader data */ | /* setup shader globals from shader data */ | ||||
| OSLThreadData *tdata = kg->osl_tdata; | OSLThreadData *tdata = kg->osl_tdata; | ||||
| shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata); | shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata); | ||||
| /* execute shader for this point */ | /* execute shader for this point */ | ||||
| OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss; | OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss; | ||||
| OSL::ShaderGlobals *globals = &tdata->globals; | OSL::ShaderGlobals *globals = &tdata->globals; | ||||
| Show All 38 Lines | #ifdef OSL_SUPPORTS_WEIGHTED_CLOSURE_COMPONENTS | ||||
| weight = weight * TO_FLOAT3(comp->w); | weight = weight * TO_FLOAT3(comp->w); | ||||
| #endif | #endif | ||||
| prim->setup(sd, 0, weight); | prim->setup(sd, 0, weight); | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| void OSLShader::eval_volume(KernelGlobals *kg, ShaderData *sd, PathState *state, int path_flag) | void OSLShader::eval_volume(const KernelGlobals *kg, | ||||
| const IntegratorState *state, | |||||
| ShaderData *sd, | |||||
| int path_flag) | |||||
| { | { | ||||
| /* setup shader globals from shader data */ | /* setup shader globals from shader data */ | ||||
| OSLThreadData *tdata = kg->osl_tdata; | OSLThreadData *tdata = kg->osl_tdata; | ||||
| shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata); | shaderdata_to_shaderglobals(kg, sd, state, path_flag, tdata); | ||||
| /* execute shader */ | /* execute shader */ | ||||
| OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss; | OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss; | ||||
| OSL::ShaderGlobals *globals = &tdata->globals; | OSL::ShaderGlobals *globals = &tdata->globals; | ||||
| OSL::ShadingContext *octx = tdata->context; | OSL::ShadingContext *octx = tdata->context; | ||||
| int shader = sd->shader & SHADER_MASK; | int shader = sd->shader & SHADER_MASK; | ||||
| if (kg->osl->volume_state[shader]) { | if (kg->osl->volume_state[shader]) { | ||||
| ss->execute(octx, *(kg->osl->volume_state[shader]), *globals); | ss->execute(octx, *(kg->osl->volume_state[shader]), *globals); | ||||
| } | } | ||||
| /* flatten closure tree */ | /* flatten closure tree */ | ||||
| if (globals->Ci) | if (globals->Ci) | ||||
| flatten_volume_closure_tree(sd, globals->Ci); | flatten_volume_closure_tree(sd, globals->Ci); | ||||
| } | } | ||||
| /* Displacement */ | /* Displacement */ | ||||
| void OSLShader::eval_displacement(KernelGlobals *kg, ShaderData *sd, PathState *state) | void OSLShader::eval_displacement(const KernelGlobals *kg, | ||||
| const IntegratorState *state, | |||||
| ShaderData *sd) | |||||
| { | { | ||||
| /* setup shader globals from shader data */ | /* setup shader globals from shader data */ | ||||
| OSLThreadData *tdata = kg->osl_tdata; | OSLThreadData *tdata = kg->osl_tdata; | ||||
| shaderdata_to_shaderglobals(kg, sd, state, 0, tdata); | shaderdata_to_shaderglobals(kg, sd, state, 0, tdata); | ||||
| /* execute shader */ | /* execute shader */ | ||||
| OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss; | OSL::ShadingSystem *ss = (OSL::ShadingSystem *)kg->osl_ss; | ||||
| OSL::ShaderGlobals *globals = &tdata->globals; | OSL::ShaderGlobals *globals = &tdata->globals; | ||||
| OSL::ShadingContext *octx = tdata->context; | OSL::ShadingContext *octx = tdata->context; | ||||
| int shader = sd->shader & SHADER_MASK; | int shader = sd->shader & SHADER_MASK; | ||||
| if (kg->osl->displacement_state[shader]) { | if (kg->osl->displacement_state[shader]) { | ||||
| ss->execute(octx, *(kg->osl->displacement_state[shader]), *globals); | ss->execute(octx, *(kg->osl->displacement_state[shader]), *globals); | ||||
| } | } | ||||
| /* get back position */ | /* get back position */ | ||||
| sd->P = TO_FLOAT3(globals->P); | sd->P = TO_FLOAT3(globals->P); | ||||
| } | } | ||||
| /* Attributes */ | /* Attributes */ | ||||
| int OSLShader::find_attribute(KernelGlobals *kg, | int OSLShader::find_attribute(const KernelGlobals *kg, | ||||
| const ShaderData *sd, | const ShaderData *sd, | ||||
| uint id, | uint id, | ||||
| AttributeDescriptor *desc) | AttributeDescriptor *desc) | ||||
| { | { | ||||
| /* for OSL, a hash map is used to lookup the attribute by name. */ | /* for OSL, a hash map is used to lookup the attribute by name. */ | ||||
| int object = sd->object * ATTR_PRIM_TYPES; | int object = sd->object * ATTR_PRIM_TYPES; | ||||
| OSLGlobals::AttributeMap &attr_map = kg->osl->attribute_map[object]; | OSLGlobals::AttributeMap &attr_map = kg->osl->attribute_map[object]; | ||||
| Show All 26 Lines | |||||