Book Image

3D Graphics Rendering Cookbook

By : Sergey Kosarevsky, Viktor Latypov
4 (2)
Book Image

3D Graphics Rendering Cookbook

4 (2)
By: Sergey Kosarevsky, Viktor Latypov

Overview of this book

OpenGL is a popular cross-language, cross-platform application programming interface (API) used for rendering 2D and 3D graphics, while Vulkan is a low-overhead, cross-platform 3D graphics API that targets high-performance applications. 3D Graphics Rendering Cookbook helps you learn about modern graphics rendering algorithms and techniques using C++ programming along with OpenGL and Vulkan APIs. The book begins by setting up a development environment and takes you through the steps involved in building a 3D rendering engine with the help of basic, yet self-contained, recipes. Each recipe will enable you to incrementally add features to your codebase and show you how to integrate different 3D rendering techniques and algorithms into one large project. You'll also get to grips with core techniques such as physically based rendering, image-based rendering, and CPU/GPU geometry culling, to name a few. As you advance, you'll explore common techniques and solutions that will help you to work with large datasets for 2D and 3D rendering. Finally, you'll discover how to apply optimization techniques to build performant and feature-rich graphics applications. By the end of this 3D rendering book, you'll have gained an improved understanding of best practices used in modern graphics APIs and be able to create fast and versatile 3D rendering frameworks.
Table of Contents (12 chapters)

Integrating EasyProfiler 

Profiling enables developers to get vital measurement data and feedback in order to optimize the performance of their applications. EasyProfiler is a lightweight cross-platform profiler library for C++, which can be used to profile multithreaded graphical applications (https://github.com/yse/easy_profiler).

Getting ready

Our example is based on EasyProfiler version 2.1. The JSON snippet for Bootstrap to download it looks like this:

{
  "name": "easy_profiler",
  "source": {
    "type": "archive",
    "url": "https://github.com/yse/easy_profiler/            releases/download/v2.1.0/            easy_profiler-v2.1.0-msvc15-win64.zip",
    "sha1": "d7b99c2b0e18e4c6f963724c0ff3a852a34b1b07"
  }
}

There are two CMake options to set up in CMakeLists.txt so that we can build EasyProfiler without the GUI and demo samples:

set(EASY_PROFILER_NO_GUI ON CACHE BOOL "")
set(EASY_PROFILER_NO_SAMPLES ON CACHE BOOL "")

Now we are good to go and can use it in our application. The full source code for this recipe can be found in Chapter2/05_EasyProfiler.

How to do it...

Let's build a small application that integrates EasyProfiler and outputs a profiling report. Perform the following steps:

  1. First, let's initialize EasyProfiler at the beginning of our main() function:
    #include <easy/profiler.h>
    ...
    int main() {
      EASY_MAIN_THREAD;
      EASY_PROFILER_ENABLE;
      ...
  2. Now we can manually mark up blocks of code to be reported by the profiler:
      EASY_BLOCK("Create resources");
      const GLuint shaderVertex =    glCreateShader(GL_VERTEX_SHADER);
      ...
      const GLuint shaderFragment =    glCreateShader(GL_FRAGMENT_SHADER);
      ...
      GLuint perFrameDataBuffer;
      glCreateBuffers(1, &perFrameDataBuffer);
      ...
      EASY_END_BLOCK;

    Blocks can be automatically scoped. So, once we exit a C++ scope via }, the block will be automatically ended even if there is no explicit call to EASY_END_BLOCK, as shown in the following snippet:

      {
         EASY_BLOCK("Set state");
         glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
         glEnable(GL_DEPTH_TEST);
         glEnable(GL_POLYGON_OFFSET_LINE);
         glPolygonOffset(-1.0f, -1.0f);
      }
  3. Let's create some nested blocks inside the main loop. We use std::this_thread::sleep_for( std::chrono::milliseconds(2) ) to simulate some heavy computations inside blocks:
      while ( !glfwWindowShouldClose(window) ) {
         EASY_BLOCK("MainLoop");
         ...
         {
            EASY_BLOCK("Pass1");
            std::this_thread::sleep_for( 
            std::chrono::milliseconds(2) );
            ...
         }
         {
            EASY_BLOCK("Pass2");
            std::this_thread::sleep_for( 
            std::chrono::milliseconds(2) );
            ...
         }
         {
            EASY_BLOCK("glfwSwapBuffers()");
            glfwSwapBuffers(window);
         }
         {
            EASY_BLOCK("glfwPollEvents()");
            std::this_thread::sleep_for( 
            std::chrono::milliseconds(2) );
            glfwPollEvents();
         }
      }
  4. At the end of the main loop, we save the profiling data to a file like this:
    profiler::dumpBlocksToFile( "profiler_dump.prof" );

Now we can use the GUI tool to inspect the results.

How it works...

On Windows, we use the precompiled version of profiler_gui.exe, which comes with EasyProfiler:

profiler_gui.exe profiler_dump.prof

The output should look similar to the following screenshot:

Figure 2.4 – The EasyProfiler GUI

Figure 2.4 – The EasyProfiler GUI

There's more...

Blocks can have different colors, for example, EASY_BLOCK("Block1", profiler::colors::Magenta). Besides that, there is an EASY_FUNCTION() macro that will automatically create a block using the current function name as the block name. Custom ARGB colors can be used in the hexadecimal notation; for example, take a look at the following:

void bar() {
  EASY_FUNCTION(0xfff080aa);
}