From 6e0338ee6bb61d16052aeb2ee766d80ca50c86a8 Mon Sep 17 00:00:00 2001 From: Dander7BD Date: Mon, 24 Feb 2014 14:35:38 +0100 Subject: [PATCH 1/3] Minor mathematical fix AngularAxis had a bug. SnapAngularAxis is determined broken. Do not use. :( --- Code/Misc/OysterMath/LinearMath.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Code/Misc/OysterMath/LinearMath.h b/Code/Misc/OysterMath/LinearMath.h index 25b65331..5b87d83d 100644 --- a/Code/Misc/OysterMath/LinearMath.h +++ b/Code/Misc/OysterMath/LinearMath.h @@ -376,9 +376,13 @@ namespace LinearAlgebra3D template inline ::LinearAlgebra::Vector3 AngularAxis( const ::LinearAlgebra::Quaternion &rotation ) { // see http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm - ScalarType angle = ScalarType(2) * (ScalarType)::std::acos( rotation.real ), - multiplier = angle / (ScalarType)::std::sqrt( ScalarType(1) - rotation.real * rotation.real ); - return multiplier * rotation.imaginary; + if( rotation.real < ScalarType(1) ) + { + ScalarType angle = ScalarType(2) * ScalarType( ::std::acos(rotation.real) ), + multiplier = angle / ScalarType( ::std::sqrt(ScalarType(1) - rotation.real * rotation.real) ); + return multiplier * rotation.imaginary; + } + else return ::LinearAlgebra::Vector3::null; } // All Matrix to AngularAxis conversions here is incorrect @@ -838,7 +842,7 @@ namespace LinearAlgebra3D template ::LinearAlgebra::Vector3 & SnapAngularAxis( const ::LinearAlgebra::Vector3 &startAngularAxis, const ::LinearAlgebra::Vector3 &localStartNormal, const ::LinearAlgebra::Vector3 &worldEndNormal, ::LinearAlgebra::Vector3 &targetMem = ::LinearAlgebra::Vector3() ) - { + { //! @todo TODO: This code is broken and do not work! ::LinearAlgebra::Vector3 worldStartNormal( WorldAxisOf(Rotation(startAngularAxis), localStartNormal) ); targetMem = worldStartNormal.Cross( worldEndNormal ); targetMem *= (ScalarType)::std::acos( ::Utility::Value::Clamp(worldStartNormal.Dot(worldEndNormal), (ScalarType)0, (ScalarType)1) ); From 37e9d4720761f5be87cd2eb14d300684554fac53 Mon Sep 17 00:00:00 2001 From: lanariel Date: Mon, 24 Feb 2014 14:44:36 +0100 Subject: [PATCH 2/3] Instanced Rendering --- .../GameClientState/NetLoadState.cpp | 2 + .../Definitions/GraphicalDefinition.h | 3 +- Code/OysterGraphics/DllInterfaces/GFXAPI.cpp | 46 +++- Code/OysterGraphics/OysterGraphics.vcxproj | 19 +- .../OysterGraphics.vcxproj.filters | 9 +- .../OysterGraphics/Render/DefaultRenderer.cpp | 67 +++++- Code/OysterGraphics/Render/Resources.cpp | 204 +++++++++++++++--- Code/OysterGraphics/Render/Resources.h | 4 +- .../Gather/{Header.hlsli => AnimHeader.hlsli} | 2 +- ...{GatherPixel.hlsl => GatherAnimPixel.hlsl} | 4 +- ...atherVertex.hlsl => GatherAnimVertex.hlsl} | 4 +- .../Shader/Passes/Gather/GatherInstPixel.hlsl | 63 ++++++ .../Passes/Gather/GatherInstVertex.hlsl | 18 ++ .../Shader/Passes/Gather/InstHeader.hlsli | 44 ++++ 14 files changed, 439 insertions(+), 50 deletions(-) rename Code/OysterGraphics/Shader/Passes/Gather/{Header.hlsli => AnimHeader.hlsli} (97%) rename Code/OysterGraphics/Shader/Passes/Gather/{GatherPixel.hlsl => GatherAnimPixel.hlsl} (95%) rename Code/OysterGraphics/Shader/Passes/Gather/{GatherVertex.hlsl => GatherAnimVertex.hlsl} (90%) create mode 100644 Code/OysterGraphics/Shader/Passes/Gather/GatherInstPixel.hlsl create mode 100644 Code/OysterGraphics/Shader/Passes/Gather/GatherInstVertex.hlsl create mode 100644 Code/OysterGraphics/Shader/Passes/Gather/InstHeader.hlsli diff --git a/Code/Game/GameClient/GameClientState/NetLoadState.cpp b/Code/Game/GameClient/GameClientState/NetLoadState.cpp index 9ef0dac9..b9256fd1 100644 --- a/Code/Game/GameClient/GameClientState/NetLoadState.cpp +++ b/Code/Game/GameClient/GameClientState/NetLoadState.cpp @@ -242,5 +242,7 @@ void NetLoadState::LoadGame( const ::std::string &fileName ) } } + Graphics::API::EndLoadingModels(); + this->privData->nextState = ClientState::ClientState_Game; } diff --git a/Code/OysterGraphics/Definitions/GraphicalDefinition.h b/Code/OysterGraphics/Definitions/GraphicalDefinition.h index 9d7d4301..77aecdc8 100644 --- a/Code/OysterGraphics/Definitions/GraphicalDefinition.h +++ b/Code/OysterGraphics/Definitions/GraphicalDefinition.h @@ -95,7 +95,8 @@ namespace Oyster { Math::Matrix WV; Math::Matrix WVP; - TintData td; + Math::Float3 Tint; + Math::Float3 GTint; }; } } diff --git a/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp b/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp index 4e61c7f3..d4c48957 100644 --- a/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp +++ b/Code/OysterGraphics/DllInterfaces/GFXAPI.cpp @@ -141,7 +141,26 @@ namespace Oyster void API::EndLoadingModels() { //TODO finalize instance buffers and create rendering map; + int maxModels = 0; + for(auto i = Render::Resources::RenderData.begin(); i != Render::Resources::RenderData.end(); i++ ) + { + if((*i).second->Models > maxModels) + { + maxModels = (*i).second->Models; + } + (*i).second->rid = new Definitions::RenderInstanceData[(*i).second->Models+1]; + } + + Core::Buffer::BUFFER_INIT_DESC desc; + + desc.ElementSize = sizeof(Definitions::RenderInstanceData); + desc.Type = Core::Buffer::VERTEX_BUFFER; + desc.Usage = Core::Buffer::BUFFER_CPU_WRITE_DISCARD; + desc.InitData = 0; + desc.NumElements = maxModels; + + Render::Resources::Gather::InstancedData.Init(desc); } //returns null for invalid filenames @@ -163,14 +182,17 @@ namespace Oyster delete mi; return NULL; } - - if(Core::loader.GetResourceCount(m->info) == 1) + + if(!m->info->Animated) { - Render::Resources::RenderData[m->info] = new Render::Resources::ModelDataWrapper(); - } - else - { - Render::Resources::RenderData[m->info]->Models++; + if(Core::loader.GetResourceCount(m->info) == 1) + { + Render::Resources::RenderData[m->info] = new Render::Resources::ModelDataWrapper(); + } + else + { + Render::Resources::RenderData[m->info]->Models++; + } } return m; @@ -207,6 +229,12 @@ namespace Oyster SAFE_RELEASE(Core::deviceContext); SAFE_RELEASE(Core::device); + for(auto i = Render::Resources::RenderData.begin(); i != Render::Resources::RenderData.end(); i++ ) + { + SAFE_DELETE((*i).second->rid); + SAFE_DELETE((*i).second); + } + } void API::AddLight(Definitions::Pointlight light) @@ -228,9 +256,9 @@ namespace Oyster void API::StartRenderWireFrame() { - Core::deviceContext->OMSetRenderTargets((UINT)Render::Resources::Gather::Pass.RTV.size(),&Render::Resources::Gather::Pass.RTV[0],NULL); + Core::deviceContext->OMSetRenderTargets((UINT)Render::Resources::Gather::AnimatedPass.RTV.size(),&Render::Resources::Gather::AnimatedPass.RTV[0],NULL); Core::deviceContext->RSSetState(wire); - Core::deviceContext->OMSetRenderTargets((UINT)Render::Resources::Gather::Pass.RTV.size(),&Render::Resources::Gather::Pass.RTV[0],NULL); + Core::deviceContext->OMSetRenderTargets((UINT)Render::Resources::Gather::AnimatedPass.RTV.size(),&Render::Resources::Gather::AnimatedPass.RTV[0],NULL); } void API::RenderDebugCube(Math::Matrix world) diff --git a/Code/OysterGraphics/OysterGraphics.vcxproj b/Code/OysterGraphics/OysterGraphics.vcxproj index 8faf4d0f..f4a3d130 100644 --- a/Code/OysterGraphics/OysterGraphics.vcxproj +++ b/Code/OysterGraphics/OysterGraphics.vcxproj @@ -239,13 +239,25 @@ Vertex Vertex + + Pixel + Pixel + Pixel + Pixel + + + Vertex + Vertex + Vertex + Vertex + Compute Compute Compute Compute - + Pixel Pixel Pixel @@ -281,7 +293,7 @@ Compute 5.0 - + Vertex Vertex Vertex @@ -328,8 +340,9 @@ + - + diff --git a/Code/OysterGraphics/OysterGraphics.vcxproj.filters b/Code/OysterGraphics/OysterGraphics.vcxproj.filters index d76a066f..83731090 100644 --- a/Code/OysterGraphics/OysterGraphics.vcxproj.filters +++ b/Code/OysterGraphics/OysterGraphics.vcxproj.filters @@ -100,8 +100,6 @@ - - @@ -109,6 +107,10 @@ + + + + @@ -117,8 +119,9 @@ - + + \ No newline at end of file diff --git a/Code/OysterGraphics/Render/DefaultRenderer.cpp b/Code/OysterGraphics/Render/DefaultRenderer.cpp index 7d0d987b..a735f4b3 100644 --- a/Code/OysterGraphics/Render/DefaultRenderer.cpp +++ b/Code/OysterGraphics/Render/DefaultRenderer.cpp @@ -19,7 +19,6 @@ namespace Oyster Preparations::Basic::ClearBackBuffer(Oyster::Math::Float4(0,0,0,0)); Preparations::Basic::ClearDepthStencil(Resources::Gui::depth); Preparations::Basic::ClearRTV(Resources::GBufferRTV,Resources::GBufferSize,Math::Float4(0,0,0,0)); - Core::PipelineManager::SetRenderPass(Graphics::Render::Resources::Gather::Pass); Lights[1]; void* data; @@ -39,15 +38,37 @@ namespace Oyster data = Resources::Light::PointLightsData.Map(); memcpy(data, Lights, sizeof(Definitions::Pointlight) * numLights); Resources::Light::PointLightsData.Unmap(); + + for(auto i = Render::Resources::RenderData.begin(); i != Render::Resources::RenderData.end(); i++ ) + { + (*i).second->Models=0; + } } void DefaultRenderer::RenderScene(Model::Model* models, int count, Math::Matrix View, Math::Matrix Projection, float deltaTime) { for(int i = 0; i < count; ++i) { - if(&models[i] == NULL) + if(&models[i] == NULL || !models[i].Visible) continue; - if(models[i].Visible) + + Model::ModelInfo* info = models[i].info; + if(!info->Animated) + { + Definitions::RenderInstanceData rid; + Math::Float3x3 normalTransform; + normalTransform = Math::Float3x3(models[i].WorldMatrix.v[0].xyz, models[i].WorldMatrix.v[1].xyz, models[i].WorldMatrix.v[2].xyz); + normalTransform.Transpose().Invert(); + Math::Matrix m = Math::Matrix(Math::Vector4(normalTransform.v[0],0.0f), Math::Vector4(normalTransform.v[1],0.0f), Math::Vector4(normalTransform.v[2],0.0f), Math::Vector4(0.0f)); + rid.WV = View * m; + rid.WVP = Projection * View * models[i].WorldMatrix; + + rid.Tint = models[i].Tint; + rid.GTint = models[i].GlowTint; + + Resources::RenderData[info]->rid[Resources::RenderData[info]->Models++] = rid; + } + else { Definitions::PerModel pm; Math::Float3x3 normalTransform; @@ -55,7 +76,6 @@ namespace Oyster normalTransform.Transpose().Invert(); Math::Matrix m = Math::Matrix(Math::Vector4(normalTransform.v[0],0.0f), Math::Vector4(normalTransform.v[1],0.0f), Math::Vector4(normalTransform.v[2],0.0f), Math::Vector4(0.0f)); pm.WV = View * m; - //pm.WV = models[i].WorldMatrix.GetTranspose().GetInverse(); pm.WVP = Projection * View * models[i].WorldMatrix; Model::ModelInfo* info = models[i].info; @@ -207,9 +227,48 @@ namespace Oyster Core::deviceContext->Dispatch((UINT)(Core::resolution.x/2), (UINT)((Core::resolution.y/2 + 127U) / 128U), 1); } + void RenderModel(Model::ModelInfo* info, Definitions::RenderInstanceData* rid , int count) + { + if(info->Material.size()) + { + Core::deviceContext->PSSetShaderResources(0,(UINT)info->Material.size(),&(info->Material[0])); + } + info->Vertices->Apply(); + if(info->Indexed) + { + info->Indecies->Apply(); + } + + void* data = Resources::Gather::InstancedData.Map(); + memcpy(data, rid, sizeof(Definitions::RenderInstanceData)*count); + Resources::Gather::InstancedData.Unmap(); + + if(info->Indexed) + { + Core::deviceContext->DrawIndexedInstanced(info->IndexCount,count,0,0,0); + //Core::deviceContext->DrawIndexed(info->IndexCount,0,0); + } + else + { + Core::deviceContext->DrawInstanced(info->VertexCount,count,0,0); + //Core::deviceContext->Draw(info->VertexCount,0); + } + } + void DefaultRenderer::EndFrame() { + Core::PipelineManager::SetRenderPass(Graphics::Render::Resources::Gather::InstancedPass); + Resources::Gather::InstancedData.Apply(1); + + for(auto i = Render::Resources::RenderData.begin(); i != Render::Resources::RenderData.end(); i++ ) + { + for(int m = 0; m <(*i).second->Models; ++m) + { + RenderModel((*i).first,(*i).second->rid, (*i).second->Models); + } + } + Core::PipelineManager::SetRenderPass(Resources::Light::Pass); Core::deviceContext->Dispatch((UINT)((Core::resolution.x + 15U) / 16U), (UINT)((Core::resolution.y + 15U) / 16U), 1); diff --git a/Code/OysterGraphics/Render/Resources.cpp b/Code/OysterGraphics/Render/Resources.cpp index 4c203cf9..91317114 100644 --- a/Code/OysterGraphics/Render/Resources.cpp +++ b/Code/OysterGraphics/Render/Resources.cpp @@ -33,7 +33,8 @@ namespace Oyster ID3D11UnorderedAccessView* Resources::Blur::BufferUAV = {0}; ID3D11ShaderResourceView* Resources::Blur::BufferSRV = {0}; - Shader::RenderPass Resources::Gather::Pass; + Shader::RenderPass Resources::Gather::AnimatedPass; + Shader::RenderPass Resources::Gather::InstancedPass; Shader::RenderPass Resources::Light::Pass; Shader::RenderPass Resources::Post::Pass; Shader::RenderPass Resources::Gui::Pass; @@ -43,6 +44,7 @@ namespace Oyster Buffer Resources::Gather::ModelData = Buffer(); Buffer Resources::Gather::AnimationData = Buffer(); + Buffer Resources::Gather::InstancedData = Buffer(); Buffer Resources::Light::LightConstantsData = Buffer(); Buffer Resources::Gui::Data = Buffer(); Buffer Resources::Color = Buffer(); @@ -78,8 +80,12 @@ namespace Oyster std::wstring end = L".cso"; #endif //Load Shaders - Core::PipelineManager::Init(path + L"GatherPixel" + end, ShaderType::Pixel, L"Gather"); - Core::PipelineManager::Init(path + L"GatherVertex" + end, ShaderType::Vertex, L"Gather"); + Core::PipelineManager::Init(path + L"GatherAnimPixel" + end, ShaderType::Pixel, L"AGather"); + Core::PipelineManager::Init(path + L"GatherAnimVertex" + end, ShaderType::Vertex, L"AGather"); + + + Core::PipelineManager::Init(path + L"GatherInstPixel" + end, ShaderType::Pixel, L"IGather"); + Core::PipelineManager::Init(path + L"GatherInstVertex" + end, ShaderType::Vertex, L"IGather"); #ifdef _DEBUG path = PathToHLSL+L"Light\\"; #endif @@ -153,6 +159,7 @@ namespace Oyster desc.NumElements = MAX_LETTER_COUNT; Gui::Text::Vertex.Init(desc); + return Core::Init::Success; } @@ -358,10 +365,11 @@ namespace Oyster { ////---------------- Geometry Pass Setup ---------------------------- - Gather::Pass.Shaders.Pixel = GetShader::Pixel(L"Gather"); - Gather::Pass.Shaders.Vertex = GetShader::Vertex(L"Gather"); +#pragma region Animated Pass + Gather::AnimatedPass.Shaders.Pixel = GetShader::Pixel(L"Gather"); + Gather::AnimatedPass.Shaders.Vertex = GetShader::Vertex(L"Gather"); - D3D11_INPUT_ELEMENT_DESC indesc[] = + D3D11_INPUT_ELEMENT_DESC AnimInDesc[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }, @@ -370,20 +378,166 @@ namespace Oyster { "BONEWEIGHT", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 48, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; - Shader::CreateInputLayout(indesc,5,GetShader::Vertex(L"Gather"),Gather::Pass.IAStage.Layout); - Gather::Pass.IAStage.Topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; - Gather::Pass.CBuffers.Vertex.push_back(Gather::AnimationData); - Gather::Pass.CBuffers.Vertex.push_back(Gather::ModelData); - Gather::Pass.CBuffers.Pixel.push_back(Color); - Gather::Pass.RenderStates.Rasterizer = RenderStates::rs; - Gather::Pass.RenderStates.SampleCount = 1; - Gather::Pass.RenderStates.SampleState = RenderStates::ss; - Gather::Pass.RenderStates.DepthStencil = RenderStates::dsState; + Shader::CreateInputLayout(AnimInDesc,5,GetShader::Vertex(L"Gather"),Gather::AnimatedPass.IAStage.Layout); + Gather::AnimatedPass.IAStage.Topology = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; + Gather::AnimatedPass.CBuffers.Vertex.push_back(Gather::AnimationData); + Gather::AnimatedPass.CBuffers.Vertex.push_back(Gather::ModelData); + Gather::AnimatedPass.CBuffers.Pixel.push_back(Color); + Gather::AnimatedPass.RenderStates.Rasterizer = RenderStates::rs; + Gather::AnimatedPass.RenderStates.SampleCount = 1; + Gather::AnimatedPass.RenderStates.SampleState = RenderStates::ss; + Gather::AnimatedPass.RenderStates.DepthStencil = RenderStates::dsState; for(int i = 0; i Date: Mon, 24 Feb 2014 14:59:01 +0100 Subject: [PATCH 3/3] Player action protocol added --- Code/Game/GameProtocols/ObjectProtocols.h | 40 +++++++++++++++++++ .../GameProtocols/ProtocolIdentificationID.h | 1 + 2 files changed, 41 insertions(+) diff --git a/Code/Game/GameProtocols/ObjectProtocols.h b/Code/Game/GameProtocols/ObjectProtocols.h index 47c2dddc..234a5301 100644 --- a/Code/Game/GameProtocols/ObjectProtocols.h +++ b/Code/Game/GameProtocols/ObjectProtocols.h @@ -937,5 +937,45 @@ namespace GameLogic Oyster::Network::CustomNetProtocol protocol; }; } +//#define protocol_Gameplay_ObjectAction 368 + struct Protocol_ObjectAction :public Oyster::Network::CustomProtocolObject + { + short objectID; + float animationID; + + Protocol_ObjectAction() + { + this->protocol[0].value = protocol_Gameplay_ObjectAction; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + this->protocol[2].type = Oyster::Network::NetAttributeType_Float; + + objectID = 0; + animationID = -1; + } + Protocol_ObjectAction(Oyster::Network::CustomNetProtocol& p) + { + objectID = p[1].value.netShort; + animationID = p[2].value.netFloat; + } + Protocol_ObjectAction(float animID, int id) + { + this->protocol[0].value = protocol_Gameplay_ObjectAction; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + this->protocol[2].type = Oyster::Network::NetAttributeType_Float; + objectID = id; + animationID = animID; + } + Oyster::Network::CustomNetProtocol GetProtocol() override + { + this->protocol[1].value = objectID; + this->protocol[2].value = animationID; + return protocol; + } + + private: + Oyster::Network::CustomNetProtocol protocol; + }; #endif // !GAMELOGIC_PLAYER_PROTOCOLS_H \ No newline at end of file diff --git a/Code/Game/GameProtocols/ProtocolIdentificationID.h b/Code/Game/GameProtocols/ProtocolIdentificationID.h index 79235e2f..7aaf2eee 100644 --- a/Code/Game/GameProtocols/ProtocolIdentificationID.h +++ b/Code/Game/GameProtocols/ProtocolIdentificationID.h @@ -70,6 +70,7 @@ #define protocol_Gameplay_ObjectRespawn 365 #define protocol_Gameplay_ObjectDie 366 #define protocol_Gameplay_ObjectDisconnectPlayer 367 +#define protocol_Gameplay_ObjectAction 368 #define protocol_GameplayMAX 399