Coder Social home page Coder Social logo

pezcode / cluster Goto Github PK

View Code? Open in Web Editor NEW
406.0 10.0 27.0 2.25 MB

Clustered shading implementation with bgfx

License: MIT License

CMake 3.28% C++ 66.32% Scala 9.04% SuperCollider 5.57% Shell 14.44% C 1.37%
bgfx clustered-shading deferred-shading physically-based-rendering pbr hdr tone-mapping shaders graphics

cluster's Introduction

Cluster

Implementation of Clustered Shading and Physically Based Rendering with the bgfx rendering library.

Render result

Cluster light count visualization

Currently bgfx's OpenGL, DirectX 11/12 and Vulkan backends are supported. I've only tested on Windows 10 with an Nvidia GTX 1070. Other hardware or operating systems might have subtle bugs I'm not aware of.

Functionality

Live-switchable render paths

  • forward, deferred and clustered shading
  • output should be near-identical (as long as you don't hit the maximum light count per cluster)

Clustered Forward Shading

  • logarithmical depth partition
  • compute shader for cluster generation
  • compute shader for light culling
    • AABB test for point lights
  • cluster light count visualization

Deferred Shading

  • G-Buffer with 4 render targets
  • light culling with light geometry
    • axis-aligned bounding box
    • backface rendering with reversed depth test
    • fragment position reconstructed from depth buffer
  • final forward pass for transparent meshes

Forward Shading

Very simple implementation, might be useful to start reading the code

Physically Based Rendering (PBR)

Tonemapping

HDR tonemapping postprocessing with different operators:

References

A few useful resources that helped with the implementation:

Compilation

CMake (>= 3.2) is required for building.

  1. Generate project files:

    mkdir build
    cd build
    # e.g. VS 2019, compile for x64 platform
    cmake -G "Visual Studio 16 2019" -A x64 ..
    cd ..
  2. Build. Open the project files with your IDE/build tool, or use CMake:

    cmake --build build/ --parallel --config Release
    

You can also grab a compiled copy for Windows with the Sponza model from the Releases page.

Libraries

Assets

License

This software is licensed under the MIT License. Basically, you can do whatever you want with it, as long as you don't remove the license and copyright notice from relevant pieces of code.

cluster's People

Contributors

alekseyt avatar pezcode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cluster's Issues

Directional Light support?

Hi am quite new to graphics programming and currently trying to add directional light support to Forward Shader.
Could you please let me know if this is a correct approach ? Also I believe spot lights branch was never actually made to support spot lights? Sorry for 2 questions in one. Please see my snippet below

    // Directional light comes after Point light calculation to add up Radiance
    vec3 L = normalize(u_sunDirection.xyz); // directional light vector
    float NoL = clamp(dot(N, L), 0.0, 1.0); // calculate the dot between the normal and sun vector

    float illuminance = 1 * NoL; // (1 picked for moon light, can be tweaked depending on day time)
    radianceOut += BRDF(V, L, N, NoV, NoL, mat) * msFactor * illuminance; // apply final radiance for the directional light

My result there are no shadows yet. Thank you

Sponza and metallic factor

Hello @pezcode

I hope you are doing well. I am reaching out about metallic factor on Sponza scene where cloth has gold string and was wondering why it differs from default GLTF model viewer?

image

What I see is

// metals have no diffuse reflection so the albedo value stores F0 (reflectance at normal incidence) instead // dielectrics are assumed to have F0 = 0.04 which equals an IOR of 1.5 mat.diffuseColor = mix(mat.albedo.rgb * (vec3_splat(1.0) - dielectricSpecular), black, mat.metallic);

in pbr.sh, changing mat.metallic to 0.5 produces more or less metallic look of the cloth

image

Removing mix with mat.diffuseColor = mat.albedo.rgb * (vec3_splat(1.0) - dielectricSpecular); also produces non black result.

image

Do you think it is expect that mix would fully use black color when mat.metallic is 1? It makes mat.diffuseColor absolutely black

Thanks !

MSAA?

Results are very noisy without any filtering. Is it possible to add MSAA to this rendering technique?

image

Distance attenuation

This is not a big deal, it's rather a question. There is this function in lights.sh:

float distanceAttenuation(float distance)
{
    // only for point lights

    // physics: inverse square falloff
    // pretend point lights are tiny spheres of 1 cm radius
    return 1.0 / max(distance * distance, 0.01 * 0.01);
}

Comment says "pretend point lights are tiny spheres of 1 cm radius", is that correct though? To me it looks like at distance 1.0 attenuation is going to be 1.0 because 1.0 / (1.0 * 1.0) == 1.0, am i right?

Shouldn't there be a falloff at 1m distance if lights are 1cm, or what are the units of distance?

I've changed it locally to this:

float distanceAttenuation(float distance)
{
    // only for point lights

    // physics: inverse square falloff
    // pretend point lights are tiny spheres of zero radius
    return 1.0 / ((distance + 1) * (distance + 1));
}

So at distance 1m there will be 0.25 falloff factor and i've got this picture:

Без имени

Above is vanilla Cluster, below modified. Note that there are no bright spots of (almost?) white light in the middle of point lights anymore.

Should those bright spots be there?

Help needed with setNormalMatrix

Hey @pezcode ,

I am working on gltf loader via cgltf and use your PBR shading , everything works flawlessly with standard Sponza scene but as soon as I load a scene with many gltf nodes they turn out to be black. After some investigation it turns out to be the setting of normal matrix for shading.

void Renderer::setNormalMatrix(const glm::mat4& modelMat)
{
    // usually the normal matrix is based on the model view matrix
    // but shading is done in world space (not eye space) so it's just the model matrix
    //glm::mat4 modelViewMat = viewMat * modelMat;

    // if we don't do non-uniform scaling, the normal matrix is the same as the model-view matrix
    // (only the magnitude of the normal is changed, but we normalize either way)
    //glm::mat3 normalMat = glm::mat3(modelMat);

    // use adjugate instead of inverse
    // see https://github.com/graphitemaster/normals_revisited#the-details-of-transforming-normals
    // cofactor is the transpose of the adjugate
    glm::mat3 normalMat = glm::transpose(glm::adjugate(glm::mat3(modelMat)));
    bgfx::setUniform(normalMatrixUniform, glm::value_ptr(normalMat));
}

Issues with many nodes :/

image

Sponza working !

image

Would you happen to know if I should supply a matrix per node? i.e calculate T * R * S per each of the dark objects so they are shaded? Thanks

Cannot build on Ubuntu due to linking errors

Hi,
Are there any known compatibility issues with compiling through cmake on ubuntu 21? I'm getting a lot of linking errors:

[ 69%] Linking CXX executable shaderc
/usr/bin/ld: libspirv-tools.a(pass.cpp.o): in function `spvtools::opt::IRContext::AddCapability(SpvCapability_)':
pass.cpp:(.text._ZN8spvtools3opt9IRContext13AddCapabilityE14SpvCapability_[_ZN8spvtools3opt9IRContext13AddCapabilityE14SpvCapability_]+0x306): undefined reference to `spvtools::opt::FeatureManager::AddCapability(SpvCapability_)'
/usr/bin/ld: pass.cpp:(.text._ZN8spvtools3opt9IRContext13AddCapabilityE14SpvCapability_[_ZN8spvtools3opt9IRContext13AddCapabilityE14SpvCapability_]+0x4ac): undefined reference to `spvtools::opt::FeatureManager::Analyze(spvtools::opt::Module*)'
/usr/bin/ld: libspirv-tools.a(private_to_local_pass.cpp.o): in function `spvtools::opt::PrivateToLocalPass::Process()':
private_to_local_pass.cpp:(.text+0x3914): undefined reference to `spvtools::opt::FeatureManager::Analyze(spvtools::opt::Module*)'
/usr/bin/ld: libspirv-tools.a(relax_float_ops_pass.cpp.o): in function `spvtools::opt::IRContext::AnalyzeFeatures()':
relax_float_ops_pass.cpp:(.text._ZN8spvtools3opt9IRContext15AnalyzeFeaturesEv[_ZN8spvtools3opt9IRContext15AnalyzeFeaturesEv]+0xd9): undefined reference to `spvtools::opt::FeatureManager::Analyze(spvtools::opt::Module*)'

[...]

I have spirv-tools installed, and have also used the machine for compiling both OpenGL and Vulkan applications. So I should have all the essentials. Any tips on what might be going wrong? :)

ImGui Font Issue

Hello, im trying to compile and run this, except i get a error at this code in the bigg library:

	io.Fonts->AddFontDefault();
	io.Fonts->GetTexDataAsRGBA32( &data, &width, &height );
	imguiFontTexture = bgfx::createTexture2D( ( uint16_t )width, ( uint16_t )height, false, 1, bgfx::TextureFormat::BGRA8, 0, bgfx::copy( data, width*height * 4 ) );
	imguiFontUniform = bgfx::createUniform( "s_tex", bgfx::UniformType::Sampler );```
	
for some reason data is always "ÿÿÿÿÿÿÿÿÿÿÿ", wdith and height seem to be correct

SSAO or GTAO

Ambient occlusion addresses medium to long range occlusion of ambient light (which is light that has undergone many bounces and is in some sense 'omnidirectional'). It would be cool to have a toggle for SSAO or GTAO on the clustered renderer.

failed to build it on windows 10 (visual studio 2019)

-- Check size of off64_t
-- Check size of off64_t - failed
-- Looking for fseeko
-- Looking for fseeko - not found
-- Looking for unistd.h
-- Looking for unistd.h - not found
-- Build an import-only version of Assimp.
CMake Warning (dev) at D:/wecode_build_tools/mingw/share/cmake-3.17/Modules/FindPackageHandleStandardArgs.cmake:272 (message):
The package name passed to find_package_handle_standard_args (rt) does
not match the name of the calling package (RT). This can lead to problems
in calling code that expects find_package result variables (e.g.,
_FOUND) to follow a certain pattern.
Call Stack (most recent call first):
3rdparty/assimp/cmake-modules/FindRT.cmake:19 (find_package_handle_standard_args)
3rdparty/assimp/code/CMakeLists.txt:1013 (FIND_PACKAGE)
This warning is for project developers. Use -Wno-dev to suppress it.

-- Could NOT find rt (missing: RT_LIBRARY)
-- Enabled importer formats: GLTF
-- Disabled importer formats: AMF 3DS AC ASE ASSBIN B3D BVH COLLADA DXF CSM HMP IRRMESH IRR LWO LWS MD2 MD3 MD5 MDC MDL NFF NDO OFF OBJ OGRE OPENGEX PLY MS3D COB BLEND IFC XGL FBX Q3D Q3BSP RAW SIB SMD STL TERRAGEN 3D X X3D 3MF MMD STEP
-- Enabled exporter formats:
-- Disabled exporter formats: 3DS ASSBIN ASSXML COLLADA OBJ OPENGEX PLY FBX STL X X3D GLTF 3MF ASSJSON STEP
-- Build spdlog: 1.8.1
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - not found
-- Found Threads: TRUE
-- Build type: Release
CMake Error at src/CMakeLists.txt:97 (configure_debugging):
Unknown CMake command "configure_debugging".

-- Configuring incomplete, errors occurred!
See also "E:/vscode_project/Cluster/build/CMakeFiles/CMakeOutput.log".
See also "E:/vscode_project/Cluster/build/CMakeFiles/CMakeError.log".
PS E:\vscode_project\Cluster\build>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.