NVIDIA Index example code nvidia_logo_transpbg.gif Up
xac_compute_plane.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright 2023 NVIDIA Corporation. All rights reserved.
3 *****************************************************************************/
4
5#include "xac_compute_plane.h"
6#include <nv/index/iplane.h>
7#include <nv/index/irendering_kernel_programs.h>
8#include <nv/index/ilight.h>
9#include <nv/index/imaterial.h>
10#include <nv/index/icompute.h>
11
12#include <nv/index/app/string_dict.h> // get_*
13
14namespace xac_compute {
15
16
17static const char* plane_program_begin =
18";NV_IDX_XAC_VERSION_1_0 \n"
19"namespace xac = nv::index::xac; \n"
20"namespace xaclib = nv::index::xaclib; \n"
21" \n"
22"class Surface_sample_program \n"
23"{ \n"
24" NV_IDX_SURFACE_SAMPLE_PROGRAM \n"
25" \n"
26"public: \n"
27" NV_IDX_DEVICE_INLINE_MEMBER void initialize() \n"
28" {} \n"
29"\n"
30;
31
32static const char* program_end =
33"}; \n"
34;
35
36static const char* plane_color_program =
37" NV_IDX_DEVICE_INLINE_MEMBER int execute( \n"
38" const Sample_info_self& sample_info, \n"
39" Sample_output& sample_output) \n"
40" { \n"
41" float4 color = sample_info.sample_color; \n"
42" float sample = 0.f; \n"
43" sample_func(sample_info.scene_position, sample, color); \n"
44" \n"
45" // boost the opacity \n"
46" color.w = min(1.f, 0.3f + (color.w * 100.f) * 0.7f); \n"
47" sample_output.set_color(color); \n"
48" return NV_IDX_PROG_OK; \n"
49" } \n"
50;
51
52static const char* plane_compute_program =
53" // The user program for compute. \n"
54" NV_IDX_DEVICE_INLINE_MEMBER \n"
55" int compute( \n"
56" const Data_point_info_self& data_point_info, \n"
57" Data_point_output& data_output) \n"
58" { \n"
59" if (!data_point_info.is_valid()) { return NV_IDX_PROG_OK; } \n"
60" \n"
61" float sample = 0.f; \n"
62" float4 vcolor; \n"
63" if (sample_func(data_point_info.get_position(), sample, vcolor)) { \n"
64" data_output.write_value(0, sample); \n"
65" data_output.write_value(1, make_float3(vcolor.x, vcolor.y, vcolor.z)); \n"
66" } \n"
67" return NV_IDX_PROG_OK; \n"
68" } \n"
69;
70
76};
77
78
80{
81 opt_map["cplane.resolution"] = "256 256";
82}
83
84
86{
87 usage.opt("cplane.resolution", "X Y", "Resolution of compute plane.");
88}
89
90
92 const mi::neuraylib::Tag compute_target,
93 const char* plane_sample_function,
94 const Option_map& opt_map,
95 Scene_info& scene_info,
96 Scene_tool& scene_tool) const
97{
98 scene_tool.add_new_group(true);
99
100 // Set transformation of the new group.
101 {
102 mi::base::Handle<nv::index::ITransformed_scene_group> xfgroup(
103 scene_tool.current_group.get_interface<nv::index::ITransformed_scene_group>());
104 check_success(xfgroup);
105
106 mi::math::Matrix<mi::Float32, 4, 4> transform_mat(1.f);
107 const Vec3f rotation(0.f, 0.f, 45.f);
108 transform_mat.rotate(rotation / 180.f * float(M_PI));
109 const Vec3f translate(70.f, 10.f, 5.f);
110 transform_mat.translate(translate);
111 xfgroup->set_transform(transform_mat);
112 }
113
114 // Add scene element mapping. This ensures presence of volume even if it is disabled.
115 {
116 mi::base::Handle<nv::index::IRendering_kernel_program_scene_element_mapping> mapping;
117 scene_tool.create_attribute(mapping);
118
119 mapping->set_mapping(0, compute_target);
120 scene_tool.store_and_add_to_group(mapping);
121 }
122
123 // Add a light, needed to suppress a warning
124 {
125 mi::base::Handle<nv::index::IDirectional_headlight> headlight;
126 scene_tool.create_attribute(headlight);
127
128 const mi::math::Color color_intensity(1.0f, 1.0f, 1.0f, 1.0f);
129 headlight->set_intensity(color_intensity);
130 headlight->set_direction(Vec3f(1.0f, -1.0f, -1.0f));
131 scene_tool.store_and_add_to_group(headlight);
132 }
133
134 // create material
135 {
136 mi::base::Handle<nv::index::IPhong_gl> mat;
137 scene_tool.create_attribute(mat);
138
139 mat->set_ambient(mi::math::Color(0.1f, 0.1f, 0.1f, 1.f));
140 mat->set_diffuse(mi::math::Color(0.5f, 0.5f, 0.5f, 1.f));
141 mat->set_specular(mi::math::Color(0.f));
142 mat->set_shininess(0.f);
143 scene_tool.store_and_add_to_group(mat);
144 }
145
146 // create ICompute_plane_parameters
147 {
148 mi::base::Handle<nv::index::ICompute_plane_parameters> comp_par;
149 scene_tool.create_attribute(comp_par);
150
151 const Vec2u plane_resolution = nv::index::app::get_vector_from_string<mi::Uint32,2>(opt_map["cplane.resolution"]);
152 const mi::Uint32 value_sizes[3] = {sizeof(float), sizeof(float) * 3, sizeof(int)};
153 comp_par->set_resolution(plane_resolution);
154 //comp_par->set_value_sizes(value_sizes, 3);
155 comp_par->set_value_sizes(value_sizes, 2); // just use 2 value slots in non-debug mode
156 scene_tool.store_and_add_to_group(comp_par);
157 }
158
159 // add compute program for plane
160 {
161 const int prg_mode = 2; // include the compute method
162 //const int prg_mode = nv::index::app::get_sint32(opt_map["user_program_mode"]);
163 std::pair<mi::neuraylib::Tag, std::string> prg_info =
166 prg_mode,
167 scene_info.plane_rtc_program_source,
169 scene_tool,
171
172 scene_info.compute_launch_info.plane_compute_program_tag = prg_info.first;
173 scene_info.plane_rtc_program_source = prg_info.second;
174 }
175
176 // create plane
177 {
178 mi::base::Handle<nv::index::IPlane> plane(
179 scene_tool.scene->create_shape<nv::index::IPlane>());
180 check_success(plane.is_valid_interface());
181
182 plane->set_point(Vec3f(0.f, 0.f, 5.f));
183 plane->set_normal(Vec3f(-1.f, 0.f, 0.f));
184 plane->set_up(Vec3f(0.f, 1.f, 0.f));
185 plane->set_extent(Vec2f(60.0f, 60.0f));
186
187 scene_info.compute_launch_info.plane_tag = scene_tool.store_and_add_to_group(plane);
188 }
189}
190
191
192
193
194}//xac_compute
void add_compute_plane_group(const mi::neuraylib::Tag compute_target, const char *plane_sample_function, const Option_map &opt_map, Scene_info &scene_info, Scene_tool &scene_tool) const
void usage_info(Usage_helper &usage) const
void add_arguments(Option_map &opt_map) const
void opt(const std::string &opt, const char *arg_info, const char *subline="")
Definition: xac_compute.h:94
XAC compute example scenes.
Definition: xac_compute.cpp:27
std::pair< mi::neuraylib::Tag, std::string > add_user_program(Sample_program_kind prg_kind, int user_prg, const std::string &user_text, const Program_source &prg_text, Scene_tool &scene_tool, const char *extra_pgr_methods)
static const char * plane_program_begin
static const char * plane_color_program
mi::math::Vector< mi::Float32, 2 > Vec2f
Definition: xac_compute.h:33
static const char * program_end
static const char * plane_compute_program
mi::math::Vector< mi::Float32, 3 > Vec3f
Definition: xac_compute.h:38
mi::math::Vector< mi::Uint32, 2 > Vec2u
Definition: xac_compute.h:31
static const Program_source plane_program
#define check_success(expr)
mi::neuraylib::Tag plane_tag
Definition: xac_compute.h:132
mi::neuraylib::Tag plane_compute_program_tag
Definition: xac_compute.h:131
std::string plane_rtc_program_source
Definition: xac_compute.h:142
Compute_launch_info compute_launch_info
Definition: xac_compute.h:140
mi::base::Handle< nv::index::IScene_group > current_group
mi::base::Handle< nv::index::IScene > scene
mi::neuraylib::Tag add_new_group(bool transformed_group=false)
mi::neuraylib::Tag store_and_add_to_group(const mi::base::Handle<T> &element)
void create_attribute(mi::base::Handle<T> &element)