• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By Achivai
      Hey, I am semi-new to 3d-programming and I've hit a snag. I have one object, let's call it Object A. This object has a long int array of 3d xyz-positions stored in it's vbo as an instanced attribute. I am using these numbers to instance object A a couple of thousand times. So far so good. 
      Now I've hit a point where I want to remove one of these instances of object A while the game is running, but I'm not quite sure how to go about it. At first my thought was to update the instanced attribute of Object A and change the positions to some dummy number that I could catch in the vertex shader and then decide there whether to draw the instance of Object A or not, but I think that would be expensive to do while the game is running, considering that it might have to be done several times every frame in some cases. 
      I'm not sure how to proceed, anyone have any tips?
    • By fleissi
      Hey guys!

      I'm new here and I recently started developing my own rendering engine. It's open source, based on OpenGL/DirectX and C++.
      The full source code is hosted on github:
      https://github.com/fleissna/flyEngine

      I would appreciate if people with experience in game development / engine desgin could take a look at my source code. I'm looking for honest, constructive criticism on how to improve the engine.
      I'm currently writing my master's thesis in computer science and in the recent year I've gone through all the basics about graphics programming, learned DirectX and OpenGL, read some articles on Nvidia GPU Gems, read books and integrated some of this stuff step by step into the engine.

      I know about the basics, but I feel like there is some missing link that I didn't get yet to merge all those little pieces together.

      Features I have so far:
      - Dynamic shader generation based on material properties
      - Dynamic sorting of meshes to be renderd based on shader and material
      - Rendering large amounts of static meshes
      - Hierarchical culling (detail + view frustum)
      - Limited support for dynamic (i.e. moving) meshes
      - Normal, Parallax and Relief Mapping implementations
      - Wind animations based on vertex displacement
      - A very basic integration of the Bullet physics engine
      - Procedural Grass generation
      - Some post processing effects (Depth of Field, Light Volumes, Screen Space Reflections, God Rays)
      - Caching mechanisms for textures, shaders, materials and meshes

      Features I would like to have:
      - Global illumination methods
      - Scalable physics
      - Occlusion culling
      - A nice procedural terrain generator
      - Scripting
      - Level Editing
      - Sound system
      - Optimization techniques

      Books I have so far:
      - Real-Time Rendering Third Edition
      - 3D Game Programming with DirectX 11
      - Vulkan Cookbook (not started yet)

      I hope you guys can take a look at my source code and if you're really motivated, feel free to contribute :-)
      There are some videos on youtube that demonstrate some of the features:
      Procedural grass on the GPU
      Procedural Terrain Engine
      Quadtree detail and view frustum culling

      The long term goal is to turn this into a commercial game engine. I'm aware that this is a very ambitious goal, but I'm sure it's possible if you work hard for it.

      Bye,

      Phil
    • By tj8146
      I have attached my project in a .zip file if you wish to run it for yourself.
      I am making a simple 2d top-down game and I am trying to run my code to see if my window creation is working and to see if my timer is also working with it. Every time I run it though I get errors. And when I fix those errors, more come, then the same errors keep appearing. I end up just going round in circles.  Is there anyone who could help with this? 
       
      Errors when I build my code:
      1>Renderer.cpp 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2039: 'string': is not a member of 'std' 1>c:\program files (x86)\windows kits\10\include\10.0.16299.0\ucrt\stddef.h(18): note: see declaration of 'std' 1>c:\users\documents\opengl\game\game\renderer.h(15): error C2061: syntax error: identifier 'string' 1>c:\users\documents\opengl\game\game\renderer.cpp(28): error C2511: 'bool Game::Rendering::initialize(int,int,bool,std::string)': overloaded member function not found in 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.h(9): note: see declaration of 'Game::Rendering' 1>c:\users\documents\opengl\game\game\renderer.cpp(35): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(36): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>c:\users\documents\opengl\game\game\renderer.cpp(43): error C2597: illegal reference to non-static member 'Game::Rendering::window' 1>Done building project "Game.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========  
       
      Renderer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include "Renderer.h" #include "Timer.h" #include <iostream> namespace Game { GLFWwindow* window; /* Initialize the library */ Rendering::Rendering() { mClock = new Clock; } Rendering::~Rendering() { shutdown(); } bool Rendering::initialize(uint width, uint height, bool fullscreen, std::string window_title) { if (!glfwInit()) { return -1; } /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return -1; } /* Make the window's context current */ glfwMakeContextCurrent(window); glViewport(0, 0, (GLsizei)width, (GLsizei)height); glOrtho(0, (GLsizei)width, (GLsizei)height, 0, 1, -1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glfwSwapInterval(1); glEnable(GL_SMOOTH); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_TEXTURE_2D); glLoadIdentity(); return true; } bool Rendering::render() { /* Loop until the user closes the window */ if (!glfwWindowShouldClose(window)) return false; /* Render here */ mClock->reset(); glfwPollEvents(); if (mClock->step()) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glfwSwapBuffers(window); mClock->update(); } return true; } void Rendering::shutdown() { glfwDestroyWindow(window); glfwTerminate(); } GLFWwindow* Rendering::getCurrentWindow() { return window; } } Renderer.h
      #pragma once namespace Game { class Clock; class Rendering { public: Rendering(); ~Rendering(); bool initialize(uint width, uint height, bool fullscreen, std::string window_title = "Rendering window"); void shutdown(); bool render(); GLFWwindow* getCurrentWindow(); private: GLFWwindow * window; Clock* mClock; }; } Timer.cpp
      #include <GL/glew.h> #include <GLFW/glfw3.h> #include <time.h> #include "Timer.h" namespace Game { Clock::Clock() : mTicksPerSecond(50), mSkipTics(1000 / mTicksPerSecond), mMaxFrameSkip(10), mLoops(0) { mLastTick = tick(); } Clock::~Clock() { } bool Clock::step() { if (tick() > mLastTick && mLoops < mMaxFrameSkip) return true; return false; } void Clock::reset() { mLoops = 0; } void Clock::update() { mLastTick += mSkipTics; mLoops++; } clock_t Clock::tick() { return clock(); } } TImer.h
      #pragma once #include "Common.h" namespace Game { class Clock { public: Clock(); ~Clock(); void update(); bool step(); void reset(); clock_t tick(); private: uint mTicksPerSecond; ufloat mSkipTics; uint mMaxFrameSkip; uint mLoops; uint mLastTick; }; } Common.h
      #pragma once #include <cstdio> #include <cstdlib> #include <ctime> #include <cstring> #include <cmath> #include <iostream> namespace Game { typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; typedef unsigned long ulong; typedef float ufloat; }  
      Game.zip
    • By lxjk
      Hi guys,
      There are many ways to do light culling in tile-based shading. I've been playing with this idea for a while, and just want to throw it out there.
      Because tile frustums are general small compared to light radius, I tried using cone test to reduce false positives introduced by commonly used sphere-frustum test.
      On top of that, I use distance to camera rather than depth for near/far test (aka. sliced by spheres).
      This method can be naturally extended to clustered light culling as well.
      The following image shows the general ideas

       
      Performance-wise I get around 15% improvement over sphere-frustum test. You can also see how a single light performs as the following: from left to right (1) standard rendering of a point light; then tiles passed the test of (2) sphere-frustum test; (3) cone test; (4) spherical-sliced cone test
       

       
      I put the details in my blog post (https://lxjk.github.io/2018/03/25/Improve-Tile-based-Light-Culling-with-Spherical-sliced-Cone.html), GLSL source code included!
       
      Eric
    • By Fadey Duh
      Good evening everyone!

      I was wondering if there is something equivalent of  GL_NV_blend_equation_advanced for AMD?
      Basically I'm trying to find more compatible version of it.

      Thank you!
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Is there an elegant way of writing straight hlsl code without preprocessor macros?

This topic is 762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

There is an approach in all samples I saw:

  1. Take an fx file in runtime.
  2. Add macros from config
  3. In runtime on user machine compile shaders with providing to FX compiler #defines
  4. Load and run the compiled code.

I can see several things I would like to avoid:

  1. FX format is deprecated
  2. Compilation time at user’s machine is not free
  3. Distribute HLSL (FX) code to user machine is not a good thing, because it can be quite fast reverse engineered.
  4. I am C++ guy, so #defines bothers me because of a lot of bugs (it is C legacy).
  5. Code become not elegant with #defines

Question 1:

When I compile HLSL code to .co object on my machine, and distribute the binary to user’s machine, is it less efficient than compilation is taking place on a user machine?

Or compilers are the same?

 

Question 2:

Is there a way to write C++ - style shaders without #defines?

For example, is it possible to make code like this without preprocessor?

 

(Target platform: Windows10+, SM 5+, no OpenGL, no consoles)

#if MSAA_
   Texture2DMS<float> DepthMap : register(t0);
#else
   Texture2D<float> DepthMap : register(t0);
#endif

#ifndef MSAASamples_
   #define MSAASamples_ 1
#endif

#if MSAA_
   uint numSamples;
   DepthMap.GetDimensions(textureSize.x, textureSize.y, numSamples);
#else
   DepthMap.GetDimensions(textureSize.x, textureSize.y);
#endif

Thanks as always

Edited by Happy SDE

Share this post


Link to post
Share on other sites
Advertisement

I don't think so, maybe shader model 6 will fix this. Of course you could create some higher level tool that generates hlsl code but that's just really moving the problem. You can pass in defines when you compile the shader but again, that's just kind of moving the problem as well.

 

Supposedly we're getting templates, virtual functions, constructors, all sorts of nonsense sometime soon.

Edited by Dingleberry

Share this post


Link to post
Share on other sites

1) Pre-compiling on your PC / late-compiling on the user's PC is the same. IMHO it's better to pre-compile and ship binary shaders to improve loading times.
 
