Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextGen shader compiling #1658

Merged
merged 6 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions zeno/include/zeno/types/MaterialObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace zeno
std::vector<std::shared_ptr<Texture2DObject>> tex2Ds;
std::vector<std::shared_ptr<TextureObjectVDB>> tex3Ds;
std::string transform;

std::string parameters; // json
std::string mtlidkey; // unused for now

size_t serializeSize() const
Expand All @@ -30,6 +32,10 @@ namespace zeno
size += sizeof(mtlidkeyLen);
size += mtlidkeyLen;

auto paramLen {parameters.size()};
size += sizeof(paramLen);
size += paramLen;

auto vertLen{vert.size()};
size += sizeof(vertLen);
size += vertLen;
Expand Down Expand Up @@ -83,6 +89,12 @@ namespace zeno
mtlidkey.copy(str + i, mtlidkeyLen);
i += mtlidkeyLen;

auto paramLen{parameters.size()};
memcpy(str+i, &paramLen, sizeof(paramLen));
i += sizeof(paramLen);
parameters.copy(str+i, paramLen);
i += paramLen;

auto vertLen{vert.size()};
memcpy(str + i, &vertLen, sizeof(vertLen));
i += sizeof(vertLen);
Expand Down Expand Up @@ -167,6 +179,12 @@ namespace zeno
this->mtlidkey = std::string{str + i, mtlidkeyLen};
i += mtlidkeyLen;

size_t paramLen;
memcpy(&paramLen, str+i, sizeof(paramLen));
i += sizeof(paramLen);
this->parameters = std::string(str+i, paramLen);
i += paramLen;

size_t vertLen;
memcpy(&vertLen, str + i, sizeof(vertLen));
i += sizeof(vertLen);
Expand Down
35 changes: 30 additions & 5 deletions zeno/src/nodes/mtl/ShaderFinalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
#include <string>
#include <iostream>
#include "magic_enum.hpp"
#include "zeno/utils/vec.h"

#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>

