NVIDIA Index example code nvidia_logo_transpbg.gif Up
constant_color_mapping.h
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright 2023 NVIDIA Corporation. All rights reserved.
3 *****************************************************************************/
4// \file
5// \brief A constant color texture generation for mapping on geometry.
6
7#ifndef NVIDIA_INDEX_BIN_GEOSPATIAL_EXAMPLE_CONSTANT_COLOR_MAPPING_H
8#define NVIDIA_INDEX_BIN_GEOSPATIAL_EXAMPLE_CONSTANT_COLOR_MAPPING_H
9
10#include <mi/dice.h>
11#include <mi/base/interface_implement.h>
12#include <mi/base/handle.h>
13#include <mi/base/uuid.h>
14#include <mi/math/vector.h>
15
16#include <nv/index/idistributed_compute_technique.h>
17#include <nv/index/idistributed_compute_destination_buffer.h>
18
19#define USE_NVINDEX_ACCESS
20#include "utility/example_shared.h"
21
22#include <cassert>
23#include <iostream>
24
25// Constant color mapping.
26//
28 public nv::index::Distributed_compute_technique<
29 0x644c61ed,0x0865,0x4be8,0x94,0xdb,0x1e,0x75,0xaa,0xa6,0x8e,0x4c>
30{
31public:
32 // constructor
33 //
34 // \param[in] extent local coordinate extent
35 // \param[in] color a constant color of this texture buffer generation technique
37 const mi::math::Vector_struct<mi::Float32, 2>& extent,
38 const mi::math::Color_struct & color)
39 :
40 m_extent(extent),
41 m_color(color)
42 {
43 // empty
44 }
45
46 // Empty default constructor for serialization
48 :
49 m_color(1.0f, 1.0f, 1.0f, 1.0f)
50 {
51 // empty
52 }
53
54public:
55 virtual void launch_compute(
56 mi::neuraylib::IDice_transaction* dice_transaction,
57 nv::index::IDistributed_compute_destination_buffer* dst_buffer) const
58 {
59
60 using nv::index::IDistributed_compute_destination_buffer_2d_texture;
61
62 // retrieve 2d texture buffer destination buffer interface
63 const mi::base::Handle<IDistributed_compute_destination_buffer_2d_texture> dst_buffer_2d(
64 dst_buffer->get_interface<IDistributed_compute_destination_buffer_2d_texture>());
65
66 if (!dst_buffer_2d)
67 {
68 std::cerr << "Distributed compute technique (Constant_color_mapping): "
69 << "Unable to retrieve valid 2d texture destination buffer interface.";
70 return;
71 }
72
73 // define the texture buffer format
74 // * this example allows to use: INTENSITY_UINT8, RGBA_UINT8, RGBA_FLOAT32
75 const IDistributed_compute_destination_buffer_2d_texture::Buffer_format texture_format = IDistributed_compute_destination_buffer_2d_texture::FORMAT_RGBA_FLOAT32;
76
77 // define the texture buffer layout
78 // * this example allows to use: ONE_DIMENSIONAL_ARRAY, TWO_DIMENSIONAL_ARRAY
79 // * the screen-space area (in raster space) that the subregion covers enables the user-defined generation to
80 // choose, for instance, the most efficient texture generation technique (for example, based on geometry area
81 // or window, or on ray/geometry intersection).
82 const IDistributed_compute_destination_buffer_2d_texture::Buffer_layout texture_layout = IDistributed_compute_destination_buffer_2d_texture::TWO_DIMENSIONAL_ARRAY;
83
84 // set the buffer config to the destination buffer, initializing the internal buffer storage to the required size.
85 IDistributed_compute_destination_buffer_2d_texture::Buffer_config texture_config;
86 texture_config.format = texture_format;
87 texture_config.layout = texture_layout;
88
89 {
90 const mi::math::Bbox<mi::Float32, 2> surface_area = dst_buffer_2d->get_surface_area();
91
92 // Compute the tile resolution
93 texture_config.resolution.x = 1u;
94 texture_config.resolution.y = 1u;
95
96 // Compute the area that is covered by the tile, which is probably larger than the requested
97 // surface_area due to rounding to full texels.
98 texture_config.covered_area = surface_area;
99
100 if (!dst_buffer_2d->generate_buffer_storage(texture_config))
101 {
102 std::cerr << "Distributed compute technique (Constant_color_mapping): "
103 << "unable to generate destination buffer storage.";
104 return;
105 }
106
107 const mi::math::Vector_struct<mi::Uint32, 2>& tile_resolution =
108 dst_buffer_2d->get_buffer_config().resolution;
109 const mi::Size tile_buf_size = tile_resolution.x * tile_resolution.y;
110 assert(tile_buf_size > 0);
111
112 // Allocate the texture buffer
113 mi::math::Color_struct* texture_tile =
114 reinterpret_cast<mi::math::Color_struct*>(dst_buffer_2d->get_buffer_storage());
115
116 // Fill in the constant color
117 for(mi::Size i = 0; i < tile_buf_size; ++i){
118 texture_tile[i] = m_color;
119 }
120 }
121 }
122
123public:
124 virtual mi::neuraylib::IElement* copy() const
125 {
127 other->m_extent = this->m_extent;
128 other->m_color = this->m_color;
129 return other;
130 }
131 virtual const char* get_class_name() const
132 {
133 return "Constant_color_mapping";
134 }
135 virtual void serialize(mi::neuraylib::ISerializer *serializer) const
136 {
137 serializer->write(&(m_extent.x), 2);
138 serializer->write(&(m_color.r), 4);
139 }
140 virtual void deserialize(mi::neuraylib::IDeserializer* deserializer)
141 {
142 deserializer->read(&(m_extent.x), 2);
143 deserializer->read(&(m_color.r), 4);
144 }
145 virtual void get_references(
146 mi::neuraylib::ITag_set* result) const
147 {
148 // empty
149 }
150
151private:
152 // Extent of the texture
153 mi::math::Vector<mi::Float32, 2> m_extent;
154 // The constant color
155 mi::math::Color m_color;
156
157private:
158 // copy constructor. prohibit until proved useful.
160 // operator=. prohibit until proved useful.
162};
163
164#endif // NVIDIA_INDEX_BIN_GEOSPATIAL_EXAMPLE_CONSTANT_COLOR_MAPPING_H
virtual void get_references(mi::neuraylib::ITag_set *result) const
virtual void deserialize(mi::neuraylib::IDeserializer *deserializer)
virtual void launch_compute(mi::neuraylib::IDice_transaction *dice_transaction, nv::index::IDistributed_compute_destination_buffer *dst_buffer) const
virtual mi::neuraylib::IElement * copy() const
Distributed_compute_constant_color_mapping(const mi::math::Vector_struct< mi::Float32, 2 > &extent, const mi::math::Color_struct &color)
virtual void serialize(mi::neuraylib::ISerializer *serializer) const