2) You can reduce the amount of #defines required by using functions and branches.
Branching is resolved at compile time if the value is known at compile time - e.g. either DoA() or DoB() will get compiled here, and the other will be removed from the shader:

#if MSAA_
const static int frob = 42;
#else
const static int frob = 12;
#endif

void test()
{
  if( frob > 20 )
    DoA();
  else
    DoB();
}

Function overloading also lets you take different paths here without repeating the #if again:

#if MSAA_
   Texture2DMS<float> DepthMap : register(t0);
#else
   Texture2D<float> DepthMap : register(t0);
#endif

void GetDimensions( Texture2D<float> t, out uint2 textureSize, out uint numSamples)
{
  numSamples = 1;
  t.GetDimensions(textureSize.x, textureSize.y);
}
void GetDimensions( Texture2DMS<float> t, out uint2 textureSize, out uint numSamples)
{
   t.GetDimensions(textureSize.x, textureSize.y, numSamples);
}

void test()
{
  uint2 textureSize;
  uint numSamples;
  GetDimensions( DepthMap, textureSize, numSamples );

  if( numSamples <= 1 )//!!
  {
    DoStuff();
  }
}

The line marked with //!! is interesting -- when MSAA_ is true, this will actually be a real runtime branch, but when MSAA_ is false, it will be resolved at compile time!
You could fix this issue with the code below, which would let the compiler always be able to resolve the "if(numSamples<=1)" branch at compile time :wink:

