
-----------------------LONGTERM TODO----------------------------------------------------------------
 - Debug tools 
   - Camera controls + world grid
 - Renderable (contains mesh/material)
 - RenderManager (iterated through Renderables and displays them on screen)
   - And its plugin implementation ForwardRenderingManager
 - Integrate with Camelot Editor
 - SceneManager plugin
   - Frustum culling and octree (or some other) acceleration structure
   - Render queue and sorting

-----------------------IMMEDIATE TODO---------------------------------------------------------------


>>>>>>>>>>FINAL SPRINT BEFORE EDITOR WORK
Once I get DX11 running make sure to test if driver complains about missing shader attributes or invalid size ones
delete d3d11 vertex declaration
 Working generic buffers
 A way to bind buffers to a Pass, while specifying buffer range
 Better creation of PrimaryWindow
  - RENDERWINDOWDESC accepts a "externalWindow" flag and an "externalHandle" so when creating the primary window with RenderSystem::initialize we don't always need to create a new window
  - Actually new OpenGL seems to support creating context without a window with the help of wglCreateContextAttribsARB and wglMakeCurrent:
 GpuParams support for bools, buffers, structs
 Static/Dynamic usage for GpuParamBlocks
 Ability to switch out GpuParamBlocks
  - Ability to mark param blocks as "Shared". Those would not get created with every pass instance and would require user to actually create and assign them
   - I can't think of any way I can do this without a preprocessor
   - (In editor it might work via GUI, but what if someone doesn't want to use the editor and only uses the engine core?)
   - Use a simple .shader file in which you specify individual GPU programs, include files, material parameters (whether they're visible
     to user or just rendering system), input parameters (useful for GLSL which doesn't have semantics as I can provide my own semantics)
	  - File format would be simple, and parser would just parse line by line, with possible option blocks
   - Issue of include files remains. How do I reference them? By path most definitely but I'd have to reference the source asset path...And in the current design source asset can move.
 Smart way of initializing GpuParamBlocks (without duplicates)
 Fix MaterialRTTI saving (Save params per gpuprogram is simplest and cleanest)
 Make sure that gpu programs assigned to Pass don't share parameters of different types
 Make RenderTexture and DepthStenciLTexture use common interface
 Fix how and when is GpuParamBlock created/destroyed. Needs to happen on the render thread
 Refactor how we handle RenderTargets (no attach/detach, and no waitForVSync propery in RenderSystem)
 waitForVsync can probably be moved somewhere other than being directly in RenderSystem? (where is it in DX11?)

 Think about how to manage resources. For example check VertexBuffer is VertexData class. How and when is that freed?
 - Big problem is also that RenderSystem in a lot of cases holds raw pointers, so what happens when resource get destroyed and RS tries to use it?

>>>>>>>>>>>>>>>START WORKING ON THE EDITOR!
 

---------------------------------------------------

<<<<Resource destruction:>>>
 Each resource has isDestroyed flag. It is set to true when the user calls destroy(). Destroy will unload all heavyweight underlying data but won't destroy the actual pointer. 
 If a destructor is called without having first called destroy() an error is logged that the resource wasn't freed properly.
 Destroy requests will always get queued up at the end of the frame, so that if render system is still using those resources, it has access to them
   - But then when and how do I actually delete the resource?
   - Also queued up? OR I don't destroy it?
   - All internal resources are handled via SharedPtrs so the lightweight reference only gets removed after all actual references are gone.
 For example unloading a texture:
  - Call texture.destroy()
    - Removes the texture from Resources and any other place that holds a known reference
    - Actually destroys the resource and sets isDestroyed flag to true
    - Reference to the empty texture continues to exist but render system need to check if its destroyed before it tries to use it

<<<<Handle multithreaded object management>>>:
 - Make everything that is possible immutable. Once created it cant be changed.
  - Example are shaders, state objects and similar
 - Things like Textures, Vertex, Index buffers, GpuParams may be changed
  - Make Vertex/Index buffers and similar only accesible from render thread. Higher level classes like meshes can have deferred methods
  - TODO - How to handle the remaining actually deferred methods? Like Textures?

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?)
 - DX9 doesn't so creating/updating resources should wait for render thread?
  - Although these are sync points which kill the whole concept of separate render thread
  - Updating via copy then? (DX11 driver does it internally if resource is used anyway)
 - OpenGL? No idea, need to study GL contexts
 - Although it seems DX11 also copies data when mapping/unmapping or updating on a non-immediate context. So maybe copy is the solution?

So final solution:
 - Copy all data that will be updated on a deferred context
  - Make deferred context have a scratch buffer it can use for storing temporary copied data
 - Immediate context will execute all commands right away
  - This applies when rendering thread calls resource create/update internally
  - Or when other thread blocks and waits for rendering thread
 - Create a simple distinction so user knows when is something executed deferred and when immediate?
  - Move resource update/create methods to DeferredContext?
    - Not ALL methods need to be moved, only those that are resource heavy
    - Smaller methods may remain and always stay async, but keep internal state?
 - Resource creation on DX11 should be direct though, without a queue (especially if we manage to populate a resource in the same step)
 - (Make sure to update GpuParamBlock implementations as they do their copying internally)


<<<<Issue of setting individual parameters on a material>>>
 - Material can contain multiple techniques
 - How do I ensure parameters are valid for all techniques?
 - Require user to create a Material parameter specification?
 - Also need a way to avoid displaying internal parameters used by the active renderer

<<<<RenderSystem needed modifications>>>>
  - Texture resource views (Specifying just a subresource of a texture as a shader parameter)
  - UAV for textures
  - Stream out (write vertex buffers) (DX11 and GL)
  - Texture buffers 
   - Just add a special texture type? OpenGL doesn't support getting offset from within a texture buffer anyway
  - Tesselation (hull/domain) shader
  - Detachable and readable depthstencil buffer (Window buffers not required as they behave a bit differently in OpenGL)
  - OpenGL provides image load/store which seems to be GL UAV equivalent (http://www.opengl.org/wiki/Image_Load_Store)
  - 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.
  - Single and dual channel textures (especially render textures, which are very important for effects like SSAO)
  - Compute pipeline
  - Instancing (DrawInstanced) (DX11 and GL)
  - OpenGL append/consume buffers
  - Indirect drawing via indirect argument buffers
  - Texture arrays
  - Rendertargets that aren't just 2D (Volumetric (3D) render targets in particular)
  - Shader support for doubles
  - Dynamic shader linkage (Interfaces and similar)
  - Multisampled texture resources
  - Multiple adapters (multi gpu)
  - Passing initial data when creating a resource (DX11, but possibly GL too)
  - Sample mask when setting blend state (DX11, check if equivalent exists in GL)
  - RGBA blend factor when setting blend state(DX11, check if equivalent exists in GL)
  - HLSL9/HLSL11/GLSL/Cg shaders need preprocessor defines & includes

Command buffer TODO:
 - Make CommandQueue not use mutexes and use atomics instead??
 - Figure out how to handle accessing texture from a non-render thread?
 - When importing a resource, and registering it with Resources I don't think it properly gets added to the loaded resources array? For some reason shaders get created twice.
 - Doing setPixels_async in the texture doesn't make sure that the user doesn't actually modify the provided PixelData after
    that call.
 - In general I need to rethink how to handle modifying resources with multithreading

-----------------------BACKLOG TODO---------------------------------------------------------------

HIGH PRIORITY TODO:
 - Issue with deserialization and value types:
  - Value types are only set at the end of deserialization, because I want to be sure all of their fields are initialized. However there is nothing stopping a custom RTTI method from accessing a (yet uninitialized) value in a ptr field. (See CmMaterialRTTI, setTexParam). I need to initialize fields in a better order.
   - Solution?: Set (empty) ptr values immediately, and only load them later
 - GetRenderOperation doesn't consider sub-meshes

Mid priority TODO:
 - monitorIndex is ignored in DX11
 - Make sure to queue up mesh deletion to make sure it gets destroyed on the render thread
 - Resource handle should store a unique integer ID, which just points to a table of GUIDs. Keeping GUID string everywhere in not efficient.
 - Add a field that tracks % of resource deserialization in BinarySerializer
 - Mesh loading:
  - Example Freefall mesh has one index per vertex, and there are 17k+ vertices. I think I need a post-process step that optimizes them.
  - Imported FBX meshes are too big
  - Search for all remaining "TODO PORT" comments and fix them
  - Ogre performed special DDS loading. I removed that. I'm not sure if I'll need to re-add it?
  - My log is not thread safe yet it is being called from multiple threads.
  - Handling of shader array parameters? This needs testing
   - I'm currently ignoring array elements in GL due to the name their names are handled
 - RTTI:
     When defining RTTIType like so: 
      RTTIType<D3D9HLSLProgram, HighLevelGpuProgram, D3D9HLSLProgramRTTI>
     I need to make sure that HighLevelGpuProgram class has valid RTTI type as well. Otherwise the inheritance hierarchy will not be correct. Right now this isn't checked anywhere.

Low priority TODO:
 - Can I assign a RenderTexture to a Pass?
 - Mesh loading:
  - Sub-meshes aren't being transformed by world matrices of their nodes
 - Are resource getting properly unloaded? e.g. when shared_ptr destroys a texture is it removed from gpu?
  - Make sure resources only get properly unloaded at end of every frame. This is because a lot of RenderSystem stuff holds raw ptrs
	to resources, and it keeps them throughout the frame (especially with deferred rendering). Plus this can only be done on the render thread anyway.
 - Remove template from RTTIType and move it to IReflectable? This way i can hopefully move GetRTTITypeStatic and GetRTTIType to IReflectable so I don't
   need to manually implement those for every method.
 - Viewport needs to be updated when I call RenderTarget::setFullscreen/finishSwitchingFullscreen/updateWindowRect/windowMovedOrResized. Currently it's not
 - D3D9Texture::createTextureResources is commented out at the moment. It gets called on device reset, and at that point I should reload texture resources.
  - I should probably keep all resources by DX managed. OpenGL apparently keeps a mirror of all its resources anyway.
 - Device reset and resource re-loading in general
 - In RTTIType it is possible to add a non-plain data type using addPlainField. This can cause memory corruption issues
   if pointer is saved/loaded as a plain field. I need to add a check that ensures the type is POD. 
   See: http://www.boost.org/doc/libs/1_51_0/boost/mpi/datatype.hpp for a possible implementation of a compile time check.
 - Fix up WorkQueue as it doesn't lock when initializing, to make sure threads are actually started before returning
 - DepthStencilBuffer & Texture should possibly share the same interface instead of being two separate classes? I'll need to assign DepthStencil to shaders sometimes. Not possible in DX9 but possible in DX11.
 - CPU reading or writing to a render texture in OpenGL is not supported. (CmGLHardwarePixelBuffer::upload/download). 
 - When saving a resource, make sure resource is properly loaded before saving
   - Add doc to Resources::save that says it will block until render thread updates the resource
   - Add documentation that tells the user that reading a resource non-async will block the thread and execute all queued render commands first
   - Remove Response handlers from Resources
- Cg doesn't work. Either remove it or get it to work
- System is not yet ready for multiple rendering contexts

Optional TODO:
 - Add precompiled headers to all projects
 - If possible, make sure GLSL uses EntryPoint and Profile fields I have added to GpuProgram
 - Move all x86 libs to x86 folders. Move all binaries to x86 folders as well
 - Serializable callbacks can't be null otherwise compiler complains
 - FBX importer can be greatly sped up by implementing a better allocator
 - Extend texture copy so it accepts different subregions & subresources (currently only entire resource can be copied)
 - Need a way to convert MSAA render texture into a normal render texture
 - Vertex buffer start offset is not supported when calling Draw methods

 -----------------------------------------------------------------------------------------------

After everything is polished
 - Each view (i.e. camera) of the scene should be put into its own thread
 - 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.
  - Asset postprocessor? Imports a regular mesh using normal importers and then postprocesses it into a specialized format?
 - Load texture mips separately so we can unload HQ textures from far away objects (like UE3)
 - Add Unified shader so I can easily switch between HLSL and GLSL shaders (they need same parameters usually, just different code)
    - 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 
	  further modifications.
 - Port boost threads to std threads (CmThreadDefines.h)
 - Remove HardwarePixelBuffer (DX11 doesn't use it, and DX9 and OpenGL textures can be rewritten so they have its methods internally)
 - Multihead device
 - 3D rendering (use low level hardware methods for it)
 
 - Don't forget to check out Unity DX11 documentation on how to implement DX11 features (http://docs.unity3d.com/Documentation/Manual/DirectX11.html)
 - Go to Game Engine Architecture book and make a list of Utility systems we will need (Config files, Parsers, File I/O etc)
 - Go to GEA book and read about resource managers before implementing them
   - Actually I should re-read most of the chapers in the book, or all of it

 - OpenGL non-Win32 window files haven't been properly parsed or tested
   - Since I probably can't compile them, try adding them to VS and see what intellisense says?

 - 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.

