Notes.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. Reminders:
  2. - When I'll be doing SRGB write make sure GUI textures are handled properly. Right now they are read in gamma space, and displayed
  3. as normal, but if I switch to SRGB write then gamma would be applied twice to those textures.
  4. - 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.
  5. - 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
  6. until I get the result. But I'd rather apply the result once render thread is finished.
  7. - 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
  8. so this isn't required. GUIManager already has the ability to properly group meshes, all that is needed is a shader.
  9. - When specifying GUIElement layout using (for example) GUILayoutOptions::expandableX then it would be useful if I didn't have to provide the Y height, and instead make it use
  10. the default value for that GUIElement type. For most elements I will only be changing width, and height will remain default (labels, buttons, toggles, drop down lists, etc.)
  11. so this would be helpful in a way I wouldn't need to look up actual element height in EngineGUI.
  12. - GUI currently ignores tooltips in GUIContent.
  13. - A way to initialize BansheeEngine without RenderSystem or any kind of UI. So that it may be used for server building as well.
  14. - Calls to "initialize()" should be protected. For example GUIWidget and all its derived classes require the user to separately call initialize() after construction.
  15. However a much better option would be to wrap construction and initialization in a create() method and make initialize protected.
  16. - GUIWidget needs to be added using addComponent which makes "create()" method not practical. However I have trouble seeing the need for initialize(),
  17. it could be replaced with a variable parameter version of addComponent.
  18. - Profiling: Create an easy to browse list of all loaded Resources (similar to Hierarchy in Unity, just for loaded resources)
  19. - Possibly also for all core objects
  20. - GUI ignores image in GUIContent for most elements
  21. - Each view (i.e. camera) of the scene should be put into its own thread
  22. - How do I handle multiple mesh formats? Some files need animation, other don't. Some would mabye like to use QTangent, others the proper tangent frame.
  23. - Asset postprocessor? Imports a regular mesh using normal importers and then postprocesses it into a specialized format?
  24. - Load texture mips separately so we can unload HQ textures from far away objects (like UE3)
  25. - Add Unified shader so I can easily switch between HLSL and GLSL shaders (they need same parameters usually, just different code)
  26. - Maybe just add support for Cg and force everyone to use that? - I'd like to be able to just switch out renderer in a single location and that everything keeps on working without
  27. further modifications.
  28. - Remove HardwarePixelBuffer (DX11 doesn't use it, and DX9 and OpenGL textures can be rewritten so they have its methods internally)
  29. - Multihead device
  30. - Don't forget to check out Unity DX11 documentation on how to implement DX11 features (http://docs.unity3d.com/Documentation/Manual/DirectX11.html)
  31. - Go to Game Engine Architecture book and make a list of Utility systems we will need (Config files, Parsers, File I/O etc)
  32. - Go to GEA book and read about resource managers before implementing them
  33. - Actually I should re-read most of the chapers in the book, or all of it
  34. - OpenGL non-Win32 window files haven't been properly parsed or tested
  35. - Since I probably can't compile them, try adding them to VS and see what intellisense says?
  36. - Textures and all other buffers keep a copy of their data in system memory. If there are memory constraints we might need a way to avoid this.
  37. - Make sure my Log system uses XML + HTML
  38. - There is an issue that custom-UIs won't have their mesh shared. For example most game UIs will be advanced and will
  39. likely use on GUIWidget per element. However currently I only perform batching within a single widget which
  40. doesn't help in the mentioned case.
  41. - Later add InputMap class in which you can bind certain actions (like move left, fire, etc.) to Keyboard, Joystick or Mouse buttons.
  42. - Also ensure button combinations are possible. e.g. on keyboard I might want to press F1 to open debug menu, but on joystick it might be A+B+X
  43. - Add a field that tracks % of resource deserialization in BinarySerializer
  44. - Add GL Texture buffers (They're equivalent to DX11 buffers) - http://www.opengl.org/wiki/Buffer_Texture
  45. - Instead of doing setThisPtr on every CoreGpuObject, use intrusive shared_ptr instead?
  46. - I should consider creating two special Mesh types:
  47. StreamMesh - constantly updated by CPU and read by GPU
  48. ReadMesh - written by GPU and easily read by CPU
  49. - OpenGL especially has no good way of reading or streaming data. It has special STREAM and COPY buffer types which I never use.
  50. - (EXTREMELY LOW PRIORITY) Scripting: It might be good to make Mono classes more generic and move them to BansheeEngine.
  51. e.g. MonoClass -> ScriptClass, where ScriptClass is just an abstract interface. Then I don't expose any Mono stuff to actually script libraries like
  52. SBansheeEngine. User could then fairly easily port the system to another scripting language just by implementing another ScriptSystem.
  53. - This would probably come with an overhead of at least one extra function call for each script call, which is currently unacceptable
  54. considering that most people will definitely won't be writing new script systems.
  55. - Perhaps add code generation functionality to the engine through Mono? I know Mono has support for it though its Embed interface
  56. - Add instancing to engine: All I really need to add are per-instance attributes in VertexData (and MeshData). And then RenderSystem::renderInstance method that also accepts an instance count.
  57. - Debug console
  58. - Add ability to add colors tags like <color=#123>
  59. - When showing a debug message, also provide a (clickable?) reference to Component it was triggered on (if applicable)
  60. - It really helps when you get an error on a Component that hundreds of SceneObjects use
  61. - When displaying an error with a callstack, make each line of the callstack clickable where it opens the external editor
  62. - std::function allocates memory but I have no got way of using custom allocators as I'd have to wrap std::bind and that seems non-trivial
  63. - Add a TaskScheduler profiler that neatly shows time slices of each task and on which thread they are run on
  64. Potential optimizations:
  65. - bulkPixelConversion is EXTREMELY poorly unoptimized. Each pixel it calls a separate method that does redudant operations every pixel.
  66. - UI shader resolution params for gui should be in a separate constant buffer
  67. ----------------------------------------------------------------------------------------------
  68. More detailed thought out system descriptions:
  69. <<<<Memory allocation critical areas>>>>
  70. - Binding gpu params. It gets copied in DeferredRenderContext
  71. - GameObjectHandle often allocates its internal data
  72. - ResourceHandle often allocates its internal data
  73. - AsyncOp allocates AsyncOpData internally
  74. - 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?
  75. - Creating SceneObjects and Components - I might want to pool them, as I suspect user might alloc many per frame
  76. - Log logMsg
  77. <<Multithreaded GUI rendering>>
  78. - Event handling and normal "update" will still be done on the main thread
  79. - At the beginning of each frame a GUI mesh update is queued on the GUI thread
  80. - Since we're queuing the update at the beggining of the frame we will be using last frames transform and gui element states.
  81. - When queing we need to make sure to store GUIWidget transform, and specific element states (e.g. "text" in GUILabel)
  82. - At the end of simulation frame wait until GUI update is complete. After both simulation and GUI updates are complete, proceed with submitting it to render system.
  83. <<Figure out how to store texture references in a font>>
  84. - Currently I store a copy of the textures but how do I automatically update the font if they change?
  85. - Flesh out the dependencies system?
  86. - I can import texture as normal, and keep it as an actual TextureHandle, only keep it hidden
  87. if it was created automatically (by FontImporter) for example?
  88. - But then who deletes the texture?
  89. - Set up an "internalResource" system where resources hold references to each other and also release them?
  90. - In inspector they can be expanded as children of the main resource, but cannot be directly modified?
  91. - Deleting the main resource deletes the children too
  92. <<<<SceneManager/SceneObject>>>>
  93. Two major parts:
  94. SceneObject update:
  95. - Just takes care of updating world/local transforms
  96. - Transforms are only updated when requested
  97. - Marking a transform dirty will also mark it dirty in the SceneManager
  98. - Only SceneObjects that have an Interactable type component (Renderer, Collider, etc.) will reside in SceneManager
  99. SceneManager maintains a list of world matrices, bounds and primitives
  100. - We can query them for various collision (ray, frustum, etc.)
  101. - Internally they likely use BVH or oct-tree
  102. - Uses a binary-tree (unlike SceneObject hierarchy which is n-ary), which is laid out neatly in memory for quick traversal.
  103. - 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.
  104. Adding a node might increase tree size which will involve a memcpy while we increase the size.
  105. - (Updating an object is SceneManager should optionally transfer its world matrix to its owner object as well)
  106. 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.
  107. How will Physics update objects (and when?)
  108. - 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.
  109. Calling setTransform after physics simulation update (and before SceneManager update) should work fine.
  110. Updating children dirty whenever setPos/rot/tfrm is called is potentially slow. Can it be avoided?
  111. - Then again such updates will only be done from the simulation thread, usually from scripts so its unlikely they will be many of them
  112. Do I separate SceneObject and Transform?
  113. - I don' think I need to
  114. Make sure to let the user know SceneManager only gets updated after simulation, so changes to objects wont be applied right away.
  115. (See that he doesn't transform something and call Raycast just so it fails)
  116. - It is unlikely functionality when query results are needed right after transform will be used much.
  117. So it is acceptable to implement it like this. We might add SceneManager::forceUpdate method in case it is not acceptable.
  118. !!!BUG!!! - When I change parent I don't update individual local position/rotation/scale on scene object
  119. - Also I don't have a way of setting world pos/rot directly
  120. <<<<Reducing render state changes>>>>
  121. - Transparent objects get sorted back to front, always
  122. - Opaque objects I can choose between front to back, no sort or back to front
  123. - 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.
  124. - For transucent objects I need to render entire material at once, and not group by pass
  125. - Ignore individual state and textures changes, just sort based on material
  126. - Use key-based approach as described here: http://realtimecollisiondetection.net/blog/?p=86
  127. Questions/Notes:
  128. 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?
  129. - 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.
  130. So probably ignore this problem for now, and just change the states.
  131. 2. Should sorting be done on main or render thread?
  132. - 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.
  133. 3. Should oct-tree queries be done on main or render thread?
  134. - 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.
  135. 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,
  136. instead of exact material? A lot of materials might share shaders and render states but not textures.
  137. 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
  138. 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.
  139. <<<<Input System>>>>
  140. - Input is currently ignoring all axes except for mouse axes
  141. - Remove/Improve smoothing
  142. - Add ability to get raw or smoothed axis input for any axis (currently you can only get mouse X and Y axes)
  143. - 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
  144. - Add a way to handle multiple devices (e.g. 2 or more joysticks)
  145. - Hook up OIS joystick callbacks and test joysticks
  146. <<<<DirectDraw>>>>
  147. - Used for quickly drawing something, usually for debug and editor purposes.
  148. - It consists of methods like: DrawLine, DrawPolygon, DrawCube, DrawSphere, etc.
  149. - It may also contain other fancier methods like DrawWireframeMesh, DrawWorldGrid etc.
  150. - 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.
  151. - 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.
  152. - Example uses for it:
  153. - Drawing GUI element bounds when debugging GUI
  154. - Drawing a wireframe selection effect when a mesh is selected in the scene
  155. <<<<Multithreaded memory allocator>>>>
  156. - Singlethreaded implementation from Game Engine Gems 2 book
  157. - Small and medium allocators separate for each thread. Memory overhead should be minimal considering how small the pages are. But performance benefits are great.
  158. - Large allocator just uses some simple form of page allocation and reuse, using atomics?
  159. - Must ensure that memory allocated on one thread can only be freed from that thread
  160. - How do I easily tell which allocator to call based on current thread? Require a thread ID with each alloc/dealloc?
  161. - Need to think this through more
  162. <<<<More on memory allocator>>>>
  163. - Regarding potentially often allocating large amounts of memory:
  164. - 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.
  165. - (But there will be multiple such updates per frame when it comes to GUI meshes for example)
  166. - However I should implement allocation counter in my allocator so I can know if I have a bottleneck.
  167. - 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.
  168. - Allocations like copying MeshData, PixelData, PassParams, etc. when queing commands for render thread should all be using this.
  169. - Problem with such allocator is safety
  170. - Allocations that are created and deleted in a single function should use a Stack allocator
  171. <<<<Resource changes and reimport>>>>
  172. Use case example:
  173. - User reimports a .gpuproginc file
  174. - Dependencies are held by CoreGpuObjectManager. Objects are required to register/unregister
  175. their dependencies in their methods manually.
  176. - Editor calls SomeClass::reimport(handle) after it detects a change
  177. - this will load a new resource, release the old one and assign the new one to Handle without changing the GUID
  178. - In order to make it thread safe force all threads to finish what they're doing and pause them until the switch is done
  179. - Should be okay considering this should only happen in edit-mode so performance isn't imperative
  180. - reimport is recursively called on all dependant objects as well.
  181. <<<<Handle multithreaded object management>>>:
  182. - Make everything that is possible immutable. Once created it cant be changed.
  183. - Example are shaders, state objects and similar
  184. - Things like Textures, Vertex, Index buffers, GpuParams may be changed
  185. - Make Vertex/Index buffers and similar only accesible from render thread. Higher level classes like meshes can have deferred methods
  186. - TODO - How to handle the remaining actually deferred methods? Like Textures?
  187. 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?)
  188. - DX9 doesn't so creating/updating resources should wait for render thread?
  189. - Although these are sync points which kill the whole concept of separate render thread
  190. - Updating via copy then? (DX11 driver does it internally if resource is used anyway)
  191. - OpenGL? No idea, need to study GL contexts
  192. - Although it seems DX11 also copies data when mapping/unmapping or updating on a non-immediate context. So maybe copy is the solution?
  193. So final solution:
  194. - Copy all data that will be updated on a deferred context
  195. - Make deferred context have a scratch buffer it can use for storing temporary copied data
  196. - Immediate context will execute all commands right away
  197. - This applies when rendering thread calls resource create/update internally
  198. - Or when other thread blocks and waits for rendering thread
  199. - Create a simple distinction so user knows when is something executed deferred and when immediate?
  200. - Move resource update/create methods to DeferredContext?
  201. - Not ALL methods need to be moved, only those that are resource heavy
  202. - Smaller methods may remain and always stay async, but keep internal state?
  203. - Resource creation on DX11 should be direct though, without a queue (especially if we manage to populate a resource in the same step)
  204. - Remove & replace internal data copying in GpuParamBlock (or just use a allocator instead of new())
  205. A POSSIBLY BETTER SOLUTION THAN COPYING ALL THE DATA?
  206. Classes derive from ISharedMemoryBuffer
  207. - For example PixelData, used when setting texture pixels
  208. - They have lock, unlock & clone methods
  209. - Users can choose whether they want to lock themselves out from modifying the class, or clone it, before passing it to a threaded method
  210. - Downside is that I need to do this for every class that will be used in threaded methods
  211. - Upside is that I think that is how DX handles its buffers at the moment
  212. <<<<RenderSystem needed modifications>>>>
  213. - Texture resource views (Specifying just a subresource of a texture as a shader parameter)
  214. - UAV for textures
  215. - Stream out (write vertex buffers) (DX11 and GL)
  216. - Texture buffers
  217. - Just add a special texture type? OpenGL doesn't support getting offset from within a texture buffer anyway
  218. - Tesselation (hull/domain) shader
  219. - Detachable and readable depthstencil buffer (Window buffers not required as they behave a bit differently in OpenGL)
  220. - OpenGL provides image load/store which seems to be GL UAV equivalent (http://www.opengl.org/wiki/Image_Load_Store)
  221. - 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.
  222. - Single and dual channel textures (especially render textures, which are very important for effects like SSAO)
  223. - Compute pipeline
  224. - Instancing (DrawInstanced) (DX11 and GL)
  225. - OpenGL append/consume buffers
  226. - Indirect drawing via indirect argument buffers
  227. - Texture arrays
  228. - Rendertargets that aren't just 2D (Volumetric (3D) render targets in particular)
  229. - Shader support for doubles
  230. - Dynamic shader linkage (Interfaces and similar)
  231. - Multisampled texture resources
  232. - Multiple adapters (multi gpu)
  233. - Passing initial data when creating a resource (DX11, but possibly GL too)
  234. - Sample mask when setting blend state (DX11, check if equivalent exists in GL)
  235. - RGBA blend factor when setting blend state(DX11, check if equivalent exists in GL)
  236. - HLSL9/HLSL11/GLSL/Cg shaders need preprocessor defines & includes
  237. - One camera -> one task (thread) approach for multithreading
  238. - Also make sure to run off a thread pool (WorkQueue class already exists that provides needed interface)
  239. - The way I handle rendering currently is to discard simulation results if gpu thread isn't finished.
  240. - This reduces input lag but at worst case scenario the effect of multithreading might be completely eliminated as
  241. GPU ends up waiting for GPU, just because it was few milliseconds late. Maybe better to wait for GPU?
  242. <<<Localization notes for MUCH LATER>>>
  243. - It would be nice if HString identifier hash was being generated at compile time
  244. - I still need an easy way to edit the string table (Editor, importer or similar)
  245. - I might need font localization for non-standard character sets (e.g. russian, greek, asian, etc.)
  246. - I probably don't want to use one huge set of textures containing both latin and asian characters but want to keep them separate
  247. - Also asian sets might be too large for textures, in which case generating them at runtime might be necessary (or parsing string table and
  248. generating textures from only used characters)