void GetDimensions( Texture2DMS<float> t, out uint2 textureSize, out uint numSamples)
{
  t.GetDimensions(textureSize.x, textureSize.y, numSamples);
  numSamples = max(2,numSamples);//assumption: Texture2DMS can't have 0/1 samples...
}

SM5 also introduced dynamic linking and interfaces... Though I prefer to do things the old way :P

Edited by Hodgman

Share this post


Link to post
Share on other sites

I am C++ guy, so #defines bothers me because of a lot of bugs (it is C legacy).

While you may have some valid concerns regarding macros, this is not one of them. They were introduced to create “inlined” functions that C lacked. If you believe that this form of their use is legacy now that C++ is finally out, then you should only be bothered when macros are used as inline functions in C++.
Otherwise, they still play a vital role in C++ for cross-platform support, metrics, etc.  There is absolutely nothing “lot of bugs” about:
#if MSAA_
   Texture2DMS<float> DepthMap : register(t0);
#else
   Texture2D<float> DepthMap : register(t0);
#endif

When I compile HLSL code to .co object on my machine, and distribute the binary to user’s machine, is it less efficient than compilation is taking place on a user machine?
Or compilers are the same?

How fast the compiler compiles depends on their machine.
How fast the code is depends on the version of their compiler. There shouldn’t to much variance here—versions are likely to be the same, or theirs will be better over time.
 

Is there a way to write C++ - style shaders without #defines?

