//********************************** Banshee Engine (www.banshee3d.com) **************************************************// //**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************// #include "BsHandleDrawManager.h" #include "BsDrawHelper.h" #include "BsMaterial.h" #include "BsGpuParamsSet.h" #include "BsBuiltinEditorResources.h" #include "BsCoreThread.h" #include "BsRendererManager.h" #include "BsCoreRenderer.h" #include "BsTransientMesh.h" #include "BsCamera.h" #include "BsRendererUtility.h" #include "BsTexture.h" #include "BsTime.h" #include "BsRenderAPI.h" using namespace std::placeholders; namespace bs { const UINT32 HandleDrawManager::SPHERE_QUALITY = 1; const UINT32 HandleDrawManager::WIRE_SPHERE_QUALITY = 10; const UINT32 HandleDrawManager::ARC_QUALITY = 10; HandleDrawManager::HandleDrawManager() :mLastFrameIdx((UINT64)-1), mCore(nullptr) { mTransform = Matrix4::IDENTITY; mDrawHelper = bs_new(); HMaterial solidMaterial = BuiltinEditorResources::instance().createSolidHandleMat(); HMaterial lineMaterial = BuiltinEditorResources::instance().createLineHandleMat(); HMaterial textMaterial = BuiltinEditorResources::instance().createTextGizmoMat(); HMaterial clearMaterial = BuiltinEditorResources::instance().createHandleClearAlphaMat(); SPtr solidMaterialProxy = solidMaterial->getCore(); SPtr lineMaterialProxy = lineMaterial->getCore(); SPtr textMaterialProxy = textMaterial->getCore(); SPtr clearMaterialProxy = clearMaterial->getCore(); mCore.store(bs_new(HandleDrawManagerCore::PrivatelyConstruct()), std::memory_order_release); gCoreThread().queueCommand(std::bind(&HandleDrawManager::initializeCore, this, lineMaterialProxy, solidMaterialProxy, textMaterialProxy, clearMaterialProxy)); } HandleDrawManager::~HandleDrawManager() { clearMeshes(); bs_delete(mDrawHelper); gCoreThread().queueCommand(std::bind(&HandleDrawManager::destroyCore, this, mCore.load(std::memory_order_relaxed))); } void HandleDrawManager::initializeCore(const SPtr& lineMat, const SPtr& solidMat, const SPtr& textMat, const SPtr& clearMat) { THROW_IF_NOT_CORE_THREAD; mCore.load(std::memory_order_acquire)->initialize(lineMat, solidMat, textMat, clearMat); } void HandleDrawManager::destroyCore(HandleDrawManagerCore* core) { THROW_IF_NOT_CORE_THREAD; bs_delete(core); } void HandleDrawManager::setColor(const Color& color) { mDrawHelper->setColor(color); } void HandleDrawManager::setTransform(const Matrix4& transform) { mTransform = transform; } void HandleDrawManager::setLayer(UINT64 layer) { mDrawHelper->setLayer(layer); } void HandleDrawManager::drawCube(const Vector3& position, const Vector3& extents, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->cube(position, extents); } void HandleDrawManager::drawSphere(const Vector3& position, float radius, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->sphere(position, radius); } void HandleDrawManager::drawWireCube(const Vector3& position, const Vector3& extents, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->wireCube(position, extents); } void HandleDrawManager::drawWireSphere(const Vector3& position, float radius, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->wireSphere(position, radius); } void HandleDrawManager::drawCone(const Vector3& base, const Vector3& normal, float height, float radius, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->cone(base, normal, height, radius); } void HandleDrawManager::drawLine(const Vector3& start, const Vector3& end, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->line(start, end); } void HandleDrawManager::drawDisc(const Vector3& position, const Vector3& normal, float radius, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->disc(position, normal, radius); } void HandleDrawManager::drawWireDisc(const Vector3& position, const Vector3& normal, float radius, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->wireDisc(position, normal, radius); } void HandleDrawManager::drawArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->arc(position, normal, radius, startAngle, amountAngle); } void HandleDrawManager::drawWireArc(const Vector3& position, const Vector3& normal, float radius, Degree startAngle, Degree amountAngle, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->wireArc(position, normal, radius, startAngle, amountAngle); } void HandleDrawManager::drawRect(const Rect3& area, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); mDrawHelper->rectangle(area); } void HandleDrawManager::drawText(const Vector3& position, const WString& text, const HFont& font, UINT32 fontSize, float size) { Matrix4 scale = Matrix4::scaling(size); mDrawHelper->setTransform(mTransform * scale); HFont myFont = font; if (myFont == nullptr) myFont = BuiltinEditorResources::instance().getDefaultAAFont(); mDrawHelper->text(position, text, myFont, fontSize); } void HandleDrawManager::draw(const SPtr& camera) { HandleDrawManagerCore* core = mCore.load(std::memory_order_relaxed); // Clear meshes from previous frame UINT64 frameIdx = gTime().getFrameIdx(); if(frameIdx != mLastFrameIdx) { gCoreThread().queueCommand(std::bind(&HandleDrawManagerCore::clearQueued, core)); clearMeshes(); mLastFrameIdx = frameIdx; } mDrawHelper->buildMeshes(DrawHelper::SortType::BackToFront, camera->getPosition(), camera->getLayers()); const Vector& meshes = mDrawHelper->getMeshes(); mActiveMeshes.push_back(meshes); Vector proxyData; for (auto& meshData : meshes) { SPtr tex; if (meshData.texture.isLoaded()) tex = meshData.texture->getCore(); if (meshData.type == DrawHelper::MeshType::Solid) { proxyData.push_back(HandleDrawManagerCore::MeshData( meshData.mesh->getCore(), tex, HandleDrawManagerCore::MeshType::Solid)); } else if (meshData.type == DrawHelper::MeshType::Line) { proxyData.push_back(HandleDrawManagerCore::MeshData( meshData.mesh->getCore(), tex, HandleDrawManagerCore::MeshType::Line)); } else // Text { proxyData.push_back(HandleDrawManagerCore::MeshData( meshData.mesh->getCore(), tex, HandleDrawManagerCore::MeshType::Text)); } } gCoreThread().queueCommand(std::bind(&HandleDrawManagerCore::queueForDraw, core, camera->getCore(), proxyData)); } void HandleDrawManager::clear() { mDrawHelper->clear(); } void HandleDrawManager::clearMeshes() { for (auto entry : mActiveMeshes) mDrawHelper->clearMeshes(entry); mActiveMeshes.clear(); } HandleParamBlockDef gHandleParamBlockDef; HandleDrawManagerCore::HandleDrawManagerCore(const PrivatelyConstruct& dummy) :mTypeCounters() { } HandleDrawManagerCore::~HandleDrawManagerCore() { clearQueued(); } void HandleDrawManagerCore::initialize(const SPtr& lineMat, const SPtr& solidMat, const SPtr& textMat, const SPtr& clearMat) { mMaterials[(UINT32)MeshType::Line] = lineMat; mMaterials[(UINT32)MeshType::Solid] = solidMat; mMaterials[(UINT32)MeshType::Text] = textMat; mClearMaterial = clearMat; mParamBuffer = gHandleParamBlockDef.createBuffer(); } void HandleDrawManagerCore::queueForDraw(const SPtr& camera, Vector& meshes) { SPtr activeRenderer = RendererManager::instance().getActive(); if (camera != nullptr) { UINT32 idx = (UINT32)mQueuedData.size(); activeRenderer->registerRenderCallback(camera.get(), 20, std::bind(&HandleDrawManagerCore::render, this, idx)); for(auto& entry : meshes) { UINT32 typeIdx = (UINT32)entry.type; UINT32 paramsIdx = mTypeCounters[typeIdx]; entry.paramIdx = paramsIdx; SPtr paramsSet; if (paramsIdx >= mParamSets[typeIdx].size()) { paramsSet = mMaterials[typeIdx]->createParamsSet(); paramsSet->setParamBlockBuffer("Uniforms", mParamBuffer, true); mParamSets[typeIdx].push_back(paramsSet); } else paramsSet = mParamSets[typeIdx][entry.paramIdx]; if(entry.type == MeshType::Text) { GpuParamTextureCore texture; paramsSet->getGpuParams()->getTextureParam(GPT_FRAGMENT_PROGRAM, "gMainTexture", texture); texture.set(entry.texture); } mTypeCounters[typeIdx]++; } mQueuedData.push_back({ camera, meshes }); } } void HandleDrawManagerCore::clearQueued() { SPtr activeRenderer = RendererManager::instance().getActive(); for (auto& entry : mQueuedData) activeRenderer->unregisterRenderCallback(entry.camera.get(), 20); mQueuedData.clear(); } void HandleDrawManagerCore::render(UINT32 queuedDataIdx) { THROW_IF_NOT_CORE_THREAD; const QueuedData& queueData = mQueuedData[queuedDataIdx]; SPtr camera = queueData.camera; const Vector& meshes = queueData.meshes; SPtr renderTarget = camera->getViewport()->getTarget(); float width = (float)renderTarget->getProperties().getWidth(); float height = (float)renderTarget->getProperties().getHeight(); Rect2 normArea = camera->getViewport()->getNormArea(); Rect2I screenArea; screenArea.x = (int)(normArea.x * width); screenArea.y = (int)(normArea.y * height); screenArea.width = (int)(normArea.width * width); screenArea.height = (int)(normArea.height * height); Matrix4 viewProjMat = camera->getProjectionMatrixRS() * camera->getViewMatrix(); gHandleParamBlockDef.gMatViewProj.set(mParamBuffer, viewProjMat); gHandleParamBlockDef.gViewDir.set(mParamBuffer, (Vector4)camera->getForward()); UINT32 currentType = -1; for (auto& meshData : meshes) { UINT32 typeIdx = (UINT32)meshData.type; if (currentType != typeIdx) { gRendererUtility().setPass(mMaterials[typeIdx]); currentType = typeIdx; } gRendererUtility().setPassParams(mParamSets[typeIdx][meshData.paramIdx]); gRendererUtility().draw(meshData.mesh, meshData.mesh->getProperties().getSubMesh(0)); } // Set alpha of everything that was drawn to 1 so we can overlay this texture onto GUI using transparency gRendererUtility().setPass(mClearMaterial, 0); gRendererUtility().drawScreenQuad(); } }