namespace zeno {

Expand Down Expand Up @@ -141,9 +147,9 @@ struct ShaderFinalize : INode {

auto sssRadiusMethod = get_input2<std::string>("sssRadius");
if (sssRadiusMethod == "Fixed") {
commonCode += "#define _SSS_FIXED_RADIUS_ 1 \n";
code += "bool sssFxiedRadius = true;\n";
} else {
commonCode += "#define _SSS_FIXED_RADIUS_ 0 \n";
code += "bool sssFxiedRadius = false;\n";
}

auto mtl = std::make_shared<MaterialObject>();
Expand Down Expand Up @@ -180,9 +186,28 @@ struct ShaderFinalize : INode {
commonCode += "#define VolumeEmissionScaler VolumeEmissionScalerType::" + VolumeEmissionScaler + "\n";

vol_depth = clamp(vol_depth, 9, 99);

commonCode += "static const int _vol_depth = " + std::to_string(vol_depth) + ";\n";
commonCode += "static const float _vol_extinction = " + std::to_string(vol_extinction) + ";\n";
vol_extinction = clamp(vol_extinction, 1e-5, 1e+5);

std::string parameters = "";
{
using namespace rapidjson;
Document d; d.SetObject();
auto& allocator = d.GetAllocator();

Value s = Value();
s.SetInt(vol_depth);
d.AddMember("vol_depth", s, allocator);

s = Value();
s.SetFloat(vol_extinction);
d.AddMember("vol_extinction", s, allocator);

StringBuffer buffer;
Writer<StringBuffer> writer(buffer);
d.Accept(writer);
parameters = buffer.GetString();
}
mtl->parameters = parameters;

auto tex3dList = get_input<ListObject>("tex3dList")->getRaw(); //get<zeno::StringObject>();

Expand Down
106 changes: 48 additions & 58 deletions zenovis/src/optx/RenderEngineOptx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ struct GraphicsManager {
std::vector<std::shared_ptr<zeno::TextureObjectVDB>> tex3Ds;
std::string common;
std::string shader;
std::string mtlidkey;
std::string extensions;
std::string mtlidkey;
std::string transform;
std::string parameters;
};

struct DetPrimitive {
std::shared_ptr<zeno::PrimitiveObject> primSp;
};
Expand Down Expand Up @@ -469,7 +471,7 @@ struct GraphicsManager {
}
else if (auto mtl = dynamic_cast<zeno::MaterialObject *>(obj))
{
det = DetMaterial{mtl->tex2Ds, mtl->tex3Ds, mtl->common, mtl->frag, mtl->mtlidkey, mtl->extensions, mtl->transform};
det = DetMaterial{mtl->tex2Ds, mtl->tex3Ds, mtl->common, mtl->frag, mtl->extensions, mtl->mtlidkey, mtl->transform, mtl->parameters};
}
}

Expand Down Expand Up @@ -876,17 +878,15 @@ struct RenderEngineOptx : RenderEngine, zeno::disable_copy {
std::pair<std::string_view, std::string_view> shadtpl2;
};

ShaderTemplateInfo _fallback_shader_template {
"DefaultFallback.cu", false, {}, {}, {}
ShaderTemplateInfo _default_callable_template {
"CallableDefault.cu", false, {}, {}, {}
};
ShaderTemplateInfo _volume_callable_template {
"CallableVolume.cu", false, {}, {}, {}
};
void ensure_fallback() {
_fallback_shader_template.shadtmpl = sutil::lookupIncFile(_fallback_shader_template.name.c_str());
}

ShaderTemplateInfo _default_shader_template {
"DeflMatShader.cu", false, {}, {}, {}
};

ShaderTemplateInfo _volume_shader_template {
"volume.cu", false, {}, {}, {}
};
Expand Down Expand Up @@ -930,6 +930,7 @@ struct RenderEngineOptx : RenderEngine, zeno::disable_copy {
_template.commontpl = tplsv.substr(0, pcommon);
}
else{
return;
throw std::runtime_error("cannot find stub COMMON_CODE in shader template");
}
std::string_view tmplstub0 = "//GENERATED_BEGIN_MARK";
Expand Down Expand Up @@ -1008,39 +1009,38 @@ struct RenderEngineOptx : RenderEngine, zeno::disable_copy {
if (meshNeedUpdate || matNeedUpdate || staticNeedUpdate) {
//zeno::log_debug("[zeno-optix] updating scene");
//zeno::log_debug("[zeno-optix] updating material");
std::vector<std::shared_ptr<ShaderPrepared>> _mesh_shader_list{};
std::vector<std::shared_ptr<ShaderPrepared>> _meshes_shader_list{};
std::vector<std::shared_ptr<ShaderPrepared>> _sphere_shader_list{};
std::vector<std::shared_ptr<ShaderPrepared>> _volume_shader_list{};

std::map<std::string, int> meshMatLUT{};
std::map<std::string, uint> matIDtoShaderIndex{};

ensure_shadtmpl(_default_callable_template);
ensure_shadtmpl(_volume_callable_template);

ensure_shadtmpl(_default_shader_template);
ensure_shadtmpl(_volume_shader_template);
ensure_shadtmpl(_light_shader_template);
ensure_fallback();

auto _default_shader_fallback = std::make_shared<std::string>(_fallback_shader_template.shadtmpl);
auto _volume_shader_fallback = std::make_shared<std::string>(_volume_shader_template.shadtmpl);

{
auto tmp = std::make_shared<ShaderPrepared>();

tmp->mark = ShaderMaker::Mesh;
tmp->matid = "Default";
tmp->source = _default_shader_template.shadtmpl;
tmp->fallback = _default_shader_fallback;
tmp->filename = _default_shader_template.name;
tmp->callable = _default_callable_template.shadtmpl;

_mesh_shader_list.push_back(tmp);
_meshes_shader_list.push_back(tmp);
}

{
auto tmp = std::make_shared<ShaderPrepared>();

tmp->mark = ShaderMaker::Sphere;
tmp->matid = "Default";
tmp->source = _default_shader_template.shadtmpl;
tmp->fallback = _default_shader_fallback;
tmp->filename = _default_shader_template.name;
tmp->callable = _default_callable_template.shadtmpl;

_sphere_shader_list.push_back(tmp);
}
Expand Down Expand Up @@ -1113,39 +1113,26 @@ struct RenderEngineOptx : RenderEngine, zeno::disable_copy {
}
}

const auto& selected_template = has_vdb? _volume_shader_template : _default_shader_template;
const auto& selected_fallback = has_vdb? _volume_shader_fallback : _default_shader_fallback;
const auto& selected_source = has_vdb? _volume_shader_template : _default_shader_template;
const auto& selected_callable = has_vdb? _volume_callable_template : _default_callable_template;

std::string shader;
std::string callable;
auto common_code = mtldet->common;
std::string tar = "uniform sampler2D";
size_t index = 0;
while (true) {
/* Locate the substring to replace. */
index = common_code.find(tar, index);
if (index == std::string::npos) break;

/* Make the replacement. */
common_code.replace(index, tar.length(), "//////////");

/* Advance index forward so the next iteration doesn't pick it up as well. */
index += tar.length();
}

auto& commontpl = selected_template.commontpl;
auto& shadtpl2 = selected_template.shadtpl2;
auto& commontpl = selected_callable.commontpl;
auto& shadtpl2 = selected_callable.shadtpl2;

shader.reserve(commontpl.size()
callable.reserve(commontpl.size()
+ common_code.size()
+ shadtpl2.first.size()
+ mtldet->shader.size()
+ shadtpl2.second.size());
shader.append(commontpl);
shader.append(common_code);
shader.append(shadtpl2.first);
shader.append(mtldet->shader);
shader.append(shadtpl2.second);
//std::cout<<shader<<std::endl;
callable.append(commontpl);
callable.append(common_code);
callable.append(shadtpl2.first);
callable.append(mtldet->shader);
callable.append(shadtpl2.second);
//std::cout<<callable<<std::endl;

std::vector<std::string> shaderTex;
int texid=0;
Expand All @@ -1157,10 +1144,13 @@ struct RenderEngineOptx : RenderEngine, zeno::disable_copy {
}

ShaderPrepared shaderP;

shaderP.callable = callable;
shaderP.filename = selected_source.name;
shaderP.parameters = mtldet->parameters;

shaderP.matid = mtldet->mtlidkey;
shaderP.source = shader;
shaderP.tex_names = shaderTex;
shaderP.fallback = selected_fallback;

if (has_vdb) {

Expand All @@ -1169,10 +1159,10 @@ struct RenderEngineOptx : RenderEngine, zeno::disable_copy {
} else {

if (cachedMeshesMaterials.count(mtldet->mtlidkey) > 0) {
meshMatLUT.insert({mtldet->mtlidkey, (int)_mesh_shader_list.size()});
meshMatLUT.insert({mtldet->mtlidkey, (int)_meshes_shader_list.size()});

shaderP.mark = ShaderMaker::Mesh;
_mesh_shader_list.push_back(std::make_shared<ShaderPrepared>(shaderP));
_meshes_shader_list.push_back(std::make_shared<ShaderPrepared>(shaderP));
}

if (cachedSphereMaterials.count(mtldet->mtlidkey) > 0) {
Expand All @@ -1186,34 +1176,34 @@ struct RenderEngineOptx : RenderEngine, zeno::disable_copy {
{
auto tmp = std::make_shared<ShaderPrepared>();

tmp->filename = _light_shader_template.name;
tmp->callable = _default_callable_template.shadtmpl;
tmp->mark = ShaderMaker::Mesh;
tmp->matid = "Light";
tmp->source = _light_shader_template.shadtmpl;
tmp->fallback = _default_shader_fallback;

_mesh_shader_list.push_back(tmp);
_meshes_shader_list.push_back(tmp);
}

{
auto tmp = std::make_shared<ShaderPrepared>();

tmp->filename = _light_shader_template.name;
tmp->callable = _default_callable_template.shadtmpl;
tmp->mark = ShaderMaker::Sphere;
tmp->matid = "Light";
tmp->source = _light_shader_template.shadtmpl;
tmp->fallback = _default_shader_fallback;


_sphere_shader_list.push_back(tmp);
}

std::vector<std::shared_ptr<ShaderPrepared>> allShaders{};
allShaders.reserve(_mesh_shader_list.size()+_sphere_shader_list.size()+_volume_shader_list.size());
allShaders.reserve(_meshes_shader_list.size()+_sphere_shader_list.size()+_volume_shader_list.size());

allShaders.insert(allShaders.end(), _mesh_shader_list.begin(), _mesh_shader_list.end());
allShaders.insert(allShaders.end(), _meshes_shader_list.begin(), _meshes_shader_list.end());
allShaders.insert(allShaders.end(), _sphere_shader_list.begin(), _sphere_shader_list.end());
allShaders.insert(allShaders.end(), _volume_shader_list.begin(), _volume_shader_list.end());

const size_t sphere_shader_offset = _mesh_shader_list.size();
const size_t volume_shader_offset = _mesh_shader_list.size() + _sphere_shader_list.size();
const size_t sphere_shader_offset = _meshes_shader_list.size();
const size_t volume_shader_offset = _meshes_shader_list.size() + _sphere_shader_list.size();

for (uint i=0; i<allShaders.size(); ++i) {
auto& ref = allShaders[i];
Expand Down
12 changes: 7 additions & 5 deletions zenovis/xinxinoptix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ set(CUDA_PATH ${CUDAToolkit_BIN_DIR}/..)

set(FILE_LIST
${OPTIX_PATH}/include/@optix.h
${OPTIX_PATH}/include/@optix_types.h
${OPTIX_PATH}/include/@optix_host.h
${OPTIX_PATH}/include/@optix_device.h
${OPTIX_PATH}/include/@optix_7_device.h
${OPTIX_PATH}/include/@optix_7_types.h
${OPTIX_PATH}/include/@internal/optix_7_device_impl.h
${OPTIX_PATH}/include/@internal/optix_7_device_impl_exception.h
${OPTIX_PATH}/include/@internal/optix_7_device_impl_transformations.h
${OPTIX_PATH}/include/@internal/optix_device_impl.h
${OPTIX_PATH}/include/@internal/optix_device_impl_exception.h
${OPTIX_PATH}/include/@internal/optix_device_impl_transformations.h

${CUDA_PATH}/include/@cuda_fp16.h
${CUDA_PATH}/include/@cuda_fp16.hpp
Expand All @@ -108,6 +108,8 @@ set(FILE_LIST
${CMAKE_CURRENT_SOURCE_DIR}/@optixPathTracer.h
${CMAKE_CURRENT_SOURCE_DIR}/@TraceStuff.h
${CMAKE_CURRENT_SOURCE_DIR}/@PTKernel.cu
${CMAKE_CURRENT_SOURCE_DIR}/@CallableDefault.cu
${CMAKE_CURRENT_SOURCE_DIR}/@CallableVolume.cu
${CMAKE_CURRENT_SOURCE_DIR}/@DefaultFallback.cu
${CMAKE_CURRENT_SOURCE_DIR}/@DeflMatShader.cu
${CMAKE_CURRENT_SOURCE_DIR}/@DisneyBRDF.h
Expand Down
Loading
Loading