diff --git a/Documentation/ScriptingAPI-Documentation.md b/Documentation/ScriptingAPI-Documentation.md index f74f1848b..340d4dd33 100644 --- a/Documentation/ScriptingAPI-Documentation.md +++ b/Documentation/ScriptingAPI-Documentation.md @@ -160,7 +160,6 @@ Render images on the screen. - GetParams() : ImageParams result - SetAnim(SpriteAnim anim) - GetAnim() : SpriteAnim result -- Destroy() #### ImageParams Specify Sprite properties, like position, size, etc. diff --git a/Documentation/WickedEngine-Documentation.md b/Documentation/WickedEngine-Documentation.md index bec8f5b28..878a52f63 100644 --- a/Documentation/WickedEngine-Documentation.md +++ b/Documentation/WickedEngine-Documentation.md @@ -198,7 +198,7 @@ Before a RenderPath is destroyed, Unload will be called, so deleting resources c ### RenderPath2D [[Header]](../WickedEngine/RenderPath2D.h) [[Cpp]](../WickedEngine/RenderPath2D.cpp) -Capable of handling 2D rendering to offscreen buffer in Render() function, or just the screen in Compose() function. It has some functionality to render wiSprite and wiFont onto rendering layers and stenciling with 3D rendered scene. +Capable of handling 2D rendering to offscreen buffer in Render() function, or just the screen in Compose() function. It has some functionality to render wiSprite and wiFont onto rendering layers and stenciling with 3D rendered scene. It has a [GUI](#gui) that is automatically updated and rendered if any elements have been added to it. ### RenderPath3D [[Header]](../WickedEngine/RenderPath3D.h) [[Cpp]](../WickedEngine/RenderPath3D.cpp) @@ -798,7 +798,10 @@ The custom GUI, implemented with engine features ### wiGUI [[Header]](../WickedEngine/wiGUI.h) [[Cpp]](../WickedEngine/wiGUI.cpp) -The wiGUI is responsible to run a GUI interface and manage widgets +The wiGUI is responsible to run a GUI interface and manage widgets. + +GUI Scaling: The wiGUI is created with a default size of (1920*1080), so by default, placing elements (widgets) onto the GUI will be relative to this resolution. This can be freely modified by calling the `wiGUI::SetSize()` function. +When the GUI is updated (usually done by [RenderPath2D](#renderpath2d) automatically), the GUI size will take the current screen size, and scale the contents accordingly. When the application window is resized, the GUI scaling will take effect automatically. When adding widgets to the GUI, they are always placed relative to the current GUI size, which could be different from the starting GUI size, if the GUI was updated and the application resolution is different from the default GUI size. ### wiEventArgs [[Header]](../WickedEngine/wiWidget.h) [[Cpp]](../WickedEngine/wiWidget.cpp) diff --git a/Editor/AnimationWindow.cpp b/Editor/AnimationWindow.cpp index c6d46c5d2..ed489d453 100644 --- a/Editor/AnimationWindow.cpp +++ b/Editor/AnimationWindow.cpp @@ -10,10 +10,6 @@ AnimationWindow::AnimationWindow(wiGUI* gui) :GUI(gui) { assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - - animWindow = new wiWindow(GUI, "Animation Window"); animWindow->SetSize(XMFLOAT2(600, 450)); GUI->AddWidget(animWindow); diff --git a/Editor/CameraWindow.cpp b/Editor/CameraWindow.cpp index 027dd4561..93599e797 100644 --- a/Editor/CameraWindow.cpp +++ b/Editor/CameraWindow.cpp @@ -21,9 +21,6 @@ CameraWindow::CameraWindow(wiGUI* gui) :GUI(gui) { assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - camera_transform.MatrixTransform(wiRenderer::GetCamera().GetInvView()); camera_transform.UpdateTransform(); @@ -140,7 +137,7 @@ CameraWindow::CameraWindow(wiGUI* gui) :GUI(gui) SetEntity(INVALID_ENTITY); - cameraWindow->Translate(XMFLOAT3(screenW - 720, 500, 0)); + cameraWindow->Translate(XMFLOAT3(gui->GetSize().x - 720, 500, 0)); cameraWindow->SetVisible(false); } diff --git a/Editor/DecalWindow.cpp b/Editor/DecalWindow.cpp index bc2c81e75..80bc65fdc 100644 --- a/Editor/DecalWindow.cpp +++ b/Editor/DecalWindow.cpp @@ -9,9 +9,6 @@ DecalWindow::DecalWindow(wiGUI* gui) : GUI(gui) { assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - decalWindow = new wiWindow(GUI, "Decal Window"); decalWindow->SetSize(XMFLOAT2(400, 300)); GUI->AddWidget(decalWindow); diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index 895cd7095..6e5df68f7 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -28,7 +28,6 @@ using namespace wiRectPacker; using namespace wiScene; using namespace wiECS; - void Editor::Initialize() { __super::Initialize(); @@ -41,18 +40,17 @@ void Editor::Initialize() wiRenderer::GetDevice()->SetVSyncEnabled(true); wiRenderer::SetOcclusionCullingEnabled(true); - renderComponent = new EditorComponent; + renderComponent = std::make_unique(); renderComponent->Initialize(); - loader = new EditorLoadingScreen; + loader = std::make_unique(); loader->Initialize(); loader->Load(); - renderComponent->loader = loader; renderComponent->main = this; - loader->addLoadingComponent(renderComponent, this, 0.2f); + loader->addLoadingComponent(renderComponent.get(), this, 0.2f); - ActivatePath(loader, 0.2f); + ActivatePath(loader.get(), 0.2f); } @@ -60,7 +58,7 @@ void EditorLoadingScreen::Load() { font = wiFont("Loading...", wiFontParams((int)(wiRenderer::GetDevice()->GetScreenWidth()*0.5f), (int)(wiRenderer::GetDevice()->GetScreenHeight()*0.5f), 36, WIFALIGN_CENTER, WIFALIGN_CENTER)); - addFont(&font); + AddFont(&font); sprite = wiSprite("../images/logo_small.png"); sprite.anim.opa = 1; @@ -70,7 +68,7 @@ void EditorLoadingScreen::Load() sprite.params.pivot = XMFLOAT2(0.5f, 1.0f); sprite.params.quality = QUALITY_LINEAR; sprite.params.blendFlag = BLENDMODE_ALPHA; - addSprite(&sprite); + AddSprite(&sprite); __super::Load(); } @@ -90,24 +88,22 @@ void EditorLoadingScreen::Unload() void EditorComponent::ChangeRenderPath(RENDERPATH path) { - SAFE_DELETE(renderPath); - switch (path) { case EditorComponent::RENDERPATH_FORWARD: - renderPath = new RenderPath3D_Forward; + renderPath = std::make_unique(); break; case EditorComponent::RENDERPATH_DEFERRED: - renderPath = new RenderPath3D_Deferred; + renderPath = std::make_unique(); break; case EditorComponent::RENDERPATH_TILEDFORWARD: - renderPath = new RenderPath3D_TiledForward; + renderPath = std::make_unique(); break; case EditorComponent::RENDERPATH_TILEDDEFERRED: - renderPath = new RenderPath3D_TiledDeferred; + renderPath = std::make_unique(); break; case EditorComponent::RENDERPATH_PATHTRACING: - renderPath = new RenderPath3D_PathTracing; + renderPath = std::make_unique(); break; default: assert(0); @@ -130,21 +126,21 @@ void EditorComponent::ChangeRenderPath(RENDERPATH path) renderPath->Load(); renderPath->Update(0); - materialWnd.reset(new MaterialWindow(&GetGUI())); - postprocessWnd.reset(new PostprocessWindow(&GetGUI(), renderPath)); - weatherWnd.reset(new WeatherWindow(&GetGUI())); - objectWnd.reset(new ObjectWindow(this)); - meshWnd.reset(new MeshWindow(&GetGUI())); - cameraWnd.reset(new CameraWindow(&GetGUI())); - rendererWnd.reset(new RendererWindow(&GetGUI(), this, renderPath)); - envProbeWnd.reset(new EnvProbeWindow(&GetGUI())); - soundWnd.reset(new SoundWindow(&GetGUI())); - decalWnd.reset(new DecalWindow(&GetGUI())); - lightWnd.reset(new LightWindow(&GetGUI())); - animWnd.reset(new AnimationWindow(&GetGUI())); - emitterWnd.reset(new EmitterWindow(&GetGUI())); - hairWnd.reset(new HairParticleWindow(&GetGUI())); - forceFieldWnd.reset(new ForceFieldWindow(&GetGUI())); + materialWnd = std::make_unique(&GetGUI()); + postprocessWnd = std::make_unique(&GetGUI(), renderPath.get()); + weatherWnd = std::make_unique(&GetGUI()); + objectWnd = std::make_unique(this); + meshWnd = std::make_unique(&GetGUI()); + cameraWnd = std::make_unique(&GetGUI()); + rendererWnd = std::make_unique(&GetGUI(), this, renderPath.get()); + envProbeWnd = std::make_unique(&GetGUI()); + soundWnd = std::make_unique(&GetGUI()); + decalWnd = std::make_unique(&GetGUI()); + lightWnd = std::make_unique(&GetGUI()); + animWnd = std::make_unique(&GetGUI()); + emitterWnd = std::make_unique(&GetGUI()); + hairWnd = std::make_unique(&GetGUI()); + forceFieldWnd = std::make_unique(&GetGUI()); ResizeBuffers(); } @@ -220,6 +216,7 @@ void EditorComponent::Load() float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); + GetGUI().SetSize(screenW, screenH); XMFLOAT2 option_size = XMFLOAT2(100, 28); float step = (option_size.y + 5) * -1, x = screenW - option_size.x, y = screenH - option_size.y; @@ -483,7 +480,7 @@ void EditorComponent::Load() { string fileName = result.filenames.front(); - loader->addLoadingFunction([=] { + main->loader->addLoadingFunction([=] { string extension = wiHelper::toUpper(wiHelper::GetExtensionFromFileName(fileName)); if (!extension.compare("WISCENE")) // engine-serialized @@ -509,11 +506,11 @@ void EditorComponent::Load() wiScene::GetScene().Merge(scene); } }); - loader->onFinished([=] { + main->loader->onFinished([=] { main->ActivatePath(this, 0.2f, wiColor::Black()); weatherWnd->Update(); }); - main->ActivatePath(loader, 0.2f, wiColor::Black()); + main->ActivatePath(main->loader.get(), 0.2f, wiColor::Black()); ResetHistory(); } }).detach(); @@ -644,7 +641,7 @@ void EditorComponent::Load() exitButton->SetSize(XMFLOAT2(50, 40)); exitButton->SetColor(wiColor(190, 0, 0, 200), wiWidget::WIDGETSTATE::IDLE); exitButton->SetColor(wiColor(255, 0, 0, 255), wiWidget::WIDGETSTATE::FOCUS); - exitButton->OnClick([](wiEventArgs args) { + exitButton->OnClick([this](wiEventArgs args) { exit(0); }); GetGUI().AddWidget(exitButton); @@ -1300,19 +1297,19 @@ void EditorComponent::Update(float dt) if (wiInput::Press(wiInput::MOUSE_BUTTON_RIGHT) || selectAll) { - wiArchive* archive = AdvanceHistory(); - *archive << HISTORYOP_SELECTION; + wiArchive& archive = AdvanceHistory(); + archive << HISTORYOP_SELECTION; // record PREVIOUS selection state... - *archive << selected.size(); + archive << selected.size(); for (auto& x : selected) { - *archive << x.entity; - *archive << x.position; - *archive << x.normal; - *archive << x.subsetIndex; - *archive << x.distance; + archive << x.entity; + archive << x.position; + archive << x.normal; + archive << x.subsetIndex; + archive << x.distance; } - savedHierarchy.Serialize(*archive); + savedHierarchy.Serialize(archive); if (selectAll) { @@ -1371,16 +1368,16 @@ void EditorComponent::Update(float dt) } // record NEW selection state... - *archive << selected.size(); + archive << selected.size(); for (auto& x : selected) { - *archive << x.entity; - *archive << x.position; - *archive << x.normal; - *archive << x.subsetIndex; - *archive << x.distance; + archive << x.entity; + archive << x.position; + archive << x.normal; + archive << x.subsetIndex; + archive << x.distance; } - savedHierarchy.Serialize(*archive); + savedHierarchy.Serialize(archive); } // Control operations... @@ -1397,12 +1394,11 @@ void EditorComponent::Update(float dt) auto prevSel = selected; EndTranslate(); - SAFE_DELETE(clipboard); - clipboard = new wiArchive(); - *clipboard << prevSel.size(); + clipboard = wiArchive(); + clipboard << prevSel.size(); for (auto& x : prevSel) { - scene.Entity_Serialize(*clipboard, x.entity, 0); + scene.Entity_Serialize(clipboard, x.entity, 0); AddSelected(x); } @@ -1414,13 +1410,13 @@ void EditorComponent::Update(float dt) auto prevSel = selected; EndTranslate(); - clipboard->SetReadModeAndResetPos(true); + clipboard.SetReadModeAndResetPos(true); size_t count; - *clipboard >> count; + clipboard >> count; for (size_t i = 0; i < count; ++i) { wiScene::PickResult picked; - picked.entity = scene.Entity_Serialize(*clipboard, INVALID_ENTITY, wiRandom::getRandom(1, INT_MAX), false); + picked.entity = scene.Entity_Serialize(clipboard, INVALID_ENTITY, wiRandom::getRandom(1, INT_MAX), false); AddSelected(picked); } @@ -1440,16 +1436,16 @@ void EditorComponent::Update(float dt) BeginTranslate(); } // Put Instances - if (clipboard != nullptr && hovered.subsetIndex >= 0 && wiInput::Down(wiInput::KEYBOARD_BUTTON_LSHIFT) && wiInput::Press(wiInput::MOUSE_BUTTON_LEFT)) + if (clipboard.IsOpen() && hovered.subsetIndex >= 0 && wiInput::Down(wiInput::KEYBOARD_BUTTON_LSHIFT) && wiInput::Press(wiInput::MOUSE_BUTTON_LEFT)) { XMMATRIX M = XMLoadFloat4x4(&hovered.orientation); - clipboard->SetReadModeAndResetPos(true); + clipboard.SetReadModeAndResetPos(true); size_t count; - *clipboard >> count; + clipboard >> count; for (size_t i = 0; i < count; ++i) { - Entity entity = scene.Entity_Serialize(*clipboard, INVALID_ENTITY, wiRandom::getRandom(1, INT_MAX), false); + Entity entity = scene.Entity_Serialize(clipboard, INVALID_ENTITY, wiRandom::getRandom(1, INT_MAX), false); TransformComponent* transform = scene.transforms.GetComponent(entity); if (transform != nullptr) { @@ -1477,17 +1473,17 @@ void EditorComponent::Update(float dt) if (wiInput::Press(wiInput::KEYBOARD_BUTTON_DELETE)) { - wiArchive* archive = AdvanceHistory(); - *archive << HISTORYOP_DELETE; + wiArchive& archive = AdvanceHistory(); + archive << HISTORYOP_DELETE; - *archive << selected.size(); + archive << selected.size(); for (auto& x : selected) { - *archive << x.entity; + archive << x.entity; } for (auto& x : selected) { - scene.Entity_Serialize(*archive, x.entity); + scene.Entity_Serialize(archive, x.entity); } for (auto& x : selected) { @@ -1593,10 +1589,10 @@ void EditorComponent::Update(float dt) if (translator.IsDragEnded()) { - wiArchive* archive = AdvanceHistory(); - *archive << HISTORYOP_TRANSLATOR; - *archive << translator.GetDragStart(); - *archive << translator.GetDragEnd(); + wiArchive& archive = AdvanceHistory(); + archive << HISTORYOP_TRANSLATOR; + archive << translator.GetDragStart(); + archive << translator.GetDragEnd(); } emitterWnd->UpdateData(); @@ -2271,28 +2267,21 @@ bool EditorComponent::IsSelected(Entity entity) const void EditorComponent::ResetHistory() { historyPos = -1; - - for(auto& x : history) - { - SAFE_DELETE(x); - } history.clear(); } -wiArchive* EditorComponent::AdvanceHistory() +wiArchive& EditorComponent::AdvanceHistory() { historyPos++; while (static_cast(history.size()) > historyPos) { - SAFE_DELETE(history.back()); history.pop_back(); } - wiArchive* archive = new wiArchive; - archive->SetReadModeAndResetPos(false); - history.push_back(archive); + history.emplace_back(); + history.back().SetReadModeAndResetPos(false); - return archive; + return history.back(); } void EditorComponent::ConsumeHistoryOperation(bool undo) { @@ -2303,11 +2292,11 @@ void EditorComponent::ConsumeHistoryOperation(bool undo) historyPos++; } - wiArchive* archive = history[historyPos]; - archive->SetReadModeAndResetPos(true); + wiArchive& archive = history[historyPos]; + archive.SetReadModeAndResetPos(true); int temp; - *archive >> temp; + archive >> temp; HistoryOperationType type = (HistoryOperationType)temp; switch (type) @@ -2315,7 +2304,7 @@ void EditorComponent::ConsumeHistoryOperation(bool undo) case HISTORYOP_TRANSLATOR: { XMFLOAT4X4 start, end; - *archive >> start >> end; + archive >> start >> end; translator.enabled = true; Scene& scene = wiScene::GetScene(); @@ -2337,18 +2326,18 @@ void EditorComponent::ConsumeHistoryOperation(bool undo) Scene& scene = wiScene::GetScene(); size_t count; - *archive >> count; + archive >> count; vector deletedEntities(count); for (size_t i = 0; i < count; ++i) { - *archive >> deletedEntities[i]; + archive >> deletedEntities[i]; } if (undo) { for (size_t i = 0; i < count; ++i) { - scene.Entity_Serialize(*archive); + scene.Entity_Serialize(archive); } } else @@ -2369,37 +2358,37 @@ void EditorComponent::ConsumeHistoryOperation(bool undo) list selectedBEFORE; size_t selectionCountBEFORE; - *archive >> selectionCountBEFORE; + archive >> selectionCountBEFORE; for (size_t i = 0; i < selectionCountBEFORE; ++i) { wiScene::PickResult sel; - *archive >> sel.entity; - *archive >> sel.position; - *archive >> sel.normal; - *archive >> sel.subsetIndex; - *archive >> sel.distance; + archive >> sel.entity; + archive >> sel.position; + archive >> sel.normal; + archive >> sel.subsetIndex; + archive >> sel.distance; selectedBEFORE.push_back(sel); } ComponentManager savedHierarchyBEFORE; - savedHierarchyBEFORE.Serialize(*archive); + savedHierarchyBEFORE.Serialize(archive); list selectedAFTER; size_t selectionCountAFTER; - *archive >> selectionCountAFTER; + archive >> selectionCountAFTER; for (size_t i = 0; i < selectionCountAFTER; ++i) { wiScene::PickResult sel; - *archive >> sel.entity; - *archive >> sel.position; - *archive >> sel.normal; - *archive >> sel.subsetIndex; - *archive >> sel.distance; + archive >> sel.entity; + archive >> sel.position; + archive >> sel.normal; + archive >> sel.subsetIndex; + archive >> sel.distance; selectedAFTER.push_back(sel); } ComponentManager savedHierarchyAFTER; - savedHierarchyAFTER.Serialize(*archive); + savedHierarchyAFTER.Serialize(archive); // Restore proper selection state: diff --git a/Editor/Editor.h b/Editor/Editor.h index ec2f5b611..55572baf0 100644 --- a/Editor/Editor.h +++ b/Editor/Editor.h @@ -60,8 +60,7 @@ public: std::unordered_set scenegraphview_closed_items; void PushToSceneGraphView(wiECS::Entity entity, int level); - EditorLoadingScreen* loader = nullptr; - RenderPath3D* renderPath = nullptr; + std::unique_ptr renderPath; enum RENDERPATH { RENDERPATH_FORWARD, @@ -109,9 +108,9 @@ public: - wiArchive *clipboard = nullptr; + wiArchive clipboard; - std::vector history; + std::vector history; int historyPos = -1; enum HistoryOperationType { @@ -122,18 +121,15 @@ public: }; void ResetHistory(); - wiArchive* AdvanceHistory(); + wiArchive& AdvanceHistory(); void ConsumeHistoryOperation(bool undo); }; class Editor : public MainComponent { public: - Editor() {} - ~Editor() {} - - EditorComponent* renderComponent = nullptr; - EditorLoadingScreen* loader = nullptr; + std::unique_ptr renderComponent; + std::unique_ptr loader; void Initialize() override; }; diff --git a/Editor/EmitterWindow.cpp b/Editor/EmitterWindow.cpp index f55ed3967..b2feb1293 100644 --- a/Editor/EmitterWindow.cpp +++ b/Editor/EmitterWindow.cpp @@ -11,10 +11,6 @@ EmitterWindow::EmitterWindow(wiGUI* gui) : GUI(gui) { assert(GUI && "Invalid GUI!"); - - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - emitterWindow = new wiWindow(GUI, "Emitter Window"); emitterWindow->SetSize(XMFLOAT2(800, 1100)); GUI->AddWidget(emitterWindow); diff --git a/Editor/EnvProbeWindow.cpp b/Editor/EnvProbeWindow.cpp index 5f0b53eb2..75cfb4be1 100644 --- a/Editor/EnvProbeWindow.cpp +++ b/Editor/EnvProbeWindow.cpp @@ -8,9 +8,6 @@ EnvProbeWindow::EnvProbeWindow(wiGUI* gui) : GUI(gui) { assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - envProbeWindow = new wiWindow(GUI, "Environment Probe Window"); envProbeWindow->SetSize(XMFLOAT2(600, 400)); GUI->AddWidget(envProbeWindow); diff --git a/Editor/ForceFieldWindow.cpp b/Editor/ForceFieldWindow.cpp index 9dbaab047..0111622f0 100644 --- a/Editor/ForceFieldWindow.cpp +++ b/Editor/ForceFieldWindow.cpp @@ -9,10 +9,6 @@ ForceFieldWindow::ForceFieldWindow(wiGUI* gui) : GUI(gui) { assert(GUI && "Invalid GUI!"); - - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - forceFieldWindow = new wiWindow(GUI, "Force Field Window"); forceFieldWindow->SetSize(XMFLOAT2(600, 300)); GUI->AddWidget(forceFieldWindow); @@ -111,7 +107,7 @@ ForceFieldWindow::ForceFieldWindow(wiGUI* gui) : GUI(gui) - forceFieldWindow->Translate(XMFLOAT3(screenW - 720, 50, 0)); + forceFieldWindow->Translate(XMFLOAT3(gui->GetSize().x - 720, 50, 0)); forceFieldWindow->SetVisible(false); SetEntity(INVALID_ENTITY); diff --git a/Editor/HairParticleWindow.cpp b/Editor/HairParticleWindow.cpp index 4e7796ed4..046de3d40 100644 --- a/Editor/HairParticleWindow.cpp +++ b/Editor/HairParticleWindow.cpp @@ -9,10 +9,6 @@ HairParticleWindow::HairParticleWindow(wiGUI* gui) : GUI(gui) { assert(GUI && "Invalid GUI!"); - - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - hairWindow = new wiWindow(GUI, "Hair Particle System Window"); hairWindow->SetSize(XMFLOAT2(800, 600)); GUI->AddWidget(hairWindow); diff --git a/Editor/LightWindow.cpp b/Editor/LightWindow.cpp index ce667ce89..3e068aa2a 100644 --- a/Editor/LightWindow.cpp +++ b/Editor/LightWindow.cpp @@ -12,9 +12,6 @@ LightWindow::LightWindow(wiGUI* gui) : GUI(gui) { assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - lightWindow = new wiWindow(GUI, "Light Window"); lightWindow->SetSize(XMFLOAT2(650, 520)); GUI->AddWidget(lightWindow); diff --git a/Editor/MaterialWindow.cpp b/Editor/MaterialWindow.cpp index af96010b1..4ea47d751 100644 --- a/Editor/MaterialWindow.cpp +++ b/Editor/MaterialWindow.cpp @@ -12,9 +12,6 @@ MaterialWindow::MaterialWindow(wiGUI* gui) : GUI(gui) { assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - materialWindow = new wiWindow(GUI, "Material Window"); materialWindow->SetSize(XMFLOAT2(760, 890)); GUI->AddWidget(materialWindow); @@ -753,7 +750,7 @@ MaterialWindow::MaterialWindow(wiGUI* gui) : GUI(gui) materialWindow->AddWidget(texture_occlusion_uvset_Field); - materialWindow->Translate(XMFLOAT3(screenW - 880, 120, 0)); + materialWindow->Translate(XMFLOAT3(gui->GetSize().x - 880, 120, 0)); materialWindow->SetVisible(false); SetEntity(INVALID_ENTITY); diff --git a/Editor/MeshWindow.cpp b/Editor/MeshWindow.cpp index 59e62ab2b..91063ae7f 100644 --- a/Editor/MeshWindow.cpp +++ b/Editor/MeshWindow.cpp @@ -11,9 +11,6 @@ MeshWindow::MeshWindow(wiGUI* gui) : GUI(gui) { assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - meshWindow = new wiWindow(GUI, "Mesh Window"); meshWindow->SetSize(XMFLOAT2(800, 700)); @@ -223,7 +220,7 @@ MeshWindow::MeshWindow(wiGUI* gui) : GUI(gui) - meshWindow->Translate(XMFLOAT3(screenW - 910, 520, 0)); + meshWindow->Translate(XMFLOAT3(gui->GetSize().x - 910, 520, 0)); meshWindow->SetVisible(false); SetEntity(INVALID_ENTITY); diff --git a/Editor/ObjectWindow.cpp b/Editor/ObjectWindow.cpp index 61511937f..fb4ce6ed7 100644 --- a/Editor/ObjectWindow.cpp +++ b/Editor/ObjectWindow.cpp @@ -248,10 +248,6 @@ ObjectWindow::ObjectWindow(EditorComponent* editor) : editor(editor) GUI = &editor->GetGUI(); assert(GUI && "Invalid GUI!"); - - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - objectWindow = new wiWindow(GUI, "Object Window"); objectWindow->SetSize(XMFLOAT2(600, 520)); GUI->AddWidget(objectWindow); @@ -574,7 +570,7 @@ ObjectWindow::ObjectWindow(EditorComponent* editor) : editor(editor) - objectWindow->Translate(XMFLOAT3(screenW - 720, 120, 0)); + objectWindow->Translate(XMFLOAT3(GUI->GetSize().x - 720, 120, 0)); objectWindow->SetVisible(false); SetEntity(INVALID_ENTITY); diff --git a/Editor/PostprocessWindow.cpp b/Editor/PostprocessWindow.cpp index 75ce3c917..d3d4c1489 100644 --- a/Editor/PostprocessWindow.cpp +++ b/Editor/PostprocessWindow.cpp @@ -12,9 +12,6 @@ PostprocessWindow::PostprocessWindow(wiGUI* gui, RenderPath3D* comp) : GUI(gui), assert(component && "PostprocessWnd invalid component!"); assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - ppWindow = new wiWindow(GUI, "PostProcess Window"); ppWindow->SetSize(XMFLOAT2(400, 740)); GUI->AddWidget(ppWindow); @@ -335,7 +332,7 @@ PostprocessWindow::PostprocessWindow(wiGUI* gui, RenderPath3D* comp) : GUI(gui), ppWindow->AddWidget(chromaticaberrationSlider); - ppWindow->Translate(XMFLOAT3(screenW - 550, 50, 0)); + ppWindow->Translate(XMFLOAT3(gui->GetSize().x - 550, 50, 0)); ppWindow->SetVisible(false); } diff --git a/Editor/RendererWindow.cpp b/Editor/RendererWindow.cpp index 14c79d797..075ddd633 100644 --- a/Editor/RendererWindow.cpp +++ b/Editor/RendererWindow.cpp @@ -8,9 +8,6 @@ RendererWindow::RendererWindow(wiGUI* gui, EditorComponent* editorcomponent, Ren { assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - wiRenderer::SetToDrawDebugEnvProbes(true); wiRenderer::SetToDrawGridHelper(true); wiRenderer::SetToDrawDebugCameras(true); diff --git a/Editor/SoundWindow.cpp b/Editor/SoundWindow.cpp index 85c8de771..a5c5370a7 100644 --- a/Editor/SoundWindow.cpp +++ b/Editor/SoundWindow.cpp @@ -13,9 +13,6 @@ SoundWindow::SoundWindow(wiGUI* gui) : GUI(gui) { assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - soundWindow = new wiWindow(GUI, "Sound Window"); soundWindow->SetSize(XMFLOAT2(440, 340)); GUI->AddWidget(soundWindow); diff --git a/Editor/Translator.cpp b/Editor/Translator.cpp index cb846228f..b21f15095 100644 --- a/Editor/Translator.cpp +++ b/Editor/Translator.cpp @@ -11,9 +11,9 @@ using namespace wiScene; PipelineState pso_solidpart; PipelineState pso_wirepart; -std::unique_ptr vertexBuffer_Axis; -std::unique_ptr vertexBuffer_Plane; -std::unique_ptr vertexBuffer_Origin; +GPUBuffer vertexBuffer_Axis; +GPUBuffer vertexBuffer_Plane; +GPUBuffer vertexBuffer_Origin; UINT vertexCount_Axis = 0; UINT vertexCount_Plane = 0; UINT vertexCount_Origin = 0; @@ -77,7 +77,7 @@ Translator::Translator() GraphicsDevice* device = wiRenderer::GetDevice(); - if (vertexBuffer_Axis == nullptr) + if (!vertexBuffer_Axis.IsValid()) { { XMFLOAT4 verts[] = { @@ -95,12 +95,11 @@ Translator::Translator() SubresourceData InitData; InitData.pSysMem = verts; - vertexBuffer_Axis.reset(new GPUBuffer); - device->CreateBuffer(&bd, &InitData, vertexBuffer_Axis.get()); + device->CreateBuffer(&bd, &InitData, &vertexBuffer_Axis); } } - if (vertexBuffer_Plane == nullptr) + if (!vertexBuffer_Plane.IsValid()) { { XMFLOAT4 verts[] = { @@ -122,12 +121,11 @@ Translator::Translator() SubresourceData InitData; InitData.pSysMem = verts; - vertexBuffer_Plane.reset(new GPUBuffer); - device->CreateBuffer(&bd, &InitData, vertexBuffer_Plane.get()); + device->CreateBuffer(&bd, &InitData, &vertexBuffer_Plane); } } - if (vertexBuffer_Origin == nullptr) + if (!vertexBuffer_Origin.IsValid()) { { float edge = 0.2f; @@ -179,8 +177,7 @@ Translator::Translator() SubresourceData InitData; InitData.pSysMem = verts; - vertexBuffer_Origin.reset(new GPUBuffer); - device->CreateBuffer(&bd, &InitData, vertexBuffer_Origin.get()); + device->CreateBuffer(&bd, &InitData, &vertexBuffer_Origin); } } } @@ -461,7 +458,7 @@ void Translator::Draw(const CameraComponent& camera, CommandList cmd) const { device->BindPipelineState(&pso_solidpart, cmd); const GPUBuffer* vbs[] = { - vertexBuffer_Plane.get(), + &vertexBuffer_Plane, }; const UINT strides[] = { sizeof(XMFLOAT4) + sizeof(XMFLOAT4), @@ -497,7 +494,7 @@ void Translator::Draw(const CameraComponent& camera, CommandList cmd) const { device->BindPipelineState(&pso_wirepart, cmd); const GPUBuffer* vbs[] = { - vertexBuffer_Axis.get(), + &vertexBuffer_Axis, }; const UINT strides[] = { sizeof(XMFLOAT4) + sizeof(XMFLOAT4), @@ -533,7 +530,7 @@ void Translator::Draw(const CameraComponent& camera, CommandList cmd) const { device->BindPipelineState(&pso_solidpart, cmd); const GPUBuffer* vbs[] = { - vertexBuffer_Origin.get(), + &vertexBuffer_Origin, }; const UINT strides[] = { sizeof(XMFLOAT4) + sizeof(XMFLOAT4), diff --git a/Editor/WeatherWindow.cpp b/Editor/WeatherWindow.cpp index ed9299201..26272b77a 100644 --- a/Editor/WeatherWindow.cpp +++ b/Editor/WeatherWindow.cpp @@ -12,10 +12,6 @@ WeatherWindow::WeatherWindow(wiGUI* gui) : GUI(gui) { assert(GUI && "Invalid GUI!"); - float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); - float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); - - weatherWindow = new wiWindow(GUI, "Weather Window"); weatherWindow->SetSize(XMFLOAT2(1000, 820)); GUI->AddWidget(weatherWindow); diff --git a/Tests/Tests.cpp b/Tests/Tests.cpp index cec9c5ce5..b913b394c 100644 --- a/Tests/Tests.cpp +++ b/Tests/Tests.cpp @@ -16,11 +16,13 @@ void Tests::Initialize() infoDisplay.fpsinfo = true; infoDisplay.resolution = true; - ActivatePath(new TestsRenderer); + renderer.Load(); + + ActivatePath(&renderer); } -TestsRenderer::TestsRenderer() +void TestsRenderer::Load() { setSSREnabled(false); setSSAOEnabled(false); @@ -29,6 +31,7 @@ TestsRenderer::TestsRenderer() float screenW = (float)wiRenderer::GetDevice()->GetScreenWidth(); float screenH = (float)wiRenderer::GetDevice()->GetScreenHeight(); + GetGUI().SetSize(screenW, screenH); wiLabel* label = new wiLabel("Label1"); label->SetText("Wicked Engine Test Framework"); @@ -36,8 +39,8 @@ TestsRenderer::TestsRenderer() label->SetPos(XMFLOAT2(screenW / 2.f - label->scale.x / 2.f, screenH*0.95f)); GetGUI().AddWidget(label); - static wiAudio::Sound* sound = nullptr; - static wiAudio::SoundInstance* soundinstance = nullptr; + static wiAudio::Sound sound; + static wiAudio::SoundInstance soundinstance; wiButton* audioTest = new wiButton("AudioTest"); audioTest->SetText("Play Test Audio"); @@ -48,22 +51,20 @@ TestsRenderer::TestsRenderer() audioTest->OnClick([=](wiEventArgs args) { static bool playing = false; - if (sound == nullptr) + if (!sound.IsValid()) { - sound = new wiAudio::Sound; - wiAudio::CreateSound("sound/music.wav", sound); - soundinstance = new wiAudio::SoundInstance; - wiAudio::CreateSoundInstance(sound, soundinstance); + wiAudio::CreateSound("sound/music.wav", &sound); + wiAudio::CreateSoundInstance(&sound, &soundinstance); } if (playing) { - wiAudio::Stop(soundinstance); + wiAudio::Stop(&soundinstance); audioTest->SetText("Play Test Audio"); } else { - wiAudio::Play(soundinstance); + wiAudio::Play(&soundinstance); audioTest->SetText("Stop Test Audio"); } @@ -79,7 +80,7 @@ TestsRenderer::TestsRenderer() volume->SetColor(wiColor(255, 205, 43, 200), wiWidget::WIDGETSTATE::IDLE); volume->SetColor(wiColor(255, 235, 173, 255), wiWidget::WIDGETSTATE::FOCUS); volume->OnSlide([](wiEventArgs args) { - wiAudio::SetVolume(args.fValue / 100.0f, soundinstance); + wiAudio::SetVolume(args.fValue / 100.0f, &soundinstance); }); GetGUI().AddWidget(volume); @@ -116,8 +117,8 @@ TestsRenderer::TestsRenderer() wiRenderer::SetTemporalAAEnabled(false); wiRenderer::ClearWorld(); wiScene::GetScene().weather = WeatherComponent(); - this->clearSprites(); - this->clearFonts(); + this->ClearSprites(); + this->ClearFonts(); wiLua::GetGlobal()->KillProcesses(); // Reset camera position: @@ -144,7 +145,7 @@ TestsRenderer::TestsRenderer() sprite.anim.wobbleAnim.amount = XMFLOAT2(0.16f, 0.16f); sprite.anim.movingTexAnim.speedX = 0; sprite.anim.movingTexAnim.speedY = 3; - this->addSprite(&sprite); + this->AddSprite(&sprite); break; } case 1: @@ -208,7 +209,7 @@ TestsRenderer::TestsRenderer() font.params.size = 20; font.params.posX = (int)screenW / 2; font.params.posY = (int)screenH / 2; - addFont(&font); + AddFont(&font); wiInput::ControllerFeedback feedback; feedback.led_color.rgba = wiRandom::getRandom(0xFFFFFF); @@ -300,7 +301,7 @@ void TestsRenderer::RunJobSystemTest() font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_CENTER; font.params.size = 24; - this->addFont(&font); + this->AddFont(&font); } void TestsRenderer::RunFontTest() { @@ -323,8 +324,8 @@ void TestsRenderer::RunFontTest() font_upscaled.params.size = 14; font_upscaled.params.scaling = 32.0f / 14.0f; - addFont(&font); - addFont(&font_upscaled); + AddFont(&font); + AddFont(&font_upscaled); static wiFont font_aligned; font_aligned = font; @@ -333,7 +334,7 @@ void TestsRenderer::RunFontTest() font_aligned.params.shadowColor = wiColor::Red(); font_aligned.params.h_align = WIFALIGN_CENTER; font_aligned.SetText("Center aligned, red shadow, bigger"); - addFont(&font_aligned); + AddFont(&font_aligned); static wiFont font_aligned2; font_aligned2 = font_aligned; @@ -341,7 +342,7 @@ void TestsRenderer::RunFontTest() font_aligned2.params.shadowColor = wiColor::Purple(); font_aligned2.params.h_align = WIFALIGN_RIGHT; font_aligned2.SetText("Right aligned, purple shadow"); - addFont(&font_aligned2); + AddFont(&font_aligned2); std::stringstream ss(""); std::ifstream file("font_test.txt"); @@ -362,7 +363,7 @@ void TestsRenderer::RunFontTest() font_japanese.params.h_align = WIFALIGN_CENTER; font_japanese.params.size = 34; font_japanese.SetText(ss.str()); - addFont(&font_japanese); + AddFont(&font_japanese); static wiFont font_colored; font_colored.params.color = wiColor::Cyan(); @@ -372,7 +373,7 @@ void TestsRenderer::RunFontTest() font_colored.params.posX = wiRenderer::GetDevice()->GetScreenWidth() / 2; font_colored.params.posY = font_japanese.params.posY + font_japanese.textHeight(); font_colored.SetText("Colored font"); - addFont(&font_colored); + AddFont(&font_colored); } void TestsRenderer::RunSpriteTest() { @@ -391,21 +392,21 @@ void TestsRenderer::RunSpriteTest() static wiFont font("For more information, please see \nTests.cpp, RunSpriteTest() function."); font.params.posX = 10; font.params.posY = 200; - addFont(&font); + AddFont(&font); } // Simple sprite, no animation: { static wiSprite sprite("../images/logo_small.png"); sprite.params = params; - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("No animation: "); font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x += sprite.params.siz.x + step; } @@ -417,14 +418,14 @@ void TestsRenderer::RunSpriteTest() sprite.anim = wiSprite::Anim(); sprite.anim.fad = 1.2f; // (you can also do opacity animation with sprite.anim.opa) sprite.anim.repeatable = true; - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("Fade animation: "); font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x += sprite.params.siz.x + step; } @@ -436,14 +437,14 @@ void TestsRenderer::RunSpriteTest() sprite.anim = wiSprite::Anim(); sprite.anim.wobbleAnim.amount = XMFLOAT2(0.11f, 0.18f); sprite.anim.wobbleAnim.speed = 1.4f; - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("Wobble animation: "); font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x += sprite.params.siz.x + step; } @@ -455,14 +456,14 @@ void TestsRenderer::RunSpriteTest() sprite.anim = wiSprite::Anim(); sprite.anim.rot = 2.8f; sprite.anim.repeatable = true; - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("Rotate animation: "); font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x += sprite.params.siz.x + step; } @@ -478,14 +479,14 @@ void TestsRenderer::RunSpriteTest() sprite.params.sampleFlag = SAMPLEMODE_MIRROR; // texcoords will be scrolled out of bounds, so set up a wrap mode other than clamp sprite.anim.movingTexAnim.speedX = 0; sprite.anim.movingTexAnim.speedY = 2; // scroll the texture vertically. This value is pixels/second. So because our texture here is 1x2 pixels, just scroll it once fully per second with a value of 2 - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("MovingTex + mask: "); font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x = startPos.x; params.pos.y += sprite.params.siz.y + step * 1.5f; @@ -498,14 +499,14 @@ void TestsRenderer::RunSpriteTest() { static wiSprite sprite("images/spritesheet_grid.png"); sprite.params = params; // nothing extra, just display the full spritesheet - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("Spritesheet: \n(without animation)"); font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x += sprite.params.siz.x + step; } @@ -519,14 +520,14 @@ void TestsRenderer::RunSpriteTest() sprite.anim.repeatable = true; // enable looping sprite.anim.drawRectAnim.frameRate = 3; // 3 FPS, to be easily readable sprite.anim.drawRectAnim.frameCount = 4; // animate only a single line horizontally - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("single line anim: \n(4 frames)"); font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x += sprite.params.siz.x + step; } @@ -541,14 +542,14 @@ void TestsRenderer::RunSpriteTest() sprite.anim.drawRectAnim.frameRate = 3; // 3 FPS, to be easily readable sprite.anim.drawRectAnim.frameCount = 4; // again, specify 4 total frames to loop... sprite.anim.drawRectAnim.horizontalFrameCount = 1; // ...but this time, limit the horizontal frame count. This way, we can get it to only animate vertically - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("single line: \n(4 vertical frames)"); font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x += sprite.params.siz.x + step; } @@ -563,14 +564,14 @@ void TestsRenderer::RunSpriteTest() sprite.anim.drawRectAnim.frameRate = 3; // 3 FPS, to be easily readable sprite.anim.drawRectAnim.frameCount = 16; // all frames sprite.anim.drawRectAnim.horizontalFrameCount = 4; // all horizontal frames - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("multiline: \n(all 16 frames)"); font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x += sprite.params.siz.x + step; } @@ -585,14 +586,14 @@ void TestsRenderer::RunSpriteTest() sprite.anim.drawRectAnim.frameRate = 3; // 3 FPS, to be easily readable sprite.anim.drawRectAnim.frameCount = 14; // NOT all frames, which makes it irregular, so the last line will not contain all horizontal frames sprite.anim.drawRectAnim.horizontalFrameCount = 4; // all horizontal frames - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("irregular multiline: \n(14 frames)"); font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x = startPos.x; params.pos.y += sprite.params.siz.y + step * 1.5f; @@ -612,13 +613,13 @@ void TestsRenderer::RunSpriteTest() sprite.anim.drawRectAnim.horizontalFrameCount = 5; // this is a multiline spritesheet, so also set how many maximum frames there are in a line sprite.anim.drawRectAnim.frameRate = 40; // animation frames per second sprite.anim.repeatable = true; // looping - addSprite(&sprite); + AddSprite(&sprite); static wiFont font("For the following spritesheets, credits belong to: https://mrbubblewand.wordpress.com/download/"); font.params.v_align = WIFALIGN_BOTTOM; font.params.posX = int(sprite.params.pos.x - sprite.params.siz.x * 0.5f); font.params.posY = int(sprite.params.pos.y - sprite.params.siz.y * 0.5f); - addFont(&font); + AddFont(&font); params.pos.x += sprite.params.siz.x + step; } @@ -632,7 +633,7 @@ void TestsRenderer::RunSpriteTest() sprite.anim.drawRectAnim.horizontalFrameCount = 5; sprite.anim.drawRectAnim.frameRate = 30; sprite.anim.repeatable = true; - addSprite(&sprite); + AddSprite(&sprite); params.pos.x += sprite.params.siz.x + step; } @@ -646,7 +647,7 @@ void TestsRenderer::RunSpriteTest() sprite.anim.drawRectAnim.horizontalFrameCount = 5; sprite.anim.drawRectAnim.frameRate = 27; sprite.anim.repeatable = true; - addSprite(&sprite); + AddSprite(&sprite); params.pos.x += sprite.params.siz.x + step; } @@ -660,7 +661,7 @@ void TestsRenderer::RunSpriteTest() sprite.anim.drawRectAnim.horizontalFrameCount = 5; sprite.anim.drawRectAnim.frameRate = 27; sprite.anim.repeatable = true; - addSprite(&sprite); + AddSprite(&sprite); params.pos.x += sprite.params.siz.x + step; } @@ -674,7 +675,7 @@ void TestsRenderer::RunSpriteTest() sprite.anim.drawRectAnim.horizontalFrameCount = 5; sprite.anim.drawRectAnim.frameRate = 27; sprite.anim.repeatable = true; - addSprite(&sprite); + AddSprite(&sprite); params.pos.x += sprite.params.siz.x + step; } @@ -756,5 +757,5 @@ void TestsRenderer::RunNetworkTest() font.params.h_align = WIFALIGN_CENTER; font.params.v_align = WIFALIGN_CENTER; font.params.size = 24; - this->addFont(&font); + AddFont(&font); } diff --git a/Tests/Tests.h b/Tests/Tests.h index 498fec6ea..19c6cc674 100644 --- a/Tests/Tests.h +++ b/Tests/Tests.h @@ -1,17 +1,11 @@ #pragma once #include "WickedEngine.h" -class Tests : public MainComponent -{ -public: - virtual void Initialize() override; -}; - class TestsRenderer : public RenderPath3D_Deferred { -public: - TestsRenderer(); +public: + void Load() override; void RunJobSystemTest(); void RunFontTest(); @@ -19,3 +13,10 @@ public: void RunNetworkTest(); }; +class Tests : public MainComponent +{ + TestsRenderer renderer; +public: + virtual void Initialize() override; +}; + diff --git a/WickedEngine/LoadingScreen_BindLua.cpp b/WickedEngine/LoadingScreen_BindLua.cpp index 6ff777c6b..a30b8ce44 100644 --- a/WickedEngine/LoadingScreen_BindLua.cpp +++ b/WickedEngine/LoadingScreen_BindLua.cpp @@ -33,20 +33,6 @@ Luna::PropertyType LoadingScreen_BindLua::properties[] = { NULL, NULL } }; -LoadingScreen_BindLua::LoadingScreen_BindLua(LoadingScreen* component) -{ - this->component = component; -} - -LoadingScreen_BindLua::LoadingScreen_BindLua(lua_State *L) -{ - component = new LoadingScreen(); -} - -LoadingScreen_BindLua::~LoadingScreen_BindLua() -{ -} - int LoadingScreen_BindLua::AddLoadingTask(lua_State* L) { int argc = wiLua::SGetArgCount(L); diff --git a/WickedEngine/LoadingScreen_BindLua.h b/WickedEngine/LoadingScreen_BindLua.h index e2560b1af..6dbf390b2 100644 --- a/WickedEngine/LoadingScreen_BindLua.h +++ b/WickedEngine/LoadingScreen_BindLua.h @@ -11,9 +11,15 @@ public: static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - LoadingScreen_BindLua(LoadingScreen* component = nullptr); - LoadingScreen_BindLua(lua_State *L); - ~LoadingScreen_BindLua(); + LoadingScreen_BindLua(LoadingScreen* component) + { + this->component = component; + } + LoadingScreen_BindLua(lua_State* L) + { + component = new LoadingScreen; + owning = true; + } int AddLoadingTask(lua_State* L); int OnFinished(lua_State* L); diff --git a/WickedEngine/MainComponent.cpp b/WickedEngine/MainComponent.cpp index a6827e52f..d6fe765e3 100644 --- a/WickedEngine/MainComponent.cpp +++ b/WickedEngine/MainComponent.cpp @@ -74,21 +74,19 @@ void MainComponent::Initialize() void MainComponent::ActivatePath(RenderPath* component, float fadeSeconds, wiColor fadeColor) { - if (component == nullptr) - { - return; - } - // Fade manager will activate on fadeout fadeManager.Clear(); fadeManager.Start(fadeSeconds, fadeColor, [this, component]() { - if (activePath != nullptr) + if (GetActivePath() != nullptr) { - activePath->Stop(); + GetActivePath()->Stop(); } - component->Start(); + if (component != nullptr) + { + component->Start(); + } activePath = component; }); diff --git a/WickedEngine/MainComponent.h b/WickedEngine/MainComponent.h index 182f9743f..172d6393c 100644 --- a/WickedEngine/MainComponent.h +++ b/WickedEngine/MainComponent.h @@ -10,7 +10,7 @@ class RenderPath; class MainComponent { protected: - RenderPath* activePath = nullptr; + RenderPath* activePath; float targetFrameRate = 60; bool frameskip = true; bool framerate_lock = false; diff --git a/WickedEngine/MainComponent_BindLua.cpp b/WickedEngine/MainComponent_BindLua.cpp index 2ee58fcab..1ed6ff260 100644 --- a/WickedEngine/MainComponent_BindLua.cpp +++ b/WickedEngine/MainComponent_BindLua.cpp @@ -36,10 +36,6 @@ MainComponent_BindLua::MainComponent_BindLua(lua_State *L) component = nullptr; } -MainComponent_BindLua::~MainComponent_BindLua() -{ -} - int MainComponent_BindLua::GetActivePath(lua_State *L) { if (component == nullptr) diff --git a/WickedEngine/MainComponent_BindLua.h b/WickedEngine/MainComponent_BindLua.h index 234955e00..70f290576 100644 --- a/WickedEngine/MainComponent_BindLua.h +++ b/WickedEngine/MainComponent_BindLua.h @@ -6,7 +6,7 @@ class MainComponent_BindLua { private: - MainComponent *component; + MainComponent* component = nullptr; public: static const char className[]; static Luna::FunctionType methods[]; @@ -14,7 +14,6 @@ public: MainComponent_BindLua(MainComponent* component = nullptr); MainComponent_BindLua(lua_State *L); - ~MainComponent_BindLua(); int GetActivePath(lua_State *L); int SetActivePath(lua_State *L); diff --git a/WickedEngine/Matrix_BindLua.cpp b/WickedEngine/Matrix_BindLua.cpp index cc7aec7cf..475d14055 100644 --- a/WickedEngine/Matrix_BindLua.cpp +++ b/WickedEngine/Matrix_BindLua.cpp @@ -63,10 +63,6 @@ Matrix_BindLua::Matrix_BindLua(lua_State* L) } -Matrix_BindLua::~Matrix_BindLua() -{ -} - int Matrix_BindLua::GetRow(lua_State* L) { int argc = wiLua::SGetArgCount(L); diff --git a/WickedEngine/Matrix_BindLua.h b/WickedEngine/Matrix_BindLua.h index 807f67c43..e9fff56d9 100644 --- a/WickedEngine/Matrix_BindLua.h +++ b/WickedEngine/Matrix_BindLua.h @@ -14,7 +14,6 @@ public: Matrix_BindLua(const DirectX::XMMATRIX& matrix); Matrix_BindLua(lua_State* L); - ~Matrix_BindLua(); int GetRow(lua_State* L); diff --git a/WickedEngine/RenderPath2D.cpp b/WickedEngine/RenderPath2D.cpp index db2fb2135..f287e7303 100644 --- a/WickedEngine/RenderPath2D.cpp +++ b/WickedEngine/RenderPath2D.cpp @@ -6,11 +6,6 @@ using namespace wiGraphics; -RenderPath2D::RenderPath2D() -{ - addLayer(DEFAULT_RENDERLAYER); -} - void RenderPath2D::ResizeBuffers() { RenderPath::ResizeBuffers(); @@ -110,7 +105,7 @@ void RenderPath2D::Update(float dt) { if (y.sprite != nullptr) { - y.sprite->Update(dt * getSpriteSpeed()); + y.sprite->Update(dt * GetSpriteSpeed()); } } } @@ -125,7 +120,7 @@ void RenderPath2D::FixedUpdate() { if (y.sprite != nullptr) { - y.sprite->FixedUpdate(getSpriteSpeed()); + y.sprite->FixedUpdate(GetSpriteSpeed()); } } } @@ -234,7 +229,7 @@ void RenderPath2D::Compose(CommandList cmd) const } -void RenderPath2D::addSprite(wiSprite* sprite, const std::string& layer) +void RenderPath2D::AddSprite(wiSprite* sprite, const std::string& layer) { for (auto& x : layers) { @@ -247,7 +242,7 @@ void RenderPath2D::addSprite(wiSprite* sprite, const std::string& layer) } SortLayers(); } -void RenderPath2D::removeSprite(wiSprite* sprite) +void RenderPath2D::RemoveSprite(wiSprite* sprite) { for (auto& x : layers) { @@ -261,7 +256,7 @@ void RenderPath2D::removeSprite(wiSprite* sprite) } CleanLayers(); } -void RenderPath2D::clearSprites() +void RenderPath2D::ClearSprites() { for (auto& x : layers) { @@ -272,7 +267,7 @@ void RenderPath2D::clearSprites() } CleanLayers(); } -int RenderPath2D::getSpriteOrder(wiSprite* sprite) +int RenderPath2D::GetSpriteOrder(wiSprite* sprite) { for (auto& x : layers) { @@ -287,7 +282,7 @@ int RenderPath2D::getSpriteOrder(wiSprite* sprite) return 0; } -void RenderPath2D::addFont(wiFont* font, const std::string& layer) +void RenderPath2D::AddFont(wiFont* font, const std::string& layer) { for (auto& x : layers) { @@ -300,7 +295,7 @@ void RenderPath2D::addFont(wiFont* font, const std::string& layer) } SortLayers(); } -void RenderPath2D::removeFont(wiFont* font) +void RenderPath2D::RemoveFont(wiFont* font) { for (auto& x : layers) { @@ -314,7 +309,7 @@ void RenderPath2D::removeFont(wiFont* font) } CleanLayers(); } -void RenderPath2D::clearFonts() +void RenderPath2D::ClearFonts() { for (auto& x : layers) { @@ -325,7 +320,7 @@ void RenderPath2D::clearFonts() } CleanLayers(); } -int RenderPath2D::getFontOrder(wiFont* font) +int RenderPath2D::GetFontOrder(wiFont* font) { for (auto& x : layers) { @@ -341,19 +336,20 @@ int RenderPath2D::getFontOrder(wiFont* font) } -void RenderPath2D::addLayer(const std::string& name) +void RenderPath2D::AddLayer(const std::string& name) { for (auto& x : layers) { if (!x.name.compare(name)) return; } - RenderLayer2D layer = RenderLayer2D(name); + RenderLayer2D layer; + layer.name = name; layer.order = (int)layers.size(); layers.push_back(layer); layers.back().items.clear(); } -void RenderPath2D::setLayerOrder(const std::string& name, int order) +void RenderPath2D::SetLayerOrder(const std::string& name, int order) { for (auto& x : layers) { diff --git a/WickedEngine/RenderPath2D.h b/WickedEngine/RenderPath2D.h index 833e897a3..d0b4768ea 100644 --- a/WickedEngine/RenderPath2D.h +++ b/WickedEngine/RenderPath2D.h @@ -7,8 +7,6 @@ class wiSprite; class wiFont; -static const std::string DEFAULT_RENDERLAYER = "default"; - struct RenderItem2D { enum TYPE @@ -25,8 +23,6 @@ struct RenderLayer2D std::vector items; std::string name; int order = 0; - - RenderLayer2D(const std::string& name) :name(name) {} }; class RenderPath2D : @@ -45,7 +41,6 @@ private: protected: void ResizeBuffers() override; public: - RenderPath2D(); void Initialize() override; void Load() override; @@ -59,21 +54,21 @@ public: const wiGraphics::Texture& GetRenderResult() const { return rtFinal; } virtual const wiGraphics::Texture* GetDepthStencil() const { return nullptr; } - void addSprite(wiSprite* sprite, const std::string& layer = DEFAULT_RENDERLAYER); - void removeSprite(wiSprite* sprite); - void clearSprites(); - void setSpriteSpeed(float value) { spriteSpeed = value; } - float getSpriteSpeed() { return spriteSpeed; } - int getSpriteOrder(wiSprite* sprite); + void AddSprite(wiSprite* sprite, const std::string& layer = ""); + void RemoveSprite(wiSprite* sprite); + void ClearSprites(); + void SetSpriteSpeed(float value) { spriteSpeed = value; } + float GetSpriteSpeed() { return spriteSpeed; } + int GetSpriteOrder(wiSprite* sprite); - void addFont(wiFont* font, const std::string& layer = DEFAULT_RENDERLAYER); - void removeFont(wiFont* font); - void clearFonts(); - int getFontOrder(wiFont* font); + void AddFont(wiFont* font, const std::string& layer = ""); + void RemoveFont(wiFont* font); + void ClearFonts(); + int GetFontOrder(wiFont* font); - std::vector layers; - void addLayer(const std::string& name); - void setLayerOrder(const std::string& name, int order); + std::vector layers{ 1 }; + void AddLayer(const std::string& name); + void SetLayerOrder(const std::string& name, int order); void SetSpriteOrder(wiSprite* sprite, int order); void SetFontOrder(wiFont* font, int order); void SortLayers(); diff --git a/WickedEngine/RenderPath2D_BindLua.cpp b/WickedEngine/RenderPath2D_BindLua.cpp index 3831149a8..2b421cef1 100644 --- a/WickedEngine/RenderPath2D_BindLua.cpp +++ b/WickedEngine/RenderPath2D_BindLua.cpp @@ -33,21 +33,6 @@ Luna::PropertyType RenderPath2D_BindLua::properties[] = { { NULL, NULL } }; -RenderPath2D_BindLua::RenderPath2D_BindLua(RenderPath2D* component) -{ - this->component = component; -} - -RenderPath2D_BindLua::RenderPath2D_BindLua(lua_State *L) -{ - component = new RenderPath2D(); -} - - -RenderPath2D_BindLua::~RenderPath2D_BindLua() -{ -} - int RenderPath2D_BindLua::AddSprite(lua_State *L) { if (component == nullptr) @@ -65,9 +50,9 @@ int RenderPath2D_BindLua::AddSprite(lua_State *L) if (ccomp != nullptr) { if(argc>1) - ccomp->addSprite(sprite->sprite, wiLua::SGetString(L,2)); + ccomp->AddSprite(&sprite->sprite, wiLua::SGetString(L,2)); else - ccomp->addSprite(sprite->sprite); + ccomp->AddSprite(&sprite->sprite); } else { @@ -100,9 +85,9 @@ int RenderPath2D_BindLua::AddFont(lua_State* L) if (ccomp != nullptr) { if (argc > 1) - ccomp->addFont(font->font, wiLua::SGetString(L, 2)); + ccomp->AddFont(&font->font, wiLua::SGetString(L, 2)); else - ccomp->addFont(font->font); + ccomp->AddFont(&font->font); } else { @@ -134,7 +119,7 @@ int RenderPath2D_BindLua::RemoveSprite(lua_State *L) RenderPath2D* ccomp = dynamic_cast(component); if (ccomp != nullptr) { - ccomp->removeSprite(sprite->sprite); + ccomp->RemoveSprite(&sprite->sprite); } else { @@ -166,7 +151,7 @@ int RenderPath2D_BindLua::RemoveFont(lua_State* L) RenderPath2D* ccomp = dynamic_cast(component); if (ccomp != nullptr) { - ccomp->removeFont(font->font); + ccomp->RemoveFont(&font->font); } else { @@ -192,7 +177,7 @@ int RenderPath2D_BindLua::ClearSprites(lua_State *L) RenderPath2D* ccomp = dynamic_cast(component); if (ccomp != nullptr) { - ccomp->clearSprites(); + ccomp->ClearSprites(); } else { @@ -210,7 +195,7 @@ int RenderPath2D_BindLua::ClearFonts(lua_State* L) RenderPath2D* ccomp = dynamic_cast(component); if (ccomp != nullptr) { - ccomp->clearFonts(); + ccomp->ClearFonts(); } else { @@ -234,7 +219,7 @@ int RenderPath2D_BindLua::GetSpriteOrder(lua_State* L) RenderPath2D* ccomp = dynamic_cast(component); if (ccomp != nullptr) { - wiLua::SSetInt(L, ccomp->getSpriteOrder(sprite->sprite)); + wiLua::SSetInt(L, ccomp->GetSpriteOrder(&sprite->sprite)); return 1; } else @@ -267,7 +252,7 @@ int RenderPath2D_BindLua::GetFontOrder(lua_State* L) RenderPath2D* ccomp = dynamic_cast(component); if (ccomp != nullptr) { - wiLua::SSetInt(L, ccomp->getFontOrder(font->font)); + wiLua::SSetInt(L, ccomp->GetFontOrder(&font->font)); return 1; } else @@ -298,7 +283,7 @@ int RenderPath2D_BindLua::AddLayer(lua_State* L) RenderPath2D* ccomp = dynamic_cast(component); if (ccomp != nullptr) { - ccomp->addLayer(wiLua::SGetString(L, 1)); + ccomp->AddLayer(wiLua::SGetString(L, 1)); } else { @@ -350,7 +335,7 @@ int RenderPath2D_BindLua::SetLayerOrder(lua_State* L) RenderPath2D* ccomp = dynamic_cast(component); if (ccomp != nullptr) { - ccomp->setLayerOrder(wiLua::SGetString(L, 1),wiLua::SGetInt(L,2)); + ccomp->SetLayerOrder(wiLua::SGetString(L, 1),wiLua::SGetInt(L,2)); } else { @@ -379,7 +364,7 @@ int RenderPath2D_BindLua::SetSpriteOrder(lua_State* L) wiSprite_BindLua* sprite = Luna::lightcheck(L, 1); if (sprite != nullptr) { - ccomp->SetSpriteOrder(sprite->sprite, wiLua::SGetInt(L, 2)); + ccomp->SetSpriteOrder(&sprite->sprite, wiLua::SGetInt(L, 2)); } else { @@ -413,7 +398,7 @@ int RenderPath2D_BindLua::SetFontOrder(lua_State* L) wiFont_BindLua* font = Luna::lightcheck(L, 1); if (font != nullptr) { - ccomp->SetFontOrder(font->font, wiLua::SGetInt(L, 2)); + ccomp->SetFontOrder(&font->font, wiLua::SGetInt(L, 2)); } else { diff --git a/WickedEngine/RenderPath2D_BindLua.h b/WickedEngine/RenderPath2D_BindLua.h index 07845da35..6ccd1e270 100644 --- a/WickedEngine/RenderPath2D_BindLua.h +++ b/WickedEngine/RenderPath2D_BindLua.h @@ -11,9 +11,16 @@ public: static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - RenderPath2D_BindLua(RenderPath2D* component = nullptr); - RenderPath2D_BindLua(lua_State *L); - ~RenderPath2D_BindLua(); + RenderPath2D_BindLua() = default; + RenderPath2D_BindLua(RenderPath2D* component) + { + this->component = component; + } + RenderPath2D_BindLua(lua_State* L) + { + component = new RenderPath2D; + owning = true; + } int AddSprite(lua_State *L); int AddFont(lua_State* L); diff --git a/WickedEngine/RenderPath3D_BindLua.cpp b/WickedEngine/RenderPath3D_BindLua.cpp index 42b2ebefb..4cb8ce1f7 100644 --- a/WickedEngine/RenderPath3D_BindLua.cpp +++ b/WickedEngine/RenderPath3D_BindLua.cpp @@ -54,20 +54,6 @@ Luna::PropertyType RenderPath3D_BindLua::properties[] = { { NULL, NULL } }; -RenderPath3D_BindLua::RenderPath3D_BindLua(RenderPath3D* component) -{ - this->component = component; -} - -RenderPath3D_BindLua::RenderPath3D_BindLua(lua_State* L) -{ - component = nullptr; -} - -RenderPath3D_BindLua::~RenderPath3D_BindLua() -{ -} - int RenderPath3D_BindLua::SetSSAOEnabled(lua_State* L) { diff --git a/WickedEngine/RenderPath3D_BindLua.h b/WickedEngine/RenderPath3D_BindLua.h index 0f5b725cf..ca8ce8fa6 100644 --- a/WickedEngine/RenderPath3D_BindLua.h +++ b/WickedEngine/RenderPath3D_BindLua.h @@ -11,9 +11,12 @@ public: static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - RenderPath3D_BindLua(RenderPath3D* component = nullptr); - RenderPath3D_BindLua(lua_State* L); - ~RenderPath3D_BindLua(); + RenderPath3D_BindLua() = default; + RenderPath3D_BindLua(RenderPath3D* component) + { + this->component = component; + } + RenderPath3D_BindLua(lua_State* L) {} int SetSSAOEnabled(lua_State* L); int SetSSREnabled(lua_State* L); diff --git a/WickedEngine/RenderPath3D_Deferred_BindLua.cpp b/WickedEngine/RenderPath3D_Deferred_BindLua.cpp index 72346923b..e3bea078f 100644 --- a/WickedEngine/RenderPath3D_Deferred_BindLua.cpp +++ b/WickedEngine/RenderPath3D_Deferred_BindLua.cpp @@ -52,21 +52,6 @@ Luna::PropertyType RenderPath3D_Deferred_BindLua: { NULL, NULL } }; -RenderPath3D_Deferred_BindLua::RenderPath3D_Deferred_BindLua(RenderPath3D_Deferred* component) -{ - this->component = component; -} - -RenderPath3D_Deferred_BindLua::RenderPath3D_Deferred_BindLua(lua_State *L) -{ - component = new RenderPath3D_Deferred(); -} - - -RenderPath3D_Deferred_BindLua::~RenderPath3D_Deferred_BindLua() -{ -} - void RenderPath3D_Deferred_BindLua::Bind() { static bool initialized = false; diff --git a/WickedEngine/RenderPath3D_Deferred_BindLua.h b/WickedEngine/RenderPath3D_Deferred_BindLua.h index 95155a2eb..b6d692915 100644 --- a/WickedEngine/RenderPath3D_Deferred_BindLua.h +++ b/WickedEngine/RenderPath3D_Deferred_BindLua.h @@ -11,9 +11,16 @@ public: static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - RenderPath3D_Deferred_BindLua(RenderPath3D_Deferred* component = nullptr); - RenderPath3D_Deferred_BindLua(lua_State *L); - ~RenderPath3D_Deferred_BindLua(); + RenderPath3D_Deferred_BindLua() = default; + RenderPath3D_Deferred_BindLua(RenderPath3D_Deferred* component) + { + this->component = component; + } + RenderPath3D_Deferred_BindLua(lua_State* L) + { + component = new RenderPath3D_Deferred; + owning = true; + } static void Bind(); }; diff --git a/WickedEngine/RenderPath3D_Forward_BindLua.cpp b/WickedEngine/RenderPath3D_Forward_BindLua.cpp index 6394a2b25..8eb60683a 100644 --- a/WickedEngine/RenderPath3D_Forward_BindLua.cpp +++ b/WickedEngine/RenderPath3D_Forward_BindLua.cpp @@ -52,27 +52,12 @@ Luna::PropertyType RenderPath3D_Forward_BindLua::p { NULL, NULL } }; -RenderPath3D_Forward_BindLua::RenderPath3D_Forward_BindLua(RenderPath3D_Forward* component) -{ - this->component = component; -} - -RenderPath3D_Forward_BindLua::RenderPath3D_Forward_BindLua(lua_State *L) -{ - component = new RenderPath3D_Forward(); -} - - -RenderPath3D_Forward_BindLua::~RenderPath3D_Forward_BindLua() -{ -} - void RenderPath3D_Forward_BindLua::Bind() { static bool initialized = false; if (!initialized) { initialized = true; - //Luna::Register(wiLua::GetGlobal()->GetLuaState()); + Luna::Register(wiLua::GetGlobal()->GetLuaState()); } } diff --git a/WickedEngine/RenderPath3D_Forward_BindLua.h b/WickedEngine/RenderPath3D_Forward_BindLua.h index 9f809b636..1074e8ceb 100644 --- a/WickedEngine/RenderPath3D_Forward_BindLua.h +++ b/WickedEngine/RenderPath3D_Forward_BindLua.h @@ -11,9 +11,16 @@ public: static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - RenderPath3D_Forward_BindLua(RenderPath3D_Forward* component = nullptr); - RenderPath3D_Forward_BindLua(lua_State *L); - ~RenderPath3D_Forward_BindLua(); + RenderPath3D_Forward_BindLua() = default; + RenderPath3D_Forward_BindLua(RenderPath3D_Forward* component) + { + this->component = component; + } + RenderPath3D_Forward_BindLua(lua_State* L) + { + component = new RenderPath3D_Forward; + owning = true; + } static void Bind(); }; diff --git a/WickedEngine/RenderPath3D_TiledDeferred_BindLua.cpp b/WickedEngine/RenderPath3D_TiledDeferred_BindLua.cpp index e35431767..263642fb4 100644 --- a/WickedEngine/RenderPath3D_TiledDeferred_BindLua.cpp +++ b/WickedEngine/RenderPath3D_TiledDeferred_BindLua.cpp @@ -52,27 +52,12 @@ Luna::PropertyType RenderPath3D_TiledDeferre { NULL, NULL } }; -RenderPath3D_TiledDeferred_BindLua::RenderPath3D_TiledDeferred_BindLua(RenderPath3D_TiledDeferred* component) -{ - this->component = component; -} - -RenderPath3D_TiledDeferred_BindLua::RenderPath3D_TiledDeferred_BindLua(lua_State *L) -{ - component = new RenderPath3D_TiledDeferred(); -} - - -RenderPath3D_TiledDeferred_BindLua::~RenderPath3D_TiledDeferred_BindLua() -{ -} - void RenderPath3D_TiledDeferred_BindLua::Bind() { static bool initialized = false; if (!initialized) { initialized = true; - //Luna::Register(wiLua::GetGlobal()->GetLuaState()); + Luna::Register(wiLua::GetGlobal()->GetLuaState()); } } diff --git a/WickedEngine/RenderPath3D_TiledDeferred_BindLua.h b/WickedEngine/RenderPath3D_TiledDeferred_BindLua.h index fbcaf69d8..b04aac6b9 100644 --- a/WickedEngine/RenderPath3D_TiledDeferred_BindLua.h +++ b/WickedEngine/RenderPath3D_TiledDeferred_BindLua.h @@ -11,9 +11,16 @@ public: static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - RenderPath3D_TiledDeferred_BindLua(RenderPath3D_TiledDeferred* component = nullptr); - RenderPath3D_TiledDeferred_BindLua(lua_State *L); - ~RenderPath3D_TiledDeferred_BindLua(); + RenderPath3D_TiledDeferred_BindLua() = default; + RenderPath3D_TiledDeferred_BindLua(RenderPath3D_TiledDeferred* component) + { + this->component = component; + } + RenderPath3D_TiledDeferred_BindLua(lua_State* L) + { + component = new RenderPath3D_TiledDeferred; + owning = true; + } static void Bind(); }; diff --git a/WickedEngine/RenderPath3D_TiledForward_BindLua.cpp b/WickedEngine/RenderPath3D_TiledForward_BindLua.cpp index 0d482325b..20fcf4f69 100644 --- a/WickedEngine/RenderPath3D_TiledForward_BindLua.cpp +++ b/WickedEngine/RenderPath3D_TiledForward_BindLua.cpp @@ -52,21 +52,6 @@ Luna::PropertyType RenderPath3D_TiledForward_ { NULL, NULL } }; -RenderPath3D_TiledForward_BindLua::RenderPath3D_TiledForward_BindLua(RenderPath3D_TiledForward* component) -{ - this->component = component; -} - -RenderPath3D_TiledForward_BindLua::RenderPath3D_TiledForward_BindLua(lua_State *L) -{ - component = new RenderPath3D_TiledForward(); -} - - -RenderPath3D_TiledForward_BindLua::~RenderPath3D_TiledForward_BindLua() -{ -} - void RenderPath3D_TiledForward_BindLua::Bind() { static bool initialized = false; diff --git a/WickedEngine/RenderPath3D_TiledForward_BindLua.h b/WickedEngine/RenderPath3D_TiledForward_BindLua.h index be47d0612..f3124f89e 100644 --- a/WickedEngine/RenderPath3D_TiledForward_BindLua.h +++ b/WickedEngine/RenderPath3D_TiledForward_BindLua.h @@ -11,9 +11,16 @@ public: static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - RenderPath3D_TiledForward_BindLua(RenderPath3D_TiledForward* component = nullptr); - RenderPath3D_TiledForward_BindLua(lua_State *L); - ~RenderPath3D_TiledForward_BindLua(); + RenderPath3D_TiledForward_BindLua() = default; + RenderPath3D_TiledForward_BindLua(RenderPath3D_TiledForward* component) + { + this->component = component; + } + RenderPath3D_TiledForward_BindLua(lua_State* L) + { + component = new RenderPath3D_TiledForward; + owning = true; + } static void Bind(); }; diff --git a/WickedEngine/RenderPath_BindLua.cpp b/WickedEngine/RenderPath_BindLua.cpp index 4e3477356..ffff7cdaa 100644 --- a/WickedEngine/RenderPath_BindLua.cpp +++ b/WickedEngine/RenderPath_BindLua.cpp @@ -16,19 +16,6 @@ Luna::PropertyType RenderPath_BindLua::properties[] = { { NULL, NULL } }; -RenderPath_BindLua::RenderPath_BindLua(RenderPath* component) :component(component) -{ -} - -RenderPath_BindLua::RenderPath_BindLua(lua_State *L) -{ - component = new RenderPath(); -} - - -RenderPath_BindLua::~RenderPath_BindLua() -{ -} int RenderPath_BindLua::Initialize(lua_State* L) { diff --git a/WickedEngine/RenderPath_BindLua.h b/WickedEngine/RenderPath_BindLua.h index 82b7d6532..dbddb1dbb 100644 --- a/WickedEngine/RenderPath_BindLua.h +++ b/WickedEngine/RenderPath_BindLua.h @@ -6,14 +6,22 @@ class RenderPath_BindLua { public: - RenderPath* component; + RenderPath* component = nullptr; + bool owning = false; static const char className[]; static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - RenderPath_BindLua(RenderPath* component = nullptr); - RenderPath_BindLua(lua_State *L); - ~RenderPath_BindLua(); + RenderPath_BindLua() = default; + RenderPath_BindLua(RenderPath* component) :component(component) {} + RenderPath_BindLua(lua_State* L) {} + virtual ~RenderPath_BindLua() + { + if (owning) + { + delete component; + } + } virtual int Initialize(lua_State* L); diff --git a/WickedEngine/SpriteAnim_BindLua.cpp b/WickedEngine/SpriteAnim_BindLua.cpp index e22cb7a6e..99ccd2549 100644 --- a/WickedEngine/SpriteAnim_BindLua.cpp +++ b/WickedEngine/SpriteAnim_BindLua.cpp @@ -40,9 +40,6 @@ SpriteAnim_BindLua::SpriteAnim_BindLua(lua_State *L) anim = wiSprite::Anim(); } -SpriteAnim_BindLua::~SpriteAnim_BindLua() -{ -} int SpriteAnim_BindLua::SetRot(lua_State *L) diff --git a/WickedEngine/SpriteAnim_BindLua.h b/WickedEngine/SpriteAnim_BindLua.h index 378c95e03..ded128b6e 100644 --- a/WickedEngine/SpriteAnim_BindLua.h +++ b/WickedEngine/SpriteAnim_BindLua.h @@ -14,7 +14,6 @@ public: SpriteAnim_BindLua(const wiSprite::Anim& anim); SpriteAnim_BindLua(lua_State *L); - ~SpriteAnim_BindLua(); int SetRot(lua_State *L); int SetRotation(lua_State *L); diff --git a/WickedEngine/Texture_BindLua.cpp b/WickedEngine/Texture_BindLua.cpp index c1bdd107c..2b664f183 100644 --- a/WickedEngine/Texture_BindLua.cpp +++ b/WickedEngine/Texture_BindLua.cpp @@ -11,14 +11,10 @@ Luna::PropertyType Texture_BindLua::properties[] = { { NULL, NULL } }; -Texture_BindLua::Texture_BindLua(Texture* texture) :texture(texture) +Texture_BindLua::Texture_BindLua(Texture texture) :texture(texture) { } Texture_BindLua::Texture_BindLua(lua_State *L) -{ - texture = nullptr; -} -Texture_BindLua::~Texture_BindLua() { } diff --git a/WickedEngine/Texture_BindLua.h b/WickedEngine/Texture_BindLua.h index c778a92b2..23c6d69af 100644 --- a/WickedEngine/Texture_BindLua.h +++ b/WickedEngine/Texture_BindLua.h @@ -6,15 +6,15 @@ class Texture_BindLua { public: - wiGraphics::Texture* texture; + wiGraphics::Texture texture; static const char className[]; static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - Texture_BindLua(wiGraphics::Texture* texture = nullptr); + Texture_BindLua() = default; + Texture_BindLua(wiGraphics::Texture texture); Texture_BindLua(lua_State *L); - ~Texture_BindLua(); static void Bind(); }; diff --git a/WickedEngine/Vector_BindLua.cpp b/WickedEngine/Vector_BindLua.cpp index a16447021..a4b5975f1 100644 --- a/WickedEngine/Vector_BindLua.cpp +++ b/WickedEngine/Vector_BindLua.cpp @@ -66,9 +66,6 @@ Vector_BindLua::Vector_BindLua(lua_State* L) vector = XMVectorSet(x, y, z, w); } -Vector_BindLua::~Vector_BindLua() -{ -} int Vector_BindLua::GetX(lua_State* L) diff --git a/WickedEngine/Vector_BindLua.h b/WickedEngine/Vector_BindLua.h index c9f7d51ee..22038ee05 100644 --- a/WickedEngine/Vector_BindLua.h +++ b/WickedEngine/Vector_BindLua.h @@ -15,7 +15,6 @@ public: Vector_BindLua(const DirectX::XMVECTOR& vector); Vector_BindLua(lua_State* L); - ~Vector_BindLua(); int GetX(lua_State* L); int GetY(lua_State* L); diff --git a/WickedEngine/wiArchive.cpp b/WickedEngine/wiArchive.cpp index c628e2c91..c81b79420 100644 --- a/WickedEngine/wiArchive.cpp +++ b/WickedEngine/wiArchive.cpp @@ -26,10 +26,10 @@ wiArchive::wiArchive(const std::string& fileName, bool readMode) : fileName(file ifstream file(fileName, ios::binary | ios::ate); if (file.is_open()) { - dataSize = (size_t)file.tellg(); + size_t dataSize = (size_t)file.tellg(); file.seekg(0, file.beg); - DATA = new uint8_t[(size_t)dataSize]; - file.read((char*)DATA, dataSize); + DATA.resize((size_t)dataSize); + file.read((char*)DATA.data(), dataSize); file.close(); (*this) >> version; if (version < __archiveVersionBarrier) @@ -56,20 +56,13 @@ wiArchive::wiArchive(const std::string& fileName, bool readMode) : fileName(file } } - -wiArchive::~wiArchive() -{ - Close(); -} - void wiArchive::CreateEmpty() { readMode = false; pos = 0; version = __archiveVersion; - dataSize = 128; // this will grow if necessary anyway... - DATA = new uint8_t[dataSize]; + DATA.resize(128); // starting size (*this) << version; } @@ -91,7 +84,7 @@ void wiArchive::SetReadModeAndResetPos(bool isReadMode) bool wiArchive::IsOpen() { // when it is open, DATA is not null because it contains the version number at least! - return DATA != nullptr; + return !DATA.empty(); } void wiArchive::Close() @@ -100,7 +93,7 @@ void wiArchive::Close() { SaveFile(fileName); } - SAFE_DELETE_ARRAY(DATA); + DATA.clear(); } bool wiArchive::SaveFile(const std::string& fileName) @@ -113,7 +106,7 @@ bool wiArchive::SaveFile(const std::string& fileName) ofstream file(fileName, ios::binary | ios::trunc); if (file.is_open()) { - file.write((const char*)DATA, (streamsize)pos); + file.write((const char*)DATA.data(), (streamsize)pos); file.close(); return true; } diff --git a/WickedEngine/wiArchive.h b/WickedEngine/wiArchive.h index 187357907..8dd3e1303 100644 --- a/WickedEngine/wiArchive.h +++ b/WickedEngine/wiArchive.h @@ -11,8 +11,7 @@ private: uint64_t version = 0; bool readMode = false; size_t pos = 0; - uint8_t* DATA = nullptr; - size_t dataSize = 0; + std::vector DATA; std::string fileName; // save to this file on closing if not empty @@ -21,11 +20,16 @@ private: public: // Create empty arhive for writing wiArchive(); + wiArchive(const wiArchive&) = default; + wiArchive(wiArchive&&) = default; // Create archive and link to file wiArchive(const std::string& fileName, bool readMode = true); - ~wiArchive(); + ~wiArchive() { Close(); } - const uint8_t* GetData() const { return DATA; } + wiArchive& operator=(const wiArchive&) = default; + wiArchive& operator=(wiArchive&&) = default; + + const uint8_t* GetData() const { return DATA.data(); } size_t GetSize() const { return pos; } uint64_t GetVersion() const { return version; } bool IsReadMode() const { return readMode; } @@ -318,15 +322,11 @@ private: { size_t _size = (size_t)(sizeof(data)*count); size_t _right = pos + _size; - if (_right > dataSize) + if (_right > DATA.size()) { - uint8_t* NEWDATA = new uint8_t[_right * 2]; - memcpy(NEWDATA, DATA, dataSize); - dataSize = _right * 2; - SAFE_DELETE_ARRAY(DATA); - DATA = NEWDATA; + DATA.resize(_right * 2); } - memcpy(reinterpret_cast((uint64_t)DATA + (uint64_t)pos), &data, _size); + memcpy(reinterpret_cast((uint64_t)DATA.data() + (uint64_t)pos), &data, _size); pos = _right; } @@ -334,7 +334,7 @@ private: template inline void _read(T& data, uint64_t count = 1) { - memcpy(&data, reinterpret_cast((uint64_t)DATA + (uint64_t)pos), (size_t)(sizeof(data)*count)); + memcpy(&data, reinterpret_cast((uint64_t)DATA.data() + (uint64_t)pos), (size_t)(sizeof(data)*count)); pos += (size_t)(sizeof(data)*count); } }; diff --git a/WickedEngine/wiAudio_BindLua.cpp b/WickedEngine/wiAudio_BindLua.cpp index acab79bb6..c6f2caf63 100644 --- a/WickedEngine/wiAudio_BindLua.cpp +++ b/WickedEngine/wiAudio_BindLua.cpp @@ -31,7 +31,7 @@ int wiAudio_BindLua::CreateSound(lua_State* L) wiSound_BindLua* sound = Luna::lightcheck(L, 2); if (sound != nullptr) { - bool result = wiAudio::CreateSound(wiLua::SGetString(L, 1), sound->sound); + bool result = wiAudio::CreateSound(wiLua::SGetString(L, 1), &sound->sound); wiLua::SSetBool(L, result); } else @@ -52,7 +52,7 @@ int wiAudio_BindLua::CreateSoundInstance(lua_State* L) wiSoundInstance_BindLua* soundinstance = Luna::lightcheck(L, 2); if (sound != nullptr && soundinstance != nullptr) { - bool result = wiAudio::CreateSoundInstance(sound->sound, &soundinstance->soundinstance); + bool result = wiAudio::CreateSoundInstance(&sound->sound, &soundinstance->soundinstance); wiLua::SSetBool(L, result); return 1; } diff --git a/WickedEngine/wiAudio_BindLua.h b/WickedEngine/wiAudio_BindLua.h index 9d34e94c3..5694657fd 100644 --- a/WickedEngine/wiAudio_BindLua.h +++ b/WickedEngine/wiAudio_BindLua.h @@ -11,7 +11,6 @@ public: static Luna::PropertyType properties[]; wiAudio_BindLua(lua_State* L) {} - ~wiAudio_BindLua() {} int CreateSound(lua_State* L); int CreateSoundInstance(lua_State* L); @@ -34,16 +33,14 @@ public: class wiSound_BindLua { public: - wiAudio::Sound* sound = nullptr; - bool owned = false; + wiAudio::Sound sound; static const char className[]; static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - wiSound_BindLua(lua_State* L) { sound = new wiAudio::Sound; owned = true; } - wiSound_BindLua(wiAudio::Sound* sound) :sound(sound) {} - ~wiSound_BindLua() { if (owned) delete sound; } + wiSound_BindLua(lua_State* L) {} + wiSound_BindLua(const wiAudio::Sound& sound) :sound(sound) {} static void Bind(); }; @@ -69,7 +66,6 @@ class wiSoundInstance3D_BindLua { public: wiAudio::SoundInstance3D soundinstance3D; - bool owned = false; static const char className[]; static Luna::FunctionType methods[]; diff --git a/WickedEngine/wiFadeManager.cpp b/WickedEngine/wiFadeManager.cpp index 376598f51..11f9b55de 100644 --- a/WickedEngine/wiFadeManager.cpp +++ b/WickedEngine/wiFadeManager.cpp @@ -49,6 +49,7 @@ void wiFadeManager::Update(float dt) else if (state == FADE_FINISHED) { opacity = 0.0f; + onFade = [] {}; } } diff --git a/WickedEngine/wiFadeManager.h b/WickedEngine/wiFadeManager.h index 6a4e1e2dd..7c65beade 100644 --- a/WickedEngine/wiFadeManager.h +++ b/WickedEngine/wiFadeManager.h @@ -21,7 +21,7 @@ public: FADE_FINISHED, } state = FADE_FINISHED; wiColor color = wiColor(0, 0, 0, 255); - std::function onFade; + std::function onFade = [] {}; wiFadeManager() { diff --git a/WickedEngine/wiFont_BindLua.cpp b/WickedEngine/wiFont_BindLua.cpp index 3d3ad643c..81d1f88e7 100644 --- a/WickedEngine/wiFont_BindLua.cpp +++ b/WickedEngine/wiFont_BindLua.cpp @@ -31,12 +31,8 @@ Luna::PropertyType wiFont_BindLua::properties[] = { { NULL, NULL } }; -wiFont_BindLua::wiFont_BindLua(wiFont* font) +wiFont_BindLua::wiFont_BindLua(const wiFont& font) : font(font) { - if (font != nullptr) - this->font = new wiFont(*font); - else - this->font = new wiFont(); } wiFont_BindLua::wiFont_BindLua(lua_State* L) { @@ -44,13 +40,8 @@ wiFont_BindLua::wiFont_BindLua(lua_State* L) if (argc > 0) { string text = wiLua::SGetString(L, 1); - font = new wiFont(text); + font.SetText(text); } - else - font = new wiFont(); -} -wiFont_BindLua::~wiFont_BindLua() -{ } @@ -60,7 +51,7 @@ int wiFont_BindLua::SetStyle(lua_State* L) if (argc > 0) { string name = wiLua::SGetString(L, 1); - font->style = wiFont::AddFontStyle(name); + font.style = wiFont::AddFontStyle(name); } else { @@ -72,9 +63,9 @@ int wiFont_BindLua::SetText(lua_State* L) { int argc = wiLua::SGetArgCount(L); if (argc > 0) - font->SetText(wiLua::SGetString(L, 1)); + font.SetText(wiLua::SGetString(L, 1)); else - font->SetText(""); + font.SetText(""); return 0; } int wiFont_BindLua::SetSize(lua_State* L) @@ -82,7 +73,7 @@ int wiFont_BindLua::SetSize(lua_State* L) int argc = wiLua::SGetArgCount(L); if (argc > 0) { - font->params.size = wiLua::SGetInt(L, 1); + font.params.size = wiLua::SGetInt(L, 1); } else wiLua::SError(L, "SetSize(int size) not enough arguments!"); @@ -96,8 +87,8 @@ int wiFont_BindLua::SetPos(lua_State* L) Vector_BindLua* param = Luna::lightcheck(L, 1); if (param != nullptr) { - font->params.posX = (int)XMVectorGetX(param->vector); - font->params.posY = (int)XMVectorGetY(param->vector); + font.params.posX = (int)XMVectorGetX(param->vector); + font.params.posY = (int)XMVectorGetY(param->vector); } else wiLua::SError(L, "SetPos(Vector pos) argument is not a vector!"); @@ -114,8 +105,8 @@ int wiFont_BindLua::SetSpacing(lua_State* L) Vector_BindLua* param = Luna::lightcheck(L, 1); if (param != nullptr) { - font->params.spacingX = (int)XMVectorGetX(param->vector); - font->params.spacingY = (int)XMVectorGetY(param->vector); + font.params.spacingX = (int)XMVectorGetX(param->vector); + font.params.spacingY = (int)XMVectorGetY(param->vector); } else wiLua::SError(L, "SetSpacing(Vector spacing) argument is not a vector!"); @@ -129,10 +120,10 @@ int wiFont_BindLua::SetAlign(lua_State* L) int argc = wiLua::SGetArgCount(L); if (argc > 0) { - font->params.h_align = (wiFontAlign)wiLua::SGetInt(L, 1); + font.params.h_align = (wiFontAlign)wiLua::SGetInt(L, 1); if (argc > 1) { - font->params.v_align = (wiFontAlign)wiLua::SGetInt(L, 2); + font.params.v_align = (wiFontAlign)wiLua::SGetInt(L, 2); } } else @@ -149,12 +140,12 @@ int wiFont_BindLua::SetColor(lua_State* L) { XMFLOAT4 unpacked; XMStoreFloat4(&unpacked, param->vector); - font->params.color = wiColor::fromFloat4(unpacked); + font.params.color = wiColor::fromFloat4(unpacked); } else { int code = wiLua::SGetInt(L, 1); - font->params.color.rgba = (uint32_t)code; + font.params.color.rgba = (uint32_t)code; } } else @@ -171,12 +162,12 @@ int wiFont_BindLua::SetShadowColor(lua_State* L) { XMFLOAT4 unpacked; XMStoreFloat4(&unpacked, param->vector); - font->params.shadowColor = wiColor::fromFloat4(unpacked); + font.params.shadowColor = wiColor::fromFloat4(unpacked); } else { int code = wiLua::SGetInt(L, 1); - font->params.shadowColor.rgba = (uint32_t)code; + font.params.shadowColor.rgba = (uint32_t)code; } } else @@ -186,38 +177,38 @@ int wiFont_BindLua::SetShadowColor(lua_State* L) int wiFont_BindLua::GetText(lua_State* L) { - wiLua::SSetString(L, font->GetTextA()); + wiLua::SSetString(L, font.GetTextA()); return 1; } int wiFont_BindLua::GetSize(lua_State* L) { - wiLua::SSetInt(L, font->params.size); + wiLua::SSetInt(L, font.params.size); return 1; } int wiFont_BindLua::GetPos(lua_State* L) { - Luna::push(L, new Vector_BindLua(XMVectorSet((float)font->params.posX, (float)font->params.posY, 0, 0))); + Luna::push(L, new Vector_BindLua(XMVectorSet((float)font.params.posX, (float)font.params.posY, 0, 0))); return 1; } int wiFont_BindLua::GetSpacing(lua_State* L) { - Luna::push(L, new Vector_BindLua(XMVectorSet((float)font->params.spacingX, (float)font->params.spacingY, 0, 0))); + Luna::push(L, new Vector_BindLua(XMVectorSet((float)font.params.spacingX, (float)font.params.spacingY, 0, 0))); return 1; } int wiFont_BindLua::GetAlign(lua_State* L) { - wiLua::SSetInt(L, font->params.h_align); - wiLua::SSetInt(L, font->params.v_align); + wiLua::SSetInt(L, font.params.h_align); + wiLua::SSetInt(L, font.params.v_align); return 2; } int wiFont_BindLua::GetColor(lua_State* L) { - Luna::push(L, new Vector_BindLua(XMLoadFloat4(&font->params.color.toFloat4()))); + Luna::push(L, new Vector_BindLua(XMLoadFloat4(&font.params.color.toFloat4()))); return 1; } int wiFont_BindLua::GetShadowColor(lua_State* L) { - Luna::push(L, new Vector_BindLua(XMLoadFloat4(&font->params.color.toFloat4()))); + Luna::push(L, new Vector_BindLua(XMLoadFloat4(&font.params.color.toFloat4()))); return 1; } diff --git a/WickedEngine/wiFont_BindLua.h b/WickedEngine/wiFont_BindLua.h index 131ab4d4f..c16ae32e0 100644 --- a/WickedEngine/wiFont_BindLua.h +++ b/WickedEngine/wiFont_BindLua.h @@ -1,21 +1,19 @@ #pragma once #include "wiLua.h" #include "wiLuna.h" - -class wiFont; +#include "wiFont.h" class wiFont_BindLua { public: - wiFont* font; + wiFont font; static const char className[]; static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - wiFont_BindLua(wiFont* font); + wiFont_BindLua(const wiFont& font); wiFont_BindLua(lua_State* L); - ~wiFont_BindLua(); int SetStyle(lua_State* L); int SetText(lua_State* L); diff --git a/WickedEngine/wiGUI.cpp b/WickedEngine/wiGUI.cpp index c7f5eef79..da7e3ed5c 100644 --- a/WickedEngine/wiGUI.cpp +++ b/WickedEngine/wiGUI.cpp @@ -49,20 +49,16 @@ void wiGUIElement::ApplyScissor(const Rect rect, CommandList cmd, bool constrain wiRenderer::GetDevice()->BindScissorRects(1, &scissor, cmd); } -wiGUI::wiGUI() : activeWidget(nullptr), focus(false), visible(true), pointerpos(XMFLOAT2(0,0)) -{ - SetDirty(); - scale_local.x = (float)wiRenderer::GetDevice()->GetScreenWidth(); - scale_local.y = (float)wiRenderer::GetDevice()->GetScreenHeight(); - UpdateTransform(); -} wiGUI::~wiGUI() { + for (auto& widget : widgets) + { + delete widget; + } } - void wiGUI::Update(float dt) { if (!visible) @@ -70,13 +66,7 @@ void wiGUI::Update(float dt) return; } - if (wiRenderer::GetDevice()->ResolutionChanged()) - { - SetDirty(); - scale_local.x = (float)wiRenderer::GetDevice()->GetScreenWidth(); - scale_local.y = (float)wiRenderer::GetDevice()->GetScreenHeight(); - UpdateTransform(); - } + SetSize((float)wiRenderer::GetDevice()->GetScreenWidth(), (float)wiRenderer::GetDevice()->GetScreenHeight()); XMFLOAT4 _p = wiInput::GetPointer(); pointerpos.x = _p.x; diff --git a/WickedEngine/wiGUI.h b/WickedEngine/wiGUI.h index f6af6e29e..41aa1826e 100644 --- a/WickedEngine/wiGUI.h +++ b/WickedEngine/wiGUI.h @@ -27,13 +27,15 @@ class wiGUI : public wiGUIElement friend class wiWidget; private: std::list widgets; - wiWidget* activeWidget; - bool focus; - bool visible; - - XMFLOAT2 pointerpos; + wiWidget* activeWidget = nullptr; + bool focus = false; + bool visible = true; + XMFLOAT2 pointerpos = XMFLOAT2(0, 0); public: - wiGUI(); + wiGUI() + { + SetSize(1920, 1080); // default size + } ~wiGUI(); void Update(float dt); @@ -55,6 +57,14 @@ public: void SetVisible(bool value) { visible = value; } bool IsVisible() { return visible; } + XMFLOAT2 GetSize() const { return XMFLOAT2(scale_local.x, scale_local.y); } + void SetSize(float width, float height) + { + SetDirty(); + scale_local.x = width; + scale_local.y = height; + UpdateTransform(); + } const XMFLOAT2& GetPointerPos() const { diff --git a/WickedEngine/wiGraphicsDevice_DX11.cpp b/WickedEngine/wiGraphicsDevice_DX11.cpp index 7495f5c27..4ca233596 100644 --- a/WickedEngine/wiGraphicsDevice_DX11.cpp +++ b/WickedEngine/wiGraphicsDevice_DX11.cpp @@ -1078,7 +1078,6 @@ namespace DX11_Internal struct Resource_DX11 { - std::shared_ptr device; ComPtr resource; ComPtr srv; ComPtr uav; @@ -1094,62 +1093,50 @@ namespace DX11_Internal }; struct InputLayout_DX11 { - std::shared_ptr device; ComPtr resource; }; struct VertexShader_DX11 { - std::shared_ptr device; ComPtr resource; }; struct HullShader_DX11 { - std::shared_ptr device; ComPtr resource; }; struct DomainShader_DX11 { - std::shared_ptr device; ComPtr resource; }; struct GeometryShader_DX11 { - std::shared_ptr device; ComPtr resource; }; struct PixelShader_DX11 { - std::shared_ptr device; ComPtr resource; }; struct ComputeShader_DX11 { - std::shared_ptr device; ComPtr resource; }; struct BlendState_DX11 { - std::shared_ptr device; ComPtr resource; }; struct DepthStencilState_DX11 { - std::shared_ptr device; ComPtr resource; }; struct RasterizerState_DX11 { - std::shared_ptr device; ComPtr resource; }; struct Sampler_DX11 { - std::shared_ptr device; ComPtr resource; }; struct Query_DX11 { - std::shared_ptr device; ComPtr resource; }; @@ -1341,6 +1328,7 @@ GraphicsDevice_DX11::GraphicsDevice_DX11(wiPlatform::window_type window, bool fu CreateBackBufferResources(); + emptyresource = std::make_shared(); wiBackLog::post("Created GraphicsDevice_DX11"); } @@ -1349,7 +1337,7 @@ void GraphicsDevice_DX11::CreateBackBufferResources() { HRESULT hr; - hr = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer); + hr = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), &backBuffer); if (FAILED(hr)) { wiHelper::messageBox("BackBuffer creation Failed!", "Error!"); exit(1); @@ -1400,7 +1388,6 @@ Texture GraphicsDevice_DX11::GetBackBuffer() bool GraphicsDevice_DX11::CreateBuffer(const GPUBufferDesc *pDesc, const SubresourceData* pInitialData, GPUBuffer *pBuffer) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pBuffer->internal_state = internal_state; pBuffer->type = GPUResource::GPU_RESOURCE_TYPE::BUFFER; @@ -1503,7 +1490,6 @@ bool GraphicsDevice_DX11::CreateBuffer(const GPUBufferDesc *pDesc, const Subreso bool GraphicsDevice_DX11::CreateTexture(const TextureDesc* pDesc, const SubresourceData *pInitialData, Texture *pTexture) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pTexture->internal_state = internal_state; pTexture->type = GPUResource::GPU_RESOURCE_TYPE::TEXTURE; @@ -1578,7 +1564,6 @@ bool GraphicsDevice_DX11::CreateTexture(const TextureDesc* pDesc, const Subresou bool GraphicsDevice_DX11::CreateInputLayout(const InputLayoutDesc *pInputElementDescs, uint32_t NumElements, const Shader* shader, InputLayout *pInputLayout) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pInputLayout->internal_state = internal_state; pInputLayout->desc.reserve((size_t)NumElements); @@ -1616,7 +1601,6 @@ bool GraphicsDevice_DX11::CreateShader(SHADERSTAGE stage, const void *pShaderByt case wiGraphics::VS: { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pShader->internal_state = internal_state; hr = device->CreateVertexShader(pShaderBytecode, BytecodeLength, nullptr, &internal_state->resource); } @@ -1624,7 +1608,6 @@ bool GraphicsDevice_DX11::CreateShader(SHADERSTAGE stage, const void *pShaderByt case wiGraphics::HS: { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pShader->internal_state = internal_state; hr = device->CreateHullShader(pShaderBytecode, BytecodeLength, nullptr, &internal_state->resource); } @@ -1632,7 +1615,6 @@ bool GraphicsDevice_DX11::CreateShader(SHADERSTAGE stage, const void *pShaderByt case wiGraphics::DS: { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pShader->internal_state = internal_state; hr = device->CreateDomainShader(pShaderBytecode, BytecodeLength, nullptr, &internal_state->resource); } @@ -1640,7 +1622,6 @@ bool GraphicsDevice_DX11::CreateShader(SHADERSTAGE stage, const void *pShaderByt case wiGraphics::GS: { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pShader->internal_state = internal_state; hr = device->CreateGeometryShader(pShaderBytecode, BytecodeLength, nullptr, &internal_state->resource); } @@ -1648,7 +1629,6 @@ bool GraphicsDevice_DX11::CreateShader(SHADERSTAGE stage, const void *pShaderByt case wiGraphics::PS: { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pShader->internal_state = internal_state; hr = device->CreatePixelShader(pShaderBytecode, BytecodeLength, nullptr, &internal_state->resource); } @@ -1656,7 +1636,6 @@ bool GraphicsDevice_DX11::CreateShader(SHADERSTAGE stage, const void *pShaderByt case wiGraphics::CS: { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pShader->internal_state = internal_state; hr = device->CreateComputeShader(pShaderBytecode, BytecodeLength, nullptr, &internal_state->resource); } @@ -1670,7 +1649,6 @@ bool GraphicsDevice_DX11::CreateShader(SHADERSTAGE stage, const void *pShaderByt bool GraphicsDevice_DX11::CreateBlendState(const BlendStateDesc *pBlendStateDesc, BlendState *pBlendState) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pBlendState->internal_state = internal_state; D3D11_BLEND_DESC desc; @@ -1697,7 +1675,6 @@ bool GraphicsDevice_DX11::CreateBlendState(const BlendStateDesc *pBlendStateDesc bool GraphicsDevice_DX11::CreateDepthStencilState(const DepthStencilStateDesc *pDepthStencilStateDesc, DepthStencilState *pDepthStencilState) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pDepthStencilState->internal_state = internal_state; D3D11_DEPTH_STENCIL_DESC desc; @@ -1725,7 +1702,6 @@ bool GraphicsDevice_DX11::CreateDepthStencilState(const DepthStencilStateDesc *p bool GraphicsDevice_DX11::CreateRasterizerState(const RasterizerStateDesc *pRasterizerStateDesc, RasterizerState *pRasterizerState) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pRasterizerState->internal_state = internal_state; pRasterizerState->desc = *pRasterizerStateDesc; @@ -1811,7 +1787,6 @@ bool GraphicsDevice_DX11::CreateRasterizerState(const RasterizerStateDesc *pRast bool GraphicsDevice_DX11::CreateSampler(const SamplerDesc *pSamplerDesc, Sampler *pSamplerState) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pSamplerState->internal_state = internal_state; D3D11_SAMPLER_DESC desc; @@ -1838,7 +1813,6 @@ bool GraphicsDevice_DX11::CreateSampler(const SamplerDesc *pSamplerDesc, Sampler bool GraphicsDevice_DX11::CreateQuery(const GPUQueryDesc *pDesc, GPUQuery *pQuery) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); pQuery->internal_state = internal_state; pQuery->desc = *pDesc; @@ -1874,7 +1848,7 @@ bool GraphicsDevice_DX11::CreateQuery(const GPUQueryDesc *pDesc, GPUQuery *pQuer } bool GraphicsDevice_DX11::CreatePipelineState(const PipelineStateDesc* pDesc, PipelineState* pso) { - pso->internal_state = shared_from_this(); + pso->internal_state = emptyresource; pso->desc = *pDesc; @@ -1882,7 +1856,7 @@ bool GraphicsDevice_DX11::CreatePipelineState(const PipelineStateDesc* pDesc, Pi } bool GraphicsDevice_DX11::CreateRenderPass(const RenderPassDesc* pDesc, RenderPass* renderpass) { - renderpass->internal_state = shared_from_this(); + renderpass->internal_state = emptyresource; renderpass->desc = *pDesc; @@ -2390,6 +2364,7 @@ void GraphicsDevice_DX11::PresentEnd(CommandList cmd) { deviceContexts[cmd]->FinishCommandList(false, &commandLists[cmd]); immediateContext->ExecuteCommandList(commandLists[cmd].Get(), false); + commandLists[cmd].Reset(); free_commandlists.push_back(cmd); } @@ -2450,7 +2425,6 @@ CommandList GraphicsDevice_DX11::BeginCommandList() bool success = CreateBuffer(&frameAllocatorDesc, nullptr, &frame_allocators[cmd].buffer); assert(success); SetName(&frame_allocators[cmd].buffer, "frame_allocator[deferred]"); - to_internal(&frame_allocators[cmd].buffer)->device.reset(); // because this is member of device, we don't keep alive device from this } diff --git a/WickedEngine/wiGraphicsDevice_DX11.h b/WickedEngine/wiGraphicsDevice_DX11.h index ed975d0e1..26107f077 100644 --- a/WickedEngine/wiGraphicsDevice_DX11.h +++ b/WickedEngine/wiGraphicsDevice_DX11.h @@ -13,7 +13,7 @@ namespace wiGraphics { - class GraphicsDevice_DX11 : public GraphicsDevice, public std::enable_shared_from_this + class GraphicsDevice_DX11 : public GraphicsDevice { private: D3D_DRIVER_TYPE driverType; @@ -65,6 +65,9 @@ namespace wiGraphics wiContainers::ThreadSafeRingBuffer free_commandlists; wiContainers::ThreadSafeRingBuffer active_commandlists; + struct EmptyResourceHandle {}; // only care about control-block + std::shared_ptr emptyresource; + public: GraphicsDevice_DX11(wiPlatform::window_type window, bool fullscreen = false, bool debuglayer = false); diff --git a/WickedEngine/wiGraphicsDevice_DX12.cpp b/WickedEngine/wiGraphicsDevice_DX12.cpp index ae6e04b49..6f27585a0 100644 --- a/WickedEngine/wiGraphicsDevice_DX12.cpp +++ b/WickedEngine/wiGraphicsDevice_DX12.cpp @@ -909,7 +909,7 @@ namespace DX12_Internal struct Resource_DX12 { - std::shared_ptr device; + std::shared_ptr allocationhandler; D3D12MA::Allocation* allocation = nullptr; ComPtr resource; D3D12_CPU_DESCRIPTOR_HANDLE cbv; @@ -920,22 +920,22 @@ namespace DX12_Internal virtual ~Resource_DX12() { - uint64_t framecount = device->GetFrameCount(); - device->destroylocker.lock(); - if (allocation) device->destroyer_allocations.push_back(std::make_pair(allocation, framecount)); - if (resource) device->destroyer_resources.push_back(std::make_pair(resource, framecount)); - if (cbv.ptr) device->destroyer_resourceviews.push_back(std::make_pair(cbv, framecount)); - if (srv.ptr) device->destroyer_resourceviews.push_back(std::make_pair(srv, framecount)); - if (uav.ptr) device->destroyer_resourceviews.push_back(std::make_pair(uav, framecount)); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; + if (allocation) allocationhandler->destroyer_allocations.push_back(std::make_pair(allocation, framecount)); + if (resource) allocationhandler->destroyer_resources.push_back(std::make_pair(resource, framecount)); + if (cbv.ptr) allocationhandler->destroyer_resourceviews.push_back(std::make_pair(cbv, framecount)); + if (srv.ptr) allocationhandler->destroyer_resourceviews.push_back(std::make_pair(srv, framecount)); + if (uav.ptr) allocationhandler->destroyer_resourceviews.push_back(std::make_pair(uav, framecount)); for (auto x : subresources_srv) { - device->destroyer_resourceviews.push_back(std::make_pair(x, framecount)); + allocationhandler->destroyer_resourceviews.push_back(std::make_pair(x, framecount)); } for (auto x : subresources_uav) { - device->destroyer_resourceviews.push_back(std::make_pair(x, framecount)); + allocationhandler->destroyer_resourceviews.push_back(std::make_pair(x, framecount)); } - device->destroylocker.unlock(); + allocationhandler->destroylocker.unlock(); } }; struct Texture_DX12 : public Resource_DX12 @@ -947,71 +947,71 @@ namespace DX12_Internal ~Texture_DX12() override { - uint64_t framecount = device->GetFrameCount(); - device->destroylocker.lock(); - if (rtv.ptr) device->destroyer_rtvs.push_back(std::make_pair(rtv, framecount)); - if (dsv.ptr) device->destroyer_dsvs.push_back(std::make_pair(dsv, framecount)); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; + if (rtv.ptr) allocationhandler->destroyer_rtvs.push_back(std::make_pair(rtv, framecount)); + if (dsv.ptr) allocationhandler->destroyer_dsvs.push_back(std::make_pair(dsv, framecount)); for (auto x : subresources_rtv) { - device->destroyer_rtvs.push_back(std::make_pair(x, framecount)); + allocationhandler->destroyer_rtvs.push_back(std::make_pair(x, framecount)); } for (auto x : subresources_dsv) { - device->destroyer_dsvs.push_back(std::make_pair(x, framecount)); + allocationhandler->destroyer_dsvs.push_back(std::make_pair(x, framecount)); } - device->destroylocker.unlock(); + allocationhandler->destroylocker.unlock(); } }; struct Sampler_DX12 { - std::shared_ptr device; + std::shared_ptr allocationhandler; D3D12_CPU_DESCRIPTOR_HANDLE descriptor; ~Sampler_DX12() { - uint64_t framecount = device->GetFrameCount(); - device->destroylocker.lock(); - if (descriptor.ptr) device->destroyer_samplers.push_back(std::make_pair(descriptor, framecount)); - device->destroylocker.unlock(); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; + if (descriptor.ptr) allocationhandler->destroyer_samplers.push_back(std::make_pair(descriptor, framecount)); + allocationhandler->destroylocker.unlock(); } }; struct Query_DX12 { - std::shared_ptr device; + std::shared_ptr allocationhandler; GPU_QUERY_TYPE query_type = GPU_QUERY_TYPE_INVALID; uint32_t query_index = ~0; ~Query_DX12() { - uint64_t framecount = device->GetFrameCount(); if (query_index != ~0) { - device->destroylocker.lock(); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; switch (query_type) { case wiGraphics::GPU_QUERY_TYPE_OCCLUSION: case wiGraphics::GPU_QUERY_TYPE_OCCLUSION_PREDICATE: - device->destroyer_queries_occlusion.push_back(std::make_pair(query_index, framecount)); + allocationhandler->destroyer_queries_occlusion.push_back(std::make_pair(query_index, framecount)); break; case wiGraphics::GPU_QUERY_TYPE_TIMESTAMP: - device->destroyer_queries_timestamp.push_back(std::make_pair(query_index, framecount)); + allocationhandler->destroyer_queries_timestamp.push_back(std::make_pair(query_index, framecount)); break; } - device->destroylocker.unlock(); + allocationhandler->destroylocker.unlock(); } } }; struct PipelineState_DX12 { - std::shared_ptr device; + std::shared_ptr allocationhandler; ComPtr resource; ~PipelineState_DX12() { - uint64_t framecount = device->GetFrameCount(); - device->destroylocker.lock(); - if (resource) device->destroyer_pipelines.push_back(std::make_pair(resource, framecount)); - device->destroylocker.unlock(); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; + if (resource) allocationhandler->destroyer_pipelines.push_back(std::make_pair(resource, framecount)); + allocationhandler->destroylocker.unlock(); } }; @@ -1416,10 +1416,10 @@ using namespace DX12_Internal; } - void GraphicsDevice_DX12::FrameResources::ResourceFrameAllocator::init(std::shared_ptr device, size_t size) + void GraphicsDevice_DX12::FrameResources::ResourceFrameAllocator::init(GraphicsDevice_DX12* device, size_t size) { auto internal_state = std::make_shared(); - internal_state->device = device; + internal_state->allocationhandler = device->allocationhandler; buffer.internal_state = internal_state; HRESULT hr; @@ -1427,7 +1427,7 @@ using namespace DX12_Internal; D3D12MA::ALLOCATION_DESC allocationDesc = {}; allocationDesc.HeapType = D3D12_HEAP_TYPE_UPLOAD; - hr = device->allocator->CreateResource( + hr = device->allocationhandler->allocator->CreateResource( &allocationDesc, &CD3DX12_RESOURCE_DESC::Buffer(size), D3D12_RESOURCE_STATE_GENERIC_READ, @@ -1485,7 +1485,7 @@ using namespace DX12_Internal; D3D12MA::ALLOCATION_DESC allocationDesc = {}; allocationDesc.HeapType = D3D12_HEAP_TYPE_UPLOAD; - hr = device->allocator->CreateResource( + hr = device->allocationhandler->allocator->CreateResource( &allocationDesc, &CD3DX12_RESOURCE_DESC::Buffer(size), D3D12_RESOURCE_STATE_GENERIC_READ, @@ -1581,7 +1581,10 @@ using namespace DX12_Internal; D3D12MA::ALLOCATOR_DESC allocatorDesc = {}; allocatorDesc.pDevice = device.Get(); - hr = D3D12MA::CreateAllocator(&allocatorDesc, &allocator); + allocationhandler = std::make_shared(); + allocationhandler->device = device; + + hr = D3D12MA::CreateAllocator(&allocatorDesc, &allocationhandler->allocator); assert(SUCCEEDED(hr)); // Create command queue @@ -1651,10 +1654,10 @@ using namespace DX12_Internal; // Create common descriptor heaps - RTAllocator.init(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1024); - DSAllocator.init(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1024); - ResourceAllocator.init(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 16384); - SamplerAllocator.init(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, 64); + allocationhandler->RTAllocator.init(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1024); + allocationhandler->DSAllocator.init(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1024); + allocationhandler->ResourceAllocator.init(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 16384); + allocationhandler->SamplerAllocator.init(device.Get(), D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER, 64); resource_descriptor_size = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); sampler_descriptor_size = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER); @@ -1728,7 +1731,7 @@ using namespace DX12_Internal; { hr = swapChain->GetBuffer(fr, IID_PPV_ARGS(&frames[fr].backBuffer)); assert(SUCCEEDED(hr)); - frames[fr].backBufferRTV = RTAllocator.allocate(); + frames[fr].backBufferRTV = allocationhandler->RTAllocator.allocate(); device->CreateRenderTargetView(frames[fr].backBuffer.Get(), nullptr, frames[fr].backBufferRTV); } @@ -1983,7 +1986,7 @@ using namespace DX12_Internal; for (uint32_t i = 0; i < timestamp_query_count; ++i) { - free_timestampqueries.push_back(i); + allocationhandler->free_timestampqueries.push_back(i); } queryheapdesc.Count = timestamp_query_count; queryheapdesc.Type = D3D12_QUERY_HEAP_TYPE_TIMESTAMP; @@ -1992,7 +1995,7 @@ using namespace DX12_Internal; for (uint32_t i = 0; i < occlusion_query_count; ++i) { - free_occlusionqueries.push_back(i); + allocationhandler->free_occlusionqueries.push_back(i); } queryheapdesc.Count = occlusion_query_count; queryheapdesc.Type = D3D12_QUERY_HEAP_TYPE_OCCLUSION; @@ -2003,7 +2006,7 @@ using namespace DX12_Internal; D3D12MA::ALLOCATION_DESC allocationDesc = {}; allocationDesc.HeapType = D3D12_HEAP_TYPE_READBACK; - hr = allocator->CreateResource( + hr = allocationhandler->allocator->CreateResource( &allocationDesc, &CD3DX12_RESOURCE_DESC::Buffer(timestamp_query_count * sizeof(uint64_t)), D3D12_RESOURCE_STATE_COPY_DEST, @@ -2013,7 +2016,7 @@ using namespace DX12_Internal; ); assert(SUCCEEDED(hr)); - hr = allocator->CreateResource( + hr = allocationhandler->allocator->CreateResource( &allocationDesc, &CD3DX12_RESOURCE_DESC::Buffer(occlusion_query_count * sizeof(uint64_t)), D3D12_RESOURCE_STATE_COPY_DEST, @@ -2035,7 +2038,6 @@ using namespace DX12_Internal; allocation_querypool_timestamp_readback->Release(); allocation_querypool_occlusion_readback->Release(); - allocator->Release(); } void GraphicsDevice_DX12::SetResolution(int width, int height) @@ -2069,7 +2071,7 @@ using namespace DX12_Internal; Texture GraphicsDevice_DX12::GetBackBuffer() { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; internal_state->resource = GetFrameResources().backBuffer; internal_state->rtv = GetFrameResources().backBufferRTV; @@ -2082,7 +2084,7 @@ using namespace DX12_Internal; bool GraphicsDevice_DX12::CreateBuffer(const GPUBufferDesc* pDesc, const SubresourceData* pInitialData, GPUBuffer* pBuffer) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; pBuffer->internal_state = internal_state; pBuffer->type = GPUResource::GPU_RESOURCE_TYPE::BUFFER; @@ -2119,7 +2121,7 @@ using namespace DX12_Internal; D3D12MA::ALLOCATION_DESC allocationDesc = {}; allocationDesc.HeapType = D3D12_HEAP_TYPE_DEFAULT; - hr = allocator->CreateResource( + hr = allocationhandler->allocator->CreateResource( &allocationDesc, &desc, resourceState, @@ -2151,7 +2153,7 @@ using namespace DX12_Internal; cbv_desc.SizeInBytes = (uint32_t)alignedSize; cbv_desc.BufferLocation = internal_state->resource->GetGPUVirtualAddress(); - internal_state->cbv = ResourceAllocator.allocate(); + internal_state->cbv = allocationhandler->ResourceAllocator.allocate(); device->CreateConstantBufferView(&cbv_desc, internal_state->cbv); } @@ -2191,7 +2193,7 @@ using namespace DX12_Internal; srv_desc.Buffer.NumElements = pDesc->ByteWidth / pDesc->StructureByteStride; } - internal_state->srv = ResourceAllocator.allocate(); + internal_state->srv = allocationhandler->ResourceAllocator.allocate(); device->CreateShaderResourceView(internal_state->resource.Get(), &srv_desc, internal_state->srv); } @@ -2228,7 +2230,7 @@ using namespace DX12_Internal; uav_desc.Buffer.NumElements = pDesc->ByteWidth / pDesc->StructureByteStride; } - internal_state->uav = ResourceAllocator.allocate(); + internal_state->uav = allocationhandler->ResourceAllocator.allocate(); device->CreateUnorderedAccessView(internal_state->resource.Get(), nullptr, &uav_desc, internal_state->uav); } @@ -2237,7 +2239,7 @@ using namespace DX12_Internal; bool GraphicsDevice_DX12::CreateTexture(const TextureDesc* pDesc, const SubresourceData* pInitialData, Texture* pTexture) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; pTexture->internal_state = internal_state; pTexture->type = GPUResource::GPU_RESOURCE_TYPE::TEXTURE; @@ -2333,7 +2335,7 @@ using namespace DX12_Internal; break; } - hr = allocator->CreateResource( + hr = allocationhandler->allocator->CreateResource( &allocationDesc, &desc, resourceState, @@ -2398,7 +2400,7 @@ using namespace DX12_Internal; } bool GraphicsDevice_DX12::CreateInputLayout(const InputLayoutDesc* pInputElementDescs, uint32_t NumElements, const Shader* shader, InputLayout* pInputLayout) { - pInputLayout->internal_state = shared_from_this(); + pInputLayout->internal_state = allocationhandler; pInputLayout->desc.clear(); pInputLayout->desc.reserve((size_t)NumElements); @@ -2411,7 +2413,7 @@ using namespace DX12_Internal; } bool GraphicsDevice_DX12::CreateShader(SHADERSTAGE stage, const void* pShaderBytecode, size_t BytecodeLength, Shader* pShader) { - pShader->internal_state = shared_from_this(); + pShader->internal_state = allocationhandler; pShader->code.resize(BytecodeLength); std::memcpy(pShader->code.data(), pShaderBytecode, BytecodeLength); @@ -2424,7 +2426,7 @@ using namespace DX12_Internal; if (stage == CS) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; pShader->internal_state = internal_state; D3D12_COMPUTE_PIPELINE_STATE_DESC desc = {}; @@ -2440,21 +2442,21 @@ using namespace DX12_Internal; } bool GraphicsDevice_DX12::CreateBlendState(const BlendStateDesc* pBlendStateDesc, BlendState* pBlendState) { - pBlendState->internal_state = shared_from_this(); + pBlendState->internal_state = allocationhandler; pBlendState->desc = *pBlendStateDesc; return S_OK; } bool GraphicsDevice_DX12::CreateDepthStencilState(const DepthStencilStateDesc* pDepthStencilStateDesc, DepthStencilState* pDepthStencilState) { - pDepthStencilState->internal_state = shared_from_this(); + pDepthStencilState->internal_state = allocationhandler; pDepthStencilState->desc = *pDepthStencilStateDesc; return S_OK; } bool GraphicsDevice_DX12::CreateRasterizerState(const RasterizerStateDesc* pRasterizerStateDesc, RasterizerState* pRasterizerState) { - pRasterizerState->internal_state = shared_from_this(); + pRasterizerState->internal_state = allocationhandler; pRasterizerState->desc = *pRasterizerStateDesc; return S_OK; @@ -2462,7 +2464,7 @@ using namespace DX12_Internal; bool GraphicsDevice_DX12::CreateSampler(const SamplerDesc* pSamplerDesc, Sampler* pSamplerState) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; pSamplerState->internal_state = internal_state; D3D12_SAMPLER_DESC desc; @@ -2482,7 +2484,7 @@ using namespace DX12_Internal; pSamplerState->desc = *pSamplerDesc; - internal_state->descriptor = SamplerAllocator.allocate(); + internal_state->descriptor = allocationhandler->SamplerAllocator.allocate(); device->CreateSampler(&desc, internal_state->descriptor); return S_OK; @@ -2490,7 +2492,7 @@ using namespace DX12_Internal; bool GraphicsDevice_DX12::CreateQuery(const GPUQueryDesc* pDesc, GPUQuery* pQuery) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; pQuery->internal_state = internal_state; HRESULT hr = E_FAIL; @@ -2501,7 +2503,7 @@ using namespace DX12_Internal; switch (pDesc->Type) { case GPU_QUERY_TYPE_TIMESTAMP: - if (free_timestampqueries.pop_front(internal_state->query_index)) + if (allocationhandler->free_timestampqueries.pop_front(internal_state->query_index)) { hr = S_OK; } @@ -2516,7 +2518,7 @@ using namespace DX12_Internal; break; case GPU_QUERY_TYPE_OCCLUSION: case GPU_QUERY_TYPE_OCCLUSION_PREDICATE: - if (free_occlusionqueries.pop_front(internal_state->query_index)) + if (allocationhandler->free_occlusionqueries.pop_front(internal_state->query_index)) { hr = S_OK; } @@ -2534,8 +2536,7 @@ using namespace DX12_Internal; } bool GraphicsDevice_DX12::CreatePipelineState(const PipelineStateDesc* pDesc, PipelineState* pso) { - auto internal_state = shared_from_this(); - pso->internal_state = internal_state; + pso->internal_state = allocationhandler; pso->desc = *pDesc; @@ -2556,7 +2557,7 @@ using namespace DX12_Internal; } bool GraphicsDevice_DX12::CreateRenderPass(const RenderPassDesc* pDesc, RenderPass* renderpass) { - renderpass->internal_state = shared_from_this(); + renderpass->internal_state = allocationhandler; renderpass->desc = *pDesc; @@ -2679,7 +2680,7 @@ using namespace DX12_Internal; srv_desc.Texture3D.MipLevels = mipCount; } - D3D12_CPU_DESCRIPTOR_HANDLE srv = ResourceAllocator.allocate(); + D3D12_CPU_DESCRIPTOR_HANDLE srv = allocationhandler->ResourceAllocator.allocate(); device->CreateShaderResourceView(internal_state->resource.Get(), &srv_desc, srv); if (internal_state->srv.ptr == NULL) @@ -2753,7 +2754,7 @@ using namespace DX12_Internal; uav_desc.Texture3D.WSize = -1; } - D3D12_CPU_DESCRIPTOR_HANDLE uav = ResourceAllocator.allocate(); + D3D12_CPU_DESCRIPTOR_HANDLE uav = allocationhandler->ResourceAllocator.allocate(); device->CreateUnorderedAccessView(internal_state->resource.Get(), nullptr, &uav_desc, uav); if (internal_state->uav.ptr == NULL) @@ -2843,7 +2844,7 @@ using namespace DX12_Internal; rtv_desc.Texture3D.WSize = -1; } - D3D12_CPU_DESCRIPTOR_HANDLE rtv = RTAllocator.allocate(); + D3D12_CPU_DESCRIPTOR_HANDLE rtv = allocationhandler->RTAllocator.allocate(); device->CreateRenderTargetView(internal_state->resource.Get(), &rtv_desc, rtv); if (internal_state->rtv.ptr == NULL) @@ -2926,7 +2927,7 @@ using namespace DX12_Internal; } } - D3D12_CPU_DESCRIPTOR_HANDLE dsv = DSAllocator.allocate(); + D3D12_CPU_DESCRIPTOR_HANDLE dsv = allocationhandler->DSAllocator.allocate(); device->CreateDepthStencilView(internal_state->resource.Get(), &dsv_desc, dsv); if (internal_state->dsv.ptr == NULL) @@ -3064,9 +3065,9 @@ using namespace DX12_Internal; } else { - destroylocker.lock(); - destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); - destroylocker.unlock(); + allocationhandler->destroylocker.lock(); + allocationhandler->destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); + allocationhandler->destroylocker.unlock(); } } pipelines_worker[cmd].clear(); @@ -3100,125 +3101,7 @@ using namespace DX12_Internal; memset(prev_pt, 0, sizeof(prev_pt)); - - // Deferred destroy of resources that the GPU is already finished with: - destroylocker.lock(); - while (!destroyer_allocations.empty()) - { - if (destroyer_allocations.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_allocations.front(); - destroyer_allocations.pop_front(); - item.first->Release(); - } - else - { - break; - } - } - while (!destroyer_resources.empty()) - { - if (destroyer_resources.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - destroyer_resources.pop_front(); - // comptr auto delete - } - else - { - break; - } - } - while (!destroyer_resourceviews.empty()) - { - if (destroyer_resourceviews.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_resourceviews.front(); - destroyer_resourceviews.pop_front(); - ResourceAllocator.free(item.first); - } - else - { - break; - } - } - while (!destroyer_rtvs.empty()) - { - if (destroyer_rtvs.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_rtvs.front(); - destroyer_rtvs.pop_front(); - RTAllocator.free(item.first); - } - else - { - break; - } - } - while (!destroyer_dsvs.empty()) - { - if (destroyer_dsvs.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_dsvs.front(); - destroyer_dsvs.pop_front(); - DSAllocator.free(item.first); - } - else - { - break; - } - } - while (!destroyer_samplers.empty()) - { - if (destroyer_samplers.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_samplers.front(); - destroyer_samplers.pop_front(); - SamplerAllocator.free(item.first); - } - else - { - break; - } - } - while (!destroyer_queries_occlusion.empty()) - { - if (destroyer_queries_occlusion.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_queries_occlusion.front(); - destroyer_queries_occlusion.pop_front(); - free_occlusionqueries.push_back(item.first); - } - else - { - break; - } - } - while (!destroyer_queries_timestamp.empty()) - { - if (destroyer_queries_timestamp.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_queries_timestamp.front(); - destroyer_queries_timestamp.pop_front(); - free_timestampqueries.push_back(item.first); - } - else - { - break; - } - } - while (!destroyer_pipelines.empty()) - { - if (destroyer_pipelines.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - destroyer_pipelines.pop_front(); - // comptr auto delete - } - else - { - break; - } - } - destroylocker.unlock(); + allocationhandler->Update(FRAMECOUNT, BACKBUFFER_COUNT); RESOLUTIONCHANGED = false; } @@ -3240,7 +3123,7 @@ using namespace DX12_Internal; hr = static_cast(frames[fr].commandLists[cmd].Get())->Close(); frames[fr].descriptors[cmd].init(this); - frames[fr].resourceBuffer[cmd].init(shared_from_this(), 1024 * 1024 * 4); + frames[fr].resourceBuffer[cmd].init(this, 1024 * 1024 * 4); } } @@ -3294,10 +3177,10 @@ using namespace DX12_Internal; } void GraphicsDevice_DX12::ClearPipelineStateCache() { - destroylocker.lock(); + allocationhandler->destroylocker.lock(); for (auto& x : pipelines_global) { - destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); + allocationhandler->destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); } pipelines_global.clear(); @@ -3305,11 +3188,11 @@ using namespace DX12_Internal; { for (auto& x : pipelines_worker[i]) { - destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); + allocationhandler->destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); } pipelines_worker[i].clear(); } - destroylocker.unlock(); + allocationhandler->destroylocker.unlock(); } void GraphicsDevice_DX12::RenderPassBegin(const RenderPass* renderpass, CommandList cmd) diff --git a/WickedEngine/wiGraphicsDevice_DX12.h b/WickedEngine/wiGraphicsDevice_DX12.h index da33bf73c..01bf4bd6a 100644 --- a/WickedEngine/wiGraphicsDevice_DX12.h +++ b/WickedEngine/wiGraphicsDevice_DX12.h @@ -20,14 +20,13 @@ namespace wiGraphics { - class GraphicsDevice_DX12 : public GraphicsDevice, public std::enable_shared_from_this + class GraphicsDevice_DX12 : public GraphicsDevice { private: Microsoft::WRL::ComPtr device; Microsoft::WRL::ComPtr directQueue; Microsoft::WRL::ComPtr frameFence; HANDLE frameFenceEvent; - D3D12MA::Allocator* allocator = nullptr; Microsoft::WRL::ComPtr copyQueue; Microsoft::WRL::ComPtr copyAllocator; @@ -48,8 +47,6 @@ namespace wiGraphics Microsoft::WRL::ComPtr querypool_occlusion; static const size_t timestamp_query_count = 1024; static const size_t occlusion_query_count = 1024; - wiContainers::ThreadSafeRingBuffer free_timestampqueries; - wiContainers::ThreadSafeRingBuffer free_occlusionqueries; Microsoft::WRL::ComPtr querypool_timestamp_readback; Microsoft::WRL::ComPtr querypool_occlusion_readback; D3D12MA::Allocation* allocation_querypool_timestamp_readback = nullptr; @@ -73,10 +70,6 @@ namespace wiGraphics void clear(); void free(D3D12_CPU_DESCRIPTOR_HANDLE descriptorHandle); }; - DescriptorAllocator RTAllocator; - DescriptorAllocator DSAllocator; - DescriptorAllocator ResourceAllocator; - DescriptorAllocator SamplerAllocator; Microsoft::WRL::ComPtr null_resource_heap_CPU; Microsoft::WRL::ComPtr null_sampler_heap_CPU; @@ -150,7 +143,7 @@ namespace wiGraphics uint8_t* dataCur = nullptr; uint8_t* dataEnd = nullptr; - void init(std::shared_ptr device, size_t size); + void init(GraphicsDevice_DX12* device, size_t size); uint8_t* allocate(size_t dataSize, size_t alignment); void clear(); @@ -283,16 +276,158 @@ namespace wiGraphics void SetMarker(const std::string& name, CommandList cmd) override; - std::mutex destroylocker; - std::deque> destroyer_allocations; - std::deque, uint64_t>> destroyer_resources; - std::deque> destroyer_resourceviews; - std::deque> destroyer_rtvs; - std::deque> destroyer_dsvs; - std::deque> destroyer_samplers; - std::deque> destroyer_queries_timestamp; - std::deque> destroyer_queries_occlusion; - std::deque, uint64_t>> destroyer_pipelines; + struct AllocationHandler + { + D3D12MA::Allocator* allocator = nullptr; + Microsoft::WRL::ComPtr device; + uint64_t framecount = 0; + std::mutex destroylocker; + std::deque> destroyer_allocations; + std::deque, uint64_t>> destroyer_resources; + std::deque> destroyer_resourceviews; + std::deque> destroyer_rtvs; + std::deque> destroyer_dsvs; + std::deque> destroyer_samplers; + std::deque> destroyer_queries_timestamp; + std::deque> destroyer_queries_occlusion; + std::deque, uint64_t>> destroyer_pipelines; + + DescriptorAllocator RTAllocator; + DescriptorAllocator DSAllocator; + DescriptorAllocator ResourceAllocator; + DescriptorAllocator SamplerAllocator; + wiContainers::ThreadSafeRingBuffer free_timestampqueries; + wiContainers::ThreadSafeRingBuffer free_occlusionqueries; + + ~AllocationHandler() + { + Update(~0, 0); // destroy all remaining + if(allocator) allocator->Release(); + } + + // Deferred destroy of resources that the GPU is already finished with: + void Update(uint64_t FRAMECOUNT, uint32_t BACKBUFFER_COUNT) + { + destroylocker.lock(); + while (!destroyer_allocations.empty()) + { + if (destroyer_allocations.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_allocations.front(); + destroyer_allocations.pop_front(); + item.first->Release(); + } + else + { + break; + } + } + while (!destroyer_resources.empty()) + { + if (destroyer_resources.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + destroyer_resources.pop_front(); + // comptr auto delete + } + else + { + break; + } + } + while (!destroyer_resourceviews.empty()) + { + if (destroyer_resourceviews.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_resourceviews.front(); + destroyer_resourceviews.pop_front(); + ResourceAllocator.free(item.first); + } + else + { + break; + } + } + while (!destroyer_rtvs.empty()) + { + if (destroyer_rtvs.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_rtvs.front(); + destroyer_rtvs.pop_front(); + RTAllocator.free(item.first); + } + else + { + break; + } + } + while (!destroyer_dsvs.empty()) + { + if (destroyer_dsvs.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_dsvs.front(); + destroyer_dsvs.pop_front(); + DSAllocator.free(item.first); + } + else + { + break; + } + } + while (!destroyer_samplers.empty()) + { + if (destroyer_samplers.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_samplers.front(); + destroyer_samplers.pop_front(); + SamplerAllocator.free(item.first); + } + else + { + break; + } + } + while (!destroyer_queries_occlusion.empty()) + { + if (destroyer_queries_occlusion.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_queries_occlusion.front(); + destroyer_queries_occlusion.pop_front(); + free_occlusionqueries.push_back(item.first); + } + else + { + break; + } + } + while (!destroyer_queries_timestamp.empty()) + { + if (destroyer_queries_timestamp.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_queries_timestamp.front(); + destroyer_queries_timestamp.pop_front(); + free_timestampqueries.push_back(item.first); + } + else + { + break; + } + } + while (!destroyer_pipelines.empty()) + { + if (destroyer_pipelines.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + destroyer_pipelines.pop_front(); + // comptr auto delete + } + else + { + break; + } + } + destroylocker.unlock(); + } + }; + std::shared_ptr allocationhandler; }; diff --git a/WickedEngine/wiGraphicsDevice_Vulkan.cpp b/WickedEngine/wiGraphicsDevice_Vulkan.cpp index 3813beac3..789c7e787 100644 --- a/WickedEngine/wiGraphicsDevice_Vulkan.cpp +++ b/WickedEngine/wiGraphicsDevice_Vulkan.cpp @@ -753,7 +753,7 @@ namespace Vulkan_Internal // Destroyers: struct Buffer_Vulkan { - std::shared_ptr device; + std::shared_ptr allocationhandler; VmaAllocation allocation = nullptr; VkBuffer resource = VK_NULL_HANDLE; VkBufferView cbv = VK_NULL_HANDLE; @@ -764,26 +764,26 @@ namespace Vulkan_Internal ~Buffer_Vulkan() { - uint64_t framecount = device->GetFrameCount(); - device->destroylocker.lock(); - if (resource) device->destroyer_buffers.push_back(std::make_pair(std::make_pair(resource, allocation), framecount)); - if (cbv) device->destroyer_bufferviews.push_back(std::make_pair(cbv, framecount)); - if (srv) device->destroyer_bufferviews.push_back(std::make_pair(srv, framecount)); - if (uav) device->destroyer_bufferviews.push_back(std::make_pair(uav, framecount)); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; + if (resource) allocationhandler->destroyer_buffers.push_back(std::make_pair(std::make_pair(resource, allocation), framecount)); + if (cbv) allocationhandler->destroyer_bufferviews.push_back(std::make_pair(cbv, framecount)); + if (srv) allocationhandler->destroyer_bufferviews.push_back(std::make_pair(srv, framecount)); + if (uav) allocationhandler->destroyer_bufferviews.push_back(std::make_pair(uav, framecount)); for (auto x : subresources_srv) { - device->destroyer_bufferviews.push_back(std::make_pair(x, framecount)); + allocationhandler->destroyer_bufferviews.push_back(std::make_pair(x, framecount)); } for (auto x : subresources_uav) { - device->destroyer_bufferviews.push_back(std::make_pair(x, framecount)); + allocationhandler->destroyer_bufferviews.push_back(std::make_pair(x, framecount)); } - device->destroylocker.unlock(); + allocationhandler->destroylocker.unlock(); } }; struct Texture_Vulkan { - std::shared_ptr device; + std::shared_ptr allocationhandler; VmaAllocation allocation = nullptr; VkImage resource = VK_NULL_HANDLE; VkImageView srv = VK_NULL_HANDLE; @@ -797,103 +797,103 @@ namespace Vulkan_Internal ~Texture_Vulkan() { - uint64_t framecount = device->GetFrameCount(); - device->destroylocker.lock(); - if (resource) device->destroyer_images.push_back(std::make_pair(std::make_pair(resource, allocation), framecount)); - if (srv) device->destroyer_imageviews.push_back(std::make_pair(srv, framecount)); - if (uav) device->destroyer_imageviews.push_back(std::make_pair(uav, framecount)); - if (srv) device->destroyer_imageviews.push_back(std::make_pair(rtv, framecount)); - if (uav) device->destroyer_imageviews.push_back(std::make_pair(dsv, framecount)); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; + if (resource) allocationhandler->destroyer_images.push_back(std::make_pair(std::make_pair(resource, allocation), framecount)); + if (srv) allocationhandler->destroyer_imageviews.push_back(std::make_pair(srv, framecount)); + if (uav) allocationhandler->destroyer_imageviews.push_back(std::make_pair(uav, framecount)); + if (srv) allocationhandler->destroyer_imageviews.push_back(std::make_pair(rtv, framecount)); + if (uav) allocationhandler->destroyer_imageviews.push_back(std::make_pair(dsv, framecount)); for (auto x : subresources_srv) { - device->destroyer_imageviews.push_back(std::make_pair(x, framecount)); + allocationhandler->destroyer_imageviews.push_back(std::make_pair(x, framecount)); } for (auto x : subresources_uav) { - device->destroyer_imageviews.push_back(std::make_pair(x, framecount)); + allocationhandler->destroyer_imageviews.push_back(std::make_pair(x, framecount)); } for (auto x : subresources_rtv) { - device->destroyer_imageviews.push_back(std::make_pair(x, framecount)); + allocationhandler->destroyer_imageviews.push_back(std::make_pair(x, framecount)); } for (auto x : subresources_dsv) { - device->destroyer_imageviews.push_back(std::make_pair(x, framecount)); + allocationhandler->destroyer_imageviews.push_back(std::make_pair(x, framecount)); } - device->destroylocker.unlock(); + allocationhandler->destroylocker.unlock(); } }; struct Sampler_Vulkan { - std::shared_ptr device; + std::shared_ptr allocationhandler; VkSampler resource = VK_NULL_HANDLE; ~Sampler_Vulkan() { - uint64_t framecount = device->GetFrameCount(); - device->destroylocker.lock(); - if (resource) device->destroyer_samplers.push_back(std::make_pair(resource, framecount)); - device->destroylocker.unlock(); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; + if (resource) allocationhandler->destroyer_samplers.push_back(std::make_pair(resource, framecount)); + allocationhandler->destroylocker.unlock(); } }; struct Query_Vulkan { - std::shared_ptr device; + std::shared_ptr allocationhandler; GPU_QUERY_TYPE query_type = GPU_QUERY_TYPE_INVALID; uint32_t query_index = ~0; ~Query_Vulkan() { - uint64_t framecount = device->GetFrameCount(); if (query_index != ~0) { - device->destroylocker.lock(); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; switch (query_type) { case wiGraphics::GPU_QUERY_TYPE_OCCLUSION: case wiGraphics::GPU_QUERY_TYPE_OCCLUSION_PREDICATE: - device->destroyer_queries_occlusion.push_back(std::make_pair(query_index, framecount)); + allocationhandler->destroyer_queries_occlusion.push_back(std::make_pair(query_index, framecount)); break; case wiGraphics::GPU_QUERY_TYPE_TIMESTAMP: - device->destroyer_queries_timestamp.push_back(std::make_pair(query_index, framecount)); + allocationhandler->destroyer_queries_timestamp.push_back(std::make_pair(query_index, framecount)); break; } - device->destroylocker.unlock(); + allocationhandler->destroylocker.unlock(); } } }; struct Shader_Vulkan { - std::shared_ptr device; + std::shared_ptr allocationhandler; VkShaderModule shaderModule = VK_NULL_HANDLE; VkPipeline pipeline_cs = VK_NULL_HANDLE; VkPipelineShaderStageCreateInfo stageInfo = {}; ~Shader_Vulkan() { - uint64_t framecount = device->GetFrameCount(); - device->destroylocker.lock(); - if (shaderModule) device->destroyer_shadermodules.push_back(std::make_pair(shaderModule, framecount)); - if (pipeline_cs) device->destroyer_pipelines.push_back(std::make_pair(pipeline_cs, framecount)); - device->destroylocker.unlock(); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; + if (shaderModule) allocationhandler->destroyer_shadermodules.push_back(std::make_pair(shaderModule, framecount)); + if (pipeline_cs) allocationhandler->destroyer_pipelines.push_back(std::make_pair(pipeline_cs, framecount)); + allocationhandler->destroylocker.unlock(); } }; struct PipelineState_Vulkan { - std::shared_ptr device; + std::shared_ptr allocationhandler; VkPipeline resource = VK_NULL_HANDLE; ~PipelineState_Vulkan() { - uint64_t framecount = device->GetFrameCount(); - device->destroylocker.lock(); - if (resource) device->destroyer_pipelines.push_back(std::make_pair(resource, framecount)); - device->destroylocker.unlock(); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; + if (resource) allocationhandler->destroyer_pipelines.push_back(std::make_pair(resource, framecount)); + allocationhandler->destroylocker.unlock(); } }; struct RenderPass_Vulkan { - std::shared_ptr device; + std::shared_ptr allocationhandler; VkRenderPass renderpass = VK_NULL_HANDLE; VkFramebuffer framebuffer = VK_NULL_HANDLE; VkRenderPassBeginInfo beginInfo; @@ -901,11 +901,11 @@ namespace Vulkan_Internal ~RenderPass_Vulkan() { - uint64_t framecount = device->GetFrameCount(); - device->destroylocker.lock(); - if (renderpass) device->destroyer_renderpasses.push_back(std::make_pair(renderpass, framecount)); - if (framebuffer) device->destroyer_framebuffers.push_back(std::make_pair(framebuffer, framecount)); - device->destroylocker.unlock(); + allocationhandler->destroylocker.lock(); + uint64_t framecount = allocationhandler->framecount; + if (renderpass) allocationhandler->destroyer_renderpasses.push_back(std::make_pair(renderpass, framecount)); + if (framebuffer) allocationhandler->destroyer_framebuffers.push_back(std::make_pair(framebuffer, framecount)); + allocationhandler->destroylocker.unlock(); } }; @@ -941,10 +941,10 @@ namespace Vulkan_Internal using namespace Vulkan_Internal; - void GraphicsDevice_Vulkan::FrameResources::ResourceFrameAllocator::init(std::shared_ptr device, size_t size) + void GraphicsDevice_Vulkan::FrameResources::ResourceFrameAllocator::init(GraphicsDevice_Vulkan* device, size_t size) { auto internal_state = std::make_shared(); - internal_state->device = device; + internal_state->allocationhandler = device->allocationhandler; buffer.internal_state = internal_state; VkBufferCreateInfo bufferInfo = {}; @@ -963,7 +963,7 @@ using namespace Vulkan_Internal; allocInfo.usage = VMA_MEMORY_USAGE_CPU_TO_GPU; allocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT; - res = vmaCreateBuffer(device->allocator, &bufferInfo, &allocInfo, &internal_state->resource, &internal_state->allocation, nullptr); + res = vmaCreateBuffer(device->allocationhandler->allocator, &bufferInfo, &allocInfo, &internal_state->resource, &internal_state->allocation, nullptr); assert(res == VK_SUCCESS); void* pData = internal_state->allocation->GetMappedData(); @@ -1021,7 +1021,7 @@ using namespace Vulkan_Internal; allocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY; allocInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT; - res = vmaCreateBuffer(device->allocator, &bufferInfo, &allocInfo, &resource, &allocation, nullptr); + res = vmaCreateBuffer(device->allocationhandler->allocator, &bufferInfo, &allocInfo, &resource, &allocation, nullptr); assert(res == VK_SUCCESS); void* pData = allocation->GetMappedData(); @@ -1030,7 +1030,7 @@ using namespace Vulkan_Internal; } GraphicsDevice_Vulkan::UploadBuffer::~UploadBuffer() { - vmaDestroyBuffer(device->allocator, resource, allocation); + vmaDestroyBuffer(device->allocationhandler->allocator, resource, allocation); } uint8_t* GraphicsDevice_Vulkan::UploadBuffer::allocate(size_t dataSize, size_t alignment) { @@ -1800,11 +1800,15 @@ using namespace Vulkan_Internal; vkGetDeviceQueue(device, queueIndices.copyFamily, 0, ©Queue); } + allocationhandler = std::make_shared(); + allocationhandler->device = device; + allocationhandler->instance = instance; + // Initialize Vulkan Memory Allocator helper: VmaAllocatorCreateInfo allocatorInfo = {}; allocatorInfo.physicalDevice = physicalDevice; allocatorInfo.device = device; - res = vmaCreateAllocator(&allocatorInfo, &allocator); + res = vmaCreateAllocator(&allocatorInfo, &allocationhandler->allocator); assert(res == VK_SUCCESS); // Extension functions: @@ -2306,8 +2310,7 @@ using namespace Vulkan_Internal; VmaAllocationCreateInfo allocInfo = {}; allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - VmaAllocation allocation; - res = vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &nullBuffer, &allocation, nullptr); + res = vmaCreateBuffer(allocationhandler->allocator, &bufferInfo, &allocInfo, &nullBuffer, &nullBufferAllocation, nullptr); assert(res == VK_SUCCESS); VkBufferViewCreateInfo viewInfo = {}; @@ -2337,8 +2340,7 @@ using namespace Vulkan_Internal; VmaAllocationCreateInfo allocInfo = {}; allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - VmaAllocation allocation; - res = vmaCreateImage(allocator, &imageInfo, &allocInfo, &nullImage, &allocation, nullptr); + res = vmaCreateImage(allocationhandler->allocator, &imageInfo, &allocInfo, &nullImage, &nullImageAllocation, nullptr); assert(res == VK_SUCCESS); VkImageViewCreateInfo viewInfo = {}; @@ -2372,7 +2374,7 @@ using namespace Vulkan_Internal; for (uint32_t i = 0; i < timestamp_query_count; ++i) { - free_timestampqueries.push_back(i); + allocationhandler->free_timestampqueries.push_back(i); } poolInfo.queryCount = timestamp_query_count; poolInfo.queryType = VK_QUERY_TYPE_TIMESTAMP; @@ -2382,7 +2384,7 @@ using namespace Vulkan_Internal; for (uint32_t i = 0; i < occlusion_query_count; ++i) { - free_occlusionqueries.push_back(i); + allocationhandler->free_occlusionqueries.push_back(i); } poolInfo.queryCount = occlusion_query_count; poolInfo.queryType = VK_QUERY_TYPE_OCCLUSION; @@ -2397,6 +2399,9 @@ using namespace Vulkan_Internal; { WaitForGPU(); + vkDestroyFence(device, copyFence, nullptr); + vkDestroyCommandPool(device, copyCommandPool, nullptr); + for (auto& frame : frames) { vkDestroyFence(device, frame.frameFence, nullptr); @@ -2406,11 +2411,24 @@ using namespace Vulkan_Internal; { vkDestroyCommandPool(device, commandPool, nullptr); } + vkDestroyCommandPool(device, frame.transitionCommandPool, nullptr); } vkDestroySemaphore(device, renderFinishedSemaphore, nullptr); vkDestroySemaphore(device, imageAvailableSemaphore, nullptr); + for (auto& x : pipelines_worker) + { + for (auto& y : x) + { + vkDestroyPipeline(device, y.second, nullptr); + } + } + for (auto& x : pipelines_global) + { + vkDestroyPipeline(device, x.second, nullptr); + } + for (int i = 0; i < SHADERSTAGE_COUNT; ++i) { vkDestroyDescriptorSetLayout(device, defaultDescriptorSetlayouts[i], nullptr); @@ -2419,26 +2437,19 @@ using namespace Vulkan_Internal; vkDestroyPipelineLayout(device, defaultPipelineLayout_Compute, nullptr); vkDestroyRenderPass(device, defaultRenderPass, nullptr); - for (auto& x : swapChainImages) - { - vkDestroyImage(device, x, nullptr); - } vkDestroySwapchainKHR(device, swapChain, nullptr); vkDestroyQueryPool(device, querypool_timestamp, nullptr); vkDestroyQueryPool(device, querypool_occlusion, nullptr); - vkDestroyBuffer(device, nullBuffer, nullptr); + vmaDestroyBuffer(allocationhandler->allocator, nullBuffer, nullBufferAllocation); vkDestroyBufferView(device, nullBufferView, nullptr); - vkDestroyImage(device, nullImage, nullptr); + vmaDestroyImage(allocationhandler->allocator, nullImage, nullImageAllocation); vkDestroyImageView(device, nullImageView, nullptr); vkDestroySampler(device, nullSampler, nullptr); - vmaDestroyAllocator(allocator); - - vkDestroyDevice(device, nullptr); DestroyDebugReportCallbackEXT(instance, callback, nullptr); - vkDestroyInstance(instance, nullptr); + vkDestroySurfaceKHR(instance, surface, nullptr); } void GraphicsDevice_Vulkan::SetResolution(int width, int height) @@ -2460,7 +2471,7 @@ using namespace Vulkan_Internal; bool GraphicsDevice_Vulkan::CreateBuffer(const GPUBufferDesc *pDesc, const SubresourceData* pInitialData, GPUBuffer *pBuffer) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; pBuffer->internal_state = internal_state; pBuffer->type = GPUResource::GPU_RESOURCE_TYPE::BUFFER; @@ -2522,7 +2533,7 @@ using namespace Vulkan_Internal; VmaAllocationCreateInfo allocInfo = {}; allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - res = vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &internal_state->resource, &internal_state->allocation, nullptr); + res = vmaCreateBuffer(allocationhandler->allocator, &bufferInfo, &allocInfo, &internal_state->resource, &internal_state->allocation, nullptr); assert(res == VK_SUCCESS); // Issue data copy on request: @@ -2640,7 +2651,7 @@ using namespace Vulkan_Internal; bool GraphicsDevice_Vulkan::CreateTexture(const TextureDesc* pDesc, const SubresourceData *pInitialData, Texture *pTexture) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; pTexture->internal_state = internal_state; pTexture->type = GPUResource::GPU_RESOURCE_TYPE::TEXTURE; @@ -2714,7 +2725,7 @@ using namespace Vulkan_Internal; VkResult res; - res = vmaCreateImage(allocator, &imageInfo, &allocInfo, &internal_state->resource, &internal_state->allocation, nullptr); + res = vmaCreateImage(allocationhandler->allocator, &imageInfo, &allocInfo, &internal_state->resource, &internal_state->allocation, nullptr); assert(res == VK_SUCCESS); // Issue data copy on request: @@ -2860,7 +2871,7 @@ using namespace Vulkan_Internal; } bool GraphicsDevice_Vulkan::CreateInputLayout(const InputLayoutDesc *pInputElementDescs, uint32_t NumElements, const Shader* shader, InputLayout *pInputLayout) { - pInputLayout->internal_state = shared_from_this(); + pInputLayout->internal_state = allocationhandler; pInputLayout->desc.clear(); pInputLayout->desc.reserve((size_t)NumElements); @@ -2874,7 +2885,7 @@ using namespace Vulkan_Internal; bool GraphicsDevice_Vulkan::CreateShader(SHADERSTAGE stage, const void *pShaderBytecode, size_t BytecodeLength, Shader *pShader) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; pShader->internal_state = internal_state; pShader->code.resize(BytecodeLength); @@ -2938,21 +2949,21 @@ using namespace Vulkan_Internal; } bool GraphicsDevice_Vulkan::CreateBlendState(const BlendStateDesc *pBlendStateDesc, BlendState *pBlendState) { - pBlendState->internal_state = shared_from_this(); + pBlendState->internal_state = allocationhandler; pBlendState->desc = *pBlendStateDesc; return true; } bool GraphicsDevice_Vulkan::CreateDepthStencilState(const DepthStencilStateDesc *pDepthStencilStateDesc, DepthStencilState *pDepthStencilState) { - pDepthStencilState->internal_state = shared_from_this(); + pDepthStencilState->internal_state = allocationhandler; pDepthStencilState->desc = *pDepthStencilStateDesc; return true; } bool GraphicsDevice_Vulkan::CreateRasterizerState(const RasterizerStateDesc *pRasterizerStateDesc, RasterizerState *pRasterizerState) { - pRasterizerState->internal_state = shared_from_this(); + pRasterizerState->internal_state = allocationhandler; pRasterizerState->desc = *pRasterizerStateDesc; return true; @@ -2960,7 +2971,7 @@ using namespace Vulkan_Internal; bool GraphicsDevice_Vulkan::CreateSampler(const SamplerDesc *pSamplerDesc, Sampler *pSamplerState) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; pSamplerState->internal_state = internal_state; pSamplerState->desc = *pSamplerDesc; @@ -3145,7 +3156,7 @@ using namespace Vulkan_Internal; bool GraphicsDevice_Vulkan::CreateQuery(const GPUQueryDesc *pDesc, GPUQuery *pQuery) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; pQuery->internal_state = internal_state; bool hr = false; @@ -3156,7 +3167,7 @@ using namespace Vulkan_Internal; switch (pDesc->Type) { case GPU_QUERY_TYPE_TIMESTAMP: - if (free_timestampqueries.pop_front(internal_state->query_index)) + if (allocationhandler->free_timestampqueries.pop_front(internal_state->query_index)) { hr = true; } @@ -3171,7 +3182,7 @@ using namespace Vulkan_Internal; break; case GPU_QUERY_TYPE_OCCLUSION: case GPU_QUERY_TYPE_OCCLUSION_PREDICATE: - if (free_occlusionqueries.pop_front(internal_state->query_index)) + if (allocationhandler->free_occlusionqueries.pop_front(internal_state->query_index)) { hr = true; } @@ -3189,7 +3200,7 @@ using namespace Vulkan_Internal; } bool GraphicsDevice_Vulkan::CreatePipelineState(const PipelineStateDesc* pDesc, PipelineState* pso) { - pso->internal_state = shared_from_this(); + pso->internal_state = allocationhandler; pso->desc = *pDesc; @@ -3211,7 +3222,7 @@ using namespace Vulkan_Internal; bool GraphicsDevice_Vulkan::CreateRenderPass(const RenderPassDesc* pDesc, RenderPass* renderpass) { auto internal_state = std::make_shared(); - internal_state->device = shared_from_this(); + internal_state->allocationhandler = allocationhandler; renderpass->internal_state = internal_state; renderpass->desc = *pDesc; @@ -3769,9 +3780,9 @@ using namespace Vulkan_Internal; } else { - destroylocker.lock(); - destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); - destroylocker.unlock(); + allocationhandler->destroylocker.lock(); + allocationhandler->destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); + allocationhandler->destroylocker.unlock(); } } pipelines_worker[cmd].clear(); @@ -3826,153 +3837,7 @@ using namespace Vulkan_Internal; assert(res == VK_SUCCESS); } - - // Deferred destroy of resources that the GPU is already finished with: - destroylocker.lock(); - while (!destroyer_images.empty()) - { - if (destroyer_images.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_images.front(); - destroyer_images.pop_front(); - vmaDestroyImage(allocator, item.first.first, item.first.second); - } - else - { - break; - } - } - while (!destroyer_imageviews.empty()) - { - if (destroyer_imageviews.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_imageviews.front(); - destroyer_imageviews.pop_front(); - vkDestroyImageView(device, item.first, nullptr); - } - else - { - break; - } - } - while (!destroyer_buffers.empty()) - { - if (destroyer_buffers.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_buffers.front(); - destroyer_buffers.pop_front(); - vmaDestroyBuffer(allocator, item.first.first, item.first.second); - } - else - { - break; - } - } - while (!destroyer_bufferviews.empty()) - { - if (destroyer_bufferviews.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_bufferviews.front(); - destroyer_bufferviews.pop_front(); - vkDestroyBufferView(device, item.first, nullptr); - } - else - { - break; - } - } - while (!destroyer_samplers.empty()) - { - if (destroyer_samplers.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_samplers.front(); - destroyer_samplers.pop_front(); - vkDestroySampler(device, item.first, nullptr); - } - else - { - break; - } - } - while (!destroyer_shadermodules.empty()) - { - if (destroyer_shadermodules.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_shadermodules.front(); - destroyer_shadermodules.pop_front(); - vkDestroyShaderModule(device, item.first, nullptr); - } - else - { - break; - } - } - while (!destroyer_pipelines.empty()) - { - if (destroyer_pipelines.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_pipelines.front(); - destroyer_pipelines.pop_front(); - vkDestroyPipeline(device, item.first, nullptr); - } - else - { - break; - } - } - while (!destroyer_renderpasses.empty()) - { - if (destroyer_renderpasses.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_renderpasses.front(); - destroyer_renderpasses.pop_front(); - vkDestroyRenderPass(device, item.first, nullptr); - } - else - { - break; - } - } - while (!destroyer_framebuffers.empty()) - { - if (destroyer_framebuffers.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_framebuffers.front(); - destroyer_framebuffers.pop_front(); - vkDestroyFramebuffer(device, item.first, nullptr); - } - else - { - break; - } - } - while (!destroyer_queries_occlusion.empty()) - { - if (destroyer_queries_occlusion.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_queries_occlusion.front(); - destroyer_queries_occlusion.pop_front(); - free_occlusionqueries.push_back(item.first); - } - else - { - break; - } - } - while (!destroyer_queries_timestamp.empty()) - { - if (destroyer_queries_timestamp.front().second + BACKBUFFER_COUNT < FRAMECOUNT) - { - auto item = destroyer_queries_timestamp.front(); - destroyer_queries_timestamp.pop_front(); - free_timestampqueries.push_back(item.first); - } - else - { - break; - } - } - destroylocker.unlock(); + allocationhandler->Update(FRAMECOUNT, BACKBUFFER_COUNT); // Restart transition command buffers: { @@ -4026,7 +3891,7 @@ using namespace Vulkan_Internal; res = vkAllocateCommandBuffers(device, &commandBufferInfo, &frame.commandBuffers[cmd]); assert(res == VK_SUCCESS); - frame.resourceBuffer[cmd].init(shared_from_this(), 4 * 1024 * 1024); + frame.resourceBuffer[cmd].init(this, 4 * 1024 * 1024); frame.descriptors[cmd].init(this); } } @@ -4102,10 +3967,10 @@ using namespace Vulkan_Internal; } void GraphicsDevice_Vulkan::ClearPipelineStateCache() { - destroylocker.lock(); + allocationhandler->destroylocker.lock(); for (auto& x : pipelines_global) { - destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); + allocationhandler->destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); } pipelines_global.clear(); @@ -4113,11 +3978,11 @@ using namespace Vulkan_Internal; { for (auto& x : pipelines_worker[i]) { - destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); + allocationhandler->destroyer_pipelines.push_back(std::make_pair(x.second, FRAMECOUNT)); } pipelines_worker[i].clear(); } - destroylocker.unlock(); + allocationhandler->destroylocker.unlock(); } diff --git a/WickedEngine/wiGraphicsDevice_Vulkan.h b/WickedEngine/wiGraphicsDevice_Vulkan.h index 42b087198..1d32769f9 100644 --- a/WickedEngine/wiGraphicsDevice_Vulkan.h +++ b/WickedEngine/wiGraphicsDevice_Vulkan.h @@ -42,7 +42,7 @@ namespace wiGraphics } }; - class GraphicsDevice_Vulkan : public GraphicsDevice, public std::enable_shared_from_this + class GraphicsDevice_Vulkan : public GraphicsDevice { friend struct DescriptorTableFrameAllocator; private: @@ -55,7 +55,6 @@ namespace wiGraphics QueueFamilyIndices queueIndices; VkQueue graphicsQueue; VkQueue presentQueue; - VmaAllocator allocator; VkPhysicalDeviceProperties physicalDeviceProperties; @@ -80,8 +79,10 @@ namespace wiGraphics uint32_t descriptorCount; VkBuffer nullBuffer; + VmaAllocation nullBufferAllocation; VkBufferView nullBufferView; VkImage nullImage; + VmaAllocation nullImageAllocation; VkImageView nullImageView; VkSampler nullSampler; @@ -90,8 +91,6 @@ namespace wiGraphics VkQueryPool querypool_occlusion; static const size_t timestamp_query_count = 1024; static const size_t occlusion_query_count = 1024; - wiContainers::ThreadSafeRingBuffer free_timestampqueries; - wiContainers::ThreadSafeRingBuffer free_occlusionqueries; bool initial_querypool_reset = false; std::vector timestamps_to_reset; std::vector occlusions_to_reset; @@ -100,8 +99,8 @@ namespace wiGraphics struct FrameResources { VkFence frameFence; - VkCommandPool commandPools[COMMANDLIST_COUNT]; - VkCommandBuffer commandBuffers[COMMANDLIST_COUNT]; + VkCommandPool commandPools[COMMANDLIST_COUNT] = {}; + VkCommandBuffer commandBuffers[COMMANDLIST_COUNT] = {}; VkImageView swapChainImageView; VkFramebuffer swapChainFramebuffer; @@ -183,7 +182,7 @@ namespace wiGraphics uint8_t* dataCur = nullptr; uint8_t* dataEnd = nullptr; - void init(std::shared_ptr device, size_t size); + void init(GraphicsDevice_Vulkan* device, size_t size); uint8_t* allocate(size_t dataSize, size_t alignment); void clear(); @@ -311,19 +310,188 @@ namespace wiGraphics void SetMarker(const std::string& name, CommandList cmd) override; + struct AllocationHandler + { + VmaAllocator allocator = VK_NULL_HANDLE; + VkDevice device = VK_NULL_HANDLE; + VkInstance instance; + uint64_t framecount = 0; + std::mutex destroylocker; + std::deque, uint64_t>> destroyer_images; + std::deque> destroyer_imageviews; + std::deque, uint64_t>> destroyer_buffers; + std::deque> destroyer_bufferviews; + std::deque> destroyer_samplers; + std::deque> destroyer_shadermodules; + std::deque> destroyer_pipelines; + std::deque> destroyer_renderpasses; + std::deque> destroyer_framebuffers; + std::deque> destroyer_queries_occlusion; + std::deque> destroyer_queries_timestamp; - std::mutex destroylocker; - std::deque, uint64_t>> destroyer_images; - std::deque> destroyer_imageviews; - std::deque, uint64_t>> destroyer_buffers; - std::deque> destroyer_bufferviews; - std::deque> destroyer_samplers; - std::deque> destroyer_shadermodules; - std::deque> destroyer_pipelines; - std::deque> destroyer_renderpasses; - std::deque> destroyer_framebuffers; - std::deque> destroyer_queries_occlusion; - std::deque> destroyer_queries_timestamp; + wiContainers::ThreadSafeRingBuffer free_timestampqueries; + wiContainers::ThreadSafeRingBuffer free_occlusionqueries; + + ~AllocationHandler() + { + Update(~0, 0); // destroy all remaining + vmaDestroyAllocator(allocator); + vkDestroyDevice(device, nullptr); + vkDestroyInstance(instance, nullptr); + } + + // Deferred destroy of resources that the GPU is already finished with: + void Update(uint64_t FRAMECOUNT, uint32_t BACKBUFFER_COUNT) + { + destroylocker.lock(); + framecount = FRAMECOUNT; + while (!destroyer_images.empty()) + { + if (destroyer_images.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_images.front(); + destroyer_images.pop_front(); + vmaDestroyImage(allocator, item.first.first, item.first.second); + } + else + { + break; + } + } + while (!destroyer_imageviews.empty()) + { + if (destroyer_imageviews.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_imageviews.front(); + destroyer_imageviews.pop_front(); + vkDestroyImageView(device, item.first, nullptr); + } + else + { + break; + } + } + while (!destroyer_buffers.empty()) + { + if (destroyer_buffers.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_buffers.front(); + destroyer_buffers.pop_front(); + vmaDestroyBuffer(allocator, item.first.first, item.first.second); + } + else + { + break; + } + } + while (!destroyer_bufferviews.empty()) + { + if (destroyer_bufferviews.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_bufferviews.front(); + destroyer_bufferviews.pop_front(); + vkDestroyBufferView(device, item.first, nullptr); + } + else + { + break; + } + } + while (!destroyer_samplers.empty()) + { + if (destroyer_samplers.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_samplers.front(); + destroyer_samplers.pop_front(); + vkDestroySampler(device, item.first, nullptr); + } + else + { + break; + } + } + while (!destroyer_shadermodules.empty()) + { + if (destroyer_shadermodules.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_shadermodules.front(); + destroyer_shadermodules.pop_front(); + vkDestroyShaderModule(device, item.first, nullptr); + } + else + { + break; + } + } + while (!destroyer_pipelines.empty()) + { + if (destroyer_pipelines.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_pipelines.front(); + destroyer_pipelines.pop_front(); + vkDestroyPipeline(device, item.first, nullptr); + } + else + { + break; + } + } + while (!destroyer_renderpasses.empty()) + { + if (destroyer_renderpasses.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_renderpasses.front(); + destroyer_renderpasses.pop_front(); + vkDestroyRenderPass(device, item.first, nullptr); + } + else + { + break; + } + } + while (!destroyer_framebuffers.empty()) + { + if (destroyer_framebuffers.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_framebuffers.front(); + destroyer_framebuffers.pop_front(); + vkDestroyFramebuffer(device, item.first, nullptr); + } + else + { + break; + } + } + while (!destroyer_queries_occlusion.empty()) + { + if (destroyer_queries_occlusion.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_queries_occlusion.front(); + destroyer_queries_occlusion.pop_front(); + free_occlusionqueries.push_back(item.first); + } + else + { + break; + } + } + while (!destroyer_queries_timestamp.empty()) + { + if (destroyer_queries_timestamp.front().second + BACKBUFFER_COUNT < FRAMECOUNT) + { + auto item = destroyer_queries_timestamp.front(); + destroyer_queries_timestamp.pop_front(); + free_timestampqueries.push_back(item.first); + } + else + { + break; + } + } + destroylocker.unlock(); + } + }; + std::shared_ptr allocationhandler; }; } diff --git a/WickedEngine/wiHairParticle.cpp b/WickedEngine/wiHairParticle.cpp index 920358200..3c0c0e683 100644 --- a/WickedEngine/wiHairParticle.cpp +++ b/WickedEngine/wiHairParticle.cpp @@ -54,14 +54,10 @@ void wiHairParticle::UpdateCPU(const TransformComponent& transform, const MeshCo if (dt > 0) { _flags &= ~REGENERATE_FRAME; - if (cb == nullptr || (strandCount * segmentCount) != particleBuffer->GetDesc().ByteWidth / sizeof(Patch)) + if (!cb.IsValid() || (strandCount * segmentCount) != particleBuffer.GetDesc().ByteWidth / sizeof(Patch)) { _flags |= REGENERATE_FRAME; - cb.reset(new GPUBuffer); - particleBuffer.reset(new GPUBuffer); - simulationBuffer.reset(new GPUBuffer); - GPUBufferDesc bd; bd.Usage = USAGE_DEFAULT; bd.BindFlags = BIND_SHADER_RESOURCE | BIND_UNORDERED_ACCESS; @@ -72,11 +68,11 @@ void wiHairParticle::UpdateCPU(const TransformComponent& transform, const MeshCo { bd.StructureByteStride = sizeof(Patch); bd.ByteWidth = bd.StructureByteStride * strandCount * segmentCount; - wiRenderer::GetDevice()->CreateBuffer(&bd, nullptr, particleBuffer.get()); + wiRenderer::GetDevice()->CreateBuffer(&bd, nullptr, &particleBuffer); bd.StructureByteStride = sizeof(PatchSimulationData); bd.ByteWidth = bd.StructureByteStride * strandCount * segmentCount; - wiRenderer::GetDevice()->CreateBuffer(&bd, nullptr, simulationBuffer.get()); + wiRenderer::GetDevice()->CreateBuffer(&bd, nullptr, &simulationBuffer); } bd.Usage = USAGE_DEFAULT; @@ -84,14 +80,14 @@ void wiHairParticle::UpdateCPU(const TransformComponent& transform, const MeshCo bd.BindFlags = BIND_CONSTANT_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; - wiRenderer::GetDevice()->CreateBuffer(&bd, nullptr, cb.get()); + wiRenderer::GetDevice()->CreateBuffer(&bd, nullptr, &cb); } } } void wiHairParticle::UpdateGPU(const MeshComponent& mesh, const MaterialComponent& material, CommandList cmd) const { - if (strandCount == 0 || particleBuffer == nullptr) + if (strandCount == 0 || !particleBuffer.IsValid()) { return; } @@ -117,13 +113,13 @@ void wiHairParticle::UpdateGPU(const MeshComponent& mesh, const MaterialComponen hcb.xHairBaseMeshVertexPositionStride = sizeof(MeshComponent::Vertex_POS); // segmentCount will be loop in the shader, not a threadgroup so we don't need it here: hcb.xHairNumDispatchGroups = (uint)ceilf((float)strandCount / (float)THREADCOUNT_SIMULATEHAIR); - device->UpdateBuffer(cb.get(), &hcb, cmd); + device->UpdateBuffer(&cb, &hcb, cmd); - device->BindConstantBuffer(CS, cb.get(), CB_GETBINDSLOT(HairParticleCB), cmd); + device->BindConstantBuffer(CS, &cb, CB_GETBINDSLOT(HairParticleCB), cmd); const GPUResource* uavs[] = { - particleBuffer.get(), - simulationBuffer.get() + &particleBuffer, + &simulationBuffer }; device->BindUAVs(CS, uavs, 0, arraysize(uavs), cmd); @@ -143,7 +139,7 @@ void wiHairParticle::UpdateGPU(const MeshComponent& mesh, const MaterialComponen void wiHairParticle::Draw(const CameraComponent& camera, const MaterialComponent& material, RENDERPASS renderPass, bool transparent, CommandList cmd) const { - if (strandCount == 0 || cb == nullptr) + if (strandCount == 0 || !cb.IsValid()) { return; } @@ -173,9 +169,9 @@ void wiHairParticle::Draw(const CameraComponent& camera, const MaterialComponent device->BindResources(VS, res, TEXSLOT_ONDEMAND0, arraysize(res), cmd); } - device->BindConstantBuffer(VS, cb.get(), CB_GETBINDSLOT(HairParticleCB), cmd); + device->BindConstantBuffer(VS, &cb, CB_GETBINDSLOT(HairParticleCB), cmd); - device->BindResource(VS, particleBuffer.get(), 0, cmd); + device->BindResource(VS, &particleBuffer, 0, cmd); device->Draw(strandCount * 12 * std::max(segmentCount, 1u), 0, cmd); diff --git a/WickedEngine/wiHairParticle.h b/WickedEngine/wiHairParticle.h index f12526fb2..ed54c349c 100644 --- a/WickedEngine/wiHairParticle.h +++ b/WickedEngine/wiHairParticle.h @@ -16,9 +16,9 @@ namespace wiScene class wiHairParticle { private: - std::unique_ptr cb; - std::unique_ptr particleBuffer; - std::unique_ptr simulationBuffer; + wiGraphics::GPUBuffer cb; + wiGraphics::GPUBuffer particleBuffer; + wiGraphics::GPUBuffer simulationBuffer; public: void UpdateCPU(const TransformComponent& transform, const MeshComponent& mesh, float dt); diff --git a/WickedEngine/wiImageParams_BindLua.cpp b/WickedEngine/wiImageParams_BindLua.cpp index 644248dcd..0e7e89854 100644 --- a/WickedEngine/wiImageParams_BindLua.cpp +++ b/WickedEngine/wiImageParams_BindLua.cpp @@ -434,9 +434,6 @@ wiImageParams_BindLua::wiImageParams_BindLua(lua_State *L) params = wiImageParams(x, y, w, h); } -wiImageParams_BindLua::~wiImageParams_BindLua() -{ -} void wiImageParams_BindLua::Bind() { diff --git a/WickedEngine/wiImageParams_BindLua.h b/WickedEngine/wiImageParams_BindLua.h index 84b679d73..19c585abb 100644 --- a/WickedEngine/wiImageParams_BindLua.h +++ b/WickedEngine/wiImageParams_BindLua.h @@ -14,7 +14,6 @@ public: wiImageParams_BindLua(const wiImageParams& params); wiImageParams_BindLua(lua_State *L); - ~wiImageParams_BindLua(); int GetPos(lua_State* L); int GetSize(lua_State* L); diff --git a/WickedEngine/wiInput_BindLua.h b/WickedEngine/wiInput_BindLua.h index 0c714ce5f..c70d51648 100644 --- a/WickedEngine/wiInput_BindLua.h +++ b/WickedEngine/wiInput_BindLua.h @@ -11,7 +11,6 @@ public: static Luna::PropertyType properties[]; wiInput_BindLua(lua_State* L){} - ~wiInput_BindLua(){} int Down(lua_State* L); int Press(lua_State* L); @@ -36,7 +35,6 @@ public: Touch_BindLua(lua_State* L) {} Touch_BindLua(const wiInput::Touch& touch) :touch(touch) {} - ~Touch_BindLua() {} int GetState(lua_State* L); int GetPos(lua_State* L); diff --git a/WickedEngine/wiIntersect_BindLua.cpp b/WickedEngine/wiIntersect_BindLua.cpp index b07c1f796..4c3bd3bb3 100644 --- a/WickedEngine/wiIntersect_BindLua.cpp +++ b/WickedEngine/wiIntersect_BindLua.cpp @@ -55,9 +55,6 @@ namespace wiIntersect_BindLua wiLua::SError(L, "Ray(Vector origin,direction) not enough arguments!"); } } - Ray_BindLua::~Ray_BindLua() - { - } int Ray_BindLua::Intersects(lua_State* L) { @@ -146,9 +143,6 @@ namespace wiIntersect_BindLua aabb = AABB(); } } - AABB_BindLua::~AABB_BindLua() - { - } int AABB_BindLua::Intersects(lua_State* L) { @@ -286,9 +280,6 @@ namespace wiIntersect_BindLua wiLua::SError(L, "Sphere(Vector center, float radius) not enough arguments!"); } } - Sphere_BindLua::~Sphere_BindLua() - { - } int Sphere_BindLua::Intersects(lua_State* L) { diff --git a/WickedEngine/wiIntersect_BindLua.h b/WickedEngine/wiIntersect_BindLua.h index 31eac0445..09ac897e6 100644 --- a/WickedEngine/wiIntersect_BindLua.h +++ b/WickedEngine/wiIntersect_BindLua.h @@ -19,7 +19,6 @@ namespace wiIntersect_BindLua Ray_BindLua(const RAY& ray) : ray(ray) {} Ray_BindLua(lua_State *L); - ~Ray_BindLua(); int Intersects(lua_State* L); int GetOrigin(lua_State* L); @@ -37,7 +36,6 @@ namespace wiIntersect_BindLua AABB_BindLua(const AABB& aabb) : aabb(aabb) {} AABB_BindLua(lua_State *L); - ~AABB_BindLua(); int Intersects(lua_State* L); int Intersects2D(lua_State* L); @@ -60,7 +58,6 @@ namespace wiIntersect_BindLua Sphere_BindLua(const SPHERE& sphere) : sphere(sphere) {} Sphere_BindLua(lua_State *L); - ~Sphere_BindLua(); int Intersects(lua_State* L); int GetCenter(lua_State* L); diff --git a/WickedEngine/wiLua.cpp b/WickedEngine/wiLua.cpp index 72ad76ee4..a366dd33d 100644 --- a/WickedEngine/wiLua.cpp +++ b/WickedEngine/wiLua.cpp @@ -26,6 +26,7 @@ #include "wiIntersect_BindLua.h" #include +#include using namespace std; @@ -47,10 +48,10 @@ wiLua::~wiLua() wiLua* wiLua::GetGlobal() { - static wiLua* globalLua = nullptr; + static std::unique_ptr globalLua; if (globalLua == nullptr) { - globalLua = new wiLua(); + globalLua = std::make_unique(); MainComponent_BindLua::Bind(); RenderPath_BindLua::Bind(); @@ -77,7 +78,7 @@ wiLua* wiLua::GetGlobal() wiIntersect_BindLua::Bind(); } - return globalLua; + return globalLua.get(); } bool wiLua::Success() diff --git a/WickedEngine/wiNetwork_BindLua.h b/WickedEngine/wiNetwork_BindLua.h index 0c30d6334..bc194246f 100644 --- a/WickedEngine/wiNetwork_BindLua.h +++ b/WickedEngine/wiNetwork_BindLua.h @@ -10,7 +10,6 @@ public: static Luna::PropertyType properties[]; wiNetwork_BindLua(lua_State* L) {} - ~wiNetwork_BindLua() {} static void Bind(); }; diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 9867ff824..c2eeaf034 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -5905,8 +5905,8 @@ void DrawDebugWorld(const CameraComponent& camera, CommandList cmd) static float col = 0.7f; static uint32_t gridVertexCount = 0; - static GPUBuffer* grid = nullptr; - if (grid == nullptr) + static GPUBuffer grid; + if (!grid.IsValid()) { const float h = 0.01f; // avoid z-fight with zero plane const int a = 20; @@ -5939,8 +5939,7 @@ void DrawDebugWorld(const CameraComponent& camera, CommandList cmd) bd.CPUAccessFlags = 0; SubresourceData InitData; InitData.pSysMem = verts; - grid = new GPUBuffer; - device->CreateBuffer(&bd, &InitData, grid); + device->CreateBuffer(&bd, &InitData, &grid); } MiscCB sb; @@ -5951,8 +5950,8 @@ void DrawDebugWorld(const CameraComponent& camera, CommandList cmd) device->BindConstantBuffer(VS, &constantBuffers[CBTYPE_MISC], CB_GETBINDSLOT(MiscCB), cmd); device->BindConstantBuffer(PS, &constantBuffers[CBTYPE_MISC], CB_GETBINDSLOT(MiscCB), cmd); - GPUBuffer* vbs[] = { - grid, + const GPUBuffer* vbs[] = { + &grid, }; const uint32_t strides[] = { sizeof(XMFLOAT4) + sizeof(XMFLOAT4), diff --git a/WickedEngine/wiSprite_BindLua.cpp b/WickedEngine/wiSprite_BindLua.cpp index 36f3a9b37..fcb8b3e26 100644 --- a/WickedEngine/wiSprite_BindLua.cpp +++ b/WickedEngine/wiSprite_BindLua.cpp @@ -12,14 +12,13 @@ Luna::FunctionType wiSprite_BindLua::methods[] = { lunamethod(wiSprite_BindLua, SetAnim), lunamethod(wiSprite_BindLua, GetAnim), - lunamethod(wiSprite_BindLua, Destroy), { NULL, NULL } }; Luna::PropertyType wiSprite_BindLua::properties[] = { { NULL, NULL } }; -wiSprite_BindLua::wiSprite_BindLua(wiSprite* sprite) :sprite(sprite) +wiSprite_BindLua::wiSprite_BindLua(const wiSprite& sprite) :sprite(sprite) { } @@ -35,28 +34,18 @@ wiSprite_BindLua::wiSprite_BindLua(lua_State *L) mask = wiLua::SGetString(L, 2); } } - sprite = new wiSprite(name, mask); -} - - -wiSprite_BindLua::~wiSprite_BindLua() -{ + sprite = wiSprite(name, mask); } int wiSprite_BindLua::SetParams(lua_State *L) { - if (sprite == nullptr) - { - wiLua::SError(L, "GetParams() sprite is null!"); - return 0; - } int argc = wiLua::SGetArgCount(L); if (argc > 0) { wiImageParams_BindLua* params = Luna::check(L, 1); if (params != nullptr) { - sprite->params = params->params; + sprite.params = params->params; } } else @@ -67,28 +56,18 @@ int wiSprite_BindLua::SetParams(lua_State *L) } int wiSprite_BindLua::GetParams(lua_State *L) { - if (sprite == nullptr) - { - wiLua::SError(L, "GetParams() sprite is null!"); - return 0; - } - Luna::push(L, new wiImageParams_BindLua(sprite->params)); + Luna::push(L, new wiImageParams_BindLua(sprite.params)); return 1; } int wiSprite_BindLua::SetAnim(lua_State *L) { - if (sprite == nullptr) - { - wiLua::SError(L, "SetAnim() sprite is null!"); - return 0; - } int argc = wiLua::SGetArgCount(L); if (argc > 0) { SpriteAnim_BindLua* anim = Luna::check(L, 1); if (anim != nullptr) { - sprite->anim = anim->anim; + sprite.anim = anim->anim; } } else @@ -99,25 +78,10 @@ int wiSprite_BindLua::SetAnim(lua_State *L) } int wiSprite_BindLua::GetAnim(lua_State *L) { - if (sprite == nullptr) - { - wiLua::SError(L, "GetAnim() sprite is null!"); - return 0; - } - Luna::push(L, new SpriteAnim_BindLua(sprite->anim)); + Luna::push(L, new SpriteAnim_BindLua(sprite.anim)); return 1; } -int wiSprite_BindLua::Destroy(lua_State* L) -{ - if (sprite != nullptr) - { - delete sprite; - sprite = nullptr; - } - return 0; -} - void wiSprite_BindLua::Bind() { static bool initialized = false; diff --git a/WickedEngine/wiSprite_BindLua.h b/WickedEngine/wiSprite_BindLua.h index c2cf944fd..54140048b 100644 --- a/WickedEngine/wiSprite_BindLua.h +++ b/WickedEngine/wiSprite_BindLua.h @@ -6,23 +6,20 @@ class wiSprite_BindLua { public: - wiSprite* sprite; + wiSprite sprite; static const char className[]; static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; - wiSprite_BindLua(wiSprite* sprite = nullptr); + wiSprite_BindLua(const wiSprite& sprite); wiSprite_BindLua(lua_State *L); - ~wiSprite_BindLua(); int SetParams(lua_State *L); int GetParams(lua_State *L); int SetAnim(lua_State *L); int GetAnim(lua_State *L); - int Destroy(lua_State* L); - static void Bind(); }; diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index 4c29d0e1d..488001e67 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates const int minor = 38; // minor bug fixes, alterations, refactors, updates - const int revision = 2; + const int revision = 3; long GetVersion()