Notes.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. - GUI currently doesn't batch elements belonging to different GUIWidgets because each of them has its own transform. Implement some form of instancing for DX11 and GL
  24. so this isn't required. GUIManager already has the ability to properly group meshes, all that is needed is a shader.
  25. Potential optimizations:
  26. - bulkPixelConversion is EXTREMELY poorly unoptimized. Each pixel it calls a separate method that does redudant operations every pixel.
  27. - UI buffer updates
  28. - https://developer.nvidia.com/sites/default/files/akamai/gamedev/files/gdc12/Efficient_Buffer_Management_McDonald.pdf
  29. - 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.
  30. - Maybe get rid of CPU UI buffers completely.
  31. - UI shader resolution params for gui should be in a separate constant buffer
  32. Memory allocation critical areas:
  33. - TextUtility a lot of allocs
  34. - Binding gpu params. It gets copied in DeferredRenderContext
  35. - GameObjectHandle often allocates its internal data
  36. - ResourceHandle often allocates its internal data
  37. - AsyncOp allocates AsyncOpData internally
  38. - Deserialization, a lot of temporary allocations going on - But how much impact on performance will allocations have considering this is probably limited by disk read?
  39. - Creating SceneObjects and Components - I might want to pool them, as I suspect user might alloc many per frame
  40. - Log logMsg
  41. ----------------------------------------------------------------------------------------------
  42. More detailed thought out system descriptions:
  43. <<<<SceneManager/SceneObject>>>>
  44. Two major parts:
  45. SceneObject update:
  46. - Just takes care of updating world/local transforms
  47. - Transforms are only updated when requested
  48. - Marking a transform dirty will also mark it dirty in the SceneManager
  49. - Only SceneObjects that have an Interactable type component (Renderer, Collider, etc.) will reside in SceneManager
  50. SceneManager maintains a list of world matrices, bounds and primitives
  51. - We can query them for various collision (ray, frustum, etc.)
  52. - Internally they likely use BVH or oct-tree
  53. - Uses a binary-tree (unlike SceneObject hierarchy which is n-ary), which is laid out neatly in memory for quick traversal.
  54. - What happens when object is removed/added? Tree keeps on growing, empty nodes have their space always reserved. Possibly add a method that can reduce tree size when enough empty nodes exist.
  55. Adding a node might increase tree size which will involve a memcpy while we increase the size.
  56. - (Updating an object is SceneManager should optionally transfer its world matrix to its owner object as well)
  57. This separation should work fine, as scripts requesting transforms is unlikely to be something that done often. Or not nearly as much as frustum culling and raycasting will be.
  58. How will Physics update objects (and when?)
  59. - It's unlikely there will be a massive amount of rigidbodies in the scene, so updating them should not be a huge matter of performance.
  60. Calling setTransform after physics simulation update (and before SceneManager update) should work fine.
  61. Updating children dirty whenever setPos/rot/tfrm is called is potentially slow. Can it be avoided?
  62. - Then again such updates will only be done from the simulation thread, usually from scripts so its unlikely they will be many of them
  63. Do I separate SceneObject and Transform?
  64. - I don' think I need to
  65. Make sure to let the user know SceneManager only gets updated after simulation, so changes to objects wont be applied right away.
  66. (See that he doesn't transform something and call Raycast just so it fails)
  67. - It is unlikely functionality when query results are needed right after transform will be used much.
  68. So it is acceptable to implement it like this. We might add SceneManager::forceUpdate method in case it is not acceptable.
  69. !!!BUG!!! - When I change parent I don't update individual local position/rotation/scale on scene object
  70. - Also I don't have a way of setting world pos/rot directly
  71. <<<<Reducing render state changes>>>>
  72. - Transparent objects get sorted back to front, always
  73. - Opaque objects I can choose between front to back, no sort or back to front
  74. - 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.
  75. - For transucent objects I need to render entire material at once, and not group by pass
  76. - Ignore individual state and textures changes, just sort based on material
  77. - Use key-based approach as described here: http://realtimecollisiondetection.net/blog/?p=86
  78. Questions/Notes:
  79. 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?
  80. - 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.
  81. So probably ignore this problem for now, and just change the states.
  82. 2. Should sorting be done on main or render thread?
  83. - 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.
  84. 3. Should oct-tree queries be done on main or render thread?
  85. - 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.
  86. 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,
  87. instead of exact material? A lot of materials might share shaders and render states but not textures.
  88. 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
  89. 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.
  90. <<<<Input System>>>>
  91. - Input is currently ignoring all axes except for mouse axes
  92. - Remove/Improve smoothing
  93. - Add ability to get raw or smoothed axis input for any axis (currently you can only get mouse X and Y axes)
  94. - Allow the user to map axes to custom keys. e.g. Left/right axis can have A and D keys where A returns -1, and D 1
  95. - Add a way to handle multiple devices (e.g. 2 or more joysticks)
  96. - Hook up OIS joystick callbacks and test joysticks
  97. <<<<DirectDraw>>>>
  98. - Used for quickly drawing something, usually for debug and editor purposes.
  99. - It consists of methods like: DrawLine, DrawPolygon, DrawCube, DrawSphere, etc.
  100. - It may also contain other fancier methods like DrawWireframeMesh, DrawWorldGrid etc.
  101. - 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.
  102. - 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.
  103. - Example uses for it:
  104. - Drawing GUI element bounds when debugging GUI
  105. - Drawing a wireframe selection effect when a mesh is selected in the scene
  106. <<<<Multithreaded memory allocator>>>>
  107. - Singlethreaded implementation from Game Engine Gems 2 book
  108. - Small and medium allocators separate for each thread. Memory overhead should be minimal considering how small the pages are. But performance benefits are great.
  109. - Large allocator just uses some simple form of page allocation and reuse, using atomics?
  110. - Must ensure that memory allocated on one thread can only be freed from that thread
  111. - How do I easily tell which allocator to call based on current thread? Require a thread ID with each alloc/dealloc?
  112. - Need to think this through more
  113. <<<<More on memory allocator>>>>
  114. - Regarding potentially often allocating large amounts of memory:
  115. - 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.
  116. - (But there will be multiple such updates per frame when it comes to GUI meshes for example)
  117. - However I should implement allocation counter in my allocator so I can know if I have a bottleneck.
  118. - 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.
  119. - Allocations like copying MeshData, PixelData, PassParams, etc. when queing commands for render thread should all be using this.
  120. - Problem with such allocator is safety
  121. - Allocations that are created and deleted in a single function should use a Stack allocator
  122. <<<<Resource changes and reimport>>>>
  123. Use case example:
  124. - User reimports a .gpuproginc file
  125. - Dependencies are held by CoreGpuObjectManager. Objects are required to register/unregister
  126. their dependencies in their methods manually.
  127. - Editor calls SomeClass::reimport(handle) after it detects a change
  128. - this will load a new resource, release the old one and assign the new one to Handle without changing the GUID
  129. - In order to make it thread safe force all threads to finish what they're doing and pause them until the switch is done
  130. - Should be okay considering this should only happen in edit-mode so performance isn't imperative
  131. - reimport is recursively called on all dependant objects as well.
  132. <<<<Handle multithreaded object management>>>:
  133. - Make everything that is possible immutable. Once created it cant be changed.
  134. - Example are shaders, state objects and similar
  135. - Things like Textures, Vertex, Index buffers, GpuParams may be changed
  136. - Make Vertex/Index buffers and similar only accesible from render thread. Higher level classes like meshes can have deferred methods
  137. - TODO - How to handle the remaining actually deferred methods? Like Textures?
  138. 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?)
  139. - DX9 doesn't so creating/updating resources should wait for render thread?
  140. - Although these are sync points which kill the whole concept of separate render thread
  141. - Updating via copy then? (DX11 driver does it internally if resource is used anyway)
  142. - OpenGL? No idea, need to study GL contexts
  143. - Although it seems DX11 also copies data when mapping/unmapping or updating on a non-immediate context. So maybe copy is the solution?
  144. So final solution:
  145. - Copy all data that will be updated on a deferred context
  146. - Make deferred context have a scratch buffer it can use for storing temporary copied data
  147. - Immediate context will execute all commands right away
  148. - This applies when rendering thread calls resource create/update internally
  149. - Or when other thread blocks and waits for rendering thread
  150. - Create a simple distinction so user knows when is something executed deferred and when immediate?
  151. - Move resource update/create methods to DeferredContext?
  152. - Not ALL methods need to be moved, only those that are resource heavy
  153. - Smaller methods may remain and always stay async, but keep internal state?
  154. - Resource creation on DX11 should be direct though, without a queue (especially if we manage to populate a resource in the same step)
  155. - Remove & replace internal data copying in GpuParamBlock (or just use a allocator instead of new())
  156. A POSSIBLY BETTER SOLUTION THAN COPYING ALL THE DATA?
  157. Classes derive from ISharedMemoryBuffer
  158. - For example PixelData, used when setting texture pixels
  159. - They have lock, unlock & clone methods
  160. - Users can choose whether they want to lock themselves out from modifying the class, or clone it, before passing it to a threaded method
  161. - Downside is that I need to do this for every class that will be used in threaded methods
  162. - Upside is that I think that is how DX handles its buffers at the moment
  163. <<<<RenderSystem needed modifications>>>>
  164. - Texture resource views (Specifying just a subresource of a texture as a shader parameter)
  165. - UAV for textures
  166. - Stream out (write vertex buffers) (DX11 and GL)
  167. - Texture buffers
  168. - Just add a special texture type? OpenGL doesn't support getting offset from within a texture buffer anyway
  169. - Tesselation (hull/domain) shader
  170. - Detachable and readable depthstencil buffer (Window buffers not required as they behave a bit differently in OpenGL)
  171. - OpenGL provides image load/store which seems to be GL UAV equivalent (http://www.opengl.org/wiki/Image_Load_Store)
  172. - 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.
  173. - Single and dual channel textures (especially render textures, which are very important for effects like SSAO)
  174. - Compute pipeline
  175. - Instancing (DrawInstanced) (DX11 and GL)
  176. - OpenGL append/consume buffers
  177. - Indirect drawing via indirect argument buffers
  178. - Texture arrays
  179. - Rendertargets that aren't just 2D (Volumetric (3D) render targets in particular)
  180. - Shader support for doubles
  181. - Dynamic shader linkage (Interfaces and similar)
  182. - Multisampled texture resources
  183. - Multiple adapters (multi gpu)
  184. - Passing initial data when creating a resource (DX11, but possibly GL too)
  185. - Sample mask when setting blend state (DX11, check if equivalent exists in GL)
  186. - RGBA blend factor when setting blend state(DX11, check if equivalent exists in GL)
  187. - HLSL9/HLSL11/GLSL/Cg shaders need preprocessor defines & includes
  188. - One camera -> one task (thread) approach for multithreading
  189. - Also make sure to run off a thread pool (WorkQueue class already exists that provides needed interface)
  190. - The way I handle rendering currently is to discard simulation results if gpu thread isn't finished.
  191. - This reduces input lag but at worst case scenario the effect of multithreading might be completely eliminated as
  192. GPU ends up waiting for GPU, just because it was few milliseconds late. Maybe better to wait for GPU?