The same way you would write C++ without #define’s: Slowly and painfully.  Maybe that means copy-paste in a bunch of places.  Maybe that means writing a whole bunch of files with only slight differences in them.  Maybe it means creating wise inlined functions. Whatever you do in C++ would work in HLSL.
 

Is it possible to make code like this without preprocessor?

How would you do it in any other language? I expect you would have to create a bunch of files that are basically the same but with the small changes your macros make. Keeping them all up-to-date as you upgrade your shaders is unreasonable, and creating a mess of files just to avoid safe usage of macros is even more unreasonable.


Solution: Don’t be obsessive-compulsive about safe usage of macros. Just use them as they are to be used and make your life easier.
 
 

…than compilation is taking place on a user machine?

Compilation never takes place on the user’s machine. Your tools compile every possible permutation of the shaders and they are distributed with the game. If there are too many permutations, then your tool needs to wisely select all permutations that will actually be used. This can be done by you actually setting up the permutations to compile, and/or by running the game with a debug feature enabled that keeps track of every permutation as they are created on your end.


L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites

Though I prefer to do things the old way :P

Can I ask: why?

Because KISS.
Disciplined use of macros to select different permutations (and then using normal function calls for most of the shader) is IMHO far simpler than this interface/linking mechanism.
The interface/linking mechanism is also fairly specific to D3D11, so a shader system designed around it wouldn't port to other platforms easily.

p.s. I edited my earlier post...

Share this post


Link to post
Share on other sites

…than compilation is taking place on a user machine?

Compilation never takes place on the user’s machine. Your tools compile every possible permutation of the shaders and they are distributed with the game. If there are too many permutations, then your tool needs to wisely select all permutations that will actually be used. This can be done by you actually setting up the permutations to compile, and/or by running the game with a debug feature enabled that keeps track of every permutation as they are created on your end.

I never heard about permutation tools.

 

Right now I use default properties of HLSL file inside VS and change only few of them:

- Shader type (VS/PS/GS/CS)

- Entry point name (if I name a function different than main())

- Till now I did not use "Preprocessor Definitions" property at all.

 

Also I have vs.props file will common HLSL-related definitions that apply to all HLSL-files:

    <FxCompile>
      <ObjectFileOutput>$(OutDir)Shaders\%(Filename)</ObjectFileOutput>
      <ShaderModel>5.0</ShaderModel>
      <AdditionalIncludeDirectories>$(SolutionDir)Libs\MGL</AdditionalIncludeDirectories>
    </FxCompile>

So, VS compiles hlsl file by data, contains in vcxproj file.

That allows me have only 1 compiled shader per hlsl-file per Debug/Release.

 

 

Can anyone know:

1. Is it possible to have multiple outputs for 1 HLSL file using native Visual Studio functional (via properties of HLSL file)?

2. Is there a standard tool to compile HLSL code with all permutaions?

3. Or do I need to create it by myself?

3.1. Besides different macro permutations, what other feature can be added to the tool (I just learning to write games, so any information is welcome)?

 

Thanks.

Edited by Happy SDE

Share this post


Link to post
Share on other sites

2. Is there a standard tool to compile HLSL code with all permutaions?

No.

3. Or do I need to create it by myself?

Yes.

3.1. Besides different macro permutations, what other feature can be added to the tool (I just learning to write games, so any information is welcome)?

Whatever you want.
The tool will just call FCX.exe, which is all Microsoft® Visual Studio® is doing.
Your tool could even be just a .BAT file with a bunch of calls with different permutations, perhaps an input parameter to tell it debug or release, etc.
And you could generate all these unique permutations by playing the game and having the game log them all (generating the .BAT file for you).
But you have to be sure not to miss any.


L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites

L Spiro mentioned this, but I want to emphazise: The macros that are discouraged are the ones being used as an inline function. That's because they don't behave the way you would expect.

 

For example:

#define MIN( x, y ) ( x < y ) ? x : y;
float a = 1;
float b = 1;

float r = MIN( a++, b );
//now r = 1; a = 2;

That's because it will translate to:

float a = 0;
float b = 1;

float r = (a++ < b) ? a++ : b;

which is probably not what you intended.

 

However using #ifdef #else #endif is perfectly fine, other than becoming less pretty to the eye, or going out of hand if not being disciplined; which is the reason some people (i.e. me) prefer to run the code through your own preprocessor to ease some stuff.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement