diff --git a/src/Layers/xrRender/BufferUtils.h b/src/Layers/xrRender/BufferUtils.h new file mode 100644 index 00000000000..846ce4ce2d0 --- /dev/null +++ b/src/Layers/xrRender/BufferUtils.h @@ -0,0 +1,142 @@ +#pragma once + +u32 GetFVFVertexSize(u32 FVF); +u32 GetDeclVertexSize(const VertexElement* decl, DWORD Stream); +u32 GetDeclLength(const VertexElement* decl); + +struct SDeclaration; + +void ConvertVertexDeclaration(u32 FVF, SDeclaration* decl); +void ConvertVertexDeclaration(const VertexElement* dxdecl, SDeclaration* decl); +void ConvertVertexDeclaration(const xr_vector& declIn, xr_vector& declOut); + +namespace BufferUtils +{ +HRESULT CreateVertexBuffer(VertexBufferHandle* pBuffer, const void* pData, UINT DataSize, bool bImmutable = true); +HRESULT CreateIndexBuffer(IndexBufferHandle* pBuffer, const void* pData, UINT DataSize, bool bImmutable = true); +HRESULT CreateConstantBuffer(ConstantBufferHandle* ppBuffer, UINT DataSize); +}; + +/** + * Staging buffer abstraction provides a convenient way to upload vertex or index data into GPU memory. + * It uses intermediate heap storage to handle host data which can be discarded or preserved for future + * use in accordance to allocation policy. + * + * Two types of allocation available: + * 1. Push once + * Such buffers can be mapped only for write and no data read back is available. Use this mode for cases + * when you have static geometry which you are going to upload only once and forget about it. To + * improve memory use the intermediate buffer will be discarded as soon as `Unmap()` called. + * 2. Persistent + * In this case unmap operation doesn't discard heap storage content which can be accessed in order + * to read the data back. Mapping with read flag can be done multiple times but you won't be able to + * modify anything. The intermediate storage can be discarded explicitly to reclaim host memory. + */ + +class VertexStagingBuffer +{ +public: + VertexStagingBuffer(); + ~VertexStagingBuffer(); + + void Create(size_t size, bool allowReadBack = false); + bool IsValid() const; + void* Map(size_t offset = 0, size_t size = 0, bool read = false); + void Unmap(bool doFlush = false); + VertexBufferHandle GetBufferHandle() const; + void DiscardHostBuffer(); + + void AddRef() + { + ++m_RefCounter; + } + + u32 Release() + { + VERIFY2(m_RefCounter, "Attempt to release unused object"); + --m_RefCounter; + if (m_RefCounter == 0) + Destroy(); + return m_RefCounter; + } + + operator VertexBufferHandle() const + { + return m_DeviceBuffer; + } + + bool VertexStagingBuffer::operator==(VertexBufferHandle other) const + { + return other == m_DeviceBuffer; + } + bool VertexStagingBuffer::operator==(const VertexStagingBuffer& other) const + { + return other.m_DeviceBuffer == m_DeviceBuffer; + } + + size_t GetSystemMemoryUsage() const; + size_t GetVideoMemoryUsage() const; + +private: + void Destroy(); + + VertexBufferHandle m_DeviceBuffer; + HostBufferHandle m_HostBuffer; + size_t m_Size{ 0 }; + u32 m_RefCounter{ 0 }; + bool m_AllowReadBack{ false }; // specifies whether host will want to have the data back (e.g. skinning code) +}; + +class IndexStagingBuffer +{ +public: + IndexStagingBuffer(); + ~IndexStagingBuffer(); + + void Create(size_t size, bool allowReadBack = false); + bool IsValid() const; + void* Map(size_t offset = 0, size_t size = 0, bool read = false); + void Unmap(bool doFlush = false); + IndexBufferHandle GetBufferHandle() const; + void DiscardHostBuffer(); + + void AddRef() + { + ++m_RefCounter; + } + + u32 Release() + { + VERIFY2(m_RefCounter, "Attempt to release unused object"); + --m_RefCounter; + if (m_RefCounter == 0) + Destroy(); + return m_RefCounter; + } + + operator IndexBufferHandle() const + { + return m_DeviceBuffer; + } + + bool IndexStagingBuffer::operator==(IndexBufferHandle other) const + { + return other == m_DeviceBuffer; + } + bool IndexStagingBuffer::operator==(const IndexStagingBuffer& other) const + { + return other.m_DeviceBuffer == m_DeviceBuffer; + } + + size_t GetSystemMemoryUsage() const; + size_t GetVideoMemoryUsage() const; + +private: + void Destroy(); + + IndexBufferHandle m_DeviceBuffer; + HostBufferHandle m_HostBuffer; + size_t m_Size{ 0 }; + u32 m_RefCounter{ 0 }; + bool m_AllowReadBack{ false }; // specifies whether host will want to have the data back (e.g. skinning code) +}; diff --git a/src/Layers/xrRender/D3DUtils.cpp b/src/Layers/xrRender/D3DUtils.cpp index 9d75f08b690..e810eca2710 100644 --- a/src/Layers/xrRender/D3DUtils.cpp +++ b/src/Layers/xrRender/D3DUtils.cpp @@ -113,34 +113,24 @@ u32 m_ColorSafeRect = 0xffB040B0; void SPrimitiveBuffer::CreateFromData( D3DPRIMITIVETYPE _pt, u32 _p_cnt, u32 FVF, LPVOID vertices, u32 _v_cnt, u16* indices, u32 _i_cnt) { -#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL) -// TODO: DX10: Implement SPrimitiveBuffer::CreateFromData for DX10 -// VERIFY(!"SPrimitiveBuffer::CreateFromData not implemented for dx10"); -#else // USE_DX10 - ID3DVertexBuffer* pVB = nullptr; - ID3DIndexBuffer* pIB = nullptr; p_cnt = _p_cnt; p_type = _pt; v_cnt = _v_cnt; i_cnt = _i_cnt; - u32 stride = D3DXGetFVFVertexSize(FVF); - R_CHK(HW.pDevice->CreateVertexBuffer(v_cnt * stride, D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &pVB, nullptr)); - HW.stats_manager.increment_stats_vb(pVB); - u8* bytes; - R_CHK(pVB->Lock(0, 0, (LPVOID*)&bytes, 0)); + u32 stride = GetFVFVertexSize(FVF); + pVB.Create(v_cnt * stride); + u8* bytes = static_cast(pVB.Map()); FLvertexVec verts(v_cnt); for (u32 k = 0; k < v_cnt; ++k) verts[k].set(((Fvector*)vertices)[k], 0xFFFFFFFF); memcpy(bytes, &*verts.begin(), v_cnt * stride); - R_CHK(pVB->Unlock()); + pVB.Unmap(true); // upload vertex data if (i_cnt) { - R_CHK(HW.pDevice->CreateIndexBuffer( - i_cnt * sizeof(u16), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &pIB, NULL)); - HW.stats_manager.increment_stats_ib(pIB); - R_CHK(pIB->Lock(0, 0, (LPVOID*)&bytes, 0)); + pIB.Create(i_cnt * sizeof(u16)); + bytes = static_cast(pIB.Map()); memcpy(bytes, indices, i_cnt * sizeof(u16)); - R_CHK(pIB->Unlock()); + pIB.Unmap(true); // upload index data OnRender.bind(this, &SPrimitiveBuffer::RenderDIP); } else @@ -148,20 +138,15 @@ void SPrimitiveBuffer::CreateFromData( OnRender.bind(this, &SPrimitiveBuffer::RenderDP); } pGeom.create(FVF, pVB, pIB); -#endif // USE_DX10 } void SPrimitiveBuffer::Destroy() { -#ifndef USE_OGL if (pGeom) { - HW.stats_manager.decrement_stats_vb(pGeom->vb); - HW.stats_manager.decrement_stats_ib(pGeom->ib); - _RELEASE(pGeom->vb); - _RELEASE(pGeom->ib); + pIB.Release(); + pVB.Release(); pGeom.destroy(); } -#endif // !USE_OGL } void CDrawUtilities::UpdateGrid(int number_of_cell, float square_size, int subdiv) diff --git a/src/Layers/xrRender/D3DUtils.h b/src/Layers/xrRender/D3DUtils.h index 34656b471ec..3b7b4a3de20 100644 --- a/src/Layers/xrRender/D3DUtils.h +++ b/src/Layers/xrRender/D3DUtils.h @@ -17,6 +17,8 @@ struct SPrimitiveBuffer { + IndexStagingBuffer pIB; + VertexStagingBuffer pVB; ref_geom pGeom; u32 v_cnt; u32 i_cnt; diff --git a/src/Layers/xrRender/DetailManager.cpp b/src/Layers/xrRender/DetailManager.cpp index 29caa6668e7..9f864770fb4 100644 --- a/src/Layers/xrRender/DetailManager.cpp +++ b/src/Layers/xrRender/DetailManager.cpp @@ -80,8 +80,6 @@ CDetailManager::CDetailManager() : xrc("detail manager") dtSlots = nullptr; hw_Geom = nullptr; hw_BatchSize = 0; - hw_VB = 0; - hw_IB = 0; m_time_rot_1 = 0; m_time_rot_2 = 0; m_time_pos = 0; diff --git a/src/Layers/xrRender/DetailManager.h b/src/Layers/xrRender/DetailManager.h index 8a0d5297fff..b2707ce7205 100644 --- a/src/Layers/xrRender/DetailManager.h +++ b/src/Layers/xrRender/DetailManager.h @@ -173,13 +173,10 @@ class ECORE_API CDetailManager // Hardware processor ref_geom hw_Geom; size_t hw_BatchSize; -#ifdef USE_OGL - GLuint hw_VB; - GLuint hw_IB; -#else - ID3DVertexBuffer* hw_VB; - ID3DIndexBuffer* hw_IB; -#endif // USE_OGL + + VertexStagingBuffer hw_VB; + IndexStagingBuffer hw_IB; + ref_constant hwc_consts; ref_constant hwc_wave; ref_constant hwc_wind; diff --git a/src/Layers/xrRender/DetailManager_VS.cpp b/src/Layers/xrRender/DetailManager_VS.cpp index c9e0bc4c796..7117710755a 100644 --- a/src/Layers/xrRender/DetailManager_VS.cpp +++ b/src/Layers/xrRender/DetailManager_VS.cpp @@ -8,8 +8,7 @@ #include "xrEngine/IGame_Persistent.h" #include "xrEngine/Environment.h" #endif -#include "Layers/xrRenderDX10/dx10BufferUtils.h" -#include "Layers/xrRenderGL/glBufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" const int quant = 16384; const int c_hdr = 10; @@ -58,30 +57,12 @@ void CDetailManager::hw_Load_Geom() } u32 vSize = sizeof(vertHW); Msg("* [DETAILS] %d v(%d), %d p", dwVerts, vSize, dwIndices / 3); - -#if !defined(USE_DX10) && !defined(USE_DX11) && !defined(USE_OGL) - // Determine POOL & USAGE - u32 dwUsage = D3DUSAGE_WRITEONLY; - - // Create VB/IB - R_CHK(HW.pDevice->CreateVertexBuffer(dwVerts * vSize, dwUsage, 0, D3DPOOL_MANAGED, &hw_VB, nullptr)); - HW.stats_manager.increment_stats_vb(hw_VB); - R_CHK(HW.pDevice->CreateIndexBuffer(dwIndices * 2, dwUsage, D3DFMT_INDEX16, D3DPOOL_MANAGED, &hw_IB, nullptr)); - HW.stats_manager.increment_stats_ib(hw_IB); - -#endif // USE_DX10 Msg("* [DETAILS] Batch(%d), VB(%dK), IB(%dK)", hw_BatchSize, (dwVerts * vSize) / 1024, (dwIndices * 2) / 1024); // Fill VB + hw_VB.Create(dwVerts * vSize); { - vertHW* pV; -#ifndef USE_DX9 - vertHW* pVOriginal; - pVOriginal = xr_alloc(dwVerts); - pV = pVOriginal; -#else // USE_DX10 - R_CHK(hw_VB->Lock(0, 0, (void**)&pV, 0)); -#endif // USE_DX10 + vertHW* pV = static_cast(hw_VB.Map()); for (u32 o = 0; o < objects.size(); o++) { const CDetail& D = *objects[o]; @@ -102,28 +83,13 @@ void CDetailManager::hw_Load_Geom() } } } -#if defined(USE_OGL) - glBufferUtils::CreateVertexBuffer(&hw_VB, pVOriginal, dwVerts * vSize); - xr_free(pVOriginal); -#elif defined(USE_DX10) || defined(USE_DX11) - R_CHK(dx10BufferUtils::CreateVertexBuffer(&hw_VB, pVOriginal, dwVerts * vSize)); - HW.stats_manager.increment_stats_vb(hw_VB); - xr_free(pVOriginal); -#else // USE_DX10 - R_CHK(hw_VB->Unlock()); -#endif // USE_DX10 + hw_VB.Unmap(true); // upload vertex data } // Fill IB + hw_IB.Create(dwIndices * sizeof(u16)); { - u16* pI; -#ifndef USE_DX9 - u16* pIOriginal; - pIOriginal = xr_alloc(dwIndices); - pI = pIOriginal; -#else // USE_DX10 - R_CHK(hw_IB->Lock(0, 0, (void**)(&pI), 0)); -#endif // USE_DX10 + u16* pI = static_cast(hw_IB.Map()); for (u32 o = 0; o < objects.size(); o++) { const CDetail& D = *objects[o]; @@ -135,16 +101,7 @@ void CDetailManager::hw_Load_Geom() offset = u16(offset + u16(D.number_vertices)); } } -#if defined(USE_OGL) - glBufferUtils::CreateIndexBuffer(&hw_IB, pIOriginal, dwIndices * 2); - xr_free(pIOriginal); -#elif defined(USE_DX10) || defined(USE_DX11) - R_CHK(dx10BufferUtils::CreateIndexBuffer(&hw_IB, pIOriginal, dwIndices * 2)); - HW.stats_manager.increment_stats_ib(hw_IB); - xr_free(pIOriginal); -#else // USE_DX10 - R_CHK(hw_IB->Unlock()); -#endif // USE_DX10 + hw_IB.Unmap(true); // upload index data } // Declare geometry @@ -155,15 +112,8 @@ void CDetailManager::hw_Unload() { // Destroy VS/VB/IB hw_Geom.destroy(); -#ifdef USE_OGL - GLuint buffers[] = { hw_IB, hw_VB }; - glDeleteBuffers(2, buffers); -#else - HW.stats_manager.decrement_stats_vb(hw_VB); - HW.stats_manager.decrement_stats_ib(hw_IB); - _RELEASE(hw_IB); - _RELEASE(hw_VB); -#endif // USE_OGL + hw_IB.Release(); + hw_VB.Release(); } #if !defined(USE_DX10) && !defined(USE_DX11) && !defined(USE_OGL) diff --git a/src/Layers/xrRender/FSkinned.cpp b/src/Layers/xrRender/FSkinned.cpp index a6e44c7adff..62ed427cd82 100644 --- a/src/Layers/xrRender/FSkinned.cpp +++ b/src/Layers/xrRender/FSkinned.cpp @@ -3,8 +3,7 @@ #include "xrCore/FMesh.hpp" #include "FSkinned.h" #include "SkeletonX.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" -#include "Layers/xrRenderGL/glBufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" #include "Layers/xrRenderGL/glBufferPool.h" #include "xrEngine/EnnumerateVertices.h" #include "xrCore/xrDebug_macros.h" @@ -401,11 +400,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) Vertices1W.create(crc, V.vCount, (vertBoned1W*)_verts_); } -#ifdef USE_OGL - u32 vStride = glBufferUtils::GetDeclVertexSize(dwDecl_01W); -#else // USE_OGL - u32 vStride = D3DXGetDeclVertexSize(dwDecl_01W, 0); -#endif // USE_OGL + u32 vStride = GetDeclVertexSize(dwDecl_01W, 0); VERIFY(vStride == sizeof(vertHW_1W)); VERIFY(nullptr == V.p_rm_Vertices); @@ -424,7 +419,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) #ifdef USE_OGL GLBuffers.CreateVertexBuffer(V.p_rm_Vertices, dstOriginal, V.vCount * vStride); #else // USE_OGL - R_CHK(dx10BufferUtils::CreateVertexBuffer(&V.p_rm_Vertices, dstOriginal, V.vCount * vStride)); + R_CHK(BufferUtils::CreateVertexBuffer(&V.p_rm_Vertices, dstOriginal, V.vCount * vStride)); HW.stats_manager.increment_stats_vb(V.p_rm_Vertices); #endif // USE_OGL xr_free(dstOriginal); @@ -439,11 +434,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) Vertices2W.create(crc, V.vCount, (vertBoned2W*)_verts_); } -#ifdef USE_OGL - u32 vStride = glBufferUtils::GetDeclVertexSize(dwDecl_2W); -#else // USE_OGL - u32 vStride = D3DXGetDeclVertexSize(dwDecl_2W, 0); -#endif // USE_OGL + u32 vStride = GetDeclVertexSize(dwDecl_2W, 0); VERIFY(vStride == sizeof(vertHW_2W)); VERIFY(nullptr == V.p_rm_Vertices); @@ -462,7 +453,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) #ifdef USE_OGL GLBuffers.CreateVertexBuffer(V.p_rm_Vertices, dstOriginal, V.vCount * vStride); #else // USE_OGL - R_CHK(dx10BufferUtils::CreateVertexBuffer(&V.p_rm_Vertices, dstOriginal, V.vCount * vStride)); + R_CHK(BufferUtils::CreateVertexBuffer(&V.p_rm_Vertices, dstOriginal, V.vCount * vStride)); HW.stats_manager.increment_stats_vb(V.p_rm_Vertices); #endif // USE_OGL xr_free(dstOriginal); @@ -476,11 +467,8 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) u32 crc = crc32(_verts_, size); Vertices3W.create(crc, V.vCount, (vertBoned3W*)_verts_); } -#ifdef USE_OGL - u32 vStride = glBufferUtils::GetDeclVertexSize(dwDecl_3W); -#else // USE_OGL - u32 vStride = D3DXGetDeclVertexSize(dwDecl_3W, 0); -#endif // USE_OGL + + u32 vStride = GetDeclVertexSize(dwDecl_3W, 0); VERIFY(vStride == sizeof(vertHW_3W)); VERIFY(nullptr == V.p_rm_Vertices); @@ -500,7 +488,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) #ifdef USE_OGL GLBuffers.CreateVertexBuffer(V.p_rm_Vertices, dstOriginal, V.vCount * vStride); #else // USE_OGL - R_CHK(dx10BufferUtils::CreateVertexBuffer(&V.p_rm_Vertices, dstOriginal, V.vCount * vStride)); + R_CHK(BufferUtils::CreateVertexBuffer(&V.p_rm_Vertices, dstOriginal, V.vCount * vStride)); HW.stats_manager.increment_stats_vb(V.p_rm_Vertices); #endif // USE_OGL xr_free(dstOriginal); @@ -515,11 +503,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) Vertices4W.create(crc, V.vCount, (vertBoned4W*)_verts_); } -#ifdef USE_OGL - u32 vStride = glBufferUtils::GetDeclVertexSize(dwDecl_4W); -#else // USE_OGL - u32 vStride = D3DXGetDeclVertexSize(dwDecl_4W, 0); -#endif // USE_OGL + u32 vStride = GetDeclVertexSize(dwDecl_4W, 0); VERIFY(vStride == sizeof(vertHW_4W)); VERIFY(nullptr == V.p_rm_Vertices); @@ -539,7 +523,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) #ifdef USE_OGL GLBuffers.CreateVertexBuffer(V.p_rm_Vertices, dstOriginal, V.vCount * vStride); #else // USE_OGL - R_CHK(dx10BufferUtils::CreateVertexBuffer(&V.p_rm_Vertices, dstOriginal, V.vCount * vStride)); + R_CHK(BufferUtils::CreateVertexBuffer(&V.p_rm_Vertices, dstOriginal, V.vCount * vStride)); HW.stats_manager.increment_stats_vb(V.p_rm_Vertices); #endif // USE_OGL xr_free(dstOriginal); @@ -566,7 +550,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) case RM_SINGLE: case RM_SKINNING_1B: { - u32 vStride = D3DXGetDeclVertexSize(dwDecl_01W, 0); + u32 vStride = GetDeclVertexSize(dwDecl_01W, 0); VERIFY(vStride == sizeof(vertHW_1W)); BYTE* bytes = nullptr; VERIFY(NULL == V.p_rm_Vertices); @@ -589,7 +573,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) break; case RM_SKINNING_2B: { - u32 vStride = D3DXGetDeclVertexSize(dwDecl_2W, 0); + u32 vStride = GetDeclVertexSize(dwDecl_2W, 0); VERIFY(vStride == sizeof(vertHW_2W)); BYTE* bytes = nullptr; VERIFY(NULL == V.p_rm_Vertices); @@ -613,7 +597,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) break; case RM_SKINNING_3B: { - u32 vStride = D3DXGetDeclVertexSize(dwDecl_3W, 0); + u32 vStride = GetDeclVertexSize(dwDecl_3W, 0); VERIFY(vStride == sizeof(vertHW_3W)); BYTE* bytes = nullptr; VERIFY(NULL == V.p_rm_Vertices); @@ -638,7 +622,7 @@ void CSkeletonX_ext::_Load_hw(Fvisual& V, void* _verts_) break; case RM_SKINNING_4B: { - u32 vStride = D3DXGetDeclVertexSize(dwDecl_4W, 0); + u32 vStride = GetDeclVertexSize(dwDecl_4W, 0); VERIFY(vStride == sizeof(vertHW_4W)); BYTE* bytes = nullptr; VERIFY(NULL == V.p_rm_Vertices); diff --git a/src/Layers/xrRender/FVisual.cpp b/src/Layers/xrRender/FVisual.cpp index 715ce6ba74b..46a28d961bd 100644 --- a/src/Layers/xrRender/FVisual.cpp +++ b/src/Layers/xrRender/FVisual.cpp @@ -3,8 +3,7 @@ #include "xrCore/FMesh.hpp" #include "FVisual.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" -#include "Layers/xrRenderGL/glBufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" #include "Layers/xrRenderGL/glBufferPool.h" ////////////////////////////////////////////////////////////////////// @@ -123,14 +122,14 @@ void Fvisual::Load(const char* N, IReader* data, u32 dwFlags) VERIFY(nullptr == p_rm_Vertices); #ifdef USE_OGL - u32 vStride = glBufferUtils::GetFVFVertexSize(fvf); + u32 vStride = GetFVFVertexSize(fvf); GLBuffers.CreateVertexBuffer(p_rm_Vertices, data->pointer(), vCount * vStride); #elif defined(USE_DX10) || defined(USE_DX11) - u32 vStride = D3DXGetFVFVertexSize(fvf); - R_CHK(dx10BufferUtils::CreateVertexBuffer(&p_rm_Vertices, data->pointer(), vCount * vStride)); + u32 vStride = GetFVFVertexSize(fvf); + R_CHK(BufferUtils::CreateVertexBuffer(&p_rm_Vertices, data->pointer(), vCount * vStride)); HW.stats_manager.increment_stats_vb(p_rm_Vertices); #else // USE_DX10 - u32 vStride = D3DXGetFVFVertexSize(fvf); + u32 vStride = GetFVFVertexSize(fvf); BOOL bSoft = HW.Caps.geometry.bSoftware; u32 dwUsage = D3DUSAGE_WRITEONLY | (bSoft ? D3DUSAGE_SOFTWAREPROCESSING : 0); BYTE* bytes = nullptr; @@ -171,7 +170,7 @@ void Fvisual::Load(const char* N, IReader* data, u32 dwFlags) #ifdef USE_OGL GLBuffers.CreateIndexBuffer(p_rm_Indices, data->pointer(), iCount * 2); #elif defined(USE_DX10) || defined(USE_DX11) - R_CHK(dx10BufferUtils::CreateIndexBuffer(&p_rm_Indices, data->pointer(), iCount * 2)); + R_CHK(BufferUtils::CreateIndexBuffer(&p_rm_Indices, data->pointer(), iCount * 2)); HW.stats_manager.increment_stats_ib(p_rm_Indices); #else // USE_DX10 BOOL bSoft = HW.Caps.geometry.bSoftware; diff --git a/src/Layers/xrRender/R_Backend.cpp b/src/Layers/xrRender/R_Backend.cpp index 30e8e77ef80..82dcfe844ad 100644 --- a/src/Layers/xrRender/R_Backend.cpp +++ b/src/Layers/xrRender/R_Backend.cpp @@ -1,144 +1,21 @@ #include "stdafx.h" #pragma hdrstop -#include "Layers/xrRenderDX10/dx10BufferUtils.h" - CBackend RCache; -// Create Quad-IB -#if defined(USE_OGL) - -// Igor: is used to test bug with rain, particles corruption -void CBackend::RestoreQuadIBData() -{ - // Not implemented -} - -void CBackend::CreateQuadIB() -{ - const u32 dwTriCount = 4 * 1024; - const u32 dwIdxCount = dwTriCount * 2 * 3; - u16 IndexBuffer[dwIdxCount]; - u16* Indices = IndexBuffer; - GLenum dwUsage = GL_STATIC_DRAW; - - int Cnt = 0; - int ICnt = 0; - for (int i = 0; iCreateIndexBuffer(dwIdxCount*2,dwUsage,D3DFMT_INDEX16,D3DPOOL_MANAGED,&QuadIB,NULL)); - - D3D_BUFFER_DESC desc; - desc.ByteWidth = dwIdxCount * 2; - // desc.Usage = D3D_USAGE_IMMUTABLE; - desc.Usage = D3D_USAGE_DEFAULT; - desc.BindFlags = D3D_BIND_INDEX_BUFFER; - desc.CPUAccessFlags = 0; - desc.MiscFlags = 0; - - D3D_SUBRESOURCE_DATA subData; - subData.pSysMem = IndexBuffer; - - // R_CHK(QuadIB->Lock(0,0,(void**)&Indices,0)); - { - int Cnt = 0; - int ICnt = 0; - for (int i = 0; i < dwTriCount; i++) - { - Indices[ICnt++] = u16(Cnt + 0); - Indices[ICnt++] = u16(Cnt + 1); - Indices[ICnt++] = u16(Cnt + 2); - - Indices[ICnt++] = u16(Cnt + 3); - Indices[ICnt++] = u16(Cnt + 2); - Indices[ICnt++] = u16(Cnt + 1); - - Cnt += 4; - } - } - // R_CHK(QuadIB->Unlock()); - - // R_CHK(HW.pDevice->CreateIndexBuffer(dwIdxCount*2,dwUsage,D3DFMT_INDEX16,D3DPOOL_MANAGED,&QuadIB,NULL)); - R_CHK(HW.pDevice->CreateBuffer(&desc, &subData, &QuadIB)); - HW.stats_manager.increment_stats_ib(QuadIB); -} - -#else // USE_DX10 - -// Igor: is used to test bug with rain, particles corruption -void CBackend::RestoreQuadIBData() -{ - const u32 dwTriCount = 4 * 1024; - u16* Indices = nullptr; - R_CHK(QuadIB->Lock(0, 0, (void**)&Indices, 0)); - { - int Cnt = 0; - int ICnt = 0; - for (int i = 0; i < dwTriCount; i++) - { - Indices[ICnt++] = u16(Cnt + 0); - Indices[ICnt++] = u16(Cnt + 1); - Indices[ICnt++] = u16(Cnt + 2); - - Indices[ICnt++] = u16(Cnt + 3); - Indices[ICnt++] = u16(Cnt + 2); - Indices[ICnt++] = u16(Cnt + 1); + constexpr auto triCount = 4 * 1024; + constexpr auto idxCount = triCount * 2 * 3; + constexpr auto idxSize = idxCount * sizeof(u16); - Cnt += 4; - } - } - R_CHK(QuadIB->Unlock()); -} + QuadIB.Create(idxSize); -void CBackend::CreateQuadIB() -{ - const u32 dwTriCount = 4 * 1024; - const u32 dwIdxCount = dwTriCount * 2 * 3; - u16* Indices = nullptr; - u32 dwUsage = D3DUSAGE_WRITEONLY; - if (HW.Caps.geometry.bSoftware) - dwUsage |= D3DUSAGE_SOFTWAREPROCESSING; - R_CHK(HW.pDevice->CreateIndexBuffer(dwIdxCount * 2, dwUsage, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &QuadIB, NULL)); - HW.stats_manager.increment_stats_ib(QuadIB); - // Msg("CBackend::CreateQuadIB(). Created buffer size = %d ", dwIdxCount*2 ); - R_CHK(QuadIB->Lock(0, 0, (void**)&Indices, 0)); + u16* Indices = static_cast(QuadIB.Map()); { int Cnt = 0; int ICnt = 0; - for (int i = 0; i < dwTriCount; i++) + for (int i = 0; i < triCount; i++) { Indices[ICnt++] = u16(Cnt + 0); Indices[ICnt++] = u16(Cnt + 1); @@ -151,18 +28,12 @@ void CBackend::CreateQuadIB() Cnt += 4; } } - R_CHK(QuadIB->Unlock()); + QuadIB.Unmap(true); // upload index data } -#endif // USE_DX10 - // Device dependance void CBackend::OnDeviceCreate() { -#if defined(USE_DX10) || defined(USE_DX11) -// CreateConstantBuffers(); -#endif // USE_DX10 - CreateQuadIB(); // streams @@ -180,32 +51,5 @@ void CBackend::OnDeviceDestroy() Vertex.Destroy(); // Quad -#ifdef USE_OGL - glDeleteBuffers(1, &QuadIB); -#else - HW.stats_manager.decrement_stats_ib(QuadIB); - _RELEASE(QuadIB); -#endif - -#if defined(USE_DX10) || defined(USE_DX11) -// DestroyConstantBuffers(); -#endif // USE_DX10 -} - -#if defined(USE_DX10) || defined(USE_DX11) -/* -void CBackend::CreateConstantBuffers() -{ - const int iVectorElements = 4; - const int iVectorNumber = 256; - dx10BufferUtils::CreateConstantBuffer(&m_pPixelConstants, iVectorNumber*iVectorElements*sizeof(float)); - dx10BufferUtils::CreateConstantBuffer(&m_pVertexConstants, iVectorNumber*iVectorElements*sizeof(float)); -} - -void CBackend::DestroyConstantBuffers() -{ - _RELEASE(m_pVertexConstants); - _RELEASE(m_pPixelConstants); + QuadIB.Release(); } -*/ -#endif // USE_DX10 diff --git a/src/Layers/xrRender/R_Backend.h b/src/Layers/xrRender/R_Backend.h index 1e607595ce5..414fdf05e26 100644 --- a/src/Layers/xrRender/R_Backend.h +++ b/src/Layers/xrRender/R_Backend.h @@ -10,6 +10,7 @@ #define PGO(a) #endif +#include "Layers/xrRender/BufferUtils.h" #include "R_DStreams.h" #include "r_constants_cache.h" #include "R_Backend_xform.h" @@ -67,15 +68,10 @@ class ECORE_API CBackend // Dynamic geometry streams _VertexStream Vertex; _IndexStream Index; -#ifdef USE_OGL - GLuint QuadIB; - GLuint old_QuadIB; - GLuint CuboidIB; -#else - ID3DIndexBuffer* QuadIB; - ID3DIndexBuffer* old_QuadIB; - ID3DIndexBuffer* CuboidIB; -#endif // USE_OGL + + IndexStagingBuffer QuadIB; + IndexStagingBuffer old_QuadIB; + R_xforms xforms; R_hemi hemi; R_tree tree; @@ -115,13 +111,8 @@ class ECORE_API CBackend #else // USE_DX10 IDirect3DVertexDeclaration9* decl; #endif // USE_DX10 -#ifdef USE_OGL - GLuint vb; - GLuint ib; -#else - ID3DVertexBuffer* vb; - ID3DIndexBuffer* ib; -#endif // USE_OGL + VertexBufferHandle vb; + IndexBufferHandle ib; u32 vb_stride; // Pixel/Vertex constants @@ -361,8 +352,8 @@ class ECORE_API CBackend ICF void set_Vertices(GLuint _vb, u32 _vb_stride); ICF void set_Indices(GLuint _ib); #else - ICF void set_Vertices(ID3DVertexBuffer* _vb, u32 _vb_stride); - ICF void set_Indices(ID3DIndexBuffer* _ib); + ICF void set_Vertices(VertexBufferHandle _vb, u32 _vb_stride); + ICF void set_Indices(IndexBufferHandle _ib); #endif // USE_OGL ICF void set_Geometry(SGeometry* _geom); ICF void set_Geometry(ref_geom& _geom) { set_Geometry(&*_geom); } diff --git a/src/Layers/xrRender/ResourceManager_Reset.cpp b/src/Layers/xrRender/ResourceManager_Reset.cpp index e028255d803..da806559167 100644 --- a/src/Layers/xrRender/ResourceManager_Reset.cpp +++ b/src/Layers/xrRender/ResourceManager_Reset.cpp @@ -29,12 +29,7 @@ void CResourceManager::reset_begin() // destroy DStreams RCache.old_QuadIB = RCache.QuadIB; -#ifdef USE_OGL - glDeleteBuffers(1, &RCache.QuadIB); -#else - HW.stats_manager.decrement_stats_ib(RCache.QuadIB); - _RELEASE(RCache.QuadIB); -#endif // USE_OGL + RCache.QuadIB.Release(); RCache.Index.reset_begin(); RCache.Vertex.reset_begin(); diff --git a/src/Layers/xrRender/ResourceManager_Resources.cpp b/src/Layers/xrRender/ResourceManager_Resources.cpp index 918ff5ce59f..169cfb5f499 100644 --- a/src/Layers/xrRender/ResourceManager_Resources.cpp +++ b/src/Layers/xrRender/ResourceManager_Resources.cpp @@ -122,8 +122,8 @@ void CResourceManager::_DeletePass(const SPass* P) static BOOL dcl_equal(D3DVERTEXELEMENT9* a, D3DVERTEXELEMENT9* b) { // check sizes - u32 a_size = D3DXGetDeclLength(a); - u32 b_size = D3DXGetDeclLength(b); + u32 a_size = GetDeclLength(a); + u32 b_size = GetDeclLength(b); if (a_size != b_size) return FALSE; return 0 == memcmp(a, b, a_size * sizeof(D3DVERTEXELEMENT9)); @@ -140,7 +140,7 @@ SDeclaration* CResourceManager::_CreateDecl(D3DVERTEXELEMENT9* dcl) // Create _new SDeclaration* D = v_declarations.emplace_back(new SDeclaration()); - u32 dcl_size = D3DXGetDeclLength(dcl) + 1; + u32 dcl_size = GetDeclLength(dcl) + 1; CHK_DX(HW.pDevice->CreateVertexDeclaration(dcl, &D->dcl)); D->dcl_code.assign(dcl, dcl + dcl_size); D->dwFlags |= xr_resource_flagged::RF_REGISTERED; @@ -301,7 +301,7 @@ SGeometry* CResourceManager::CreateGeom(D3DVERTEXELEMENT9* decl, ID3DVertexBuffe R_ASSERT(decl && vb); SDeclaration* dcl = _CreateDecl(decl); - u32 vb_stride = D3DXGetDeclVertexSize(decl, 0); + u32 vb_stride = GetDeclVertexSize(decl, 0); // ***** first pass - search already loaded shader for (SGeometry* v_geom : v_geoms) diff --git a/src/Layers/xrRender/stats_manager.cpp b/src/Layers/xrRender/stats_manager.cpp index 735e78e6deb..747dbdf252a 100644 --- a/src/Layers/xrRender/stats_manager.cpp +++ b/src/Layers/xrRender/stats_manager.cpp @@ -40,7 +40,7 @@ void stats_manager::increment_stats(u32 size, enum_stats_buffer_type type, _D3DP void stats_manager::increment_stats_rtarget(ID3DTexture2D* buff) { - if (GEnv.isDedicatedServer) + if (buff == nullptr || GEnv.isDedicatedServer) return; _D3DPOOL pool = D3DPOOL_MANAGED; @@ -59,7 +59,7 @@ void stats_manager::increment_stats_rtarget(ID3DTexture2D* buff) void stats_manager::increment_stats_vb(ID3DVertexBuffer* buff) { - if (GEnv.isDedicatedServer) + if (buff == nullptr || GEnv.isDedicatedServer) return; #if defined(USE_DX10) || defined(USE_DX11) @@ -75,7 +75,7 @@ void stats_manager::increment_stats_vb(ID3DVertexBuffer* buff) void stats_manager::increment_stats_ib(ID3DIndexBuffer* buff) { - if (GEnv.isDedicatedServer) + if (buff == nullptr || GEnv.isDedicatedServer) return; #if defined(USE_DX10) || defined(USE_DX11) diff --git a/src/Layers/xrRender/xrRender_console.cpp b/src/Layers/xrRender/xrRender_console.cpp index 9b8275b6824..682c5a51078 100644 --- a/src/Layers/xrRender/xrRender_console.cpp +++ b/src/Layers/xrRender/xrRender_console.cpp @@ -375,13 +375,6 @@ class CCC_Screenshot : public IConsole_Command } }; -class CCC_RestoreQuadIBData : public IConsole_Command -{ -public: - CCC_RestoreQuadIBData(LPCSTR N) : IConsole_Command(N){}; - virtual void Execute(LPCSTR /*args*/) { RCache.RestoreQuadIBData(); } -}; - class CCC_ModelPoolStat : public IConsole_Command { public: @@ -704,8 +697,6 @@ void xrRender_initconsole() // Common CMD1(CCC_Screenshot, "screenshot"); - // Igor: just to test bug with rain/particles corruption - CMD1(CCC_RestoreQuadIBData, "r_restore_quad_ib_data"); #ifdef DEBUG #if RENDER != R_R1 && RENDER != R_GL CMD1(CCC_BuildSSA, "build_ssa"); diff --git a/src/Layers/xrRenderDX10/3DFluid/dx103DFluidGrid.cpp b/src/Layers/xrRenderDX10/3DFluid/dx103DFluidGrid.cpp index f351a0ac2cc..f9a387db1b3 100644 --- a/src/Layers/xrRenderDX10/3DFluid/dx103DFluidGrid.cpp +++ b/src/Layers/xrRenderDX10/3DFluid/dx103DFluidGrid.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" #include "dx103DFluidGrid.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" struct VS_INPUT_FLUIDSIM_STRUCT { @@ -64,7 +64,7 @@ void dx103DFluidGrid::CreateVertexBuffers() {0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0}, {0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0}, D3DDECL_END()}; - u32 vSize = D3DXGetDeclVertexSize(layoutDesc, 0); + u32 vSize = GetDeclVertexSize(layoutDesc, 0); // UINT numElements = sizeof(layoutDesc)/sizeof(layoutDesc[0]); // CreateLayout( layoutDesc, numElements, technique, &layout); @@ -97,7 +97,7 @@ void dx103DFluidGrid::CreateVertexBuffers() // CreateVertexBuffer(sizeof(VS_INPUT_FLUIDSIM_STRUCT)*numVerticesRenderQuad, // D3Dxx_BIND_VERTEX_BUFFER, &renderQuadBuffer, renderQuad, numVerticesRenderQuad)); - CHK_DX(dx10BufferUtils::CreateVertexBuffer(&m_pRenderQuadBuffer, renderQuad, vSize * m_iNumVerticesRenderQuad)); + CHK_DX(BufferUtils::CreateVertexBuffer(&m_pRenderQuadBuffer, renderQuad, vSize * m_iNumVerticesRenderQuad)); m_GeomRenderQuad.create(layoutDesc, m_pRenderQuadBuffer, 0); // Vertex buffer for "m_vDim[2]" quads to draw all the slices to a 3D texture @@ -108,7 +108,7 @@ void dx103DFluidGrid::CreateVertexBuffers() VERIFY(index == m_iNumVerticesSlices); // V_RETURN(CreateVertexBuffer(sizeof(VS_INPUT_FLUIDSIM_STRUCT)*numVerticesSlices, // D3Dxx_BIND_VERTEX_BUFFER, &slicesBuffer, slices , numVerticesSlices)); - CHK_DX(dx10BufferUtils::CreateVertexBuffer(&m_pSlicesBuffer, slices, vSize * m_iNumVerticesSlices)); + CHK_DX(BufferUtils::CreateVertexBuffer(&m_pSlicesBuffer, slices, vSize * m_iNumVerticesSlices)); m_GeomSlices.create(layoutDesc, m_pSlicesBuffer, 0); // Vertex buffers for boundary geometry @@ -118,7 +118,7 @@ void dx103DFluidGrid::CreateVertexBuffers() VERIFY(index == m_iNumVerticesBoundarySlices); // V_RETURN(CreateVertexBuffer(sizeof(VS_INPUT_FLUIDSIM_STRUCT)*numVerticesBoundarySlices, // D3Dxx_BIND_VERTEX_BUFFER, &boundarySlicesBuffer, boundarySlices, numVerticesBoundarySlices)); - CHK_DX(dx10BufferUtils::CreateVertexBuffer( + CHK_DX(BufferUtils::CreateVertexBuffer( &m_pBoundarySlicesBuffer, boundarySlices, vSize * m_iNumVerticesBoundarySlices)); m_GeomBoundarySlices.create(layoutDesc, m_pBoundarySlicesBuffer, 0); @@ -128,7 +128,7 @@ void dx103DFluidGrid::CreateVertexBuffers() VERIFY(index == m_iNumVerticesBoundaryLines); // V_RETURN(CreateVertexBuffer(sizeof(VS_INPUT_FLUIDSIM_STRUCT)*numVerticesBoundaryLines, // D3Dxx_BIND_VERTEX_BUFFER, &boundaryLinesBuffer, boundaryLines, numVerticesBoundaryLines)); - CHK_DX(dx10BufferUtils::CreateVertexBuffer( + CHK_DX(BufferUtils::CreateVertexBuffer( &m_pBoundaryLinesBuffer, boundaryLines, vSize * m_iNumVerticesBoundaryLines)); m_GeomBoundaryLines.create(layoutDesc, m_pBoundaryLinesBuffer, 0); diff --git a/src/Layers/xrRenderDX10/3DFluid/dx103DFluidRenderer.cpp b/src/Layers/xrRenderDX10/3DFluid/dx103DFluidRenderer.cpp index 2602d1f40bc..463abae6f73 100644 --- a/src/Layers/xrRenderDX10/3DFluid/dx103DFluidRenderer.cpp +++ b/src/Layers/xrRenderDX10/3DFluid/dx103DFluidRenderer.cpp @@ -1,7 +1,7 @@ #include "stdafx.h" #include "dx103DFluidRenderer.h" #include "dx103DFluidBlenders.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" #include "dx103DFluidData.h" struct VsInput @@ -127,14 +127,14 @@ void dx103DFluidRenderer::CreateGridBox() }; m_iGridBoxVertNum = sizeof(vertices) / sizeof(vertices[0]); - CHK_DX(dx10BufferUtils::CreateVertexBuffer(&m_pGridBoxVertexBuffer, vertices, sizeof(vertices))); + CHK_DX(BufferUtils::CreateVertexBuffer(&m_pGridBoxVertexBuffer, vertices, sizeof(vertices))); // Create index buffer u16 indices[] = { 0, 4, 1, 1, 4, 5, 0, 1, 2, 2, 1, 3, 4, 6, 5, 6, 7, 5, 2, 3, 6, 3, 7, 6, 1, 5, 3, 3, 5, 7, 0, 2, 4, 2, 6, 4}; m_iGridBoxFaceNum = (sizeof(indices) / sizeof(indices[0])) / 3; - CHK_DX(dx10BufferUtils::CreateIndexBuffer(&m_pGridBoxIndexBuffer, indices, sizeof(indices))); + CHK_DX(BufferUtils::CreateIndexBuffer(&m_pGridBoxIndexBuffer, indices, sizeof(indices))); HW.stats_manager.increment_stats(sizeof(indices), enum_stats_buffer_type_index, D3DPOOL_MANAGED); // Define the input layout @@ -158,7 +158,7 @@ void dx103DFluidRenderer::CreateScreenQuad() svQuad[2].pos = D3DXVECTOR3(-1.0f, -1.0f, 0.0f); svQuad[3].pos = D3DXVECTOR3(1.0f, -1.0f, 0.0f); - CHK_DX(dx10BufferUtils::CreateVertexBuffer(&m_pQuadVertexBuffer, svQuad, sizeof(svQuad))); + CHK_DX(BufferUtils::CreateVertexBuffer(&m_pQuadVertexBuffer, svQuad, sizeof(svQuad))); m_GeomQuadVertex.create(quadlayout, m_pQuadVertexBuffer, 0); } diff --git a/src/Layers/xrRenderDX10/CommonTypes.h b/src/Layers/xrRenderDX10/CommonTypes.h index 781ba71aac5..9793e70664a 100644 --- a/src/Layers/xrRenderDX10/CommonTypes.h +++ b/src/Layers/xrRenderDX10/CommonTypes.h @@ -270,3 +270,11 @@ typedef ID3D10ShaderReflectionType ID3DShaderReflectionType; typedef dx10State ID3DState; #define DX10_ONLY(expr) expr + +using VertexBufferHandle = ID3D10Buffer*; +using IndexBufferHandle = ID3D10Buffer*; +using ConstantBufferHandle = ID3D10Buffer*; +using HostBufferHandle = void*; + +using VertexElement = D3DVERTEXELEMENT9; +using InputElementDesc = D3D10_INPUT_ELEMENT_DESC; diff --git a/src/Layers/xrRenderDX10/dx10BufferUtils.cpp b/src/Layers/xrRenderDX10/dx10BufferUtils.cpp index ba16b844d53..ba6b6dc1f2b 100644 --- a/src/Layers/xrRenderDX10/dx10BufferUtils.cpp +++ b/src/Layers/xrRenderDX10/dx10BufferUtils.cpp @@ -1,7 +1,22 @@ #include "stdafx.h" -#include "dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" -namespace dx10BufferUtils +u32 GetFVFVertexSize(u32 FVF) +{ + return D3DXGetFVFVertexSize(FVF); +} + +u32 GetDeclVertexSize(const VertexElement* decl, DWORD Stream) +{ + return D3DXGetDeclVertexSize(decl, Stream); +} + +u32 GetDeclLength(const VertexElement* decl) +{ + return D3DXGetDeclLength(decl); +} + +namespace BufferUtils { HRESULT IC CreateBuffer(ID3DBuffer** ppBuffer, const void* pData, UINT DataSize, bool /*bImmutable*/, bool bIndexBuffer) { @@ -21,17 +36,17 @@ HRESULT IC CreateBuffer(ID3DBuffer** ppBuffer, const void* pData, UINT DataSize, return res; } -HRESULT CreateVertexBuffer(ID3DVertexBuffer** ppBuffer, const void* pData, UINT DataSize, bool bImmutable) +HRESULT CreateVertexBuffer(VertexBufferHandle* ppBuffer, const void* pData, UINT DataSize, bool bImmutable) { return CreateBuffer(ppBuffer, pData, DataSize, bImmutable, false); } -HRESULT CreateIndexBuffer(ID3DIndexBuffer** ppBuffer, const void* pData, UINT DataSize, bool bImmutable) +HRESULT CreateIndexBuffer(IndexBufferHandle* ppBuffer, const void* pData, UINT DataSize, bool bImmutable) { return CreateBuffer(ppBuffer, pData, DataSize, bImmutable, true); } -HRESULT CreateConstantBuffer(ID3DBuffer** ppBuffer, UINT DataSize) +HRESULT CreateConstantBuffer(ConstantBufferHandle* ppBuffer, UINT DataSize) { D3D_BUFFER_DESC desc; desc.ByteWidth = DataSize; @@ -44,6 +59,7 @@ HRESULT CreateConstantBuffer(ID3DBuffer** ppBuffer, UINT DataSize) // R_CHK(res); return res; } +}; struct VertexFormatPairs { @@ -144,4 +160,195 @@ void ConvertVertexDeclaration(const xr_vector& declIn, xr_vec if (iDeclSize >= 0) ZeroMemory(&declOut[iDeclSize], sizeof(declOut[iDeclSize])); } -}; + +//----------------------------------------------------------------------------- +VertexStagingBuffer::VertexStagingBuffer() + : m_DeviceBuffer{ nullptr } + , m_HostBuffer{ nullptr } +{ +} + +VertexStagingBuffer::~VertexStagingBuffer() +{ + Destroy(); +} + +void VertexStagingBuffer::Create(size_t size, bool allowReadBack /*= false*/) +{ + m_Size = size; + m_AllowReadBack = allowReadBack; + + m_HostBuffer = xr_alloc(size); + AddRef(); +} + +bool VertexStagingBuffer::IsValid() const +{ + return !!m_DeviceBuffer; +} + +void* VertexStagingBuffer::Map( + size_t offset /*= 0*/, + size_t size /*= 0*/, + bool read /*= false*/) +{ + VERIFY2(m_HostBuffer, "Buffer wasn't created or already discarded"); + VERIFY2(!read || m_AllowReadBack, "Can't read from write only buffer"); + VERIFY2((size + offset) <= m_Size, "Map region is too large"); + + return static_cast(m_HostBuffer) + offset; +} + +void VertexStagingBuffer::Unmap(bool doFlush /*= false*/) +{ + if (!doFlush) + { + /* Do nothing*/ + return; + } + + VERIFY(m_HostBuffer && m_Size); + + // Upload data to device + BufferUtils::CreateVertexBuffer(&m_DeviceBuffer, m_HostBuffer, m_Size, false); + HW.stats_manager.increment_stats_vb(m_DeviceBuffer); + + if (!m_AllowReadBack) + { + // Cache buffer isn't required anymore. Free host memory + DiscardHostBuffer(); + } +} + +VertexBufferHandle VertexStagingBuffer::GetBufferHandle() const +{ + return m_DeviceBuffer; +} + +void VertexStagingBuffer::Destroy() +{ + DiscardHostBuffer(); + m_Size = 0; + + HW.stats_manager.decrement_stats_vb(m_DeviceBuffer); + _RELEASE(m_DeviceBuffer); +} + +void VertexStagingBuffer::DiscardHostBuffer() +{ + if (m_HostBuffer) + xr_delete(m_HostBuffer); +} + +size_t VertexStagingBuffer::GetSystemMemoryUsage() const +{ + return m_HostBuffer ? m_Size : 0; +} + +size_t VertexStagingBuffer::GetVideoMemoryUsage() const +{ + if (m_DeviceBuffer) + { + D3D_BUFFER_DESC desc; + m_DeviceBuffer->GetDesc(&desc); + return desc.ByteWidth; + } + + return 0; +} + +//----------------------------------------------------------------------------- +IndexStagingBuffer::IndexStagingBuffer() + : m_DeviceBuffer{ nullptr } + , m_HostBuffer{ nullptr } +{ +} + +IndexStagingBuffer::~IndexStagingBuffer() +{ + Destroy(); +} + +void IndexStagingBuffer::Create(size_t size, bool allowReadBack /*= false*/) +{ + m_Size = size; + m_AllowReadBack = allowReadBack; + + m_HostBuffer = xr_alloc(size); + AddRef(); +} + +bool IndexStagingBuffer::IsValid() const +{ + return !!m_DeviceBuffer; +} + +void* IndexStagingBuffer::Map( + size_t offset /*= 0*/, + size_t size /*= 0*/, + bool read /*= LOCKFLAG_NOT_SET*/) +{ + VERIFY2(m_HostBuffer, "Buffer wasn't created or already discarded"); + VERIFY2(!read || m_AllowReadBack, "Can't read from write only buffer"); + VERIFY2((size + offset) <= m_Size, "Map region is too large"); + + return static_cast(m_HostBuffer) + offset; +} + +void IndexStagingBuffer::Unmap(bool doFlush /*= false*/) +{ + if (!doFlush) + { + /* Do nothing*/ + return; + } + + VERIFY(m_HostBuffer && m_Size); + + // Upload data to device + BufferUtils::CreateIndexBuffer(&m_DeviceBuffer, m_HostBuffer, m_Size, false); + HW.stats_manager.increment_stats_ib(m_DeviceBuffer); + + if (!m_AllowReadBack) + { + // Cache buffer isn't required anymore. Free host memory + DiscardHostBuffer(); + } +} + +IndexBufferHandle IndexStagingBuffer::GetBufferHandle() const +{ + return m_DeviceBuffer; +} + +void IndexStagingBuffer::Destroy() +{ + DiscardHostBuffer(); + m_Size = 0; + + HW.stats_manager.decrement_stats_ib(m_DeviceBuffer); + _RELEASE(m_DeviceBuffer); +} + +void IndexStagingBuffer::DiscardHostBuffer() +{ + if (m_HostBuffer) + xr_delete(m_HostBuffer); +} + +size_t IndexStagingBuffer::GetSystemMemoryUsage() const +{ + return m_HostBuffer ? m_Size : 0; +} + +size_t IndexStagingBuffer::GetVideoMemoryUsage() const +{ + if (m_DeviceBuffer) + { + D3D_BUFFER_DESC desc; + m_DeviceBuffer->GetDesc(&desc); + return desc.ByteWidth; + } + + return 0; +} diff --git a/src/Layers/xrRenderDX10/dx10BufferUtils.h b/src/Layers/xrRenderDX10/dx10BufferUtils.h deleted file mode 100644 index 76b14cb3978..00000000000 --- a/src/Layers/xrRenderDX10/dx10BufferUtils.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef dx10BufferUtils_included -#define dx10BufferUtils_included -#pragma once -#if defined(USE_DX10) || defined(USE_DX11) - -namespace dx10BufferUtils -{ -HRESULT CreateVertexBuffer(ID3DVertexBuffer** ppBuffer, const void* pData, UINT DataSize, bool bImmutable = true); -HRESULT CreateIndexBuffer(ID3DIndexBuffer** ppBuffer, const void* pData, UINT DataSize, bool bImmutable = true); -HRESULT CreateConstantBuffer(ID3DBuffer** ppBuffer, UINT DataSize); -void ConvertVertexDeclaration(const xr_vector& declIn, xr_vector& declOut); -}; - -#endif // USE_DX10 -#endif // dx10BufferUtils_included diff --git a/src/Layers/xrRenderDX10/dx10ConstantBuffer.cpp b/src/Layers/xrRenderDX10/dx10ConstantBuffer.cpp index b5df9fc4669..2c7b8a70af2 100644 --- a/src/Layers/xrRenderDX10/dx10ConstantBuffer.cpp +++ b/src/Layers/xrRenderDX10/dx10ConstantBuffer.cpp @@ -1,7 +1,7 @@ #include "stdafx.h" #include "dx10ConstantBuffer.h" -#include "dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" dx10ConstantBuffer::~dx10ConstantBuffer() { @@ -43,7 +43,7 @@ dx10ConstantBuffer::dx10ConstantBuffer(ID3DShaderReflectionConstantBuffer* pTabl m_uiMembersCRC = crc32(&m_MembersList[0], Desc.Variables * sizeof(m_MembersList[0])); - R_CHK(dx10BufferUtils::CreateConstantBuffer(&m_pBuffer, Desc.Size)); + R_CHK(BufferUtils::CreateConstantBuffer(&m_pBuffer, Desc.Size)); VERIFY(m_pBuffer); m_pBufferData = xr_malloc(Desc.Size); VERIFY(m_pBufferData); diff --git a/src/Layers/xrRenderDX10/dx10DetailManager_VS.cpp b/src/Layers/xrRenderDX10/dx10DetailManager_VS.cpp index 06daa3aa2b9..cb2ade40157 100644 --- a/src/Layers/xrRenderDX10/dx10DetailManager_VS.cpp +++ b/src/Layers/xrRenderDX10/dx10DetailManager_VS.cpp @@ -2,7 +2,7 @@ #include "Layers/xrRender/DetailManager.h" #include "xrEngine/IGame_Persistent.h" #include "xrEngine/Environment.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" const int quant = 16384; const int c_hdr = 10; diff --git a/src/Layers/xrRenderDX10/dx10ResourceManager_Resources.cpp b/src/Layers/xrRenderDX10/dx10ResourceManager_Resources.cpp index a23c3465288..8c089deb249 100644 --- a/src/Layers/xrRenderDX10/dx10ResourceManager_Resources.cpp +++ b/src/Layers/xrRenderDX10/dx10ResourceManager_Resources.cpp @@ -16,7 +16,7 @@ #include "Layers/xrRender/tss.h" #include "Layers/xrRender/blenders/blender.h" #include "Layers/xrRender/blenders/blender_recorder.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" #include "Layers/xrRenderDX10/dx10ConstantBuffer.h" #include "Layers/xrRender/ShaderResourceTraits.h" @@ -199,8 +199,8 @@ void CResourceManager::_DeletePS(const SPS* ps) { DestroyShader(ps); } static BOOL dcl_equal(D3DVERTEXELEMENT9* a, D3DVERTEXELEMENT9* b) { // check sizes - u32 a_size = D3DXGetDeclLength(a); - u32 b_size = D3DXGetDeclLength(b); + u32 a_size = GetDeclLength(a); + u32 b_size = GetDeclLength(b); if (a_size != b_size) return FALSE; return 0 == memcmp(a, b, a_size * sizeof(D3DVERTEXELEMENT9)); @@ -217,11 +217,11 @@ SDeclaration* CResourceManager::_CreateDecl(D3DVERTEXELEMENT9* dcl) // Create _new SDeclaration* D = v_declarations.emplace_back(new SDeclaration()); - u32 dcl_size = D3DXGetDeclLength(dcl) + 1; + u32 dcl_size = GetDeclLength(dcl) + 1; // Don't need it for DirectX 10 here // CHK_DX (HW.pDevice->CreateVertexDeclaration(dcl,&D->dcl)); D->dcl_code.assign(dcl, dcl + dcl_size); - dx10BufferUtils::ConvertVertexDeclaration(D->dcl_code, D->dx10_dcl_code); + ConvertVertexDeclaration(D->dcl_code, D->dx10_dcl_code); D->dwFlags |= xr_resource_flagged::RF_REGISTERED; return D; @@ -347,7 +347,7 @@ SGeometry* CResourceManager::CreateGeom(D3DVERTEXELEMENT9* decl, ID3DVertexBuffe R_ASSERT(decl && vb); SDeclaration* dcl = _CreateDecl(decl); - u32 vb_stride = D3DXGetDeclVertexSize(decl, 0); + u32 vb_stride = GetDeclVertexSize(decl, 0); // ***** first pass - search already loaded shader for (SGeometry* v_geom : v_geoms) diff --git a/src/Layers/xrRenderDX11/CommonTypes.h b/src/Layers/xrRenderDX11/CommonTypes.h index 29456aa251c..5fc176b32f8 100644 --- a/src/Layers/xrRenderDX11/CommonTypes.h +++ b/src/Layers/xrRenderDX11/CommonTypes.h @@ -267,3 +267,11 @@ typedef ID3D11ShaderReflectionType ID3DShaderReflectionType; typedef dx10State ID3DState; #define DX10_ONLY(expr) expr + +using VertexBufferHandle = ID3D11Buffer*; +using IndexBufferHandle = ID3D11Buffer*; +using ConstantBufferHandle = ID3D11Buffer*; +using HostBufferHandle = void*; + +using VertexElement = D3DVERTEXELEMENT9; +using InputElementDesc = D3D11_INPUT_ELEMENT_DESC; diff --git a/src/Layers/xrRenderDX9/BufferUtils.cpp b/src/Layers/xrRenderDX9/BufferUtils.cpp new file mode 100644 index 00000000000..b6ebd7e0234 --- /dev/null +++ b/src/Layers/xrRenderDX9/BufferUtils.cpp @@ -0,0 +1,209 @@ +#include "stdafx.h" + +u32 GetFVFVertexSize(u32 FVF) +{ + return D3DXGetFVFVertexSize(FVF); +} + +u32 GetDeclVertexSize(const VertexElement* decl, DWORD Stream) +{ + return D3DXGetDeclVertexSize(decl, Stream); +} + +u32 GetDeclLength(const VertexElement* decl) +{ + return D3DXGetDeclLength(decl); +} + +//----------------------------------------------------------------------------- +VertexStagingBuffer::VertexStagingBuffer() + : m_DeviceBuffer{ nullptr } + , m_HostBuffer{ nullptr } +{ +} + +VertexStagingBuffer::~VertexStagingBuffer() +{ + Destroy(); +} + +void VertexStagingBuffer::Create(size_t size, bool allowReadBack /*= false*/) +{ + m_Size = size; + m_AllowReadBack = allowReadBack; + + u32 dwUsage = allowReadBack ? 0 : D3DUSAGE_WRITEONLY; + if (HW.Caps.geometry.bSoftware) + dwUsage |= D3DUSAGE_SOFTWAREPROCESSING; + R_CHK(HW.pDevice->CreateVertexBuffer(size, dwUsage, 0, D3DPOOL_MANAGED, &m_DeviceBuffer, nullptr)); + VERIFY(m_DeviceBuffer); + + HW.stats_manager.increment_stats_vb(m_DeviceBuffer); + AddRef(); +} + +bool VertexStagingBuffer::IsValid() const +{ + return !!m_DeviceBuffer; +} + +void* VertexStagingBuffer::Map( + size_t offset /*= 0 */, + size_t size /*= 0 */, + bool read /*= false*/) +{ + VERIFY(IsValid()); + VERIFY2(!read || m_AllowReadBack, "Can't read from write only buffer"); + VERIFY(size <= m_Size); + + DWORD mapMode = read ? D3DLOCK_READONLY : 0; + R_CHK(m_DeviceBuffer->Lock(offset, size, const_cast(&m_HostBuffer), mapMode)); + return m_HostBuffer; +} + +void VertexStagingBuffer::Unmap(bool doFlush /*= false*/) +{ + VERIFY(IsValid()); + R_CHK(m_DeviceBuffer->Unlock()); +} + +void VertexStagingBuffer::DiscardHostBuffer() +{ + /* Do nothing */ +} + +VertexBufferHandle VertexStagingBuffer::GetBufferHandle() const +{ + return m_DeviceBuffer; +} + +void VertexStagingBuffer::Destroy() +{ + HW.stats_manager.decrement_stats_vb(m_DeviceBuffer); + _RELEASE(m_DeviceBuffer); + m_DeviceBuffer = nullptr; +} + +size_t VertexStagingBuffer::GetSystemMemoryUsage() const +{ + if (IsValid()) + { + D3DVERTEXBUFFER_DESC desc; + m_DeviceBuffer->GetDesc(&desc); + + if (desc.Pool == D3DPOOL_MANAGED || desc.Pool == D3DPOOL_SCRATCH) + return desc.Size; + } + + return 0; +} + +size_t VertexStagingBuffer::GetVideoMemoryUsage() const +{ + if (IsValid()) + { + D3DVERTEXBUFFER_DESC desc; + m_DeviceBuffer->GetDesc(&desc); + + if (desc.Pool == D3DPOOL_DEFAULT || desc.Pool == D3DPOOL_MANAGED) + return desc.Size; + } + + return 0; +} + +//----------------------------------------------------------------------------- +IndexStagingBuffer::IndexStagingBuffer() + : m_DeviceBuffer{ nullptr } + , m_HostBuffer{ nullptr } +{ +} + +IndexStagingBuffer::~IndexStagingBuffer() +{ + Destroy(); +} + +void IndexStagingBuffer::Create(size_t size, bool allowReadBack /*= false*/) +{ + m_Size = size; + m_AllowReadBack = allowReadBack; + + u32 dwUsage = m_AllowReadBack ? 0 : D3DUSAGE_WRITEONLY; + if (HW.Caps.geometry.bSoftware) + dwUsage |= D3DUSAGE_SOFTWAREPROCESSING; + R_CHK(HW.pDevice->CreateIndexBuffer(size, dwUsage, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &m_DeviceBuffer, NULL)); + + HW.stats_manager.increment_stats_ib(m_DeviceBuffer); + AddRef(); +} + +bool IndexStagingBuffer::IsValid() const +{ + return !!m_DeviceBuffer; +} + +void* IndexStagingBuffer::Map( + size_t offset /*= 0 */, + size_t size /*= 0 */, + bool read /*= false*/) +{ + VERIFY(IsValid()); + VERIFY2(!read || m_AllowReadBack, "Can't read from write only buffer"); + VERIFY(size <= m_Size); + + DWORD mapMode = read ? D3DLOCK_READONLY : 0; + R_CHK(m_DeviceBuffer->Lock(offset, size, const_cast(&m_HostBuffer), mapMode)); + return m_HostBuffer; +} + +void IndexStagingBuffer::Unmap(bool doFlush /*= false*/) +{ + VERIFY(IsValid()); + R_CHK(m_DeviceBuffer->Unlock()); +} + +void IndexStagingBuffer::DiscardHostBuffer() +{ + /* Do nothing */ +} + +IndexBufferHandle IndexStagingBuffer::GetBufferHandle() const +{ + return m_DeviceBuffer; +} + +void IndexStagingBuffer::Destroy() +{ + HW.stats_manager.decrement_stats_ib(m_DeviceBuffer); + _RELEASE(m_DeviceBuffer); + m_DeviceBuffer = nullptr; +} + +size_t IndexStagingBuffer::GetSystemMemoryUsage() const +{ + if (IsValid()) + { + D3DINDEXBUFFER_DESC desc; + m_DeviceBuffer->GetDesc(&desc); + + if (desc.Pool == D3DPOOL_MANAGED || desc.Pool == D3DPOOL_SCRATCH) + return desc.Size; + } + + return 0; +} + +size_t IndexStagingBuffer::GetVideoMemoryUsage() const +{ + if (IsValid()) + { + D3DINDEXBUFFER_DESC desc; + m_DeviceBuffer->GetDesc(&desc); + + if (desc.Pool == D3DPOOL_DEFAULT || desc.Pool == D3DPOOL_MANAGED) + return desc.Size; + } + + return 0; +} diff --git a/src/Layers/xrRenderDX9/CommonTypes.h b/src/Layers/xrRenderDX9/CommonTypes.h index e1144218807..6c122e4970a 100644 --- a/src/Layers/xrRenderDX9/CommonTypes.h +++ b/src/Layers/xrRenderDX9/CommonTypes.h @@ -20,3 +20,13 @@ typedef IDirect3DVolumeTexture9 ID3DTexture3D; typedef IDirect3DStateBlock9 ID3DState; #define DX10_ONLY(expr) do {} while (0) + +using unused_t = int[0]; + +using VertexBufferHandle = ID3DVertexBuffer*; +using IndexBufferHandle = ID3DIndexBuffer*; +using ConstantBufferHandle = unused_t; +using HostBufferHandle = void*; + +using VertexElement = D3DVERTEXELEMENT9; +using InputElementDesc = unused_t; diff --git a/src/Layers/xrRenderDX9/dx9R_Backend_Runtime.h b/src/Layers/xrRenderDX9/dx9R_Backend_Runtime.h index 29aca50ca42..83002a6d53c 100644 --- a/src/Layers/xrRenderDX9/dx9R_Backend_Runtime.h +++ b/src/Layers/xrRenderDX9/dx9R_Backend_Runtime.h @@ -71,7 +71,7 @@ ICF void CBackend::set_VS(ID3DVertexShader* _vs, LPCSTR _n) } } -ICF void CBackend::set_Vertices(ID3DVertexBuffer* _vb, u32 _vb_stride) +ICF void CBackend::set_Vertices(VertexBufferHandle _vb, u32 _vb_stride) { if ((vb != _vb) || (vb_stride != _vb_stride)) { @@ -85,7 +85,7 @@ ICF void CBackend::set_Vertices(ID3DVertexBuffer* _vb, u32 _vb_stride) } } -ICF void CBackend::set_Indices(ID3DIndexBuffer* _ib) +ICF void CBackend::set_Indices(IndexBufferHandle _ib) { if (ib != _ib) { diff --git a/src/Layers/xrRenderGL/CommonTypes.h b/src/Layers/xrRenderGL/CommonTypes.h index 96170311459..7a96d865836 100644 --- a/src/Layers/xrRenderGL/CommonTypes.h +++ b/src/Layers/xrRenderGL/CommonTypes.h @@ -24,3 +24,13 @@ typedef enum D3D_COMPARISON_FUNC { } D3D_COMPARISON_FUNC; #define DX10_ONLY(expr) do {} while (0) + +using unused_t = int[0]; + +using IndexBufferHandle = GLuint; +using VertexBufferHandle = GLuint; +using ConstantBufferHandle = GLuint; +using HostBufferHandle = void*; + +using VertexElement = D3DVERTEXELEMENT9; +using InputElementDesc = unused_t; diff --git a/src/Layers/xrRenderGL/glBufferPool.cpp b/src/Layers/xrRenderGL/glBufferPool.cpp index 85cd38cfb2b..bc22619bea2 100644 --- a/src/Layers/xrRenderGL/glBufferPool.cpp +++ b/src/Layers/xrRenderGL/glBufferPool.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" #include "glBufferPool.h" -#include "glBufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" CBufferPool GLBuffers; @@ -40,7 +40,7 @@ void CBufferPool::CreateVertexBuffer(IGLVertexBuffer* &pBuffer, const void* pDat R_ASSERT(nullptr == pBuffer); pBuffer = new IGLVertexBuffer(); - glBufferUtils::CreateVertexBuffer(&pBuffer->m_Buffer, pData, DataSize, bImmutable); + BufferUtils::CreateVertexBuffer(&pBuffer->m_Buffer, pData, DataSize, bImmutable); m_VertexBuffers.emplace_back(pBuffer); } @@ -49,7 +49,7 @@ void CBufferPool::CreateIndexBuffer(IGLIndexBuffer* &pBuffer, const void* pData, R_ASSERT(nullptr == pBuffer); pBuffer = new IGLIndexBuffer(); - glBufferUtils::CreateIndexBuffer(&pBuffer->m_Buffer, pData, DataSize, bImmutable); + BufferUtils::CreateIndexBuffer(&pBuffer->m_Buffer, pData, DataSize, bImmutable); m_IndexBuffers.emplace_back(pBuffer); } diff --git a/src/Layers/xrRenderGL/glBufferUtils.cpp b/src/Layers/xrRenderGL/glBufferUtils.cpp index a24c63727db..38845c573c0 100644 --- a/src/Layers/xrRenderGL/glBufferUtils.cpp +++ b/src/Layers/xrRenderGL/glBufferUtils.cpp @@ -1,9 +1,9 @@ #include "stdafx.h" -#include "glBufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" -namespace glBufferUtils +namespace BufferUtils { -void CreateBuffer(GLuint* pBuffer, const void* pData, UINT DataSize, bool bImmutable, bool bIndexBuffer) +HRESULT CreateBuffer(GLuint* pBuffer, const void* pData, UINT DataSize, bool bImmutable, bool bIndexBuffer) { GLenum usage = bImmutable ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW; GLenum target = bIndexBuffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER; @@ -11,17 +11,19 @@ void CreateBuffer(GLuint* pBuffer, const void* pData, UINT DataSize, bool bImmut glGenBuffers(1, pBuffer); glBindBuffer(target, *pBuffer); CHK_GL(glBufferData(target, DataSize, pData, usage)); + return S_OK; } -void CreateVertexBuffer(GLuint* pBuffer, const void* pData, UINT DataSize, bool bImmutable) +HRESULT CreateVertexBuffer(VertexBufferHandle* pBuffer, const void* pData, UINT DataSize, bool bImmutable) { return CreateBuffer(pBuffer, pData, DataSize, bImmutable, false); } -void CreateIndexBuffer(GLuint* pBuffer, const void* pData, UINT DataSize, bool bImmutable) +HRESULT CreateIndexBuffer(IndexBufferHandle* pBuffer, const void* pData, UINT DataSize, bool bImmutable) { - return CreateBuffer(pBuffer, pData, DataSize, bImmutable, true); + return CreateBuffer(static_cast(pBuffer), pData, DataSize, bImmutable, true); } +} // namespace glBufferUtils const GLsizei VertexSizeList[] = { @@ -125,7 +127,7 @@ const GLuint VertexUsageList[] = ~0u, // D3DDECLUSAGE_SAMPLE }; -GLsizei GetDeclVertexSize(const D3DVERTEXELEMENT9* decl) +u32 GetDeclVertexSize(const VertexElement* decl, DWORD Stream) { GLsizei size = 0; for (int i = 0; i < MAXD3DDECLLENGTH; ++i) @@ -140,12 +142,12 @@ GLsizei GetDeclVertexSize(const D3DVERTEXELEMENT9* decl) return size; } -void ConvertVertexDeclaration(const D3DVERTEXELEMENT9* dxdecl, SDeclaration* decl) +void ConvertVertexDeclaration(const VertexElement* dxdecl, SDeclaration* decl) { RCache.set_Format(decl); // XXX: tamlin: use 'stride', or drop it. - GLsizei stride = GetDeclVertexSize(dxdecl); + GLsizei stride = GetDeclVertexSize(dxdecl, 0); for (int i = 0; i < MAXD3DDECLLENGTH; ++i) { const D3DVERTEXELEMENT9& desc = dxdecl[i]; @@ -168,7 +170,7 @@ void ConvertVertexDeclaration(const D3DVERTEXELEMENT9* dxdecl, SDeclaration* dec } } -GLsizei GetFVFVertexSize(u32 FVF) +u32 GetFVFVertexSize(u32 FVF) { GLsizei offset = 0; @@ -278,4 +280,197 @@ u32 GetDeclLength(const D3DVERTEXELEMENT9* decl) return element - decl; } -} // namespace glBufferUtils + +//----------------------------------------------------------------------------- +VertexStagingBuffer::VertexStagingBuffer() + : m_DeviceBuffer{ 0 } + , m_HostBuffer{ nullptr } +{ +} + +VertexStagingBuffer::~VertexStagingBuffer() +{ + Destroy(); +} + +void VertexStagingBuffer::Create(size_t size, bool allowReadBack /*= false*/) +{ + m_Size = size; + m_AllowReadBack = allowReadBack; + + m_HostBuffer = xr_alloc(size); + AddRef(); +} + +bool VertexStagingBuffer::IsValid() const +{ + return !!m_DeviceBuffer; +} + +void* VertexStagingBuffer::Map( + size_t offset /*= 0*/, + size_t size /*= 0*/, + bool read /*= false*/) +{ + VERIFY2(m_HostBuffer, "Buffer wasn't created or already discarded"); + VERIFY2(!read || m_AllowReadBack, "Can't read from write only buffer"); + VERIFY2((size + offset) <= m_Size, "Map region is too large"); + + return static_cast(m_HostBuffer) + offset; +} + +void VertexStagingBuffer::Unmap(bool doFlush /*= false*/) +{ + if (!doFlush) + { + /* Do nothing*/ + return; + } + + VERIFY(m_HostBuffer && m_Size); + + // Upload data to device + BufferUtils::CreateVertexBuffer(&m_DeviceBuffer, m_HostBuffer, m_Size, true); + + if (!m_AllowReadBack) + { + // Cache buffer isn't required anymore. Free host memory + DiscardHostBuffer(); + } +} + +VertexBufferHandle VertexStagingBuffer::GetBufferHandle() const +{ + return m_DeviceBuffer; +} + +void VertexStagingBuffer::Destroy() +{ + DiscardHostBuffer(); + m_Size = 0; + + if (m_DeviceBuffer) + { + glDeleteBuffers(1, &m_DeviceBuffer); + m_DeviceBuffer = 0; + } +} + +void VertexStagingBuffer::DiscardHostBuffer() +{ + if (m_HostBuffer) + xr_delete(m_HostBuffer); +} + +size_t VertexStagingBuffer::GetSystemMemoryUsage() const +{ + return m_HostBuffer ? m_Size : 0; +} + +size_t VertexStagingBuffer::GetVideoMemoryUsage() const +{ + if (!m_DeviceBuffer) + return 0; + + GLint bufferSize; + glBindBuffer(GL_ARRAY_BUFFER, m_DeviceBuffer); + CHK_GL(glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize)); + return bufferSize; +} + +//----------------------------------------------------------------------------- +IndexStagingBuffer::IndexStagingBuffer() + : m_DeviceBuffer{ 0 } + , m_HostBuffer{ nullptr } +{ +} + +IndexStagingBuffer::~IndexStagingBuffer() +{ + Destroy(); +} + +void IndexStagingBuffer::Create(size_t size, bool allowReadBack /*= false*/) +{ + m_Size = size; + m_AllowReadBack = allowReadBack; + + m_HostBuffer = xr_alloc(size); + AddRef(); +} + +bool IndexStagingBuffer::IsValid() const +{ + return !!m_DeviceBuffer; +} + +void* IndexStagingBuffer::Map( + size_t offset /*= 0*/, + size_t size /*= 0*/, + bool read /*= false*/) +{ + VERIFY2(m_HostBuffer, "Buffer wasn't created or already discarded"); + VERIFY2(!read || m_AllowReadBack, "Can't read from write only buffer"); + VERIFY2((size + offset) <= m_Size, "Map region is too large"); + + return static_cast(m_HostBuffer) + offset; +} + +void IndexStagingBuffer::Unmap(bool doFlush /*= false*/) +{ + if (!doFlush) + { + /* Do nothing*/ + return; + } + + VERIFY(m_HostBuffer && m_Size); + + // Upload data to device + BufferUtils::CreateVertexBuffer(&m_DeviceBuffer, m_HostBuffer, m_Size, true); + + if (!m_AllowReadBack) + { + // Cache buffer isn't required anymore. Free host memory + DiscardHostBuffer(); + } +} + +IndexBufferHandle IndexStagingBuffer::GetBufferHandle() const +{ + return m_DeviceBuffer; +} + +void IndexStagingBuffer::Destroy() +{ + DiscardHostBuffer(); + m_Size = 0; + + if (m_DeviceBuffer) + { + glDeleteBuffers(1, &m_DeviceBuffer); + m_DeviceBuffer = 0; + } +} + +void IndexStagingBuffer::DiscardHostBuffer() +{ + if (m_HostBuffer) + xr_delete(m_HostBuffer); +} + +size_t IndexStagingBuffer::GetSystemMemoryUsage() const +{ + return m_HostBuffer ? m_Size : 0; +} + +size_t IndexStagingBuffer::GetVideoMemoryUsage() const +{ + if (!m_DeviceBuffer) + return 0; + + GLint bufferSize; + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_DeviceBuffer); + CHK_GL(glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize)); + return bufferSize; +} diff --git a/src/Layers/xrRenderGL/glBufferUtils.h b/src/Layers/xrRenderGL/glBufferUtils.h deleted file mode 100644 index 9b4aa751806..00000000000 --- a/src/Layers/xrRenderGL/glBufferUtils.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#ifdef USE_OGL - -namespace glBufferUtils -{ -void CreateVertexBuffer(GLuint* pBuffer, const void* pData, UINT DataSize, bool bImmutable = true); -void CreateIndexBuffer(GLuint* pBuffer, const void* pData, UINT DataSize, bool bImmutable = true); -GLsizei GetFVFVertexSize(u32 FVF); -GLsizei GetDeclVertexSize(const D3DVERTEXELEMENT9* decl); -u32 GetDeclLength(const D3DVERTEXELEMENT9* decl); -void ConvertVertexDeclaration(u32 FVF, SDeclaration* decl); -void ConvertVertexDeclaration(const D3DVERTEXELEMENT9* dxdecl, SDeclaration* decl); -}; - -#endif // USE_OGL diff --git a/src/Layers/xrRenderGL/glDetailManager_VS.cpp b/src/Layers/xrRenderGL/glDetailManager_VS.cpp index 69c3a9c624b..4da2a4d0e45 100644 --- a/src/Layers/xrRenderGL/glDetailManager_VS.cpp +++ b/src/Layers/xrRenderGL/glDetailManager_VS.cpp @@ -2,7 +2,7 @@ #include "Layers/xrRender/DetailManager.h" #include "xrEngine/IGame_Persistent.h" #include "xrEngine/Environment.h" -#include "Layers/xrRenderGL/glBufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" const int quant = 16384; const int c_hdr = 10; diff --git a/src/Layers/xrRenderPC_GL/glResourceManager_Resources.cpp b/src/Layers/xrRenderPC_GL/glResourceManager_Resources.cpp index 4b0412613f9..fd348be794a 100644 --- a/src/Layers/xrRenderPC_GL/glResourceManager_Resources.cpp +++ b/src/Layers/xrRenderPC_GL/glResourceManager_Resources.cpp @@ -9,7 +9,7 @@ #include "../xrRender/tss.h" #include "../xrRender/blenders/Blender.h" #include "../xrRender/blenders/Blender_Recorder.h" -#include "../xrRenderGL/glBufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" #include "Layers/xrRender/ShaderResourceTraits.h" void fix_texture_name(LPSTR fn); @@ -107,8 +107,8 @@ void CResourceManager::_DeletePass(const SPass* P) static BOOL dcl_equal(D3DVERTEXELEMENT9* a, D3DVERTEXELEMENT9* b) { // check sizes - u32 a_size = glBufferUtils::GetDeclLength(a); - u32 b_size = glBufferUtils::GetDeclLength(b); + u32 a_size = GetDeclLength(a); + u32 b_size = GetDeclLength(b); if (a_size != b_size) return FALSE; return 0 == memcmp(a, b, a_size * sizeof(D3DVERTEXELEMENT9)); } @@ -126,7 +126,7 @@ SDeclaration* CResourceManager::_CreateDecl(u32 FVF) glGenVertexArrays(1, &D->dcl); D->FVF = FVF; - glBufferUtils::ConvertVertexDeclaration(FVF, D); + ConvertVertexDeclaration(FVF, D); D->dwFlags |= xr_resource_flagged::RF_REGISTERED; return D; @@ -145,9 +145,9 @@ SDeclaration* CResourceManager::_CreateDecl(D3DVERTEXELEMENT9* dcl) glGenVertexArrays(1, &D->dcl); D->FVF = 0; - u32 dcl_size = glBufferUtils::GetDeclLength(dcl) + 1; + u32 dcl_size = GetDeclLength(dcl) + 1; D->dcl_code.assign(dcl, dcl + dcl_size); - glBufferUtils::ConvertVertexDeclaration(dcl, D); + ConvertVertexDeclaration(dcl, D); D->dwFlags |= xr_resource_flagged::RF_REGISTERED; return D; @@ -298,7 +298,7 @@ SGeometry* CResourceManager::CreateGeom(D3DVERTEXELEMENT9* decl, GLuint vb, GLui R_ASSERT(decl && vb); SDeclaration* dcl = _CreateDecl(decl); - u32 vb_stride = glBufferUtils::GetDeclVertexSize(decl); + u32 vb_stride = GetDeclVertexSize(decl, 0); // ***** first pass - search already loaded shader for (SGeometry* geom : v_geoms) @@ -322,7 +322,7 @@ SGeometry* CResourceManager::CreateGeom(u32 FVF, GLuint vb, GLuint ib) R_ASSERT(FVF && vb); SDeclaration* dcl = _CreateDecl(FVF); - u32 vb_stride = glBufferUtils::GetFVFVertexSize(FVF); + u32 vb_stride = GetFVFVertexSize(FVF); // ***** first pass - search already loaded shader for (SGeometry* geom : v_geoms) diff --git a/src/Layers/xrRenderPC_GL/gl_loader.cpp b/src/Layers/xrRenderPC_GL/gl_loader.cpp index a26d01f25e5..9037bb87d55 100644 --- a/src/Layers/xrRenderPC_GL/gl_loader.cpp +++ b/src/Layers/xrRenderPC_GL/gl_loader.cpp @@ -7,7 +7,7 @@ #include "xrEngine/x_ray.h" #include "xrEngine/IGame_Persistent.h" #include "xrCore/stream_reader.h" -#include "Layers/xrRenderGL/glBufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" #include "Layers/xrRenderGL/glBufferPool.h" #include "Layers/xrRender/FHierrarhyVisual.h" @@ -203,13 +203,13 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) fs->r(dcl, buffer_size); fs->advance(-(int)buffer_size); - u32 dcl_len = glBufferUtils::GetDeclLength(dcl) + 1; + u32 dcl_len = GetDeclLength(dcl) + 1; _DC[i].resize(dcl_len); fs->r(_DC[i].begin(), dcl_len * sizeof(D3DVERTEXELEMENT9)); // count, size u32 vCount = fs->r_u32(); - u32 vSize = glBufferUtils::GetDeclVertexSize(dcl); + u32 vSize = GetDeclVertexSize(dcl, 0); Msg("* [Loading VB] %d verts, %d Kb", vCount, vCount * vSize / 1024); // Check if buffer is less then 2048 kb diff --git a/src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj b/src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj index f1ca40877ab..903bea2c9c3 100644 --- a/src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj +++ b/src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj @@ -348,7 +348,6 @@ - @@ -357,6 +356,7 @@ + diff --git a/src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj.filters b/src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj.filters index accc8d22569..2c94b17723d 100644 --- a/src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj.filters +++ b/src/Layers/xrRenderPC_GL/xrRender_GL.vcxproj.filters @@ -971,9 +971,6 @@ Models\Visuals - - dx9 to gl Utils - Models\Visuals @@ -1133,6 +1130,9 @@ Kernel + + dx9 to gl Utils + diff --git a/src/Layers/xrRenderPC_R1/FStaticRender.h b/src/Layers/xrRenderPC_R1/FStaticRender.h index 80e199c0fcd..6603b94e961 100644 --- a/src/Layers/xrRenderPC_R1/FStaticRender.h +++ b/src/Layers/xrRenderPC_R1/FStaticRender.h @@ -51,8 +51,8 @@ class CRender : public D3DXRenderBase xr_vector Shaders; typedef svector VertexDeclarator; xr_vector nDC, xDC; - xr_vector nVB, xVB; - xr_vector nIB, xIB; + xr_vector nVB, xVB; + xr_vector nIB, xIB; xr_vector Visuals; CPSLibrary PSLibrary; CLight_DB Lights; diff --git a/src/Layers/xrRenderPC_R1/FStaticRender_Loader.cpp b/src/Layers/xrRenderPC_R1/FStaticRender_Loader.cpp index 886a9216fbe..c5590cf7583 100644 --- a/src/Layers/xrRenderPC_R1/FStaticRender_Loader.cpp +++ b/src/Layers/xrRenderPC_R1/FStaticRender_Loader.cpp @@ -161,28 +161,24 @@ void CRender::level_Unload() //*** VB/IB for (I = 0; I < nVB.size(); I++) { - HW.stats_manager.decrement_stats_vb(nVB[I]); - _RELEASE(nVB[I]); + nVB[I].Release(); } for (I = 0; I < xVB.size(); I++) { - HW.stats_manager.decrement_stats_vb(xVB[I]); - _RELEASE(xVB[I]); + xVB[I].Release(); } nVB.clear(); xVB.clear(); for (I = 0; I < nIB.size(); I++) { - HW.stats_manager.decrement_stats_ib(nIB[I]); - _RELEASE(nIB[I]); + nIB[I].Release(); } for (I = 0; I < xIB.size(); I++) { - HW.stats_manager.decrement_stats_ib(xIB[I]); - _RELEASE(xIB[I]); + xIB[I].Release(); } nIB.clear(); @@ -210,13 +206,12 @@ void CRender::level_Unload() void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) { Resources->Evict(); - u32 dwUsage = D3DUSAGE_WRITEONLY | (HW.Caps.geometry.bSoftware ? D3DUSAGE_SOFTWAREPROCESSING : 0); // Vertex buffers if (base_fs->find_chunk(fsL_VB)) { xr_vector& _DC = alternative ? xDC : nDC; - xr_vector& _VB = alternative ? xVB : nVB; + xr_vector& _VB = alternative ? xVB : nVB; // Use DX9-style declarators CStreamReader* fs = base_fs->open_chunk(fsL_VB); @@ -224,8 +219,8 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) _DC.resize(count); _VB.resize(count); - u32 buffer_size = (MAXD3DDECLLENGTH + 1) * sizeof(D3DVERTEXELEMENT9); - D3DVERTEXELEMENT9* dcl = (D3DVERTEXELEMENT9*)xr_alloca(buffer_size); + u32 buffer_size = (MAXD3DDECLLENGTH + 1) * sizeof(VertexElement); + VertexElement* dcl = (VertexElement*)xr_alloca(buffer_size); for (u32 i = 0; i < count; i++) { @@ -236,7 +231,7 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) fs->r(dcl, buffer_size); fs->advance(-(int)buffer_size); - u32 dcl_len = D3DXGetDeclLength(dcl) + 1; + u32 dcl_len = GetDeclLength(dcl) + 1; _DC[i].resize(dcl_len); fs->r(_DC[i].begin(), dcl_len * sizeof(D3DVERTEXELEMENT9)); @@ -244,17 +239,15 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) // count, size u32 vCount = fs->r_u32(); - u32 vSize = D3DXGetDeclVertexSize(dcl, 0); + u32 vSize = GetDeclVertexSize(dcl, 0); Msg("* [Loading VB] %d verts, %d Kb", vCount, (vCount * vSize) / 1024); // Create and fill - BYTE* pData = nullptr; - R_CHK(HW.pDevice->CreateVertexBuffer(vCount * vSize, dwUsage, 0, D3DPOOL_MANAGED, &_VB[i], nullptr)); - HW.stats_manager.increment_stats_vb(_VB[i]); - R_CHK(_VB[i]->Lock(0, 0, (void**)&pData, 0)); + _VB[i].Create(vCount * vSize); + BYTE* pData = static_cast(_VB[i].Map()); fs->r(pData, vCount * vSize); // CopyMemory (pData,fs->pointer(),vCount*vSize); //.???? copy while skip T&B - _VB[i]->Unlock(); + _VB[i].Unmap(); // upload vertex data // fs->advance (vCount*vSize); } @@ -268,7 +261,7 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) // Index buffers if (base_fs->find_chunk(fsL_IB)) { - xr_vector& _IB = alternative ? xIB : nIB; + xr_vector& _IB = alternative ? xIB : nIB; CStreamReader* fs = base_fs->open_chunk(fsL_IB); u32 count = fs->r_u32(); @@ -279,13 +272,11 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) Msg("* [Loading IB] %d indices, %d Kb", iCount, (iCount * 2) / 1024); // Create and fill - BYTE* pData = nullptr; - R_CHK(HW.pDevice->CreateIndexBuffer(iCount * 2, dwUsage, D3DFMT_INDEX16, D3DPOOL_MANAGED, &_IB[i], nullptr)); - HW.stats_manager.increment_stats_ib(_IB[i]); - R_CHK(_IB[i]->Lock(0, 0, (void**)&pData, 0)); + _IB[i].Create(iCount * 2); + BYTE* pData = static_cast(_IB[i].Map()); // CopyMemory (pData,fs->pointer(),iCount*2); fs->r(pData, iCount * 2); - _IB[i]->Unlock(); + _IB[i].Unmap(true); // upload index data // fs->advance (iCount*2); } diff --git a/src/Layers/xrRenderPC_R1/stdafx.h b/src/Layers/xrRenderPC_R1/stdafx.h index 96afd0510ce..a085a0b76d2 100644 --- a/src/Layers/xrRenderPC_R1/stdafx.h +++ b/src/Layers/xrRenderPC_R1/stdafx.h @@ -7,13 +7,12 @@ #endif #include "xrEngine/stdafx.h" -#if defined(WINDOWS) + #include #include #include "Layers/xrRenderDX9/CommonTypes.h" -#endif -#include "Layers/xrRender/Debug/dxPixEventWrapper.h" +#include "Layers/xrRender/BufferUtils.h" #include "Layers/xrRenderDX9/dx9HW.h" #include "Layers/xrRender/Shader.h" #include "Layers/xrRender/R_Backend.h" @@ -26,6 +25,7 @@ #define R_R4 4 #define RENDER R_R1 +#include "Layers/xrRender/Debug/dxPixEventWrapper.h" #include "Layers/xrRender/ResourceManager.h" #include "xrEngine/vis_common.h" #include "xrEngine/Render.h" diff --git a/src/Layers/xrRenderPC_R1/xrRender_R1.vcxproj b/src/Layers/xrRenderPC_R1/xrRender_R1.vcxproj index 86018cb4180..abce8f99525 100644 --- a/src/Layers/xrRenderPC_R1/xrRender_R1.vcxproj +++ b/src/Layers/xrRenderPC_R1/xrRender_R1.vcxproj @@ -204,6 +204,7 @@ + @@ -306,6 +307,7 @@ + diff --git a/src/Layers/xrRenderPC_R1/xrRender_R1.vcxproj.filters b/src/Layers/xrRenderPC_R1/xrRender_R1.vcxproj.filters index d4e5f91f021..4bf238be60d 100644 --- a/src/Layers/xrRenderPC_R1/xrRender_R1.vcxproj.filters +++ b/src/Layers/xrRenderPC_R1/xrRender_R1.vcxproj.filters @@ -576,6 +576,9 @@ Kernel + + Refactored\HW + @@ -962,6 +965,9 @@ Refactored\Execution & 3D\Shaders\Resources + + Refactored\HW + diff --git a/src/Layers/xrRenderPC_R2/r2.h b/src/Layers/xrRenderPC_R2/r2.h index 6ddb406a6e9..c7b7b105273 100644 --- a/src/Layers/xrRenderPC_R2/r2.h +++ b/src/Layers/xrRenderPC_R2/r2.h @@ -118,8 +118,8 @@ class CRender : public D3DXRenderBase xr_vector Shaders; typedef svector VertexDeclarator; xr_vector nDC, xDC; - xr_vector nVB, xVB; - xr_vector nIB, xIB; + xr_vector nVB, xVB; + xr_vector nIB, xIB; xr_vector Visuals; CPSLibrary PSLibrary; diff --git a/src/Layers/xrRenderPC_R2/r2_loader.cpp b/src/Layers/xrRenderPC_R2/r2_loader.cpp index 8a8c9dd16fd..8569faad9b8 100644 --- a/src/Layers/xrRenderPC_R2/r2_loader.cpp +++ b/src/Layers/xrRenderPC_R2/r2_loader.cpp @@ -158,28 +158,24 @@ void CRender::level_Unload() //*** VB/IB for (I = 0; I < nVB.size(); I++) { - HW.stats_manager.decrement_stats_vb(nVB[I]); - _RELEASE(nVB[I]); + nVB[I].Release(); } for (I = 0; I < xVB.size(); I++) { - HW.stats_manager.decrement_stats_vb(xVB[I]); - _RELEASE(xVB[I]); + xVB[I].Release(); } nVB.clear(); xVB.clear(); for (I = 0; I < nIB.size(); I++) { - HW.stats_manager.decrement_stats_ib(nIB[I]); - _RELEASE(nIB[I]); + nIB[I].Release(); } for (I = 0; I < xIB.size(); I++) { - HW.stats_manager.decrement_stats_ib(xIB[I]); - _RELEASE(xIB[I]); + xIB[I].Release(); } nIB.clear(); @@ -212,7 +208,7 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) // Vertex buffers { xr_vector& _DC = alternative ? xDC : nDC; - xr_vector& _VB = alternative ? xVB : nVB; + xr_vector& _VB = alternative ? xVB : nVB; // Use DX9-style declarators CStreamReader* fs = base_fs->open_chunk(fsL_VB); @@ -224,28 +220,26 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) { // decl // D3DVERTEXELEMENT9* dcl = (D3DVERTEXELEMENT9*) fs().pointer(); - u32 buffer_size = (MAXD3DDECLLENGTH + 1) * sizeof(D3DVERTEXELEMENT9); - D3DVERTEXELEMENT9* dcl = (D3DVERTEXELEMENT9*)xr_alloca(buffer_size); + u32 buffer_size = (MAXD3DDECLLENGTH + 1) * sizeof(VertexElement); + VertexElement* dcl = (VertexElement*)xr_alloca(buffer_size); fs->r(dcl, buffer_size); fs->advance(-(int)buffer_size); - u32 dcl_len = D3DXGetDeclLength(dcl) + 1; + u32 dcl_len = GetDeclLength(dcl) + 1; _DC[i].resize(dcl_len); - fs->r(_DC[i].begin(), dcl_len * sizeof(D3DVERTEXELEMENT9)); + fs->r(_DC[i].begin(), dcl_len * sizeof(VertexElement)); // count, size u32 vCount = fs->r_u32(); - u32 vSize = D3DXGetDeclVertexSize(dcl, 0); + u32 vSize = GetDeclVertexSize(dcl, 0); Msg("* [Loading VB] %d verts, %d Kb", vCount, (vCount * vSize) / 1024); // Create and fill - BYTE* pData = 0; - R_CHK(HW.pDevice->CreateVertexBuffer(vCount * vSize, dwUsage, 0, D3DPOOL_MANAGED, &_VB[i], 0)); - HW.stats_manager.increment_stats_vb(_VB[i]); - R_CHK(_VB[i]->Lock(0, 0, (void**)&pData, 0)); + _VB[i].Create(vCount * vSize); + BYTE* pData = static_cast(_VB[i].Map()); // CopyMemory (pData,fs().pointer(),vCount*vSize); fs->r(pData, vCount * vSize); - _VB[i]->Unlock(); + _VB[i].Unmap(true); // upload vertex data // fs->advance (vCount*vSize); } @@ -254,7 +248,7 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) // Index buffers { - xr_vector& _IB = alternative ? xIB : nIB; + xr_vector& _IB = alternative ? xIB : nIB; CStreamReader* fs = base_fs->open_chunk(fsL_IB); u32 count = fs->r_u32(); @@ -265,13 +259,11 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) Msg("* [Loading IB] %d indices, %d Kb", iCount, (iCount * 2) / 1024); // Create and fill - BYTE* pData = 0; - R_CHK(HW.pDevice->CreateIndexBuffer(iCount * 2, dwUsage, D3DFMT_INDEX16, D3DPOOL_MANAGED, &_IB[i], 0)); - HW.stats_manager.increment_stats_ib(_IB[i]); - R_CHK(_IB[i]->Lock(0, 0, (void**)&pData, 0)); + _IB[i].Create(iCount * 2); + BYTE* pData = static_cast(_IB[i].Map()); // CopyMemory (pData,fs().pointer(),iCount*2); fs->r(pData, iCount * 2); - _IB[i]->Unlock(); + _IB[i].Unmap(true); // upload index data // fs().advance (iCount*2); } diff --git a/src/Layers/xrRenderPC_R2/r2_rendertarget.h b/src/Layers/xrRenderPC_R2/r2_rendertarget.h index 5f4bdca6080..d42e6ff3eae 100644 --- a/src/Layers/xrRenderPC_R2/r2_rendertarget.h +++ b/src/Layers/xrRenderPC_R2/r2_rendertarget.h @@ -105,17 +105,17 @@ class CRenderTarget : public IRender_Target ref_geom g_accum_omnipart; ref_geom g_accum_volumetric; - ID3DVertexBuffer* g_accum_point_vb; - ID3DIndexBuffer* g_accum_point_ib; + VertexStagingBuffer g_accum_point_vb; + IndexStagingBuffer g_accum_point_ib; - ID3DVertexBuffer* g_accum_omnip_vb; - ID3DIndexBuffer* g_accum_omnip_ib; + VertexStagingBuffer g_accum_omnip_vb; + IndexStagingBuffer g_accum_omnip_ib; - ID3DVertexBuffer* g_accum_spot_vb; - ID3DIndexBuffer* g_accum_spot_ib; + VertexStagingBuffer g_accum_spot_vb; + IndexStagingBuffer g_accum_spot_ib; - ID3DVertexBuffer* g_accum_volumetric_vb; - ID3DIndexBuffer* g_accum_volumetric_ib; + VertexStagingBuffer g_accum_volumetric_vb; + IndexStagingBuffer g_accum_volumetric_ib; // SSAO ref_shader s_ssao; diff --git a/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_omnipart_geom.cpp b/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_omnipart_geom.cpp index cb500454167..c3e6bbf20c4 100644 --- a/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_omnipart_geom.cpp +++ b/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_omnipart_geom.cpp @@ -3,45 +3,36 @@ void CRenderTarget::accum_omnip_geom_create() { - u32 dwUsage = D3DUSAGE_WRITEONLY; - // vertices { u32 vCount = DU_SPHERE_PART_NUMVERTEX; u32 vSize = 3 * 4; - R_CHK(HW.pDevice->CreateVertexBuffer(vCount * vSize, dwUsage, 0, D3DPOOL_MANAGED, &g_accum_omnip_vb, 0)); - HW.stats_manager.increment_stats_vb(g_accum_omnip_vb); + g_accum_omnip_vb.Create(vCount * vSize); - BYTE* pData = 0; - R_CHK(g_accum_omnip_vb->Lock(0, 0, (void**)&pData, 0)); + BYTE* pData = static_cast(g_accum_omnip_vb.Map()); CopyMemory(pData, du_sphere_part_vertices, vCount * vSize); - g_accum_omnip_vb->Unlock(); + g_accum_omnip_vb.Unmap(true); // upload vertex data } // Indices { u32 iCount = DU_SPHERE_PART_NUMFACES * 3; - BYTE* pData = 0; - R_CHK( - HW.pDevice->CreateIndexBuffer(iCount * 2, dwUsage, D3DFMT_INDEX16, D3DPOOL_MANAGED, &g_accum_omnip_ib, 0)); - HW.stats_manager.increment_stats_ib(g_accum_omnip_ib); - R_CHK(g_accum_omnip_ib->Lock(0, 0, (void**)&pData, 0)); + g_accum_omnip_ib.Create(iCount * 2); + BYTE* pData = static_cast(g_accum_omnip_ib.Map()); CopyMemory(pData, du_sphere_part_faces, iCount * 2); - g_accum_omnip_ib->Unlock(); + g_accum_omnip_ib.Unmap(true); // upload index data } } void CRenderTarget::accum_omnip_geom_destroy() { #ifdef DEBUG - _SHOW_REF("g_accum_omnip_ib", g_accum_omnip_ib); + _SHOW_REF("g_accum_omnip_ib", &g_accum_omnip_ib); #endif // DEBUG - HW.stats_manager.decrement_stats_ib(g_accum_omnip_ib); - _RELEASE(g_accum_omnip_ib); + g_accum_omnip_ib.Release(); #ifdef DEBUG - _SHOW_REF("g_accum_omnip_vb", g_accum_omnip_vb); + _SHOW_REF("g_accum_omnip_vb", &g_accum_omnip_vb); #endif // DEBUG - HW.stats_manager.decrement_stats_vb(g_accum_omnip_vb); - _RELEASE(g_accum_omnip_vb); + g_accum_omnip_vb.Release(); } diff --git a/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_point_geom.cpp b/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_point_geom.cpp index a97f8129537..5b03362db2d 100644 --- a/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_point_geom.cpp +++ b/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_point_geom.cpp @@ -285,45 +285,36 @@ u16 du_sphere_faces[DU_SPHERE_NUMFACES*3]={ void CRenderTarget::accum_point_geom_create() { - u32 dwUsage = D3DUSAGE_WRITEONLY; - // vertices { u32 vCount = DU_SPHERE_NUMVERTEX; u32 vSize = 3 * 4; - R_CHK(HW.pDevice->CreateVertexBuffer(vCount * vSize, dwUsage, 0, D3DPOOL_MANAGED, &g_accum_point_vb, 0)); - HW.stats_manager.increment_stats_vb(g_accum_point_vb); + g_accum_point_vb.Create(vCount * vSize); - BYTE* pData = 0; - R_CHK(g_accum_point_vb->Lock(0, 0, (void**)&pData, 0)); + BYTE* pData = static_cast(g_accum_point_vb.Map()); CopyMemory(pData, du_sphere_vertices, vCount * vSize); - g_accum_point_vb->Unlock(); + g_accum_point_vb.Unmap(true); // upload vertex data } // Indices { u32 iCount = DU_SPHERE_NUMFACES * 3; - BYTE* pData = 0; - R_CHK( - HW.pDevice->CreateIndexBuffer(iCount * 2, dwUsage, D3DFMT_INDEX16, D3DPOOL_MANAGED, &g_accum_point_ib, 0)); - HW.stats_manager.increment_stats_ib(g_accum_point_ib); - R_CHK(g_accum_point_ib->Lock(0, 0, (void**)&pData, 0)); + g_accum_point_ib.Create(iCount * 2); + BYTE* pData = static_cast(g_accum_point_ib.Map()); CopyMemory(pData, du_sphere_faces, iCount * 2); - g_accum_point_ib->Unlock(); + g_accum_point_ib.Unmap(true); // upload index data } } void CRenderTarget::accum_point_geom_destroy() { #ifdef DEBUG - _SHOW_REF("g_accum_point_ib", g_accum_point_ib); + _SHOW_REF("g_accum_point_ib", &g_accum_point_ib); #endif // DEBUG - HW.stats_manager.decrement_stats_ib(g_accum_point_ib); - _RELEASE(g_accum_point_ib); + g_accum_point_ib.Release(); #ifdef DEBUG - _SHOW_REF("g_accum_point_vb", g_accum_point_vb); + _SHOW_REF("g_accum_point_vb", &g_accum_point_vb); #endif // DEBUG - HW.stats_manager.decrement_stats_vb(g_accum_point_vb); - _RELEASE(g_accum_point_vb); + g_accum_point_vb.Release(); } diff --git a/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_spot_geom.cpp b/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_spot_geom.cpp index 25eafaf6305..f2c1cead340 100644 --- a/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_spot_geom.cpp +++ b/src/Layers/xrRenderPC_R2/r2_rendertarget_accum_spot_geom.cpp @@ -62,46 +62,38 @@ u16 du_cone_faces[DU_CONE_NUMFACES*3]= void CRenderTarget::accum_spot_geom_create() { - u32 dwUsage = D3DUSAGE_WRITEONLY; - // vertices { u32 vCount = DU_CONE_NUMVERTEX; u32 vSize = 3 * 4; - R_CHK(HW.pDevice->CreateVertexBuffer(vCount * vSize, dwUsage, 0, D3DPOOL_MANAGED, &g_accum_spot_vb, 0)); - HW.stats_manager.increment_stats_vb(g_accum_spot_vb); - BYTE* pData = 0; - R_CHK(g_accum_spot_vb->Lock(0, 0, (void**)&pData, 0)); + g_accum_spot_vb.Create(vCount * vSize); + BYTE* pData = static_cast(g_accum_spot_vb.Map()); CopyMemory(pData, du_cone_vertices, vCount * vSize); - g_accum_spot_vb->Unlock(); + g_accum_spot_vb.Unmap(true); // upload vertex data } // Indices { u32 iCount = DU_CONE_NUMFACES * 3; - BYTE* pData = 0; - R_CHK(HW.pDevice->CreateIndexBuffer(iCount * 2, dwUsage, D3DFMT_INDEX16, D3DPOOL_MANAGED, &g_accum_spot_ib, 0)); - HW.stats_manager.increment_stats_ib(g_accum_spot_ib); - R_CHK(g_accum_spot_ib->Lock(0, 0, (void**)&pData, 0)); + g_accum_spot_ib.Create(iCount * 2); + BYTE* pData = static_cast(g_accum_spot_ib.Map()); CopyMemory(pData, du_cone_faces, iCount * 2); - g_accum_spot_ib->Unlock(); + g_accum_spot_ib.Unmap(true); // upload index data } } void CRenderTarget::accum_spot_geom_destroy() { #ifdef DEBUG - _SHOW_REF("g_accum_spot_ib", g_accum_spot_ib); + _SHOW_REF("g_accum_spot_ib", &g_accum_spot_ib); #endif // DEBUG - HW.stats_manager.decrement_stats_ib(g_accum_spot_ib); - _RELEASE(g_accum_spot_ib); + g_accum_spot_ib.Release(); #ifdef DEBUG - _SHOW_REF("g_accum_spot_vb", g_accum_spot_vb); + _SHOW_REF("g_accum_spot_vb", &g_accum_spot_vb); #endif // DEBUG - HW.stats_manager.decrement_stats_vb(g_accum_spot_vb); - _RELEASE(g_accum_spot_vb); + g_accum_spot_vb.Release(); } struct Slice @@ -111,18 +103,14 @@ struct Slice void CRenderTarget::accum_volumetric_geom_create() { - u32 dwUsage = D3DUSAGE_WRITEONLY; - // vertices { // VOLUMETRIC_SLICES quads const u32 vCount = VOLUMETRIC_SLICES * 4; u32 vSize = 3 * 4; - R_CHK(HW.pDevice->CreateVertexBuffer(vCount * vSize, dwUsage, 0, D3DPOOL_MANAGED, &g_accum_volumetric_vb, 0)); - HW.stats_manager.increment_stats_vb(g_accum_volumetric_vb); + g_accum_volumetric_vb.Create(vCount * vSize); - BYTE* pData = 0; - R_CHK(g_accum_volumetric_vb->Lock(0, 0, (void**)&pData, 0)); + BYTE* pData = static_cast(g_accum_volumetric_vb.Map()); Slice* pSlice = (Slice*)pData; float t = 0; float dt = 1.0f / (VOLUMETRIC_SLICES - 1); @@ -134,20 +122,15 @@ void CRenderTarget::accum_volumetric_geom_create() pSlice[i].m_Vert[3] = Fvector().set(1, 1, t); t += dt; } - - g_accum_volumetric_vb->Unlock(); + g_accum_volumetric_vb.Unmap(true); // upload vertex data } // Indices { const u32 iCount = VOLUMETRIC_SLICES * 6; - BYTE* pData = 0; - R_CHK(HW.pDevice->CreateIndexBuffer( - iCount * 2, dwUsage, D3DFMT_INDEX16, D3DPOOL_MANAGED, &g_accum_volumetric_ib, 0)); - HW.stats_manager.increment_stats_ib(g_accum_volumetric_ib); - R_CHK(g_accum_volumetric_ib->Lock(0, 0, (void**)&pData, 0)); - u16* pInd = (u16*)pData; + g_accum_volumetric_ib.Create(iCount * 2); + u16* pInd = static_cast(g_accum_volumetric_ib.Map()); for (u16 i = 0; i < VOLUMETRIC_SLICES; ++i, pInd += 6) { u16 basevert = i * 4; @@ -158,21 +141,19 @@ void CRenderTarget::accum_volumetric_geom_create() pInd[4] = basevert + 1; pInd[5] = basevert + 3; } - g_accum_volumetric_ib->Unlock(); + g_accum_volumetric_ib.Unmap(true); // upload index data } } void CRenderTarget::accum_volumetric_geom_destroy() { #ifdef DEBUG - _SHOW_REF("g_accum_volumetric_ib", g_accum_volumetric_ib); + _SHOW_REF("g_accum_volumetric_ib", &g_accum_volumetric_ib); #endif // DEBUG - HW.stats_manager.decrement_stats_ib(g_accum_volumetric_ib); - _RELEASE(g_accum_volumetric_ib); + g_accum_volumetric_ib.Release(); #ifdef DEBUG - _SHOW_REF("g_accum_volumetric_vb", g_accum_volumetric_vb); + _SHOW_REF("g_accum_volumetric_vb", &g_accum_volumetric_vb); #endif // DEBUG - HW.stats_manager.decrement_stats_vb(g_accum_volumetric_vb); - _RELEASE(g_accum_volumetric_vb); + g_accum_volumetric_vb.Release(); } diff --git a/src/Layers/xrRenderPC_R2/xrRender_R2.vcxproj b/src/Layers/xrRenderPC_R2/xrRender_R2.vcxproj index ac69f7aaf0a..3a4b914fafe 100644 --- a/src/Layers/xrRenderPC_R2/xrRender_R2.vcxproj +++ b/src/Layers/xrRenderPC_R2/xrRender_R2.vcxproj @@ -205,6 +205,7 @@ + @@ -319,6 +320,7 @@ + diff --git a/src/Layers/xrRenderPC_R2/xrRender_R2.vcxproj.filters b/src/Layers/xrRenderPC_R2/xrRender_R2.vcxproj.filters index eddd89cabb4..80d53c5236b 100644 --- a/src/Layers/xrRenderPC_R2/xrRender_R2.vcxproj.filters +++ b/src/Layers/xrRenderPC_R2/xrRender_R2.vcxproj.filters @@ -618,6 +618,9 @@ Kernel + + Refactored\HW + @@ -1109,6 +1112,9 @@ Refactored\Execution & 3D\Shaders\Resources + + Refactored\HW + diff --git a/src/Layers/xrRenderPC_R3/r3.h b/src/Layers/xrRenderPC_R3/r3.h index 131177cdd46..fe62e0e0f44 100644 --- a/src/Layers/xrRenderPC_R3/r3.h +++ b/src/Layers/xrRenderPC_R3/r3.h @@ -151,8 +151,8 @@ class CRender : public D3DXRenderBase xr_vector Shaders; typedef svector VertexDeclarator; xr_vector nDC, xDC; - xr_vector nVB, xVB; - xr_vector nIB, xIB; + xr_vector nVB, xVB; + xr_vector nIB, xIB; xr_vector Visuals; CPSLibrary PSLibrary; diff --git a/src/Layers/xrRenderPC_R3/r3_loader.cpp b/src/Layers/xrRenderPC_R3/r3_loader.cpp index 81dd65d8151..8bcffa9c801 100644 --- a/src/Layers/xrRenderPC_R3/r3_loader.cpp +++ b/src/Layers/xrRenderPC_R3/r3_loader.cpp @@ -7,7 +7,7 @@ #include "xrEngine/x_ray.h" #include "xrEngine/IGame_Persistent.h" #include "xrCore/stream_reader.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" #include "Layers/xrRenderDX10/3DFluid/dx103DFluidVolume.h" #include "Layers/xrRender/FHierrarhyVisual.h" @@ -164,26 +164,22 @@ void CRender::level_Unload() //*** VB/IB for (I = 0; I < nVB.size(); I++) { - HW.stats_manager.decrement_stats_vb(nVB[I]); - _RELEASE(nVB[I]); + nVB[I].Release(); } for (I = 0; I < xVB.size(); I++) { - HW.stats_manager.decrement_stats_vb(xVB[I]); - _RELEASE(xVB[I]); + xVB[I].Release(); } nVB.clear(); xVB.clear(); for (I = 0; I < nIB.size(); I++) { - HW.stats_manager.decrement_stats_ib(nIB[I]); - _RELEASE(nIB[I]); + nIB[I].Release(); } for (I = 0; I < xIB.size(); I++) { - HW.stats_manager.decrement_stats_ib(xIB[I]); - _RELEASE(xIB[I]); + xIB[I].Release(); } nIB.clear(); xIB.clear(); @@ -216,7 +212,7 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) // Vertex buffers { xr_vector& _DC = alternative ? xDC : nDC; - xr_vector& _VB = alternative ? xVB : nVB; + xr_vector& _VB = alternative ? xVB : nVB; // Use DX9-style declarators CStreamReader* fs = base_fs->open_chunk(fsL_VB); @@ -224,8 +220,8 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) u32 count = fs->r_u32(); _DC.resize(count); _VB.resize(count); - u32 bufferSize = (MAXD3DDECLLENGTH + 1) * sizeof(D3DVERTEXELEMENT9); - D3DVERTEXELEMENT9* dcl = (D3DVERTEXELEMENT9*)xr_alloca(bufferSize); + u32 bufferSize = (MAXD3DDECLLENGTH + 1) * sizeof(VertexElement); + VertexElement* dcl = (VertexElement*)xr_alloca(bufferSize); for (u32 i = 0; i < count; i++) { // decl @@ -233,29 +229,21 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) fs->r(dcl, bufferSize); fs->advance(-(int)bufferSize); - u32 dcl_len = D3DXGetDeclLength(dcl) + 1; + u32 dcl_len = GetDeclLength(dcl) + 1; _DC[i].resize(dcl_len); - fs->r(_DC[i].begin(), dcl_len * sizeof(D3DVERTEXELEMENT9)); + fs->r(_DC[i].begin(), dcl_len * sizeof(VertexElement)); // count, size u32 vCount = fs->r_u32(); - u32 vSize = D3DXGetDeclVertexSize(dcl, 0); + u32 vSize = GetDeclVertexSize(dcl, 0); Msg("* [Loading VB] %d verts, %d Kb", vCount, (vCount * vSize) / 1024); - // Create and fill - // BYTE* pData = 0; - // R_CHK (HW.pDevice->CreateVertexBuffer(vCount*vSize,dwUsage,0,D3DPOOL_MANAGED,&_VB[i],0)); - // R_CHK (_VB[i]->Lock(0,0,(void**)&pData,0)); - // CopyMemory (pData,fs().pointer(),vCount*vSize); - // fs->r (pData,vCount*vSize); - //_VB[i]->Unlock (); // TODO: DX10: Check fragmentation. // Check if buffer is less then 2048 kb - BYTE* pData = xr_alloc(vCount * vSize); + _VB[i].Create(vCount * vSize); + BYTE* pData = static_cast(_VB[i].Map()); fs->r(pData, vCount * vSize); - dx10BufferUtils::CreateVertexBuffer(&_VB[i], pData, vCount * vSize); - HW.stats_manager.increment_stats_vb(_VB[i]); - xr_free(pData); + _VB[i].Unmap(true); // upload vertex data // fs->advance (vCount*vSize); } @@ -264,7 +252,7 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) // Index buffers { - xr_vector& _IB = alternative ? xIB : nIB; + xr_vector& _IB = alternative ? xIB : nIB; CStreamReader* fs = base_fs->open_chunk(fsL_IB); u32 count = fs->r_u32(); @@ -274,22 +262,12 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) u32 iCount = fs->r_u32(); Msg("* [Loading IB] %d indices, %d Kb", iCount, (iCount * 2) / 1024); - // Create and fill - // BYTE* pData = 0; - // R_CHK - // (HW.pDevice->CreateIndexBuffer(iCount*2,dwUsage,D3DFMT_INDEX16,D3DPOOL_MANAGED,&_IB[i],0)); - // R_CHK (_IB[i]->Lock(0,0,(void**)&pData,0)); - // CopyMemory (pData,fs().pointer(),iCount*2); - // fs->r (pData,iCount*2); - //_IB[i]->Unlock (); - // TODO: DX10: Check fragmentation. // Check if buffer is less then 2048 kb - BYTE* pData = xr_alloc(iCount * 2); + _IB[i].Create(iCount * 2); + BYTE* pData = static_cast(_IB[i].Map()); fs->r(pData, iCount * 2); - dx10BufferUtils::CreateIndexBuffer(&_IB[i], pData, iCount * 2); - HW.stats_manager.increment_stats_ib(_IB[i]); - xr_free(pData); + _IB[i].Unmap(true); // upload index data // fs().advance (iCount*2); } diff --git a/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_omnipart_geom.cpp b/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_omnipart_geom.cpp index 1e6d4c443c3..81127262a5e 100644 --- a/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_omnipart_geom.cpp +++ b/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_omnipart_geom.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" #include "Layers/xrRender/du_sphere_part.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" void CRenderTarget::accum_omnip_geom_create() { @@ -22,7 +22,7 @@ void CRenderTarget::accum_omnip_geom_create() // CopyMemory (pData,du_sphere_part_vertices,vCount*vSize); // g_accum_omnip_vb->Unlock (); - R_CHK(dx10BufferUtils::CreateVertexBuffer(&g_accum_omnip_vb, du_sphere_part_vertices, vCount * vSize)); + R_CHK(BufferUtils::CreateVertexBuffer(&g_accum_omnip_vb, du_sphere_part_vertices, vCount * vSize)); HW.stats_manager.increment_stats_vb(g_accum_omnip_vb); } @@ -37,7 +37,7 @@ void CRenderTarget::accum_omnip_geom_create() // CopyMemory (pData,du_sphere_part_faces,iCount*2); // g_accum_omnip_ib->Unlock (); - R_CHK(dx10BufferUtils::CreateIndexBuffer(&g_accum_omnip_ib, du_sphere_part_faces, iCount * 2)); + R_CHK(BufferUtils::CreateIndexBuffer(&g_accum_omnip_ib, du_sphere_part_faces, iCount * 2)); HW.stats_manager.increment_stats_ib(g_accum_omnip_ib); } } diff --git a/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_point_geom.cpp b/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_point_geom.cpp index 48a3a2eec21..4b2083042a9 100644 --- a/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_point_geom.cpp +++ b/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_point_geom.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" #include "Layers/xrRender/du_sphere.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" /* Fvector du_sphere_vertices[DU_SPHERE_NUMVERTEX]= @@ -303,7 +303,7 @@ void CRenderTarget::accum_point_geom_create() // CopyMemory (pData,du_sphere_vertices,vCount*vSize); // g_accum_point_vb->Unlock (); - R_CHK(dx10BufferUtils::CreateVertexBuffer(&g_accum_point_vb, du_sphere_vertices, vCount * vSize)); + R_CHK(BufferUtils::CreateVertexBuffer(&g_accum_point_vb, du_sphere_vertices, vCount * vSize)); HW.stats_manager.increment_stats_vb(g_accum_point_vb); } @@ -318,7 +318,7 @@ void CRenderTarget::accum_point_geom_create() // CopyMemory (pData,du_sphere_faces,iCount*2); // g_accum_point_ib->Unlock (); - R_CHK(dx10BufferUtils::CreateIndexBuffer(&g_accum_point_ib, du_sphere_faces, iCount * 2)); + R_CHK(BufferUtils::CreateIndexBuffer(&g_accum_point_ib, du_sphere_faces, iCount * 2)); HW.stats_manager.increment_stats_ib(g_accum_point_ib); } } diff --git a/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_spot_geom.cpp b/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_spot_geom.cpp index e67a4be7382..c2c2208129b 100644 --- a/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_spot_geom.cpp +++ b/src/Layers/xrRenderPC_R3/r3_rendertarget_accum_spot_geom.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" #include "Layers/xrRender/du_cone.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" /* Fvector du_cone_vertices[DU_CONE_NUMVERTEX]= @@ -81,7 +81,7 @@ void CRenderTarget::accum_spot_geom_create() // CopyMemory (pData,du_cone_vertices,vCount*vSize); // g_accum_spot_vb->Unlock (); - R_CHK(dx10BufferUtils::CreateVertexBuffer(&g_accum_spot_vb, du_cone_vertices, vCount * vSize)); + R_CHK(BufferUtils::CreateVertexBuffer(&g_accum_spot_vb, du_cone_vertices, vCount * vSize)); HW.stats_manager.increment_stats_vb(g_accum_spot_vb); } @@ -96,7 +96,7 @@ void CRenderTarget::accum_spot_geom_create() // CopyMemory (pData,du_cone_faces,iCount*2); // g_accum_spot_ib->Unlock (); - R_CHK(dx10BufferUtils::CreateIndexBuffer(&g_accum_spot_ib, du_cone_faces, iCount * 2)); + R_CHK(BufferUtils::CreateIndexBuffer(&g_accum_spot_ib, du_cone_faces, iCount * 2)); HW.stats_manager.increment_stats_ib(g_accum_spot_ib); } } @@ -163,7 +163,7 @@ void CRenderTarget::accum_volumetric_geom_create() t += dt; } - R_CHK(dx10BufferUtils::CreateVertexBuffer(&g_accum_volumetric_vb, &pSlice, vCount * vSize)); + R_CHK(BufferUtils::CreateVertexBuffer(&g_accum_volumetric_vb, &pSlice, vCount * vSize)); HW.stats_manager.increment_stats_vb(g_accum_volumetric_vb); } @@ -202,7 +202,7 @@ void CRenderTarget::accum_volumetric_geom_create() pInd[5] = basevert + 3; } - R_CHK(dx10BufferUtils::CreateIndexBuffer(&g_accum_volumetric_ib, &Datap, iCount * 2)); + R_CHK(BufferUtils::CreateIndexBuffer(&g_accum_volumetric_ib, &Datap, iCount * 2)); HW.stats_manager.increment_stats_ib(g_accum_volumetric_ib); // R_CHK diff --git a/src/Layers/xrRenderPC_R3/xrRender_R3.vcxproj b/src/Layers/xrRenderPC_R3/xrRender_R3.vcxproj index da64b326c78..8e0f253a2bd 100644 --- a/src/Layers/xrRenderPC_R3/xrRender_R3.vcxproj +++ b/src/Layers/xrRenderPC_R3/xrRender_R3.vcxproj @@ -195,7 +195,6 @@ - @@ -224,6 +223,7 @@ + diff --git a/src/Layers/xrRenderPC_R3/xrRender_R3.vcxproj.filters b/src/Layers/xrRenderPC_R3/xrRender_R3.vcxproj.filters index eff6088e20a..8289c312e9c 100644 --- a/src/Layers/xrRenderPC_R3/xrRender_R3.vcxproj.filters +++ b/src/Layers/xrRenderPC_R3/xrRender_R3.vcxproj.filters @@ -627,9 +627,6 @@ Interfase implementations\MSAARender - - dx9 to dx10 Utils - dx9 to dx10 Utils @@ -699,6 +696,9 @@ Kernel + + dx9 to dx10 Utils + diff --git a/src/Layers/xrRenderPC_R4/r4.h b/src/Layers/xrRenderPC_R4/r4.h index 58b5f38263b..6f5af17fe93 100644 --- a/src/Layers/xrRenderPC_R4/r4.h +++ b/src/Layers/xrRenderPC_R4/r4.h @@ -159,8 +159,8 @@ class CRender : public D3DXRenderBase xr_vector Shaders; typedef svector VertexDeclarator; xr_vector nDC, xDC; - xr_vector nVB, xVB; - xr_vector nIB, xIB; + xr_vector nVB, xVB; + xr_vector nIB, xIB; xr_vector Visuals; CPSLibrary PSLibrary; diff --git a/src/Layers/xrRenderPC_R4/r4_loader.cpp b/src/Layers/xrRenderPC_R4/r4_loader.cpp index 60b65f75220..2278dc3ab37 100644 --- a/src/Layers/xrRenderPC_R4/r4_loader.cpp +++ b/src/Layers/xrRenderPC_R4/r4_loader.cpp @@ -7,7 +7,7 @@ #include "xrEngine/x_ray.h" #include "xrEngine/IGame_Persistent.h" #include "xrCore/stream_reader.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" #include "Layers/xrRenderDX10/3DFluid/dx103DFluidVolume.h" #include "Layers/xrRender/FHierrarhyVisual.h" @@ -164,26 +164,22 @@ void CRender::level_Unload() //*** VB/IB for (I = 0; I < nVB.size(); I++) { - HW.stats_manager.decrement_stats_vb(nVB[I]); - _RELEASE(nVB[I]); + nVB[I].Release(); } for (I = 0; I < xVB.size(); I++) { - HW.stats_manager.decrement_stats_vb(xVB[I]); - _RELEASE(xVB[I]); + xVB[I].Release(); } nVB.clear(); xVB.clear(); for (I = 0; I < nIB.size(); I++) { - HW.stats_manager.decrement_stats_ib(nIB[I]); - _RELEASE(nIB[I]); + nIB[I].Release(); } for (I = 0; I < xIB.size(); I++) { - HW.stats_manager.decrement_stats_ib(xIB[I]); - _RELEASE(xIB[I]); + xIB[I].Release(); } nIB.clear(); xIB.clear(); @@ -208,7 +204,7 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) // Vertex buffers { xr_vector& _DC = alternative ? xDC : nDC; - xr_vector& _VB = alternative ? xVB : nVB; + xr_vector& _VB = alternative ? xVB : nVB; // Use DX9-style declarators CStreamReader* fs = base_fs->open_chunk(fsL_VB); @@ -216,8 +212,8 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) u32 count = fs->r_u32(); _DC.resize(count); _VB.resize(count); - u32 bufferSize = (MAXD3DDECLLENGTH + 1) * sizeof(D3DVERTEXELEMENT9); - D3DVERTEXELEMENT9* dcl = (D3DVERTEXELEMENT9*)xr_alloca(bufferSize); + u32 bufferSize = (MAXD3DDECLLENGTH + 1) * sizeof(VertexElement); + VertexElement* dcl = (VertexElement*)xr_alloca(bufferSize); for (u32 i = 0; i < count; i++) { // decl @@ -225,29 +221,22 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) fs->r(dcl, bufferSize); fs->advance(-(int)bufferSize); - u32 dcl_len = D3DXGetDeclLength(dcl) + 1; + u32 dcl_len = GetDeclLength(dcl) + 1; _DC[i].resize(dcl_len); - fs->r(_DC[i].begin(), dcl_len * sizeof(D3DVERTEXELEMENT9)); + fs->r(_DC[i].begin(), dcl_len * sizeof(VertexElement)); // count, size u32 vCount = fs->r_u32(); - u32 vSize = D3DXGetDeclVertexSize(dcl, 0); + u32 vSize = GetDeclVertexSize(dcl, 0); Msg("* [Loading VB] %d verts, %d Kb", vCount, (vCount * vSize) / 1024); - // Create and fill - // BYTE* pData = 0; - // R_CHK (HW.pDevice->CreateVertexBuffer(vCount*vSize,dwUsage,0,D3DPOOL_MANAGED,&_VB[i],0)); - // R_CHK (_VB[i]->Lock(0,0,(void**)&pData,0)); - // CopyMemory (pData,fs().pointer(),vCount*vSize); - // fs->r (pData,vCount*vSize); - //_VB[i]->Unlock (); + // TODO: DX10: Check fragmentation. // Check if buffer is less then 2048 kb - BYTE* pData = xr_alloc(vCount * vSize); + _VB[i].Create(vCount * vSize); + BYTE* pData = static_cast(_VB[i].Map()); fs->r(pData, vCount * vSize); - dx10BufferUtils::CreateVertexBuffer(&_VB[i], pData, vCount * vSize); - HW.stats_manager.increment_stats_vb(_VB[i]); - xr_free(pData); + _VB[i].Unmap(true); // upload vertex data // fs->advance (vCount*vSize); } @@ -256,7 +245,7 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) // Index buffers { - xr_vector& _IB = alternative ? xIB : nIB; + xr_vector& _IB = alternative ? xIB : nIB; CStreamReader* fs = base_fs->open_chunk(fsL_IB); u32 count = fs->r_u32(); @@ -266,22 +255,12 @@ void CRender::LoadBuffers(CStreamReader* base_fs, bool alternative) u32 iCount = fs->r_u32(); Msg("* [Loading IB] %d indices, %d Kb", iCount, (iCount * 2) / 1024); - // Create and fill - // BYTE* pData = 0; - // R_CHK - // (HW.pDevice->CreateIndexBuffer(iCount*2,dwUsage,D3DFMT_INDEX16,D3DPOOL_MANAGED,&_IB[i],0)); - // R_CHK (_IB[i]->Lock(0,0,(void**)&pData,0)); - // CopyMemory (pData,fs().pointer(),iCount*2); - // fs->r (pData,iCount*2); - //_IB[i]->Unlock (); - // TODO: DX10: Check fragmentation. // Check if buffer is less then 2048 kb - BYTE* pData = xr_alloc(iCount * 2); + _IB[i].Create(iCount * 2); + BYTE* pData = static_cast(_IB[i].Map()); fs->r(pData, iCount * 2); - dx10BufferUtils::CreateIndexBuffer(&_IB[i], pData, iCount * 2); - HW.stats_manager.increment_stats_ib(_IB[i]); - xr_free(pData); + _IB[i].Unmap(true); // upload index data // fs().advance (iCount*2); } diff --git a/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_omnipart_geom.cpp b/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_omnipart_geom.cpp index cd8f175fb7b..dcd833bfac3 100644 --- a/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_omnipart_geom.cpp +++ b/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_omnipart_geom.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" #include "Layers/xrRender/du_sphere_part.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" void CRenderTarget::accum_omnip_geom_create() { @@ -22,7 +22,7 @@ void CRenderTarget::accum_omnip_geom_create() // CopyMemory (pData,du_sphere_part_vertices,vCount*vSize); // g_accum_omnip_vb->Unlock (); - R_CHK(dx10BufferUtils::CreateVertexBuffer(&g_accum_omnip_vb, du_sphere_part_vertices, vCount * vSize)); + R_CHK(BufferUtils::CreateVertexBuffer(&g_accum_omnip_vb, du_sphere_part_vertices, vCount * vSize)); HW.stats_manager.increment_stats_vb(g_accum_omnip_vb); } @@ -37,7 +37,7 @@ void CRenderTarget::accum_omnip_geom_create() // CopyMemory (pData,du_sphere_part_faces,iCount*2); // g_accum_omnip_ib->Unlock (); - R_CHK(dx10BufferUtils::CreateIndexBuffer(&g_accum_omnip_ib, du_sphere_part_faces, iCount * 2)); + R_CHK(BufferUtils::CreateIndexBuffer(&g_accum_omnip_ib, du_sphere_part_faces, iCount * 2)); HW.stats_manager.increment_stats_ib(g_accum_omnip_ib); } } diff --git a/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_point_geom.cpp b/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_point_geom.cpp index e0db57e5e43..ae971355159 100644 --- a/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_point_geom.cpp +++ b/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_point_geom.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" #include "Layers/xrRender/du_sphere.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" /* Fvector du_sphere_vertices[DU_SPHERE_NUMVERTEX]= @@ -303,7 +303,7 @@ void CRenderTarget::accum_point_geom_create() // CopyMemory (pData,du_sphere_vertices,vCount*vSize); // g_accum_point_vb->Unlock (); - R_CHK(dx10BufferUtils::CreateVertexBuffer(&g_accum_point_vb, du_sphere_vertices, vCount * vSize)); + R_CHK(BufferUtils::CreateVertexBuffer(&g_accum_point_vb, du_sphere_vertices, vCount * vSize)); HW.stats_manager.increment_stats_vb(g_accum_point_vb); } @@ -318,7 +318,7 @@ void CRenderTarget::accum_point_geom_create() // CopyMemory (pData,du_sphere_faces,iCount*2); // g_accum_point_ib->Unlock (); - R_CHK(dx10BufferUtils::CreateIndexBuffer(&g_accum_point_ib, du_sphere_faces, iCount * 2)); + R_CHK(BufferUtils::CreateIndexBuffer(&g_accum_point_ib, du_sphere_faces, iCount * 2)); HW.stats_manager.increment_stats_ib(g_accum_point_ib); } } diff --git a/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_spot_geom.cpp b/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_spot_geom.cpp index fa137dfa1a9..e259044f204 100644 --- a/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_spot_geom.cpp +++ b/src/Layers/xrRenderPC_R4/r4_rendertarget_accum_spot_geom.cpp @@ -1,6 +1,6 @@ #include "stdafx.h" #include "Layers/xrRender/du_cone.h" -#include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRender/BufferUtils.h" /* Fvector du_cone_vertices[DU_CONE_NUMVERTEX]= @@ -81,7 +81,7 @@ void CRenderTarget::accum_spot_geom_create() // CopyMemory (pData,du_cone_vertices,vCount*vSize); // g_accum_spot_vb->Unlock (); - R_CHK(dx10BufferUtils::CreateVertexBuffer(&g_accum_spot_vb, du_cone_vertices, vCount * vSize)); + R_CHK(BufferUtils::CreateVertexBuffer(&g_accum_spot_vb, du_cone_vertices, vCount * vSize)); HW.stats_manager.increment_stats_vb(g_accum_spot_vb); } @@ -96,7 +96,7 @@ void CRenderTarget::accum_spot_geom_create() // CopyMemory (pData,du_cone_faces,iCount*2); // g_accum_spot_ib->Unlock (); - R_CHK(dx10BufferUtils::CreateIndexBuffer(&g_accum_spot_ib, du_cone_faces, iCount * 2)); + R_CHK(BufferUtils::CreateIndexBuffer(&g_accum_spot_ib, du_cone_faces, iCount * 2)); HW.stats_manager.increment_stats_ib(g_accum_spot_ib); } } @@ -164,7 +164,7 @@ void CRenderTarget::accum_volumetric_geom_create() t += dt; } - R_CHK(dx10BufferUtils::CreateVertexBuffer(&g_accum_volumetric_vb, &pSlice, vCount * vSize)); + R_CHK(BufferUtils::CreateVertexBuffer(&g_accum_volumetric_vb, &pSlice, vCount * vSize)); HW.stats_manager.increment_stats_vb(g_accum_volumetric_vb); } @@ -203,7 +203,7 @@ void CRenderTarget::accum_volumetric_geom_create() pInd[5] = basevert + 3; } - R_CHK(dx10BufferUtils::CreateIndexBuffer(&g_accum_volumetric_ib, &Datap, iCount * 2)); + R_CHK(BufferUtils::CreateIndexBuffer(&g_accum_volumetric_ib, &Datap, iCount * 2)); HW.stats_manager.increment_stats_ib(g_accum_volumetric_ib); // R_CHK diff --git a/src/Layers/xrRenderPC_R4/xrRender_R4.vcxproj b/src/Layers/xrRenderPC_R4/xrRender_R4.vcxproj index aee8f8993ba..1864840f83a 100644 --- a/src/Layers/xrRenderPC_R4/xrRender_R4.vcxproj +++ b/src/Layers/xrRenderPC_R4/xrRender_R4.vcxproj @@ -194,7 +194,6 @@ - @@ -223,6 +222,7 @@ + diff --git a/src/Layers/xrRenderPC_R4/xrRender_R4.vcxproj.filters b/src/Layers/xrRenderPC_R4/xrRender_R4.vcxproj.filters index 73420bface4..47023df055c 100644 --- a/src/Layers/xrRenderPC_R4/xrRender_R4.vcxproj.filters +++ b/src/Layers/xrRenderPC_R4/xrRender_R4.vcxproj.filters @@ -645,9 +645,6 @@ Interfase implementations\MSAARender - - dx9 to dx10 Utils - dx9 to dx10 Utils @@ -717,6 +714,9 @@ Kernel + + dx9 to dx10 Utils +