Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 16 Aug 2010
Offline Last Active Today, 09:44 AM

#5316254 Questions about Physical based shading(PBS)?

Posted by on 22 October 2016 - 12:38 PM

Rendering is to make something visible. Usually with geometry, material and light. Shading is how light reacts with a material so it is almost the same but a subset excluding geometry.

#5315858 What programming language should someone pair with LUA?

Posted by on 19 October 2016 - 01:22 PM

The common arguments for C++ is "because it is faster".

  But for whom is it faster?


The common arguments for scripts is "because C++ is a pain to use".

  So why do you use C++ in the first place?


The common arguments for OOP in C++ is "because it is higher".

  So why do you use a low level language just to fragment the heap with allocations that Java could move to the stack automatically?


If you use a safe compiled language as a middle layer then you can get both safety and performance at the same time which I consider to be a good tradeoff for most of the code.


Scripts are supposed to allow hard coded events for dynamically loaded things so that the core of the game does not have to change for each user made level.

A compiled language (C / C++) will catch a lot of run-time bugs as compile-time errors so use it instead of scripts whenever it makes sense.

A safe compiled language (Basic / Java) can prevent access violation and memory leaks leading to robust execution for the level designer and other tools.


Whenever you use scripts, consider if function pointers, class inheritance, data flow graphs, state machines or tables can be used instead.

The simpler it is, the easier it is to maintain when the interface has to change and graphical representations are nice to have in level editors when you just want a door to open when a button is pressed.


Use scripts when:

* You have dynamically loaded content and want players to be able to make their own things

* You want to do something that cannot be done easily with a compiled language because it is not expressive enough


When it comes to performance, higher languages give faster code given that the programmer is a beginner by applying high level optimizations more freely.

A low level language cannot do high optimizations because it is too explicit and assumes a certain level of hand optimization.

When writing code in C, you can be amazed by how poor the optimization is when looking at the assembly code. (around 5% of obvious optimizations applied)

Writing the same code in a higher language can then apply most of the obvious optimizations and some that you did not even think about.


Use C/C++ when:
* You are optimizing some things on a level where a higher language has awkward workarounds

* You want to use SSE/NEON SIMD intrinsics with multi-threading

* There is a certain library that has no wrapper yet

* You want to make code that can be called from another language without heavy dependencies

* You need the generic pointers for data management

* ...

#5315563 CPU + GPU rendering with OpenGL

Posted by on 17 October 2016 - 11:37 AM

The implementation is done with gl_FragDepth now and the performance is great! :) Thanks for the help everyone.

#5315411 Questions about Physical based shading(PBS)?

Posted by on 16 October 2016 - 07:41 AM

I would not call the first method physically based since it does not even trace the path of photons.

Physically based rendering in my definition is forward ray-tracing so that a water surface can focus light to create bright spots of light.

Backward ray-tracing is a cheap trick to save performance but will not look realistic.


Not many games use forward ray-tracing because it is very slow from missing a lot of light that is never comming to the camera.


The scientifically correct way to use both reflection map and light sources is to add them together but since reflection maps are often reused approximations, you can multiply them instead so that a shiny object in a dark place will not glow from having a static reflection map.

#5315404 CPU + GPU rendering with OpenGL

Posted by on 16 October 2016 - 04:33 AM

I should try it then. :)

#5315352 Shader Permutations

Posted by on 15 October 2016 - 12:19 PM

There is not much you can do with an old OpenGL version so you should really consider upgrading for both performance and sanity.

As an ugly and slow workaround, you can wrap the shader into a class and let the class insert what the shader needs from all the data but the dynamic call overhead from using class inheritance will stall the branching predictions for out of order execution. Class objects will also ruin cache locality and load lots of cache lines with garbage even if you have an arena allocator. Even a static function call to a getter or setter will usually fail to inline and generate 16 assembly instructions of bloat just to set up all the registers and then perform the one instruction that you actually wanted to do. The API overhead for redundant safety checks when not using pipeline objects in Vulkan adds more wasted time in your render loop.

#5315344 Shader Permutations

Posted by on 15 October 2016 - 11:04 AM

Just ignore that arguments are given to the shaders in OpenGL. That is a poor legacy design just like the coupling of textures and sampler states that got stuck from old hardware refusing to be compabile with DirectX. Upload the per instance uniforms each time to make sure that no garbage is left from the previous use or you might get highly unpredictable behaviour that is very hard to debug because of the global state causing flickering colors.


If you have a lot of data to share between shaders and performance is no problem then you can also upload a texture in advance encoded with general information. I made a text editor by storing character codes in a texture and rendering them using a font atlas.

Generating lots of alternative shaders can be messy if you don't have dynamic shader linking but if it is only for enabling/disabling normal mapping and such then consider making your own GLSL precompiler or simply a function taking boolean feature flags and returning a shader string since the shader compiler in OpenGL is highly unpredictable on different graphics drivers. For example, the token concatunation operand ## is sometimes not available on certain phone models. Integer math operations may sometimes generate random noise.

Some engines even have their own shading languages but that is overkill when you can now use Spir-V and get consistent offline compilation for Vulkan instead of guessing what works everywhere.

#5315339 Shader Permutations

Posted by on 15 October 2016 - 10:12 AM

