daily pastebin goal
15%
SHARE
TWEET

device.h

a guest May 6th, 2016 525 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class GpuDevice : NonCopyable
  2. {
  3. public:
  4.     //--------------------------------------------------------------------------
  5.     //These functions should only be used by the thread that created the device
  6.     //--------------------------------------------------------------------------
  7.  
  8.     //Call before starting each frame. Do not render if it returns false (lost device, etc, has occurred).
  9.     bool ReadyToRender( void(*preReset)(void*), void(*postReset)(void*), void* arg=0 );
  10.    
  11.     //End a frame and queue it to be displayed to the window
  12.     void Present( TextureId );
  13.  
  14.     //On single-threaded APIs, this is the only context that directly uses the native API.
  15.     //Searching for code that calls this function is a good way to find rendering code that's explicitly single-threaded...
  16.     GpuContext& GetImmediateContext();//the 'main' context. Can ONLY be used by the thread that created the device!
  17.  
  18.     //Submit a finished deferred/compute context to the immediate context. Do not pass the immediate context into these!
  19.     void Submit( GpuContext& );
  20.     void Submit( ComputeContext& );
  21.     void Submit( DmaContext& );
  22.  
  23.     //Stall the CPU until an event has completed, retrieve it's value
  24.     u64 CpuWait( GpuEventId );
  25.  
  26.     //--------------------------------------------------------------------------
  27.     //Everything below here is thread-safe - in that multiple threads can use the device at once.
  28.     // No guarantees are made here about using other objects in parallel, such as the loaders, scopes, or resources...
  29.     //--------------------------------------------------------------------------
  30.  
  31.     GpuCapabilities GetCapabilities() const;
  32.     SingleThread ResourceLoadingThread() const;
  33.  
  34.     //Signal an event from the CPU, allowing the GPU to proceed
  35.     void CpuSignal( GpuEventId );
  36.  
  37.     // Multi-threaded command submission:
  38.         GpuContext& AcquireDeferredContext();//must be submitted to the device, or aborted
  39.                void   AbortDeferredContext( GpuContext& );//releases a deferred context without submitting
  40.            
  41.     ComputeContext& AcquireComputeContext();//must be submitted to the device, or aborted
  42.                void   AbortComputeContext( ComputeContext& );//releases a compute context without submitting
  43.            
  44.         DmaContext& AcquireDmaContext();//must be submitted to the device, or aborted
  45.                void   AbortDmaContext( DmaContext& );//releases a DMA context without submitting
  46.  
  47.     // Default data
  48.                           void GetDefaultViewportSize( int& x, int& y );
  49.     static TextureFormat::Type GetDefaultColorFormat();
  50.     static TextureFormat::Type GetDefaultDepthFormat();
  51.              const StateGroup* GetDefaultStates() const;
  52.               const PassState* DefaultPassState() const;
  53.  
  54.     // Loads a shader pack with the same lifetime as the scope object.
  55.     ShaderPackAsset* LoadShaderPack( AssetName, AssetScope&, BlobLoader& );
  56.  
  57.     ShaderTechniqueId GetTechnqiue( ShaderPackId, int techniqueIdx );
  58.     ShaderProgramsId GetGraphicsProgram( ShaderTechniqueId, u32 pass, const ShaderOptions&, u32& validCBufferMask, u32& validResourceListMask, u32& validSamplerMask, VertexFormatId& vertexFormat );
  59.     ShaderProgramsId GetComputeProgram( ShaderTechniqueId, u32 pass, const ShaderOptions&, u32& validCBufferMask, u32& validResourceListMask, u32& validSamplerMask, u32& validRwResourceListMask );
  60.  
  61.     // Loads a stream format pack with the same lifetime as the scope object.
  62.               void LoadStreamFormatPack( AssetName, AssetScope&, BlobLoader& );
  63.     StreamFormatId FindStreamFormat( u32 name );
  64.     static     u32 StreamFormatHash( const char *name );//convert a string into the format used by FindStreamFormat
  65.  
  66.     InputAssemblerLayoutId FindInputAssemblerLayout( StreamFormatId, VertexFormatId, ShaderProgramsId );
  67.  
  68.     // Textures
  69.     // If passing an ID as a recycle parameter, the internal resource allocations attached to it will be released, but the same ID will (likely) be returned with the newly requested resource allocations attached.
  70.     // The Array and Volume flags cannot be used simultanously: d is volume depth or array size. If not passing the Volume or Array flags, set d to 1.
  71.     // If the Array and Cube flags are set, the 6 2D-faces-per-cube factor is automatic -- i.e. the internal 2d-texture array size will be d*6, and the alias offset into the array will be startSlice*6.
  72.       TextureId CreateNullTexture( TextureId recycle=TextureId(0) );//reserve a TextureId but make no resource allocations. Using the recycle parameter allows internal resource to be released while retaining the ID.
  73.       TextureId CreateTexture( const char* name, TextureFlags::Type, float fullScreenRatio,         TextureFormat::Type,                     TextureId recycle=TextureId(0), CreateTextureCallback*cb=0 );
  74.       TextureId CreateTexture( const char* name, TextureFlags::Type, u32 w, u32 h, u32 d, u32 mips, TextureFormat::Type, const u8* pixels=0, TextureId recycle=TextureId(0), CreateTextureCallback*cb=0, void* vram=0, u32 vramSize=0 );
  75.       TextureId CreateTexture( const char* name, const u8* header, const u8* vram, u32 vramSize, bool ownsVram,                              TextureId recycle=TextureId(0), CreateTextureCallback*cb=0 );
  76.       TextureId CreateTextureAlias( const char* name, TextureId source, uint startSlice, uint startMip, TextureFlags::Type, u32 w, u32 h, u32 d, u32 mips, TextureFormat::Type, TextureId recycle=TextureId(0) );
  77.            void ReleaseTexture( TextureId ); // Release the ID and the internal allocations.
  78.     TextureInfo GetTextureInfo( TextureId ) const;
  79.            bool IsTextureNull( TextureId ) const; // true if there are no resource allocations attached to this ID
  80.       TextureId GetErrorTexture() const;
  81.      
  82.     // Buffers hold Vertex, Index, and other data (bytes, structures)
  83.     //      If useExistingAllocation is true, then the buffer object will use the vram pointer directly, and assume that this allocation has a longer lifetime than the buffer object itself (the buffer will not attempt to free the vram pointer).
  84.     //      If useExistingAllocation is false, and vram is not-NULL, it will be memcpy'ed into the new buffer allocation.
  85.     BufferId CreateBuffer( const char* name, const u8* vram, u32 size, BufferBindFlags::Type, UsageFlags::Type = UsageFlags::GpuRead, bool useExistingAllocation=false, BufferId recycle=BufferId(0) );
  86.         void ReleaseBuffer( BufferId );
  87.  
  88.     // ConstantBuffers supply uniform values to shaders
  89.     ConstantBufferId CreateConstantBuffer( const char* name, const void* initialData, u32 size, UsageFlags::Type = UsageFlags::CpuWrite|UsageFlags::GpuRead, ConstantBufferId recycle=ConstantBufferId(0) );
  90.     ConstantBufferId CreateConstantBuffer( ConstantBufferNative& );
  91.                 void ReleaseConstantBuffer( ConstantBufferId );
  92.     // This grants you a writable pointer into the cbuffer's memory allocation. However, you MUST call GpuContext::Synchronize after changing the data but before the data is used by a Submit call.
  93.     void* MapPersistentUnsynchronized( ConstantBufferId );
  94.     // This creates a copy of the supplied constant buffer. However, this relies on the source buffer being created with CpuRead & CpuWrite.
  95.     inline ConstantBufferId CreateConstantBuffer( const char* name, ConstantBufferId source, UsageFlags::Type usageFlags = UsageFlags::CpuWrite|UsageFlags::GpuRead, ConstantBufferId recycle=ConstantBufferId(0) )
  96.     {
  97.         u32 GetConstantBufferSize( ConstantBufferId, GpuDevice& );
  98.         return CreateConstantBuffer(name, MapPersistentUnsynchronized(source), GetConstantBufferSize(source, *this), usageFlags, recycle);
  99.     }
  100.  
  101.     // ResourceLists supply TextureIds and BufferIds to shaders
  102.     ResourceListId CreateResourceList( const char* name, u32 numItems, u32 initCount, TextureId* initialData );
  103.     ResourceListId CreateResourceList( const char* name, u32 numItems, u32 initCount=0, ResourceId* initialData=0 );
  104.              void ReleaseResourceList( ResourceListId );
  105.  
  106. protected:
  107.     ~GpuDevice(){}
  108. };
  109. //==============================================================================
  110.  
  111. //==============================================================================
  112. // Functionality common to Gpu/Compute/DMA context types.
  113. class BaseGpuContext : NoCreate
  114. {
  115. public:
  116.     GpuDevice& Device();//Get the device that owns this context.
  117. };
  118.  
  119. //==============================================================================
  120. // Allows one thread to send DMA commands to a GPU
  121. class DmaContext : public BaseGpuContext
  122. {
  123. public:
  124.     //Insert a GPU event
  125.     GpuEventId Submit( GpuFenceFlags::Type, u64 arg=0 );
  126.  
  127.     //Have the GPU stall execution of this context until an event has completed
  128.     void Wait( GpuEventId );
  129.  
  130.     //Have the GPU signal an event as having been completed
  131.     void Signal( GpuEventId );
  132.  
  133.     //Have the GPU clone one resource into another with compatible type/format/dimensions
  134.     void Copy( ResourceId dst, ResourceId src );//Copy all subresources
  135.     void Copy( ResourceId dst, const ResourceOffset& dstOffset, ResourceId src, const ResourceArea* srcArea=0 );//Copy [part of] one subresource
  136.  
  137.     //Upload new data from the CPU to GPU-resources. Best for infrequent changes to static resources.
  138.     void Update( TextureId, const void* data, u32 size, u32 mip=0, u32 arrayIdx=0 );
  139.     void Update( BufferId, const void* data, u32 size );
  140.     void Update( ConstantBufferId, const void* data, u32 size );
  141.     void Update( ResourceListId, u32 idCount, const ResourceId* ids );
  142.  
  143.     //Map GPU-resources into CPU address space. For dynamic resources, ideally use WriteNoOverwrite for efficiency.
  144.     void Map( ResourceLock&, MapMode::Type, TextureId, u8*& buffer, u32& width, u32& height, u32& pitchBytes, u32 mip=0, u32 arrayIdx=0 );//todo - move these out params into the ResourceLock
  145.     void Map( ResourceLock&, MapMode::Type, BufferId, u32 offset, u32 size );
  146.     void Map( ResourceLock&, MapMode::Type, ConstantBufferId, u32 offset=0, u32 size=0 );
  147.     void Map( ResourceLock&, MapMode::Type, ResourceListId, u32 offset=0, u32 size=0 );
  148.     void Unmap( ResourceLock&, TextureId );
  149.     void Unmap( ResourceLock&, BufferId );
  150.     void Unmap( ResourceLock&, ConstantBufferId );
  151.     void Unmap( ResourceLock&, ResourceListId );
  152.  
  153.     //For use in conjunction with GpuDevice::MapPersistentUnsynchronized. Makes sure that any changes to the mapped buffer are GPU-visible.
  154.     void Synchronize( ConstantBufferId );
  155. };
  156.  
  157. //==============================================================================
  158. // Allows one thread to send compute and DMA commands to a GPU
  159. class ComputeContext : public DmaContext
  160. {
  161. public:
  162.     //Submit a stateless compute job (dispatch call)
  163.     using DmaContext::Submit;
  164.     void Submit( const RenderPass&, const DispatchItem& );
  165.  
  166.     //Mark a texture as having undefined pixel contents.
  167.     void Discard( TextureId );
  168. };
  169.  
  170. //==============================================================================
  171. // Allows one thread to send draw, compute and DMA commands to a GPU
  172. class GpuContext : public ComputeContext
  173. {
  174. public:
  175.     using DmaContext::Submit;
  176.     using ComputeContext::Submit;
  177.  
  178.     void Finish();//Call when no more commands will be made via this context on this frame. Must be called on deferred contexts before submitting them!
  179.  
  180.     //Submit a clear command only
  181.     void Submit( const RenderPass&, const ClearCommand& c );
  182.  
  183.     //Submit a list of draw-calls, optionally clearing before the first draw
  184.     void Submit( const RenderPass&, const DrawList&, const ClearCommand* c=0 );
  185. };
  186. //==============================================================================
  187.  
  188. struct DrawList
  189. {
  190.     //Array of pointers
  191.     DrawList( u32 count, const DrawItem*const* items     ) : count(count),    items(items),              stride(sizeof(DrawItem*)),   helper() {}
  192.     DrawList( u32 count, const DrawItemKey*    items     ) : count(count),    items(&items[0].item),     stride(sizeof(DrawItemKey)),   helper() {}
  193.     DrawList( const      rde::vector<      DrawItem*>& v ) : count(v.size()), items(count?&v[0]:0),      stride(sizeof(DrawItem*)),   helper() {}
  194.     DrawList( const      rde::vector<    DrawItemKey>& v ) : count(v.size()), items(count?&v[0].item:0), stride(sizeof(DrawItemKey)), helper() {}
  195.     DrawList( const      rde::vector<const DrawItem*>& v ) : count(v.size()), items(count?&v[0]:0),      stride(sizeof(DrawItem*)),   helper() {}
  196.     template<class T> DrawList( const T& v )               : count(v.size()), items(count?&v[0]:0),      stride(sizeof(DrawItem*)),   helper() {}
  197.     //Contiguous blob of DrawItem instances
  198.     DrawList( u32 count, const DrawItem* items ) : count(count), stride(), items(), helper(items) {}
  199.     //Single DrawItem
  200.     DrawList( const DrawItem& item ) : count(1), stride(), items(&helper), helper(&item) {}
  201.  
  202.     u32 count;
  203.     u32 stride;
  204.     const void* items;
  205.     const DrawItem* helper;
  206. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top