1 // Copyright Contributors to the Open Shading Language project.
2 // SPDX-License-Identifier: BSD-3-Clause
3 // https://github.com/AcademySoftwareFoundation/OpenShadingLanguage
4 
5 #pragma once
6 
7 #include <OpenImageIO/ustring.h>
8 #include <OSL/oslexec.h>
9 #include <OSL/device_string.h>
10 #include "optix_compat.h"
11 #include "simplerend.h"
12 #include "../testrender/optix_stringtable.h"
13 
14 
15 OSL_NAMESPACE_ENTER
16 
17 
18 class OptixGridRenderer final : public SimpleRenderer
19 {
20 public:
21     // Just use 4x4 matrix for transformations
22     typedef Matrix44 Transformation;
23 
24     OptixGridRenderer ();
25     virtual ~OptixGridRenderer ();
26 
register_string(const std::string & str,const std::string & var_name)27     uint64_t register_string (const std::string& str, const std::string& var_name)
28     {
29         uint64_t addr = m_str_table.addString (ustring(str), ustring(var_name));
30         if (!var_name.empty())
31             register_global (var_name, addr);
32         return addr;
33     }
34 
35     uint64_t register_global (const std::string& str, uint64_t value);
36     bool     fetch_global (const std::string& str, uint64_t *value);
37 
supports(string_view feature)38     virtual int supports (string_view feature) const
39     {
40         if (feature == "OptiX")
41             return true;
42         return SimpleRenderer::supports(feature);
43     }
44 
45     std::string load_ptx_file (string_view filename);
46     bool synch_attributes ();
47 
48     virtual void init_shadingsys (ShadingSystem *ss);
49     virtual bool init_optix_context (int xres, int yres);
50     virtual bool make_optix_materials ();
51     virtual bool finalize_scene ();
52     virtual void prepare_render ();
53     virtual void warmup ();
54     virtual void render (int xres, int yres);
55     virtual void finalize_pixel_buffer ();
56     virtual void clear ();
57 
58     /// Return true if the texture handle (previously returned by
59     /// get_texture_handle()) is a valid texture that can be subsequently
60     /// read or sampled.
61     virtual bool good (TextureHandle *handle);
62 
63     /// Given the name of a texture, return an opaque handle that can be
64     /// used with texture calls to avoid the name lookups.
65     virtual TextureHandle * get_texture_handle(ustring filename, ShadingContext* shading_context);
66 
67 #if (OPTIX_VERSION < 70000)
68     // Easy way to do Optix calls
optix_ctx()69     optix::Context& optix_ctx()            { return m_optix_ctx; }
context()70     optix::Context& context()              { return m_optix_ctx; }
71     optix::Context& operator -> ()         { return context(); }
72 #else
optix_ctx()73     OptixDeviceContext optix_ctx()         { return m_optix_ctx; }
context()74     OptixDeviceContext context()           { return m_optix_ctx; }
75     OptixDeviceContext operator -> ()      { return context(); }
76 #endif
77 
78 private:
79 
80     optix::Context m_optix_ctx = nullptr;
81     OptiXStringTable m_str_table;
82 
83 #if (OPTIX_VERSION < 70000)
84     optix::Program m_program = nullptr;
85 #else
86     CUstream                m_cuda_stream;
87     OptixShaderBindingTable m_optix_sbt = {};
88     OptixPipeline           m_optix_pipeline = {};
89     CUdeviceptr             d_output_buffer;
90     CUdeviceptr             d_launch_params = 0;
91     int                     m_xres, m_yres;
92 #endif
93     std::string m_materials_ptx;
94     std::unordered_map<OIIO::ustring, optix::TextureSampler, OIIO::ustringHash> m_samplers;
95     std::unordered_map<OIIO::ustring, uint64_t, OIIO::ustringHash> m_globals_map;
96 };
97 
98 #if (OPTIX_VERSION >= 70000)
99 struct EmptyRecord
100 {
101     __align__( OPTIX_SBT_RECORD_ALIGNMENT ) char header[OPTIX_SBT_RECORD_HEADER_SIZE];
102     void *data;
103 };
104 #endif
105 
106 OSL_NAMESPACE_EXIT
107