Notes.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. ----------------------------------------------------------------------------------------------
  2. General longterm systems:
  3. - Camera controls + world grid
  4. - Frustum culling and octree (or some other) acceleration structure
  5. - Render queue and sorting
  6. ----------------------------------------------------------------------------------------------
  7. Reminders:
  8. - Assets manifest
  9. - I need to be able to create a list of assets used in the game. Assets referenced from the editor
  10. should be easy to account for. But assets loaded from code are special. Maybe be like Unity and allow
  11. special Resources folder?
  12. - When displaying inspector data for a component, take into consideration that it will need to be able
  13. to display that data for user created C# classes as well. AND I will most certainly have C# versions of all my
  14. components. Therefore is there any purpose of having C++ only inspector parsing code?
  15. - When I'll be doing SRGB write make sure GUI textures are handled properly. Right now they are read in gamma space, and displayed
  16. as normal, but if I switch to SRGB write then gamma would be applied twice to those textures.
  17. - Make editor background have a tileable pattern (see Mini paper app on my cell). Since tileable images wouldn't work with scale9grid, it should be an overlay over the solid background.
  18. - Maybe use the background type I used for website in WebDIP?
  19. - Use black with yellow-orange highlights (highlight = selected button, selected frame border, selected entry box, etc.)
  20. - Async callbacks. I'd like to be able to assign a callback to an async method, that will execute on the calling thread once the async operation is complete.
  21. - For example when setting PixelData for a cursor I need to get PixelData from a texture, which is an async operation, in which case I need to block the calling thread
  22. until I get the result. But I'd rather apply the result once render thread is finished.
  23. Potential optimizations:
  24. - bulkPixelConversion is EXTREMELY poorly unoptimized. Each pixel it calls a separate method that does redudant operations every pixel.
  25. - UI buffer updates
  26. - https://developer.nvidia.com/sites/default/files/akamai/gamedev/files/gdc12/Efficient_Buffer_Management_McDonald.pdf
  27. - See here how to avoid locks and stalls when updating just parts of a GUI buffer. In need to modify my mesh update code because it currently always updates 100% of the buffer.
  28. - Maybe get rid of CPU UI buffers completely.
  29. - UI shader resolution params for gui should be in a separate constant buffer
  30. ----------------------------------------------------------------------------------------------
  31. More detailed thought out system descriptions:
  32. <<<<Reducing render state changes>>>>
  33. - Transparent objects get sorted back to front, always
  34. - Opaque objects I can choose between front to back, no sort or back to front
  35. - Then sort based on material-pass combo, rendering all passes of the same material at once, then moving to next pass, then to next material, etc.
  36. - For transucent objects I need to render entire material at once, and not group by pass
  37. - Ignore individual state and textures changes, just sort based on material
  38. - Use key-based approach as described here: http://realtimecollisiondetection.net/blog/?p=86
  39. Questions/Notes:
  40. 1. Could I make use of multiple texture slots so I don't have to re-assign textures for every material when rendering translucent objects pass by pass?
  41. - When sorting back to front (or front to back) it's highly unlikely that there will be many objects sharing the same material next to the same depth level anyway.
  42. So probably ignore this problem for now, and just change the states.
  43. 2. Should sorting be done on main or render thread?
  44. - Main thread. It's highly unlikely main thread will be using as much CPU as render thread will, so free up render thread as much as possible.
  45. 3. Should oct-tree queries be done on main or render thread?
  46. - Main thread as I would need to save and copy the state of the entire scene, in order to pass it to the render thread. Otherwise we risk race conditions.
  47. 4. Since render state and shader changes are much more expensive than shader constant/buffer/mesh (and even texture) changes, it might be a good idea to sort based on these,
  48. instead of exact material? A lot of materials might share shaders and render states but not textures.
  49. 5. This guy: http://home.comcast.net/~tom_forsyth/blog.wiki.html#%5B%5BRenderstate%20change%20costs%5D%5D (who's a driver programmer) sorts all opaque objects based on shader/state
  50. into buckets, and then orders elements in these bucks in front to back order. This gives him best of two worlds, early z rejection and low state changes.
  51. <<<<DirectDraw>>>>
  52. - Used for quickly drawing something, usually for debug and editor purposes.
  53. - It consists of methods like: DrawLine, DrawPolygon, DrawCube, DrawSphere, etc.
  54. - It may also contain other fancier methods like DrawWireframeMesh, DrawWorldGrid etc.
  55. - Commands get queued from various Component::update methods and get executed at the end of frame. After they're executed they are cleared and need to be re-queued next frame.
  56. - Internally DirectDraw manages dynamic meshes so it can merge multiple DrawLine class into one and such. This can help performance, but generally performance of this class should not be a major concern.
  57. - Example uses for it:
  58. - Drawing GUI element bounds when debugging GUI
  59. - Drawing a wireframe selection effect when a mesh is selected in the scene
  60. <<<<Multithreaded memory allocator>>>>
  61. - Singlethreaded implementation from Game Engine Gems 2 book
  62. - Small and medium allocators separate for each thread. Memory overhead should be minimal considering how small the pages are. But performance benefits are great.
  63. - Large allocator just uses some simple form of page allocation and reuse, using atomics?
  64. - Must ensure that memory allocated on one thread can only be freed from that thread
  65. - How do I easily tell which allocator to call based on current thread? Require a thread ID with each alloc/dealloc?
  66. - Need to think this through more
  67. <<<<More on memory allocator>>>>
  68. - Regarding potentially often allocating large amounts of memory:
  69. - Ignore this for now. Allocating large amounts (16K+ of memory often probably won't be the case). This will only happen when modifying textures or meshes and I can assume there won't be many of such updates.
  70. - (But there will be multiple such updates per frame when it comes to GUI meshes for example)
  71. - However I should implement allocation counter in my allocator so I can know if I have a bottleneck.
  72. - For those allocations that do hit this limit I should implement a FrameAllocator. Memory is allocated during simulation step and the entire block is cleared when the frame ends.
  73. - Allocations like copying MeshData, PixelData, PassParams, etc. when queing commands for render thread should all be using this.
  74. - Problem with such allocator is safety
  75. - Allocations that are created and deleted in a single function should use a Stack allocator
  76. <<<<Resource changes and reimport>>>>
  77. Use case example:
  78. - User reimports a .gpuproginc file
  79. - Dependencies are held by CoreGpuObjectManager. Objects are required to register/unregister
  80. their dependencies in their methods manually.
  81. - Editor calls SomeClass::reimport(handle) after it detects a change
  82. - this will load a new resource, release the old one and assign the new one to Handle without changing the GUID
  83. - In order to make it thread safe force all threads to finish what they're doing and pause them until the switch is done
  84. - Should be okay considering this should only happen in edit-mode so performance isn't imperative
  85. - reimport is recursively called on all dependant objects as well.
  86. <<<<Handle multithreaded object management>>>:
  87. - Make everything that is possible immutable. Once created it cant be changed.
  88. - Example are shaders, state objects and similar
  89. - Things like Textures, Vertex, Index buffers, GpuParams may be changed
  90. - Make Vertex/Index buffers and similar only accesible from render thread. Higher level classes like meshes can have deferred methods
  91. - TODO - How to handle the remaining actually deferred methods? Like Textures?
  92. DirectX11 supports concurrent drawing and resource creation so all my resource updates should be direct calls to DX methods (I'll need a deferred context?)
  93. - DX9 doesn't so creating/updating resources should wait for render thread?
  94. - Although these are sync points which kill the whole concept of separate render thread
  95. - Updating via copy then? (DX11 driver does it internally if resource is used anyway)
  96. - OpenGL? No idea, need to study GL contexts
  97. - Although it seems DX11 also copies data when mapping/unmapping or updating on a non-immediate context. So maybe copy is the solution?
  98. So final solution:
  99. - Copy all data that will be updated on a deferred context
  100. - Make deferred context have a scratch buffer it can use for storing temporary copied data
  101. - Immediate context will execute all commands right away
  102. - This applies when rendering thread calls resource create/update internally
  103. - Or when other thread blocks and waits for rendering thread
  104. - Create a simple distinction so user knows when is something executed deferred and when immediate?
  105. - Move resource update/create methods to DeferredContext?
  106. - Not ALL methods need to be moved, only those that are resource heavy
  107. - Smaller methods may remain and always stay async, but keep internal state?
  108. - Resource creation on DX11 should be direct though, without a queue (especially if we manage to populate a resource in the same step)
  109. - Remove & replace internal data copying in GpuParamBlock (or just use a allocator instead of new())
  110. A POSSIBLY BETTER SOLUTION THAN COPYING ALL THE DATA?
  111. Classes derive from ISharedMemoryBuffer
  112. - For example PixelData, used when setting texture pixels
  113. - They have lock, unlock & clone methods
  114. - Users can choose whether they want to lock themselves out from modifying the class, or clone it, before passing it to a threaded method
  115. - Downside is that I need to do this for every class that will be used in threaded methods
  116. - Upside is that I think that is how DX handles its buffers at the moment
  117. <<<<RenderSystem needed modifications>>>>
  118. - Texture resource views (Specifying just a subresource of a texture as a shader parameter)
  119. - UAV for textures
  120. - Stream out (write vertex buffers) (DX11 and GL)
  121. - Texture buffers
  122. - Just add a special texture type? OpenGL doesn't support getting offset from within a texture buffer anyway
  123. - Tesselation (hull/domain) shader
  124. - Detachable and readable depthstencil buffer (Window buffers not required as they behave a bit differently in OpenGL)
  125. - OpenGL provides image load/store which seems to be GL UAV equivalent (http://www.opengl.org/wiki/Image_Load_Store)
  126. - Resolving MSAA textures (i.e. copying them to non-MSAA so they can be displayed on-screen). DX has ResolveSubresource, and OpenGL might have something similar.
  127. - Single and dual channel textures (especially render textures, which are very important for effects like SSAO)
  128. - Compute pipeline
  129. - Instancing (DrawInstanced) (DX11 and GL)
  130. - OpenGL append/consume buffers
  131. - Indirect drawing via indirect argument buffers
  132. - Texture arrays
  133. - Rendertargets that aren't just 2D (Volumetric (3D) render targets in particular)
  134. - Shader support for doubles
  135. - Dynamic shader linkage (Interfaces and similar)
  136. - Multisampled texture resources
  137. - Multiple adapters (multi gpu)
  138. - Passing initial data when creating a resource (DX11, but possibly GL too)
  139. - Sample mask when setting blend state (DX11, check if equivalent exists in GL)
  140. - RGBA blend factor when setting blend state(DX11, check if equivalent exists in GL)
  141. - HLSL9/HLSL11/GLSL/Cg shaders need preprocessor defines & includes
  142. - One camera -> one task (thread) approach for multithreading
  143. - Also make sure to run off a thread pool (WorkQueue class already exists that provides needed interface)
  144. - The way I handle rendering currently is to discard simulation results if gpu thread isn't finished.
  145. - This reduces input lag but at worst case scenario the effect of multithreading might be completely eliminated as
  146. GPU ends up waiting for GPU, just because it was few milliseconds late. Maybe better to wait for GPU?