From 0ef0e089ec6afd74e36c8983f6f8ede461110b87 Mon Sep 17 00:00:00 2001 From: Jules Blok Date: Tue, 24 Nov 2015 22:57:11 +0100 Subject: [PATCH] Visuals: Add OpenGL implementations. --- src/Layers/xrRender/FBasicVisual.cpp | 11 ++++-- src/Layers/xrRender/FSkinned.cpp | 55 ++++++++++++++++++++------ src/Layers/xrRender/FTreeVisual.cpp | 14 ++++++- src/Layers/xrRender/FVisual.cpp | 59 ++++++++++++++++++++-------- src/Layers/xrRender/SkeletonX.h | 2 +- 5 files changed, 105 insertions(+), 36 deletions(-) diff --git a/src/Layers/xrRender/FBasicVisual.cpp b/src/Layers/xrRender/FBasicVisual.cpp index d0f2c40f370..a0c7e24e4dc 100644 --- a/src/Layers/xrRender/FBasicVisual.cpp +++ b/src/Layers/xrRender/FBasicVisual.cpp @@ -17,9 +17,14 @@ ////////////////////////////////////////////////////////////////////// IRender_Mesh::~IRender_Mesh() -{ - _RELEASE(p_rm_Vertices); - _RELEASE(p_rm_Indices); +{ +#ifdef USE_OGL + GLuint buffers[] = { p_rm_Vertices, p_rm_Indices }; + glDeleteBuffers(2, buffers); +#else + _RELEASE(p_rm_Vertices); + _RELEASE(p_rm_Indices); +#endif // USE_OGL } dxRender_Visual::dxRender_Visual () diff --git a/src/Layers/xrRender/FSkinned.cpp b/src/Layers/xrRender/FSkinned.cpp index 4a31f805786..e0f7c394708 100644 --- a/src/Layers/xrRender/FSkinned.cpp +++ b/src/Layers/xrRender/FSkinned.cpp @@ -1,14 +1,11 @@ #include "stdafx.h" #pragma hdrstop -#pragma warning(disable:4995) -#include -#pragma warning(default:4995) - #include "xrCore/FMesh.hpp" #include "FSkinned.h" #include "SkeletonX.h" #include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRenderGL/glBufferUtils.h" #include "xrEngine/EnnumerateVertices.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction @@ -417,7 +414,11 @@ void CSkeletonX_ext::_Load_hw (Fvisual& V, void * _verts_) Vertices1W.create ( crc, V.vCount, (vertBoned1W*)_verts_); } - u32 vStride = D3DXGetDeclVertexSize (dwDecl_01W,0); +#ifdef USE_OGL + u32 vStride = glBufferUtils::GetDeclVertexSize(dwDecl_01W); +#else + u32 vStride = D3DXGetDeclVertexSize (dwDecl_01W, 0); +#endif // USE_OGL VERIFY (vStride==sizeof(vertHW_1W)); // BYTE* bytes = 0; VERIFY (NULL==V.p_rm_Vertices); @@ -443,8 +444,12 @@ void CSkeletonX_ext::_Load_hw (Fvisual& V, void * _verts_) dst++; src++; } //R_CHK (HW.pDevice->CreateVertexBuffer(V.vCount*vStride,dwUsage,0,D3DPOOL_MANAGED,&V.p_rm_Vertices,0)); +#ifdef USE_OGL + glBufferUtils::CreateVertexBuffer (&V.p_rm_Vertices, dstOriginal, V.vCount*vStride); +#else R_CHK(dx10BufferUtils::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); V.rm_geom.create (dwDecl_01W, V.p_rm_Vertices, V.p_rm_Indices); @@ -458,7 +463,11 @@ 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 u32 vStride = D3DXGetDeclVertexSize (dwDecl_2W,0); +#endif // USE_OGL VERIFY (vStride==sizeof(vertHW_2W)); // BYTE* bytes = 0; VERIFY (NULL==V.p_rm_Vertices); @@ -485,8 +494,12 @@ void CSkeletonX_ext::_Load_hw (Fvisual& V, void * _verts_) dst->set (src->P,src->N,src->T,src->B,uv,int(src->matrix0)*3,int(src->matrix1)*3,src->w); dst++; src++; } +#ifdef USE_OGL + glBufferUtils::CreateVertexBuffer (&V.p_rm_Vertices, dstOriginal, V.vCount*vStride); +#else R_CHK(dx10BufferUtils::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); V.rm_geom.create (dwDecl_2W, V.p_rm_Vertices, V.p_rm_Indices); @@ -499,7 +512,11 @@ void CSkeletonX_ext::_Load_hw (Fvisual& V, void * _verts_) Vertices3W.create ( crc, V.vCount, (vertBoned3W*)_verts_); } +#ifdef USE_OGL + u32 vStride = glBufferUtils::GetDeclVertexSize(dwDecl_3W); +#else u32 vStride = D3DXGetDeclVertexSize (dwDecl_3W,0); +#endif // USE_OGL VERIFY (vStride==sizeof(vertHW_3W)); // BYTE* bytes = 0; VERIFY (NULL==V.p_rm_Vertices); @@ -529,8 +546,12 @@ void CSkeletonX_ext::_Load_hw (Fvisual& V, void * _verts_) dst++; src++; } +#ifdef USE_OGL + glBufferUtils::CreateVertexBuffer (&V.p_rm_Vertices, dstOriginal, V.vCount*vStride); +#else R_CHK(dx10BufferUtils::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); V.rm_geom.create (dwDecl_3W, V.p_rm_Vertices, V.p_rm_Indices); @@ -543,7 +564,11 @@ 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 u32 vStride = D3DXGetDeclVertexSize (dwDecl_4W,0); +#endif // USE_OGL VERIFY (vStride==sizeof(vertHW_4W)); // BYTE* bytes = 0; VERIFY (NULL==V.p_rm_Vertices); @@ -573,8 +598,12 @@ void CSkeletonX_ext::_Load_hw (Fvisual& V, void * _verts_) dst++; src++; } +#ifdef USE_OGL + glBufferUtils::CreateVertexBuffer (&V.p_rm_Vertices, dstOriginal, V.vCount*vStride); +#else R_CHK(dx10BufferUtils::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); V.rm_geom.create (dwDecl_4W, V.p_rm_Vertices, V.p_rm_Indices); @@ -716,7 +745,7 @@ void CSkeletonX_ext::_CollectBoneFaces(Fvisual* V, u32 iBase, u32 iCount) { u16* indices = 0; -#if defined(USE_DX10) || defined(USE_DX11) +#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL) indices = *m_Indices; #else // USE_DX10 R_CHK (V->p_rm_Indices->Lock(0,V->dwPrimitives*3,(void**)&indices,D3DLOCK_READONLY)); @@ -724,7 +753,7 @@ void CSkeletonX_ext::_CollectBoneFaces(Fvisual* V, u32 iBase, u32 iCount) indices += iBase; -#if !defined(USE_DX10) && !defined(USE_DX11) // Don't use hardware buffers in DX10 since we can't read them +#if !defined(USE_DX10) && !defined(USE_DX11) && !defined(USE_OGL) // Don't use hardware buffers in DX10 since we can't read them switch (RenderMode) { case RM_SKINNING_SOFT: @@ -796,7 +825,7 @@ void CSkeletonX_ext::_CollectBoneFaces(Fvisual* V, u32 iBase, u32 iCount) R_ASSERT2(0,"not implemented yet"); } -#if !defined(USE_DX10) && !defined(USE_DX11) // Don't use hardware buffers in DX10 since we can't read them +#if !defined(USE_DX10) && !defined(USE_DX11) && !defined(USE_OGL) // Don't use hardware buffers in DX10 since we can't read them break; case RM_SINGLE: case RM_SKINNING_1B: @@ -1011,7 +1040,7 @@ BOOL CSkeletonX_ext::_PickBone (IKinematics::pick_result &r, float dist, const CBoneData::FacesVec* faces = &BD.child_faces[ChildIDX]; BOOL result = FALSE; u16* indices = 0; -#if defined(USE_DX10) || defined(USE_DX11) +#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL) indices = *m_Indices; #else // USE_DX10 CHK_DX (V->p_rm_Indices->Lock(0,V->dwPrimitives*3,(void**)&indices,D3DLOCK_READONLY)); @@ -1032,7 +1061,7 @@ case RM_SKINNING_SOFT: result = _PickBoneSoft4W (r,dist,start,dir,indices+iBase,*faces); } -#if !defined(USE_DX10) && !defined(USE_DX11) +#if !defined(USE_DX10) && !defined(USE_DX11) && !defined(USE_OGL) break; case RM_SINGLE: case RM_SKINNING_1B: result = _PickBoneHW1W (r,dist,start,dir,V,indices+iBase,*faces); break; @@ -1068,7 +1097,7 @@ void CSkeletonX_PM:: EnumBoneVertices( SEnumVerticesCallback &C, u16 bone_id ) inherited2::_EnumBoneVertices( C, this, bone_id, iBase+SW.offset, SW.num_tris*3 ); } -#if defined(USE_DX10) || defined(USE_DX11) +#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL) void CSkeletonX_ext::_FillVerticesHW1W(const Fmatrix& view, CSkeletonWallmark& wm, const Fvector& normal, float size, Fvisual* V, u16* indices, CBoneData::FacesVec& faces) { @@ -1300,7 +1329,7 @@ void CSkeletonX_ext::_EnumBoneVertices ( SEnumVerticesCallback &C, Fvisual* V, u u16* indices = 0; //. R_CHK (V->pIndices->Lock(iBase,iCount, (void**)&indices, D3DLOCK_READONLY)); -#if defined(USE_DX10) || defined(USE_DX11) +#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL) VERIFY(*m_Indices); indices = *m_Indices; #else USE_DX10 @@ -1323,7 +1352,7 @@ void CSkeletonX_ext::_EnumBoneVertices ( SEnumVerticesCallback &C, Fvisual* V, u VERIFY( !!(*Vertices4W) ); TEnumBoneVertices( Vertices4W, indices+iBase, *faces, C ); } -#if !defined(USE_DX10) && !defined(USE_DX11) +#if !defined(USE_DX10) && !defined(USE_DX11) && !defined(USE_OGL) break; case RM_SINGLE: case RM_SKINNING_1B: TEnumBoneVertices ( (vertHW_1W*)vertices, indices+iBase, *faces, C );break; diff --git a/src/Layers/xrRender/FTreeVisual.cpp b/src/Layers/xrRender/FTreeVisual.cpp index 4031a21dae1..6189de29117 100644 --- a/src/Layers/xrRender/FTreeVisual.cpp +++ b/src/Layers/xrRender/FTreeVisual.cpp @@ -47,7 +47,9 @@ void FTreeVisual::Load (const char* N, IReader *data, u32 dwFlags) VERIFY (NULL==p_rm_Vertices); p_rm_Vertices = RImplementation.getVB (ID); +#ifndef USE_OGL p_rm_Vertices->AddRef(); +#endif // !USE_OGL // indices dwPrimitives = 0; @@ -58,7 +60,9 @@ void FTreeVisual::Load (const char* N, IReader *data, u32 dwFlags) VERIFY (NULL==p_rm_Indices); p_rm_Indices = RImplementation.getIB (ID); +#ifndef USE_OGL p_rm_Indices->AddRef (); +#endif // !USE_OGL } // load tree-def @@ -147,12 +151,18 @@ void FTreeVisual::Copy (dxRender_Visual *pSrc) PCOPY(rm_geom); - PCOPY(p_rm_Vertices); if (p_rm_Vertices) p_rm_Vertices->AddRef(); + PCOPY(p_rm_Vertices); +#ifndef USE_OGL + if (p_rm_Vertices) p_rm_Vertices->AddRef(); +#endif // !USE_OGL PCOPY(vBase); PCOPY(vCount); - PCOPY(p_rm_Indices); if (p_rm_Indices) p_rm_Indices->AddRef(); + PCOPY(p_rm_Indices); +#ifndef USE_OGL + if (p_rm_Indices) p_rm_Indices->AddRef(); +#endif // !USE_OGL PCOPY(iBase); PCOPY(iCount); diff --git a/src/Layers/xrRender/FVisual.cpp b/src/Layers/xrRender/FVisual.cpp index 80e3d941dc1..ffbd5a79ac5 100644 --- a/src/Layers/xrRender/FVisual.cpp +++ b/src/Layers/xrRender/FVisual.cpp @@ -1,13 +1,10 @@ #include "stdafx.h" #pragma hdrstop -#pragma warning(disable:4995) -#include -#pragma warning(default:4995) - #include "xrCore/FMesh.hpp" #include "FVisual.h" #include "Layers/xrRenderDX10/dx10BufferUtils.h" +#include "Layers/xrRenderGL/glBufferUtils.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction @@ -36,7 +33,7 @@ void Fvisual::Load (const char* N, IReader *data, u32 dwFlags) { dxRender_Visual::Load (N,data,dwFlags); - D3DVERTEXELEMENT9 dcl [MAX_FVF_DECL_SIZE]; + u32 fvf = 0; D3DVERTEXELEMENT9* vFormat = 0; dwPrimitives = 0; BOOL loaded_v=false; @@ -51,7 +48,9 @@ void Fvisual::Load (const char* N, IReader *data, u32 dwFlags) VERIFY (NULL==p_rm_Vertices); p_rm_Vertices = RImplementation.getVB (ID); +#ifndef USE_OGL p_rm_Vertices->AddRef (); +#endif // !USE_OGL vFormat = RImplementation.getVB_Format (ID); loaded_v = true; @@ -63,9 +62,11 @@ void Fvisual::Load (const char* N, IReader *data, u32 dwFlags) VERIFY (NULL==p_rm_Indices); p_rm_Indices = RImplementation.getIB (ID); +#ifndef USE_OGL p_rm_Indices->AddRef(); +#endif // !USE_OGL #endif -#if (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) +#if (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) || (RENDER==R_GL) // check for fast-vertices if (data->find_chunk(OGF_FASTPATH)) { destructor geomdef (data->open_chunk (OGF_FASTPATH)); @@ -82,7 +83,9 @@ void Fvisual::Load (const char* N, IReader *data, u32 dwFlags) VERIFY (NULL==m_fast->p_rm_Vertices); m_fast->p_rm_Vertices = RImplementation.getVB (ID,true); +#ifndef USE_OGL m_fast->p_rm_Vertices->AddRef(); +#endif // !USE_OGL fmt = RImplementation.getVB_Format(ID,true); // indices @@ -93,12 +96,14 @@ void Fvisual::Load (const char* N, IReader *data, u32 dwFlags) VERIFY (NULL==m_fast->p_rm_Indices); m_fast->p_rm_Indices = RImplementation.getIB (ID,true); +#ifndef USE_OGL m_fast->p_rm_Indices->AddRef(); +#endif // !USE_OGL // geom m_fast->rm_geom.create (fmt,m_fast->p_rm_Vertices,m_fast->p_rm_Indices); } -#endif // (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) +#endif // (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) || (RENDER==R_R4) } // read vertices @@ -111,19 +116,26 @@ void Fvisual::Load (const char* N, IReader *data, u32 dwFlags) vCount = data->r_u32 (); VERIFY (NULL==p_rm_Vertices); p_rm_Vertices = RImplementation.getVB (ID); +#ifndef USE_OGL p_rm_Vertices->AddRef(); +#endif // !USE_OGL vFormat = RImplementation.getVB_Format (ID); #endif } else { R_ASSERT (data->find_chunk(OGF_VERTICES)); vBase = 0; - u32 fvf = data->r_u32 (); - CHK_DX (D3DXDeclaratorFromFVF(fvf,dcl)); - vFormat = dcl; + fvf = data->r_u32 (); vCount = data->r_u32 (); +#ifdef USE_OGL + u32 vStride = glBufferUtils::GetFVFVertexSize (fvf); +#else u32 vStride = D3DXGetFVFVertexSize (fvf); +#endif // USE_OGL -#if defined(USE_DX10) || defined(USE_DX11) +#if defined(USE_OGL) + VERIFY(NULL == p_rm_Vertices); + glBufferUtils::CreateVertexBuffer(&p_rm_Vertices, data->pointer(), vCount*vStride); +#elif defined(USE_DX10) || defined(USE_DX11) VERIFY (NULL==p_rm_Vertices); R_CHK (dx10BufferUtils::CreateVertexBuffer(&p_rm_Vertices, data->pointer(), vCount*vStride)); HW.stats_manager.increment_stats_vb (p_rm_Vertices); @@ -153,7 +165,9 @@ void Fvisual::Load (const char* N, IReader *data, u32 dwFlags) dwPrimitives = iCount/3; VERIFY (NULL==p_rm_Indices); p_rm_Indices = RImplementation.getIB (ID); +#ifndef USE_OGL p_rm_Indices->AddRef (); +#endif // !USE_OGL #endif } else { R_ASSERT (data->find_chunk(OGF_INDICES)); @@ -161,7 +175,10 @@ void Fvisual::Load (const char* N, IReader *data, u32 dwFlags) iCount = data->r_u32(); dwPrimitives = iCount/3; -#if defined(USE_DX10) || defined(USE_DX11) +#if defined(USE_OGL) + VERIFY(NULL == p_rm_Indices); + glBufferUtils::CreateIndexBuffer(&p_rm_Indices, data->pointer(), iCount * 2); +#elif defined(USE_DX10) || defined(USE_DX11) //BOOL bSoft = HW.Caps.geometry.bSoftware || (dwFlags&VLOAD_FORCESOFTWARE); //u32 dwUsage = /*D3DUSAGE_WRITEONLY |*/ (bSoft?D3DUSAGE_SOFTWAREPROCESSING:0); // indices are read in model-wallmarks code //BYTE* bytes = 0; @@ -191,13 +208,15 @@ void Fvisual::Load (const char* N, IReader *data, u32 dwFlags) if (dwFlags&VLOAD_NOVERTICES) return; + else if (fvf) + rm_geom.create (fvf,p_rm_Vertices,p_rm_Indices); else rm_geom.create (vFormat,p_rm_Vertices,p_rm_Indices); } void Fvisual::Render (float ) { -#if (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) +#if (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) || (RENDER==R_GL) if (m_fast && RImplementation.phase==CRender::PHASE_SMAP && !RCache.is_TessEnabled()) { RCache.set_Geometry (m_fast->rm_geom); @@ -208,11 +227,11 @@ void Fvisual::Render (float ) RCache.Render (D3DPT_TRIANGLELIST,vBase,0,vCount,iBase,dwPrimitives); RCache.stat.r.s_static.add (vCount); } -#else // (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) +#else // (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) || (RENDER==R_GL) RCache.set_Geometry (rm_geom); RCache.Render (D3DPT_TRIANGLELIST,vBase,0,vCount,iBase,dwPrimitives); RCache.stat.r.s_static.add (vCount); -#endif // (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) +#endif // (RENDER==R_R2) || (RENDER==R_R3) || (RENDER==R_R4) || (RENDER==R_GL) } #define PCOPY(a) a = pFrom->a @@ -224,11 +243,17 @@ void Fvisual::Copy (dxRender_Visual *pSrc) PCOPY (rm_geom); - PCOPY (p_rm_Vertices); if (p_rm_Vertices) p_rm_Vertices->AddRef(); + PCOPY (p_rm_Vertices); +#ifndef USE_OGL + if (p_rm_Vertices) p_rm_Vertices->AddRef(); +#endif // !USE_OGL PCOPY (vBase); PCOPY (vCount); - PCOPY (p_rm_Indices); if (p_rm_Indices) p_rm_Indices->AddRef(); + PCOPY(p_rm_Indices); +#ifndef USE_OGL + if (p_rm_Indices) p_rm_Indices->AddRef(); +#endif // !USE_OGL PCOPY (iBase); PCOPY (iCount); PCOPY (dwPrimitives); diff --git a/src/Layers/xrRender/SkeletonX.h b/src/Layers/xrRender/SkeletonX.h index 1852d645ff1..a3ff6ca4c8c 100644 --- a/src/Layers/xrRender/SkeletonX.h +++ b/src/Layers/xrRender/SkeletonX.h @@ -111,7 +111,7 @@ BOOL pick_bone(T_buffer vertices, CKinematics* Parent, IKinematics::pick_result return FALSE; } -#if defined(USE_DX10) || defined(USE_DX11) +#if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL) template BOOL pick_bone(CKinematics* Parent, IKinematics::pick_result &r, float dist, const Fvector& S, const Fvector& D, Fvisual* V, u16* indices, CBoneData::FacesVec& faces) {