diff --git a/ScriptingAPI-Documentation.md b/ScriptingAPI-Documentation.md index 108977ea1..b52aed107 100644 --- a/ScriptingAPI-Documentation.md +++ b/ScriptingAPI-Documentation.md @@ -112,7 +112,7 @@ getprops(YourObject) on them (where YourObject is the object which is to be insp - Tags: - [constructor] : The method is a constructor for the same type of object. - [void-constructor] : The method is a constructor for the same type of object, but the object it creates is empty, so cannot be used. - - [outer] : The method is in the global scope, so not a method of any objects. + - [outer] : The method or value is in the global scope, so not a method of any objects. ### Font Gives you the ability to render text with a custom font. @@ -123,12 +123,12 @@ Gives you the ability to render text with a custom font. - SetPos(Vector pos) - SetSpacing(Vector spacing) - SetAlign(WIFALIGN Halign, opt WIFALIGN Valign) - - WIFALIGN_LEFT - - WIFALIGN_CENTER - - WIFALIGN_MID - - WIFALIGN_RIGHT - - WIFALIGN_TOP - - WIFALIGN_BOTTOM + - [outer]WIFALIGN_LEFT : int + - [outer]WIFALIGN_CENTER : int + - [outer]WIFALIGN_MID : int + - [outer]WIFALIGN_RIGHT : int + - [outer]WIFALIGN_TOP : int + - [outer]WIFALIGN_BOTTOM : int - GetText() : string result - GetSize() : float result - GetPos() : Vector result diff --git a/WickedEngine/CommonInclude.h b/WickedEngine/CommonInclude.h index 7870d7f2a..08b13e3fb 100644 --- a/WickedEngine/CommonInclude.h +++ b/WickedEngine/CommonInclude.h @@ -42,6 +42,8 @@ using namespace std; #define SAFE_RELEASE(a) if(a!=nullptr){a->Release();a=nullptr;} #define SAFE_DELETE(a) if(a!=nullptr){delete a;a=nullptr;} #define SAFE_DELETE_ARRAY(a) if(a!=nullptr){delete[]a;a=nullptr;} +#define GFX_STRUCT struct alignas(16) +#define GFX_CLASS class alignas(16) #endif //WICKEDENGINE_COMMONINCLUDE_H diff --git a/WickedEngine/Renderable3DComponent.cpp b/WickedEngine/Renderable3DComponent.cpp index c1498f0f6..79f13d9cd 100644 --- a/WickedEngine/Renderable3DComponent.cpp +++ b/WickedEngine/Renderable3DComponent.cpp @@ -212,7 +212,7 @@ void Renderable3DComponent::RenderReflections(wiRenderer::DeviceContext context) wiRenderer::UpdatePerViewCB(context, wiRenderer::getRefCamera(), wiRenderer::getCamera(), water); wiRenderer::DrawWorld(wiRenderer::getRefCamera(), false, 0, context , false, wiRenderer::SHADED_NONE - , nullptr, false, GRAPHICSTHREAD_REFLECTIONS); + , nullptr, true, GRAPHICSTHREAD_REFLECTIONS); wiRenderer::DrawSky(context); } } diff --git a/WickedEngine/wiEmittedParticle.cpp b/WickedEngine/wiEmittedParticle.cpp index c257ab532..cb2ea0ecb 100644 --- a/WickedEngine/wiEmittedParticle.cpp +++ b/WickedEngine/wiEmittedParticle.cpp @@ -253,17 +253,17 @@ void wiEmittedParticle::Draw(Camera* camera, ID3D11DeviceContext *context, ID3D1 wiRenderer::BindTexturePS(depth,1,context); - ConstantBuffer cb; - cb.mView = XMMatrixTranspose(camera->GetView()); - cb.mProjection = XMMatrixTranspose( camera->GetProjection() ); - cb.mCamPos = camera->GetEye(); - cb.mAdd.x = additive; - cb.mAdd.y = (FLAG==DRAW_DARK?true:false); - cb.mMotionBlurAmount = motionBlurAmount; + static thread_local ConstantBuffer* cb = new ConstantBuffer; + (*cb).mView = XMMatrixTranspose(camera->GetView()); + (*cb).mProjection = XMMatrixTranspose( camera->GetProjection() ); + (*cb).mCamPos = camera->GetEye(); + (*cb).mAdd.x = additive; + (*cb).mAdd.y = (FLAG==DRAW_DARK?true:false); + (*cb).mMotionBlurAmount = motionBlurAmount; //context->UpdateSubresource( constantBuffer, 0, NULL, &cb, 0, 0 ); - wiRenderer::UpdateBuffer(constantBuffer,&cb,context); + wiRenderer::UpdateBuffer(constantBuffer,cb,context); //D3D11_MAPPED_SUBRESOURCE mappedResource; //ConstantBuffer* dataPtr; //context->Map(constantBuffer,0,D3D11_MAP_WRITE_DISCARD,0,&mappedResource); diff --git a/WickedEngine/wiEmittedParticle.h b/WickedEngine/wiEmittedParticle.h index e061b49b1..d60cc5528 100644 --- a/WickedEngine/wiEmittedParticle.h +++ b/WickedEngine/wiEmittedParticle.h @@ -36,12 +36,10 @@ private: sizBeginEnd[0] = sizOpaMir.x; sizBeginEnd[1] = sizOpaMir.x*scaleX; } - - ALIGN_16 }; deque points; - struct ConstantBuffer + GFX_STRUCT ConstantBuffer { XMMATRIX mView; XMMATRIX mProjection; diff --git a/WickedEngine/wiFont.cpp b/WickedEngine/wiFont.cpp index 1f98e815e..a17328ffb 100644 --- a/WickedEngine/wiFont.cpp +++ b/WickedEngine/wiFont.cpp @@ -330,12 +330,12 @@ void wiFont::Draw(wiRenderer::DeviceContext context){ wiRenderer::BindPS(pixelShader,context); - ConstantBuffer cb = ConstantBuffer(); - cb.mProjection = XMMatrixTranspose( XMMatrixOrthographicLH((float)wiRenderer::GetScreenWidth(),(float)wiRenderer::GetScreenHeight(),0,100) ); - cb.mTrans = XMMatrixTranspose(XMMatrixTranslation(newProps.posX, newProps.posY, 0)); - cb.mDimensions = XMFLOAT4((float)wiRenderer::RENDERWIDTH, (float)wiRenderer::RENDERHEIGHT, 0, 0); + static thread_local ConstantBuffer* cb = new ConstantBuffer; + (*cb).mProjection = XMMatrixTranspose( XMMatrixOrthographicLH((float)wiRenderer::GetScreenWidth(),(float)wiRenderer::GetScreenHeight(),0,100) ); + (*cb).mTrans = XMMatrixTranspose(XMMatrixTranslation(newProps.posX, newProps.posY, 0)); + (*cb).mDimensions = XMFLOAT4((float)wiRenderer::RENDERWIDTH, (float)wiRenderer::RENDERHEIGHT, 0, 0); - wiRenderer::UpdateBuffer(constantBuffer,&cb,context); + wiRenderer::UpdateBuffer(constantBuffer,cb,context); wiRenderer::BindConstantBufferVS(constantBuffer,0,context); diff --git a/WickedEngine/wiFont.h b/WickedEngine/wiFont.h index b3f14d3ae..90d0bc1bb 100644 --- a/WickedEngine/wiFont.h +++ b/WickedEngine/wiFont.h @@ -34,13 +34,15 @@ public: class wiFont { protected: - struct Vertex + GFX_STRUCT Vertex { XMFLOAT2 Pos; XMFLOAT2 Tex; + + ALIGN_16 }; - static std::vector vertexList; - struct ConstantBuffer + static vector vertexList; + GFX_STRUCT ConstantBuffer { XMMATRIX mProjection; XMMATRIX mTrans; diff --git a/WickedEngine/wiFont_BindLua.cpp b/WickedEngine/wiFont_BindLua.cpp index 31986d5b7..4e4e8cba2 100644 --- a/WickedEngine/wiFont_BindLua.cpp +++ b/WickedEngine/wiFont_BindLua.cpp @@ -69,6 +69,8 @@ int wiFont_BindLua::SetText(lua_State* L) int argc = wiLua::SGetArgCount(L); if (argc > 0) font->SetText(wiLua::SGetString(L, 1)); + else + font->SetText(""); return 0; } int wiFont_BindLua::SetSize(lua_State* L) diff --git a/WickedEngine/wiHairParticle.cpp b/WickedEngine/wiHairParticle.cpp index fead2c43a..d0e6170a7 100644 --- a/WickedEngine/wiHairParticle.cpp +++ b/WickedEngine/wiHairParticle.cpp @@ -312,7 +312,7 @@ void wiHairParticle::SetUpPatches() for (unsigned int i = 0; iindices.size() - 3; i += 3){ - int vi[]={mesh->indices[i],mesh->indices[i+1],mesh->indices[i+2]}; + unsigned int vi[]={mesh->indices[i],mesh->indices[i+1],mesh->indices[i+2]}; float denMod[]={1,1,1},lenMod[]={1,1,1}; if(dVG>=0){ auto found = mesh->vertexGroups[dVG].vertices.find(vi[0]); @@ -453,7 +453,7 @@ void wiHairParticle::SetUpPatches() void wiHairParticle::Draw(Camera* camera, ID3D11DeviceContext *context) { - Frustum frustum = Frustum(); + static thread_local Frustum frustum = Frustum(); frustum.ConstructFrustum((float)LOD[2], camera->Projection, camera->View); @@ -481,18 +481,18 @@ void wiHairParticle::Draw(Camera* camera, ID3D11DeviceContext *context) wiRenderer::BindRasterizerState(ncrs,context); - CBGS gcb; - gcb.mView = XMMatrixTranspose(camera->GetView()); - gcb.mProj = XMMatrixTranspose(camera->GetProjection()); - gcb.mPrevViewProjection = XMMatrixTranspose(wiRenderer::prevFrameCam->GetViewProjection()); - gcb.colTime=XMFLOAT4(material->diffuseColor.x,material->diffuseColor.y,material->diffuseColor.z,wiRenderer::wind.time); - gcb.eye=camera->translation; - gcb.drawdistance = (float)LOD[2]; - gcb.wind=wiRenderer::wind.direction; - gcb.windRandomness=wiRenderer::wind.randomness; - gcb.windWaveSize=wiRenderer::wind.waveSize; + static thread_local CBGS* gcb = new CBGS; + (*gcb).mView = XMMatrixTranspose(camera->GetView()); + (*gcb).mProj = XMMatrixTranspose(camera->GetProjection()); + (*gcb).mPrevViewProjection = XMMatrixTranspose(wiRenderer::prevFrameCam->GetViewProjection()); + (*gcb).colTime=XMFLOAT4(material->diffuseColor.x,material->diffuseColor.y,material->diffuseColor.z,wiRenderer::wind.time); + (*gcb).eye=camera->translation; + (*gcb).drawdistance = (float)LOD[2]; + (*gcb).wind=wiRenderer::wind.direction; + (*gcb).windRandomness=wiRenderer::wind.randomness; + (*gcb).windWaveSize=wiRenderer::wind.waveSize; - wiRenderer::UpdateBuffer(cbgs,&gcb,context); + wiRenderer::UpdateBuffer(cbgs,gcb,context); wiRenderer::BindDepthStencilState(dss,STENCILREF_DEFAULT,context); wiRenderer::BindConstantBufferGS(cbgs,0,context); diff --git a/WickedEngine/wiHairParticle.h b/WickedEngine/wiHairParticle.h index b40027d40..541d77ebc 100644 --- a/WickedEngine/wiHairParticle.h +++ b/WickedEngine/wiHairParticle.h @@ -9,11 +9,13 @@ class wiHairParticle : public wiParticle { public: - struct Point + GFX_STRUCT Point { XMFLOAT4 posRand; XMFLOAT4 normalLen; XMFLOAT4 tangent; + + ALIGN_16 }; struct Patch { @@ -29,7 +31,7 @@ private: int count; string name,densityG,lenG; Material* material; - struct CBGS + GFX_STRUCT CBGS { XMMATRIX mView; XMMATRIX mProj; diff --git a/WickedEngine/wiImage.cpp b/WickedEngine/wiImage.cpp index 9aac80e4d..17b33d122 100644 --- a/WickedEngine/wiImage.cpp +++ b/WickedEngine/wiImage.cpp @@ -316,15 +316,15 @@ void wiImage::Draw(wiRenderer::TextureView texture, const wiImageEffects& effect if(!effects.blur){ if(!effects.process.active && !effects.bloom.separate && !effects.sunPos.x && !effects.sunPos.y){ - ConstantBuffer cb; + static thread_local ConstantBuffer* cb = new ConstantBuffer; if(effects.typeFlag==SCREEN){ - cb.mViewProjection = XMMatrixTranspose( wiRenderer::getCamera()->GetOProjection() ); - cb.mTrans = XMMatrixTranspose(XMMatrixTranslation(wiRenderer::RENDERWIDTH / 2 - effects.siz.x / 2, -wiRenderer::RENDERHEIGHT / 2 + effects.siz.y / 2, 0) * XMMatrixRotationZ(effects.rotation) + (*cb).mViewProjection = XMMatrixTranspose( wiRenderer::getCamera()->GetOProjection() ); + (*cb).mTrans = XMMatrixTranspose(XMMatrixTranslation(wiRenderer::RENDERWIDTH / 2 - effects.siz.x / 2, -wiRenderer::RENDERHEIGHT / 2 + effects.siz.y / 2, 0) * XMMatrixRotationZ(effects.rotation) * XMMatrixTranslation(-wiRenderer::RENDERWIDTH / 2 + effects.pos.x + effects.siz.x*0.5f, wiRenderer::RENDERHEIGHT / 2 + effects.pos.y - effects.siz.y*0.5f, 0)); //AUTO ORIGIN CORRECTION APPLIED! NO FURTHER TRANSLATIONS NEEDED! - cb.mDimensions = XMFLOAT4((float)wiRenderer::RENDERWIDTH, (float)wiRenderer::RENDERHEIGHT, effects.siz.x, effects.siz.y); + (*cb).mDimensions = XMFLOAT4((float)wiRenderer::RENDERWIDTH, (float)wiRenderer::RENDERHEIGHT, effects.siz.x, effects.siz.y); } else if(effects.typeFlag==WORLD){ - cb.mViewProjection = XMMatrixTranspose( wiRenderer::getCamera()->GetView() * wiRenderer::getCamera()->GetProjection() ); + (*cb).mViewProjection = XMMatrixTranspose( wiRenderer::getCamera()->GetView() * wiRenderer::getCamera()->GetProjection() ); XMMATRIX faceRot = XMMatrixIdentity(); if(effects.lookAt.w){ XMVECTOR vvv = (effects.lookAt.x==1 && !effects.lookAt.y && !effects.lookAt.z)?XMVectorSet(0,1,0,0):XMVectorSet(1,0,0,0); @@ -339,22 +339,22 @@ void wiImage::Draw(wiRenderer::TextureView texture, const wiImageEffects& effect } else faceRot=XMMatrixRotationQuaternion(XMLoadFloat4(&wiRenderer::getCamera()->rotation)); - cb.mTrans = XMMatrixTranspose( + (*cb).mTrans = XMMatrixTranspose( XMMatrixScaling(effects.scale.x,effects.scale.y,1) *XMMatrixRotationZ(effects.rotation) *faceRot //*XMMatrixInverse(0,XMMatrixLookAtLH(XMVectorSet(0,0,0,0),XMLoadFloat3(&effects.pos)-wiRenderer::getCamera()->Eye,XMVectorSet(0,1,0,0))) *XMMatrixTranslation(effects.pos.x,effects.pos.y,effects.pos.z) ); - cb.mDimensions = XMFLOAT4(0,0,effects.siz.x,effects.siz.y); + (*cb).mDimensions = XMFLOAT4(0,0,effects.siz.x,effects.siz.y); } - cb.mOffsetMirFade = XMFLOAT4(effects.offset.x,effects.offset.y,effects.mirror,effects.fade); - cb.mDrawRec = effects.drawRec; - cb.mBlurOpaPiv = XMFLOAT4(effects.blurDir, effects.blur, effects.opacity, (float)effects.pivotFlag); - cb.mTexOffset = XMFLOAT4(effects.texOffset.x, effects.texOffset.y, effects.mipLevel, 0); + (*cb).mOffsetMirFade = XMFLOAT4(effects.offset.x,effects.offset.y,effects.mirror,effects.fade); + (*cb).mDrawRec = effects.drawRec; + (*cb).mBlurOpaPiv = XMFLOAT4(effects.blurDir, effects.blur, effects.opacity, (float)effects.pivotFlag); + (*cb).mTexOffset = XMFLOAT4(effects.texOffset.x, effects.texOffset.y, effects.mipLevel, 0); - wiRenderer::UpdateBuffer(constantBuffer,&cb,context); + wiRenderer::UpdateBuffer(constantBuffer,cb,context); //context->UpdateSubresource( constantBuffer, 0, NULL, &cb, 0, 0 ); @@ -407,11 +407,11 @@ void wiImage::Draw(wiRenderer::TextureView texture, const wiImageEffects& effect wiRenderer::BindPS(bloomSeparatePS,context); else wiHelper::messageBox("Postprocess branch not implemented!"); - ProcessBuffer prcb; - prcb.mPostProcess=XMFLOAT4(effects.process.motionBlur,effects.process.outline,effects.process.dofStrength,effects.process.ssss.x); - prcb.mBloom=XMFLOAT4(effects.bloom.separate,effects.bloom.threshold,effects.bloom.saturation,effects.process.ssss.y); + static thread_local ProcessBuffer* prcb = new ProcessBuffer; + (*prcb).mPostProcess=XMFLOAT4(effects.process.motionBlur,effects.process.outline,effects.process.dofStrength,effects.process.ssss.x); + (*prcb).mBloom=XMFLOAT4(effects.bloom.separate,effects.bloom.threshold,effects.bloom.saturation,effects.process.ssss.y); - wiRenderer::UpdateBuffer(processCb,&prcb,context); + wiRenderer::UpdateBuffer(processCb,prcb,context); //D3D11_MAPPED_SUBRESOURCE mappedResource; //ProcessBuffer* dataPtr2; //context->Map(processCb,0,D3D11_MAP_WRITE_DISCARD,0,&mappedResource); @@ -429,11 +429,11 @@ void wiImage::Draw(wiRenderer::TextureView texture, const wiImageEffects& effect wiRenderer::BindVS(screenVS,context); wiRenderer::BindPS(shaftPS,context); - LightShaftBuffer scb; - scb.mProperties=XMFLOAT4(0.65f,0.25f,0.945f,0.2f); //Density|Weight|Decay|Exposure - scb.mLightShaft=effects.sunPos; + static thread_local LightShaftBuffer* scb = new LightShaftBuffer; + (*scb).mProperties=XMFLOAT4(0.65f,0.25f,0.945f,0.2f); //Density|Weight|Decay|Exposure + (*scb).mLightShaft=effects.sunPos; - wiRenderer::UpdateBuffer(shaftCb,&scb,context); + wiRenderer::UpdateBuffer(shaftCb,scb,context); //D3D11_MAPPED_SUBRESOURCE mappedResource; //LightShaftBuffer* dataPtr2; //context->Map(shaftCb,0,D3D11_MAP_WRITE_DISCARD,0,&mappedResource); @@ -447,16 +447,16 @@ void wiImage::Draw(wiRenderer::TextureView texture, const wiImageEffects& effect //D3D11_MAPPED_SUBRESOURCE mappedResource; - PSConstantBuffer pscb; + static thread_local PSConstantBuffer* pscb = new PSConstantBuffer; int normalmapmode=0; if(effects.normalMap && effects.refractionMap) normalmapmode=1; if(effects.extractNormalMap==true) normalmapmode=2; - pscb.mMaskFadOpaDis = XMFLOAT4((effects.maskMap ? 1.f : 0.f), effects.fade, effects.opacity, (float)normalmapmode); - pscb.mDimension = XMFLOAT4((float)wiRenderer::RENDERWIDTH, (float)wiRenderer::RENDERHEIGHT, effects.siz.x, effects.siz.y); + (*pscb).mMaskFadOpaDis = XMFLOAT4((effects.maskMap ? 1.f : 0.f), effects.fade, effects.opacity, (float)normalmapmode); + (*pscb).mDimension = XMFLOAT4((float)wiRenderer::RENDERWIDTH, (float)wiRenderer::RENDERHEIGHT, effects.siz.x, effects.siz.y); - wiRenderer::UpdateBuffer(PSCb,&pscb,context); + wiRenderer::UpdateBuffer(PSCb,pscb,context); //PSConstantBuffer* dataPtr2; //context->Map(PSCb,0,D3D11_MAP_WRITE_DISCARD,0,&mappedResource); //dataPtr2 = (PSConstantBuffer*)mappedResource.pData; @@ -475,14 +475,14 @@ void wiImage::Draw(wiRenderer::TextureView texture, const wiImageEffects& effect else{ //BLUR wiRenderer::BindVS(screenVS,context); - BlurBuffer cb; + static thread_local BlurBuffer* cb = new BlurBuffer; if(effects.blurDir==0){ wiRenderer::BindPS(blurHPS,context); - cb.mWeightTexelStrenMip.y = 1.0f / wiRenderer::RENDERWIDTH; + (*cb).mWeightTexelStrenMip.y = 1.0f / wiRenderer::RENDERWIDTH; } else{ wiRenderer::BindPS(blurVPS,context); - cb.mWeightTexelStrenMip.y = 1.0f / wiRenderer::RENDERHEIGHT; + (*cb).mWeightTexelStrenMip.y = 1.0f / wiRenderer::RENDERHEIGHT; } float weight0 = 1.0f; @@ -491,12 +491,12 @@ void wiImage::Draw(wiRenderer::TextureView texture, const wiImageEffects& effect float weight3 = 0.18f; float weight4 = 0.1f; float normalization = (weight0 + 2.0f * (weight1 + weight2 + weight3 + weight4)); - cb.mWeight=XMVectorSet(weight0,weight1,weight2,weight3) / normalization; - cb.mWeightTexelStrenMip.x = weight4 / normalization; - cb.mWeightTexelStrenMip.z = effects.blur; - cb.mWeightTexelStrenMip.w = effects.mipLevel; + (*cb).mWeight=XMVectorSet(weight0,weight1,weight2,weight3) / normalization; + (*cb).mWeightTexelStrenMip.x = weight4 / normalization; + (*cb).mWeightTexelStrenMip.z = effects.blur; + (*cb).mWeightTexelStrenMip.w = effects.mipLevel; - wiRenderer::UpdateBuffer(blurCb,&cb,context); + wiRenderer::UpdateBuffer(blurCb,cb,context); wiRenderer::BindConstantBufferPS(blurCb,0,context); } @@ -597,16 +597,16 @@ void wiImage::DrawDeferred(wiRenderer::TextureView texture wiRenderer::BindBlendState(blendStateNoBlend,context); - DeferredBuffer cb; + static thread_local DeferredBuffer* cb = new DeferredBuffer; //cb.mSun=XMVector3Normalize(wiRenderer::GetSunPosition()); //cb.mEye=wiRenderer::getCamera()->Eye; - cb.mAmbient=wiRenderer::worldInfo.ambient; + (*cb).mAmbient=wiRenderer::worldInfo.ambient; //cb.mBiasResSoftshadow=shadowProps; - cb.mHorizon=wiRenderer::worldInfo.horizon; - cb.mViewProjInv=XMMatrixInverse( 0,XMMatrixTranspose(wiRenderer::getCamera()->GetView()*wiRenderer::getCamera()->GetProjection()) ); - cb.mFogSEH=wiRenderer::worldInfo.fogSEH; + (*cb).mHorizon=wiRenderer::worldInfo.horizon; + (*cb).mViewProjInv=XMMatrixInverse( 0,XMMatrixTranspose(wiRenderer::getCamera()->GetView()*wiRenderer::getCamera()->GetProjection()) ); + (*cb).mFogSEH=wiRenderer::worldInfo.fogSEH; - wiRenderer::UpdateBuffer(deferredCb,&cb,context); + wiRenderer::UpdateBuffer(deferredCb,cb,context); /*cb.mShM[0] = shMs[0]; cb.mShM[1] = shMs[1]; diff --git a/WickedEngine/wiImage.h b/WickedEngine/wiImage.h index fdbd9ddf1..2fe7d6c65 100644 --- a/WickedEngine/wiImage.h +++ b/WickedEngine/wiImage.h @@ -9,7 +9,7 @@ class wiImage private: static mutex MUTEX; protected: - struct ConstantBuffer + GFX_STRUCT ConstantBuffer { XMMATRIX mViewProjection; XMMATRIX mTrans; @@ -18,36 +18,48 @@ protected: XMFLOAT4 mDrawRec; XMFLOAT4 mBlurOpaPiv; XMFLOAT4 mTexOffset; + + ALIGN_16 }; - struct PSConstantBuffer + GFX_STRUCT PSConstantBuffer { XMFLOAT4 mMaskFadOpaDis; XMFLOAT4 mDimension; PSConstantBuffer(){mMaskFadOpaDis=mDimension=XMFLOAT4(0,0,0,0);} + + ALIGN_16 }; - struct ProcessBuffer + GFX_STRUCT ProcessBuffer { XMFLOAT4 mPostProcess; XMFLOAT4 mBloom; ProcessBuffer(){mPostProcess=mBloom=XMFLOAT4(0,0,0,0);} + + ALIGN_16 }; - struct LightShaftBuffer + GFX_STRUCT LightShaftBuffer { XMFLOAT4 mProperties; XMFLOAT2 mLightShaft; XMFLOAT2 mPadding; LightShaftBuffer(){mProperties=XMFLOAT4(0,0,0,0);mLightShaft=XMFLOAT2(0,0);} + + ALIGN_16 }; - struct DeferredBuffer + GFX_STRUCT DeferredBuffer { XMFLOAT3 mAmbient; float pad; XMFLOAT3 mHorizon; float pad1; XMMATRIX mViewProjInv; XMFLOAT3 mFogSEH; float pad2; + + ALIGN_16 }; - struct BlurBuffer + GFX_STRUCT BlurBuffer { XMVECTOR mWeight; XMFLOAT4 mWeightTexelStrenMip; + + ALIGN_16 }; static ID3D11BlendState* blendState, *blendStateAdd, *blendStateNoBlend, *blendStateAvg; diff --git a/WickedEngine/wiLoader.cpp b/WickedEngine/wiLoader.cpp index bce285c20..6f6adbeb1 100644 --- a/WickedEngine/wiLoader.cpp +++ b/WickedEngine/wiLoader.cpp @@ -8,6 +8,9 @@ #include "wiRenderTarget.h" #include "wiDepthTarget.h" +thread_local vector Mesh::instances; +ID3D11Buffer* Mesh::meshInstanceBuffer=nullptr; + void Mesh::LoadFromFile(const string& newName, const string& fname , const MaterialCollection& materialColl, vector armatures, const string& identifier){ @@ -303,7 +306,6 @@ void Mesh::LoadFromFile(const string& newName, const string& fname Optimize(); - usedBy.resize(1); CreateBuffers(); } } @@ -314,23 +316,19 @@ void Mesh::Optimize() void Mesh::CreateBuffers(){ if(!buffersComplete){ - usedBy.resize(1); - - for(int i=0;iCreateBuffer( &bd, 0, &meshInstanceBuffer ); + if (meshInstanceBuffer == nullptr) + { + ZeroMemory(&bd, sizeof(bd)); + bd.Usage = D3D11_USAGE_DYNAMIC; + bd.ByteWidth = sizeof(Instance) * 2; + bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; + bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + wiRenderer::graphicsDevice->CreateBuffer(&bd, 0, &meshInstanceBuffer); + } //bool armatureDeformedMesh=false; //for(int u : usedBy){ @@ -432,25 +430,17 @@ void Mesh::CreateVertexArrays() } } } -void Mesh::AddInstance(int count){ - usedBy.resize(usedBy.size()+count); - for(int i=0;i(meshInstanceBuffer,usedBy.size()*2); -} -void Mesh::AddRenderableInstance(const Instance& instance, int numerator, GRAPHICSTHREAD thread) +void Mesh::AddRenderableInstance(const Instance& instance, int numerator) { - if (numerator >= (int)instances[thread].size()) + if (numerator >= (int)instances.size()) { - instances[thread].resize(instances[thread].size() * 2); + instances.resize( (instances.size() + 1) * 2); } - instances[thread][numerator] = instance; + instances[numerator] = instance; } -void Mesh::UpdateRenderableInstances(int count, GRAPHICSTHREAD thread, wiRenderer::DeviceContext context) +void Mesh::UpdateRenderableInstances(int count, wiRenderer::DeviceContext context) { - wiRenderer::UpdateBuffer(meshInstanceBuffer, instances[thread].data(), context, sizeof(Instance)*count); + wiRenderer::UpdateBuffer(meshInstanceBuffer, instances.data(), context, sizeof(Instance)*count); } void LoadWiArmatures(const string& directory, const string& name, const string& identifier, vector& armatures @@ -806,13 +796,12 @@ void LoadWiObjects(const string& directory, const string& name, const string& id } else{ objects.back()->mesh=iter->second; - objects.back()->mesh->AddInstance(1); } } else{ if(iter!=meshes.end()) { objects.back()->mesh=iter->second; - objects.back()->mesh->usedBy.push_back(objects.size()-1); + //objects.back()->mesh->usedBy.push_back(objects.size()-1); } } } diff --git a/WickedEngine/wiLoader.h b/WickedEngine/wiLoader.h index 099f1fb95..9fce9ff79 100644 --- a/WickedEngine/wiLoader.h +++ b/WickedEngine/wiLoader.h @@ -284,10 +284,10 @@ struct Mesh{ vector physicsindices; vector physicalmapGP; - vector instances[GRAPHICSTHREAD_COUNT]; + static thread_local vector instances; ID3D11Buffer* meshVertBuff; - ID3D11Buffer* meshInstanceBuffer; + static ID3D11Buffer* meshInstanceBuffer; ID3D11Buffer* meshIndexBuff; ID3D11Buffer* boneBuffer; ID3D11Buffer* sOutBuffer; @@ -297,7 +297,7 @@ struct Mesh{ vector materials; bool renderable,doubleSided; STENCILREF stencilRef; - vector usedBy; + //vector usedBy; bool calculatedAO; @@ -336,9 +336,6 @@ struct Mesh{ vertexGroups.clear(); goalPositions.clear(); goalNormals.clear(); - for(int i=0;i<5;++i){ - instances[i].clear(); - } } void LoadFromFile(const string& newName, const string& fname , const MaterialCollection& materialColl, vector armatures, const string& identifier=""); @@ -346,9 +343,8 @@ struct Mesh{ void Optimize(); void CreateBuffers(); void CreateVertexArrays(); - void AddInstance(int count); - void AddRenderableInstance(const Instance& instance, int numerator, GRAPHICSTHREAD thread); - void UpdateRenderableInstances(int count, GRAPHICSTHREAD thread, ID3D11DeviceContext* context); + static void AddRenderableInstance(const Instance& instance, int numerator); + static void UpdateRenderableInstances(int count, ID3D11DeviceContext* context); void init(){ parent=""; vertices.resize(0); @@ -364,7 +360,7 @@ struct Mesh{ renderable=false; doubleSided=false; stencilRef = STENCILREF::STENCILREF_DEFAULT; - usedBy.resize(0); + //usedBy.resize(0); aabb=AABB(); trailInfo=RibbonTrail(); armatureIndex=0; diff --git a/WickedEngine/wiLoader_BindLua.cpp b/WickedEngine/wiLoader_BindLua.cpp index cbe6af962..180d6d0e3 100644 --- a/WickedEngine/wiLoader_BindLua.cpp +++ b/WickedEngine/wiLoader_BindLua.cpp @@ -603,7 +603,7 @@ int Object_BindLua::GetEmitter(lua_State *L) { id = wiLua::SGetInt(L, 1); } - if (object->eParticleSystems.size() > id) + if ((int)object->eParticleSystems.size() > id) { Luna::push(L, new EmittedParticle_BindLua(object->eParticleSystems[id])); } diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 92bc00291..5688da3bf 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -2541,7 +2541,7 @@ void wiRenderer::DrawLights(Camera* camera, ID3D11DeviceContext* context , unsigned int stencilRef){ - Frustum frustum = Frustum(); + static thread_local Frustum frustum = Frustum(); frustum.ConstructFrustum(min(camera->zFarP, worldInfo.fogSEH.y),camera->Projection,camera->View); CulledList culledObjects; @@ -2585,40 +2585,40 @@ void wiRenderer::DrawLights(Camera* camera, ID3D11DeviceContext* context if(type==0) //dir { - dLightBuffer lcb; - lcb.direction=XMVector3Normalize( + static thread_local dLightBuffer* lcb = new dLightBuffer; + (*lcb).direction=XMVector3Normalize( -XMVector3Transform( XMVectorSet(0,-1,0,1), XMMatrixRotationQuaternion( XMLoadFloat4(&l->rotation) ) ) ); - lcb.col=XMFLOAT4(l->color.x*l->enerDis.x,l->color.y*l->enerDis.x,l->color.z*l->enerDis.x,1); - lcb.mBiasResSoftshadow=XMFLOAT4(l->shadowBias,(float)SHADOWMAPRES,(float)SOFTSHADOW,0); + (*lcb).col=XMFLOAT4(l->color.x*l->enerDis.x,l->color.y*l->enerDis.x,l->color.z*l->enerDis.x,1); + (*lcb).mBiasResSoftshadow=XMFLOAT4(l->shadowBias,(float)SHADOWMAPRES,(float)SOFTSHADOW,0); for (unsigned int shmap = 0; shmap < l->shadowMaps_dirLight.size(); ++shmap){ - lcb.mShM[shmap]=l->shadowCam[shmap].getVP(); + (*lcb).mShM[shmap]=l->shadowCam[shmap].getVP(); BindTexturePS(l->shadowMaps_dirLight[shmap].depth->shaderResource,4+shmap,context); } - UpdateBuffer(lightCb[type],&lcb,context); + UpdateBuffer(lightCb[type],lcb,context); Draw(6, context); } else if(type==1) //point { - pLightBuffer lcb; - lcb.pos=l->translation; - lcb.col=l->color; - lcb.enerdis=l->enerDis; - lcb.enerdis.w = 0.f; + static thread_local pLightBuffer* lcb = new pLightBuffer; + (*lcb).pos=l->translation; + (*lcb).col=l->color; + (*lcb).enerdis=l->enerDis; + (*lcb).enerdis.w = 0.f; if (l->shadow && l->shadowMap_index>=0) { - lcb.enerdis.w = 1.f; + (*lcb).enerdis.w = 1.f; BindTexturePS(Light::shadowMaps_pointLight[l->shadowMap_index].depth->shaderResource, 7, context); } - UpdateBuffer(lightCb[type], &lcb, context); + UpdateBuffer(lightCb[type], lcb, context); Draw(240, context); } else if(type==2) //spot { - sLightBuffer lcb; + static thread_local sLightBuffer* lcb = new sLightBuffer; const float coneS=(const float)(l->enerDis.z/0.7853981852531433); XMMATRIX world,rot; world = XMMatrixTranspose( @@ -2627,22 +2627,22 @@ void wiRenderer::DrawLights(Camera* camera, ID3D11DeviceContext* context XMMatrixTranslationFromVector( XMLoadFloat3(&l->translation) ) ); rot=XMMatrixRotationQuaternion( XMLoadFloat4(&l->rotation) ); - lcb.direction=XMVector3Normalize( + (*lcb).direction=XMVector3Normalize( -XMVector3Transform( XMVectorSet(0,-1,0,1), rot ) ); - lcb.world=world; - lcb.mBiasResSoftshadow=XMFLOAT4(l->shadowBias,(float)SPOTLIGHTSHADOWRES,(float)SOFTSHADOW,0); - lcb.mShM = XMMatrixIdentity(); - lcb.col=l->color; - lcb.enerdis=l->enerDis; - lcb.enerdis.z=(float)cos(l->enerDis.z/2.0); + (*lcb).world=world; + (*lcb).mBiasResSoftshadow=XMFLOAT4(l->shadowBias,(float)SPOTLIGHTSHADOWRES,(float)SOFTSHADOW,0); + (*lcb).mShM = XMMatrixIdentity(); + (*lcb).col=l->color; + (*lcb).enerdis=l->enerDis; + (*lcb).enerdis.z=(float)cos(l->enerDis.z/2.0); if (l->shadow && l->shadowMap_index>=0) { - lcb.mShM = l->shadowCam[0].getVP(); + (*lcb).mShM = l->shadowCam[0].getVP(); BindTexturePS(Light::shadowMaps_spotLight[l->shadowMap_index].depth->shaderResource, 4, context); } - UpdateBuffer(lightCb[type], &lcb, context); + UpdateBuffer(lightCb[type], lcb, context); Draw(192, context); } @@ -2656,7 +2656,7 @@ void wiRenderer::DrawLights(Camera* camera, ID3D11DeviceContext* context void wiRenderer::DrawVolumeLights(Camera* camera, ID3D11DeviceContext* context) { - Frustum frustum = Frustum(); + static thread_local Frustum frustum = Frustum(); frustum.ConstructFrustum(min(camera->zFarP, worldInfo.fogSEH.y), camera->Projection, camera->View); @@ -2693,7 +2693,7 @@ void wiRenderer::DrawVolumeLights(Camera* camera, ID3D11DeviceContext* context) Light* l = (Light*)c; if(l->type==type && l->noHalo==false){ - vLightBuffer lcb; + static thread_local vLightBuffer* lcb = new vLightBuffer; XMMATRIX world; float sca=1; //if(type<1){ //sun @@ -2721,12 +2721,12 @@ void wiRenderer::DrawVolumeLights(Camera* camera, ID3D11DeviceContext* context) XMMatrixTranslationFromVector( XMLoadFloat3(&l->translation) ) ); } - lcb.world=world; - lcb.col=l->color; - lcb.enerdis=l->enerDis; - lcb.enerdis.w=sca; + (*lcb).world=world; + (*lcb).col=l->color; + (*lcb).enerdis=l->enerDis; + (*lcb).enerdis.w=sca; - UpdateBuffer(vLightCb,&lcb,context); + UpdateBuffer(vLightCb,lcb,context); if(type<=1) Draw(108,context); @@ -2901,16 +2901,16 @@ void wiRenderer::DrawForShadowMap(ID3D11DeviceContext* context) for (CulledObjectList::iterator viter = visibleInstances.begin(); viter != visibleInstances.end(); ++viter) { if ((*viter)->particleEmitter != Object::wiParticleEmitter::EMITTER_INVISIBLE) { if (mesh->softBody || (*viter)->armatureDeform) - mesh->AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency), k, GRAPHICSTHREAD_SCENE); + Mesh::AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency), k); else - mesh->AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency), k, GRAPHICSTHREAD_SCENE); + Mesh::AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency), k); ++k; } } if (k < 1) continue; - mesh->UpdateRenderableInstances(visibleInstances.size(), GRAPHICSTHREAD_SCENE, context); + mesh->UpdateRenderableInstances(visibleInstances.size(), context); BindVertexBuffer((mesh->sOutBuffer ? mesh->sOutBuffer : mesh->meshVertBuff), 0, sizeof(Vertex), context); @@ -2927,10 +2927,10 @@ void wiRenderer::DrawForShadowMap(ID3D11DeviceContext* context) - static MaterialCB mcb; - mcb.Create(*iMat, m); + static thread_local MaterialCB* mcb = new MaterialCB; + (*mcb).Create(*iMat, m); - UpdateBuffer(matCb, &mcb, context); + UpdateBuffer(matCb, mcb, context); DrawIndexedInstanced(mesh->indices.size(), visibleInstances.size(), context); @@ -2964,7 +2964,7 @@ void wiRenderer::DrawForShadowMap(ID3D11DeviceContext* context) CulledCollection culledwiRenderer; Light::shadowMaps_spotLight[i].Set(context); - Frustum frustum = Frustum(); + static thread_local Frustum frustum = Frustum(); XMFLOAT4X4 proj, view; XMStoreFloat4x4(&proj, XMLoadFloat4x4(&l->shadowCam[0].Projection)); XMStoreFloat4x4(&view, XMLoadFloat4x4(&l->shadowCam[0].View)); @@ -3006,16 +3006,16 @@ void wiRenderer::DrawForShadowMap(ID3D11DeviceContext* context) for (CulledObjectList::iterator viter = visibleInstances.begin(); viter != visibleInstances.end(); ++viter) { if ((*viter)->particleEmitter != Object::wiParticleEmitter::EMITTER_INVISIBLE) { if (mesh->softBody || (*viter)->armatureDeform) - mesh->AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency), k, GRAPHICSTHREAD_SCENE); + Mesh::AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency), k); else - mesh->AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency), k, GRAPHICSTHREAD_SCENE); + Mesh::AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency), k); ++k; } } if (k < 1) continue; - mesh->UpdateRenderableInstances(visibleInstances.size(), GRAPHICSTHREAD_SCENE, context); + mesh->UpdateRenderableInstances(visibleInstances.size(), context); BindVertexBuffer((mesh->sOutBuffer ? mesh->sOutBuffer : mesh->meshVertBuff), 0, sizeof(Vertex), context); @@ -3032,10 +3032,10 @@ void wiRenderer::DrawForShadowMap(ID3D11DeviceContext* context) - static MaterialCB mcb; - mcb.Create(*iMat, m); + static thread_local MaterialCB* mcb = new MaterialCB; + (*mcb).Create(*iMat, m); - UpdateBuffer(matCb, &mcb, context); + UpdateBuffer(matCb, mcb, context); DrawIndexedInstanced(mesh->indices.size(), visibleInstances.size(), context); @@ -3112,16 +3112,16 @@ void wiRenderer::DrawForShadowMap(ID3D11DeviceContext* context) for (CulledObjectList::iterator viter = visibleInstances.begin(); viter != visibleInstances.end(); ++viter) { if ((*viter)->particleEmitter != Object::wiParticleEmitter::EMITTER_INVISIBLE) { if (mesh->softBody || (*viter)->armatureDeform) - mesh->AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency), k, GRAPHICSTHREAD_SCENE); + Mesh::AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency), k); else - mesh->AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency), k, GRAPHICSTHREAD_SCENE); + Mesh::AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency), k); ++k; } } if (k < 1) continue; - mesh->UpdateRenderableInstances(visibleInstances.size(), GRAPHICSTHREAD_SCENE, context); + mesh->UpdateRenderableInstances(visibleInstances.size(), context); BindVertexBuffer((mesh->sOutBuffer ? mesh->sOutBuffer : mesh->meshVertBuff), 0, sizeof(Vertex), context); @@ -3135,10 +3135,10 @@ void wiRenderer::DrawForShadowMap(ID3D11DeviceContext* context) if (!wireRender && !iMat->isSky && !iMat->water && iMat->cast_shadow) { BindTexturePS(iMat->texture, 0, context); - static MaterialCB mcb; - mcb.Create(*iMat, m); + static thread_local MaterialCB* mcb = new MaterialCB; + (*mcb).Create(*iMat, m); - UpdateBuffer(matCb, &mcb, context); + UpdateBuffer(matCb, mcb, context); DrawIndexedInstanced(mesh->indices.size(), visibleInstances.size(), context); } @@ -3226,9 +3226,9 @@ void wiRenderer::DrawForShadowMap(ID3D11DeviceContext* context) // for (CulledObjectList::iterator viter = visibleInstances.begin(); viter != visibleInstances.end(); ++viter){ // if ((*viter)->particleEmitter != Object::wiParticleEmitter::EMITTER_INVISIBLE){ // if (mesh->softBody || (*viter)->armatureDeform) - // mesh->AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency), k, GRAPHICSTHREAD_SHADOWS); + // Mesh::AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency), k, GRAPHICSTHREAD_SHADOWS); // else - // mesh->AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency), k, GRAPHICSTHREAD_SHADOWS); + // Mesh::AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency), k, GRAPHICSTHREAD_SHADOWS); // ++k; // } // } @@ -3345,9 +3345,9 @@ void wiRenderer::DrawForShadowMap(ID3D11DeviceContext* context) // for (CulledObjectList::iterator viter = visibleInstances.begin(); viter != visibleInstances.end(); ++viter){ // if ((*viter)->particleEmitter != Object::wiParticleEmitter::EMITTER_INVISIBLE){ // if (mesh->softBody || (*viter)->armatureDeform) - // mesh->AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency), k, GRAPHICSTHREAD_SHADOWS); + // Mesh::AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency), k, GRAPHICSTHREAD_SHADOWS); // else - // mesh->AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency), k, GRAPHICSTHREAD_SHADOWS); + // Mesh::AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency), k, GRAPHICSTHREAD_SHADOWS); // ++k; // } // } @@ -3542,29 +3542,29 @@ void wiRenderer::DrawWorld(Camera* camera, bool DX11Eff, int tessF, ID3D11Device int matsiz = mesh->materialIndices.size(); if(DX11 && tessF){ - ConstantBuffer cb; - if(matsiz == 1) cb.mDisplace = XMVectorSet(mesh->materials[0]->hasDisplacementMap,0,0,0); - else if(matsiz == 2) cb.mDisplace = XMVectorSet(mesh->materials[0]->hasDisplacementMap,mesh->materials[1]->hasDisplacementMap,0,0); - else if(matsiz == 3) cb.mDisplace = XMVectorSet(mesh->materials[0]->hasDisplacementMap,mesh->materials[1]->hasDisplacementMap,mesh->materials[2]->hasDisplacementMap,0); - else if(matsiz == 4) cb.mDisplace = XMVectorSet(mesh->materials[0]->hasDisplacementMap,mesh->materials[1]->hasDisplacementMap,mesh->materials[2]->hasDisplacementMap,mesh->materials[3]->hasDisplacementMap); + static thread_local ConstantBuffer* cb = new ConstantBuffer; + if(matsiz == 1) (*cb).mDisplace = XMVectorSet(mesh->materials[0]->hasDisplacementMap,0,0,0); + else if(matsiz == 2) (*cb).mDisplace = XMVectorSet(mesh->materials[0]->hasDisplacementMap,mesh->materials[1]->hasDisplacementMap,0,0); + else if(matsiz == 3) (*cb).mDisplace = XMVectorSet(mesh->materials[0]->hasDisplacementMap,mesh->materials[1]->hasDisplacementMap,mesh->materials[2]->hasDisplacementMap,0); + else if(matsiz == 4) (*cb).mDisplace = XMVectorSet(mesh->materials[0]->hasDisplacementMap,mesh->materials[1]->hasDisplacementMap,mesh->materials[2]->hasDisplacementMap,mesh->materials[3]->hasDisplacementMap); - UpdateBuffer(constantBuffer,&cb,context); + UpdateBuffer(constantBuffer,cb,context); } int k=0; for(CulledObjectList::iterator viter=visibleInstances.begin();viter!=visibleInstances.end();++viter){ if((*viter)->particleEmitter!=Object::wiParticleEmitter::EMITTER_INVISIBLE){ if (mesh->softBody || (*viter)->armatureDeform) - mesh->AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency, (*viter)->color), k, thread); + Mesh::AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency, (*viter)->color), k); else - mesh->AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency, (*viter)->color), k, thread); + Mesh::AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency, (*viter)->color), k); ++k; } } if(k<1) continue; - mesh->UpdateRenderableInstances(visibleInstances.size(), thread, context); + mesh->UpdateRenderableInstances(visibleInstances.size(), context); BindIndexBuffer(mesh->meshIndexBuff,context); @@ -3584,10 +3584,10 @@ void wiRenderer::DrawWorld(Camera* camera, bool DX11Eff, int tessF, ID3D11Device BindDepthStencilState(depthStencilState,mesh->stencilRef,context); - static MaterialCB mcb[GRAPHICSTHREAD_COUNT]; - mcb[thread].Create(*iMat,m); + static thread_local MaterialCB* mcb = new MaterialCB; + (*mcb).Create(*iMat, m); - UpdateBuffer(matCb,&mcb[thread],context); + UpdateBuffer(matCb, mcb, context); if(!wireRender) BindTexturePS(iMat->texture,3,context); @@ -3672,16 +3672,16 @@ void wiRenderer::DrawWorldWater(Camera* camera, ID3D11ShaderResourceView* refrac for (CulledObjectList::iterator viter = visibleInstances.begin(); viter != visibleInstances.end(); ++viter){ if ((*viter)->particleEmitter != Object::wiParticleEmitter::EMITTER_INVISIBLE){ if (mesh->softBody || (*viter)->armatureDeform) - mesh->AddRenderableInstance(Instance(XMMatrixIdentity()), k, GRAPHICSTHREAD_MISC1); + Mesh::AddRenderableInstance(Instance(XMMatrixIdentity()), k); else - mesh->AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world))), k, GRAPHICSTHREAD_MISC1); + Mesh::AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world))), k); ++k; } } if (k<1) continue; - mesh->UpdateRenderableInstances(visibleInstances.size(), GRAPHICSTHREAD_MISC1, context); + mesh->UpdateRenderableInstances(visibleInstances.size(), context); BindIndexBuffer(mesh->meshIndexBuff,context); BindVertexBuffer((mesh->sOutBuffer?mesh->sOutBuffer:mesh->meshVertBuff),0,sizeof(Vertex),context); @@ -3693,10 +3693,10 @@ void wiRenderer::DrawWorldWater(Camera* camera, ID3D11ShaderResourceView* refrac if(iMat->water){ - static MaterialCB mcb[GRAPHICSTHREAD_COUNT]; - mcb[GRAPHICSTHREAD_MISC1].Create(*iMat, m); + static thread_local MaterialCB* mcb = new MaterialCB; + (*mcb).Create(*iMat, m); - UpdateBuffer(matCb, &mcb[GRAPHICSTHREAD_MISC1], context); + UpdateBuffer(matCb, mcb, context); if(!wireRender) BindTexturePS(iMat->texture,3,context); if(!wireRender) BindTexturePS(iMat->refMap,4,context); @@ -3774,16 +3774,16 @@ void wiRenderer::DrawWorldTransparent(Camera* camera, ID3D11ShaderResourceView* for (CulledObjectList::iterator viter = visibleInstances.begin(); viter != visibleInstances.end(); ++viter){ if ((*viter)->particleEmitter != Object::wiParticleEmitter::EMITTER_INVISIBLE){ if (mesh->softBody || (*viter)->armatureDeform) - mesh->AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency, (*viter)->color), k, GRAPHICSTHREAD_MISC1); + Mesh::AddRenderableInstance(Instance(XMMatrixIdentity(), (*viter)->transparency, (*viter)->color), k); else - mesh->AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency, (*viter)->color), k, GRAPHICSTHREAD_MISC1); + Mesh::AddRenderableInstance(Instance(XMMatrixTranspose(XMLoadFloat4x4(&(*viter)->world)), (*viter)->transparency, (*viter)->color), k); ++k; } } if (k<1) continue; - mesh->UpdateRenderableInstances(visibleInstances.size(), GRAPHICSTHREAD_MISC1, context); + mesh->UpdateRenderableInstances(visibleInstances.size(), context); BindIndexBuffer(mesh->meshIndexBuff,context); @@ -3797,10 +3797,10 @@ void wiRenderer::DrawWorldTransparent(Camera* camera, ID3D11ShaderResourceView* if(iMat->transparent && iMat->alpha>0 && !iMat->water && !iMat->isSky){ - static MaterialCB mcb[GRAPHICSTHREAD_COUNT]; - mcb[GRAPHICSTHREAD_MISC1].Create(*iMat, m); + static thread_local MaterialCB* mcb = new MaterialCB; + (*mcb).Create(*iMat, m); - UpdateBuffer(matCb, &mcb[GRAPHICSTHREAD_MISC1], context); + UpdateBuffer(matCb, mcb, context); if(!wireRender) BindTexturePS(iMat->texture,3,context); if(!wireRender) BindTexturePS(iMat->refMap,4,context); @@ -3865,15 +3865,15 @@ void wiRenderer::DrawDecals(Camera* camera, DeviceContext context, TextureView d BindTexturePS(decal->texture,2,context); BindTexturePS(decal->normal,3,context); - DecalCBVS dcbvs; - dcbvs.mWVP= + static thread_local DecalCBVS* dcbvs = new DecalCBVS; + dcbvs->mWVP= XMMatrixTranspose( XMLoadFloat4x4(&decal->world) *camera->GetViewProjection() ); - UpdateBuffer(decalCbVS,&dcbvs,context); + UpdateBuffer(decalCbVS,dcbvs,context); - DecalCBPS* dcbps = (DecalCBPS*)_aligned_malloc(sizeof(DecalCBPS),16); + static thread_local DecalCBPS* dcbps = new DecalCBPS; dcbps->mDecalVP= XMMatrixTranspose( XMLoadFloat4x4(&decal->view)*XMLoadFloat4x4(&decal->projection) @@ -3887,7 +3887,6 @@ void wiRenderer::DrawDecals(Camera* camera, DeviceContext context, TextureView d dcbps->opacity=wiMath::Clamp((decal->life<=-2?1:decal->lifefadeStart?decal->life/decal->fadeStart:1),0,1); dcbps->front=decal->front; UpdateBuffer(decalCbPS,dcbps,context); - _aligned_free(dcbps); Draw(36,context); @@ -3900,66 +3899,66 @@ void wiRenderer::DrawDecals(Camera* camera, DeviceContext context, TextureView d void wiRenderer::UpdatePerWorldCB(ID3D11DeviceContext* context){ - PixelCB pcb; - pcb.mSun=XMVector3Normalize( GetSunPosition() ); - pcb.mHorizon=worldInfo.horizon; - pcb.mAmbient=worldInfo.ambient; - pcb.mSunColor=GetSunColor(); - pcb.mFogSEH=worldInfo.fogSEH; - UpdateBuffer(pixelCB, &pcb, context); + static thread_local PixelCB* pcb = new PixelCB; + (*pcb).mSun=XMVector3Normalize( GetSunPosition() ); + (*pcb).mHorizon=worldInfo.horizon; + (*pcb).mAmbient=worldInfo.ambient; + (*pcb).mSunColor=GetSunColor(); + (*pcb).mFogSEH=worldInfo.fogSEH; + UpdateBuffer(pixelCB, pcb, context); } void wiRenderer::UpdatePerFrameCB(ID3D11DeviceContext* context){ - ViewPropCB cb; - cb.mZFarP=cam->zFarP; - cb.mZNearP=cam->zNearP; - cb.matView = XMMatrixTranspose( cam->GetView() ); - cb.matProj = XMMatrixTranspose( cam->GetProjection() ); - UpdateBuffer(viewPropCB,&cb,context); + static thread_local ViewPropCB* cb = new ViewPropCB; + (*cb).mZFarP=cam->zFarP; + (*cb).mZNearP=cam->zNearP; + (*cb).matView = XMMatrixTranspose( cam->GetView() ); + (*cb).matProj = XMMatrixTranspose( cam->GetProjection() ); + UpdateBuffer(viewPropCB,cb,context); BindConstantBufferPS(viewPropCB,10,context); } void wiRenderer::UpdatePerRenderCB(ID3D11DeviceContext* context, int tessF){ if(tessF){ - TessBuffer tb; - tb.g_f4Eye = cam->GetEye(); - tb.g_f4TessFactors = XMFLOAT4A( (float)tessF,2.f,4.f,6.f ); - UpdateBuffer(tessBuf,&tb,context); + static thread_local TessBuffer* tb = new TessBuffer; + (*tb).g_f4Eye = cam->GetEye(); + (*tb).g_f4TessFactors = XMFLOAT4A( (float)tessF,2.f,4.f,6.f ); + UpdateBuffer(tessBuf,tb,context); } } void wiRenderer::UpdatePerViewCB(ID3D11DeviceContext* context, Camera* camera, Camera* refCamera, const XMFLOAT4& newClipPlane){ - StaticCB cb; - cb.mViewProjection = XMMatrixTranspose(camera->GetViewProjection()); - cb.mRefViewProjection = XMMatrixTranspose( refCamera->GetViewProjection()); - cb.mPrevViewProjection = XMMatrixTranspose(prevFrameCam->GetViewProjection()); - cb.mCamPos = camera->GetEye(); - cb.mClipPlane = newClipPlane; - cb.mWind=wind.direction; - cb.time=wind.time; - cb.windRandomness=wind.randomness; - cb.windWaveSize=wind.waveSize; - UpdateBuffer(staticCb,&cb,context); + static thread_local StaticCB* cb = new StaticCB; + (*cb).mViewProjection = XMMatrixTranspose(camera->GetViewProjection()); + (*cb).mRefViewProjection = XMMatrixTranspose( refCamera->GetViewProjection()); + (*cb).mPrevViewProjection = XMMatrixTranspose(prevFrameCam->GetViewProjection()); + (*cb).mCamPos = camera->GetEye(); + (*cb).mClipPlane = newClipPlane; + (*cb).mWind=wind.direction; + (*cb).time=wind.time; + (*cb).windRandomness=wind.randomness; + (*cb).windWaveSize=wind.waveSize; + UpdateBuffer(staticCb,cb,context); - SkyBuffer scb; - scb.mV=XMMatrixTranspose(camera->GetView()); - scb.mP = XMMatrixTranspose(camera->GetProjection()); - scb.mPrevView = XMMatrixTranspose(prevFrameCam->GetView()); - scb.mPrevProjection = XMMatrixTranspose(prevFrameCam->GetProjection()); - UpdateBuffer(skyCb,&scb,context); + static thread_local SkyBuffer* scb = new SkyBuffer; + (*scb).mV=XMMatrixTranspose(camera->GetView()); + (*scb).mP = XMMatrixTranspose(camera->GetProjection()); + (*scb).mPrevView = XMMatrixTranspose(prevFrameCam->GetView()); + (*scb).mPrevProjection = XMMatrixTranspose(prevFrameCam->GetProjection()); + UpdateBuffer(skyCb,scb,context); UpdateBuffer(trailCB, &XMMatrixTranspose(camera->GetViewProjection()), context); - LightStaticCB lcb; - lcb.mProjInv = XMMatrixInverse(0, XMMatrixTranspose(camera->GetViewProjection())); - UpdateBuffer(lightStaticCb,&lcb,context); + static thread_local LightStaticCB* lcb = new LightStaticCB; + (*lcb).mProjInv = XMMatrixInverse(0, XMMatrixTranspose(camera->GetViewProjection())); + UpdateBuffer(lightStaticCb,lcb,context); } void wiRenderer::UpdatePerEffectCB(ID3D11DeviceContext* context, const XMFLOAT4& blackoutBlackWhiteInvCol, const XMFLOAT4 colorMask){ - FxCB fb; - fb.mFx = blackoutBlackWhiteInvCol; - fb.colorMask=colorMask; - UpdateBuffer(fxCb,&fb,context); + static thread_local FxCB* fb = new FxCB; + (*fb).mFx = blackoutBlackWhiteInvCol; + (*fb).colorMask=colorMask; + UpdateBuffer(fxCb,fb,context); } void wiRenderer::FinishLoading(){ @@ -3999,7 +3998,7 @@ void wiRenderer::FinishLoading(){ for (Object* o : objects) { //if (o->mesh->renderable && !o->mesh->calculatedAO && o->mesh->usedBy.size() == 1 && !o->isDynamic() && o->particleEmitter != Object::EMITTER_INVISIBLE) - if (o->mesh->renderable && o->mesh->usedBy.size() <2) + if (o->mesh->renderable /*&& o->mesh->usedBy.size() <2*/) { //aoThreads.push_back(thread(CalculateVertexAO, o)); } @@ -4382,9 +4381,10 @@ void wiRenderer::LoadModel(const string& dir, const string& name, const XMMATRIX UpdateLights(); lights.insert(lights.end(),newLights.begin(),newLights.end()); + unique_identifier++; + graphicsMutex.unlock(); - unique_identifier++; } void wiRenderer::LoadWorldInfo(const string& dir, const string& name) { diff --git a/WickedEngine/wiRenderer.h b/WickedEngine/wiRenderer.h index 12442bc20..42b2f5af4 100644 --- a/WickedEngine/wiRenderer.h +++ b/WickedEngine/wiRenderer.h @@ -143,13 +143,13 @@ public: static void SetSpotLightShadowProps(int count, int resolution); protected: - struct ConstantBuffer + GFX_STRUCT ConstantBuffer { XMVECTOR mDisplace; ALIGN_16 }; - struct StaticCB + GFX_STRUCT StaticCB { XMMATRIX mViewProjection; XMMATRIX mRefViewProjection; @@ -164,7 +164,7 @@ protected: ALIGN_16 }; - struct PixelCB + GFX_STRUCT PixelCB { //XMFLOAT4A mFx; XMFLOAT3 mHorizon; float pad; @@ -175,13 +175,13 @@ protected: ALIGN_16 }; - struct FxCB{ + GFX_STRUCT FxCB{ XMFLOAT4 mFx; XMFLOAT4 colorMask; ALIGN_16 }; - struct MaterialCB + GFX_STRUCT MaterialCB { XMFLOAT4 difColor; UINT hasRef,hasNor,hasTex,hasSpe; @@ -202,7 +202,7 @@ protected: ALIGN_16 }; - struct ForShadowMapCB + GFX_STRUCT ForShadowMapCB { XMMATRIX mViewProjection; XMFLOAT3 mWind; float time; @@ -211,19 +211,19 @@ protected: ALIGN_16 }; - struct CubeShadowCb{ + GFX_STRUCT CubeShadowCb{ XMMATRIX mViewProjection[6]; ALIGN_16 }; - struct TessBuffer + GFX_STRUCT TessBuffer { XMVECTOR g_f4Eye; XMFLOAT4A g_f4TessFactors; ALIGN_16 }; - struct dLightBuffer + GFX_STRUCT dLightBuffer { XMVECTOR direction; XMFLOAT4 col; @@ -232,7 +232,7 @@ protected: ALIGN_16 }; - struct pLightBuffer + GFX_STRUCT pLightBuffer { XMFLOAT3 pos; float pad; XMFLOAT4 col; @@ -240,7 +240,7 @@ protected: ALIGN_16 }; - struct sLightBuffer + GFX_STRUCT sLightBuffer { XMMATRIX world; XMVECTOR direction; @@ -251,7 +251,7 @@ protected: ALIGN_16 }; - struct vLightBuffer + GFX_STRUCT vLightBuffer { XMMATRIX world; XMFLOAT4 col; @@ -259,18 +259,18 @@ protected: ALIGN_16 }; - struct LightStaticCB{ + GFX_STRUCT LightStaticCB{ XMMATRIX mProjInv; ALIGN_16 }; - struct LineBuffer{ + GFX_STRUCT LineBuffer{ XMMATRIX mWorldViewProjection; XMFLOAT4 color; ALIGN_16 }; - struct SkyBuffer + GFX_STRUCT SkyBuffer { XMMATRIX mV; XMMATRIX mP; @@ -279,13 +279,13 @@ protected: ALIGN_16 }; - struct DecalCBVS + GFX_STRUCT DecalCBVS { XMMATRIX mWVP; ALIGN_16 }; - struct DecalCBPS + GFX_STRUCT DecalCBPS { XMMATRIX mDecalVP; int hasTexNor; @@ -295,7 +295,7 @@ protected: ALIGN_16 }; - struct ViewPropCB + GFX_STRUCT ViewPropCB { XMMATRIX matView; XMMATRIX matProj;