Then you would not be able to reuse the same shader with multiple materials, if I understand correct this time.

#5315329 Shader Permutations

Posted by on 15 October 2016 - 09:00 AM

I have not tried OpenGL 4.5 yet but if you go with uniform buffers or descriptor sets then you will probably apply the same principles as DirectX has used for a long time.

Large things like light sources do not have to change too often so we keep them in a buffer that rarely has to update.

Then you have data associated with instances in one buffer and another buffer for each material if you have the time left for the most frequently changed settings.


* A set of variables that update once per frame/pass.

    Light sources[]

    Camera settings

    Fog color

* A set of variables that update once per instance



* A set of variables that update once per material within the instance (optional)




If something like bone data takes extra space then additional buffers can be used only when needed.

#5315312 Shader Permutations

Posted by on 15 October 2016 - 06:41 AM

If you care that much about performance then maybe it is easier to learn descriptor sets in Vulkan that should be closer to what I tried in DirectX. Each individual uniform takes a lot of time to upload separatelly but if you pack them together in groups with correct alignment for the GPU then not using all of them is okay because it is not a lot of data. If they are not aligned then you can have memory corruption which is why the OpenGL way is said to be easier.

#5294850 Do I need to know Algorithms and Data Structures?

Posted by on 03 June 2016 - 01:57 PM

If you have lots of if statements that skips execution 99% of the time and you know some that will always be false then pre-buffering the positive and plausible ones can improve cache locality and avoid stalling hyper-threading with calculations that would otherwise be done and thrown away. Learning about broad phases is essential for both physics and game logic so that the games can scale up without lagging.

#5294691 implementation of neural network

Posted by on 02 June 2016 - 01:20 PM

Implementing genetic algorithms works excellent on the graphics card because you can try a million alternatives in each draw call and then iterate upon be best results.

Try to find a way to express data using an image and define steps of processing as image manipulations in read-paradigm.


For a single actor:

A pixel's color or location might represent a direction of some action.

Make a utility pass that gets the score of each action in a new image.

A laying 1D texture can look for the best result. Let the height be 1 if you use a 2D texture to represent it since padding is applied per row.

Read the 1D texture back to the CPU. (This will eat up 70% of your time unless you use DirectX 12 or Vulkan to run multiple things at the same time)

Do the last sweep on the CPU taking the best option and execute the action.


For multiple actors:

Each column can represent a unit and each row can represent a possible action.

The code to implement this will look a lot like spaghetti assembler code when fully optimized which is why I abandoned that strategy game when nothing more could be added.


I tried genetic algorithms on the GPU using DirectX and got really nice results but the shader compiler will fail 90% of the time when trying to do anything advanced like physical simulation because it was only tested for graphics.


Vulkan is still in Alpha stage with drivers missing but there you will be able to choose other compilers using SPIR-V intermediate code. What I don't like about Vulkan is that sampler states are connected to views and you have to create pipeline objects. Vulkan might win over DirectX 12 by simply supporting Windows 7 and phones.

#5294686 Building a game like "Game of War"

Posted by on 02 June 2016 - 01:03 PM

Everyone takes ideas or images from somewhere and changes it into their own. So yea. I can take images and change them and nobody would know.

I can look at anything and draw it and change it so it becomes my own. Art works is no problem.

So I can't use just unity? I'll need an art program like gimp? Someone earlier stated all I need is unity. :( I'm confused.

My game has no story. Lol. It doesn't need a story. Here is the story. U r on a map to build the most powerful kingdom. End of story.

Is network programming hard? I understand I have a year to get this game done. I need all the help I can get.

In simplicity. Step by step. Write down the game and mechanics and have a game name that's legal. Do the art work. Next is use gimp to create the art then transfer it to unity. Then use JavaScript to create the code and game mechanics. Then I need networking online for an mmo. Is this about right?
The hardest part to all of this will be learning how to use unity and javascript. I really only need to learn what I need to develope the game. I don't need all.

Would anyone be interested in helping to build the game? As far as advertising. Key words are 65% then 35% word of mouth. I'm not worried about the game being popular. I based my facts on realistic facts and what people like. :)


Everyone who reads your post knows. I would never steal artwork.

#5294684 Switching from C++ to Java

Posted by on 02 June 2016 - 12:49 PM

In a university you will learn the basics of functions, subroutines, lists, sorting, recursion, hashing and tree structures before diving into object orientation since there are many things that can go wrong with inheritance if you start to inherit something as soon as you want to reuse its code. I dislike using classes in C++ because abstraction without memory protection can make it harder to see why the application is crashing while a plain C subset would make it obvious but that might be because I learned C before C++.


For learning programming, Java would make it a lot more difficult to practice functional programming because Java outlaws global functions which is very annoying for functional purists and modular programmers. You might have a course in the first year where you have to write an operating system kernel without using dynamic memory allocation.

#5294677 New fast way to loop!

Posted by on 02 June 2016 - 12:07 PM

If b is written to without being used then a good compiler will cheat and only execute the last iteration of the loop as the first iterations are dead code.


If modified to read the previous result then, the Hexagon compiler for Snapdragon's aDSP would divide the loop into start, multi instruction machine words and termination to run 4 iterations in the same machine instruction with a zero cost hardware loop.