Skip to content

Commit

Permalink
Unified and cleaned up ID3D11* usage (ComPtr vs raw pointer). Fixed r…
Browse files Browse the repository at this point in the history
…esolution change lockup.
  • Loading branch information
Friendly0Fire committed Feb 27, 2022
1 parent 445b30e commit 923559e
Show file tree
Hide file tree
Showing 19 changed files with 161 additions and 147 deletions.
2 changes: 1 addition & 1 deletion GW2Radial/d3d9_wrapper
8 changes: 3 additions & 5 deletions GW2Radial/include/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ class Core : public Singleton<Core>
HWND gameWindow_ = nullptr;
HMODULE dllModule_ = nullptr;
WNDPROC baseWndProc_ = nullptr;
ID3D11Device* device_ = nullptr;
ID3D11DeviceContext* context_ = nullptr;
IDXGISwapChain* swc_ = nullptr;
ComPtr<ID3D11Device> device_;
ComPtr<ID3D11DeviceContext> context_;
ComPtr<IDXGISwapChain> swc_;

uint screenWidth_ = 0, screenHeight_ = 0;
bool firstFrame_ = true;
Expand All @@ -97,8 +97,6 @@ class Core : public Singleton<Core>
HMODULE user32_ = 0;
GetDpiForWindow_t getDpiForWindow_ = nullptr;

ComPtr<ID3D11RenderTargetView> backBufferRTV_;

ComPtr<ID3DUserDefinedAnnotation> annotations_;

RENDERDOC_API_1_5_0* rdoc_ = nullptr;
Expand Down
10 changes: 5 additions & 5 deletions GW2Radial/include/CustomWheel.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace GW2Radial
ComPtr<ID3D11BlendState> textBlendState_;
std::shared_ptr<Texture2D> backgroundTexture_;

std::unique_ptr<Wheel> BuildWheel(const std::filesystem::path& configPath, ID3D11Device* dev);
std::unique_ptr<Wheel> BuildWheel(const std::filesystem::path& configPath);

struct QueuedTextDraw
{
Expand All @@ -29,13 +29,13 @@ namespace GW2Radial
};
std::list<QueuedTextDraw> textDraws_;

void Reload(ID3D11Device* dev);
void Reload();

public:
CustomWheelsManager(ID3D11Device* dev, std::shared_ptr<Texture2D> bgTexture, std::vector<std::unique_ptr<Wheel>>& wheels, ImFont* font);
CustomWheelsManager(std::shared_ptr<Texture2D> bgTexture, std::vector<std::unique_ptr<Wheel>>& wheels, ImFont* font);

void Draw(ID3D11DeviceContext* ctx);
void DrawOffscreen(ID3D11Device* dev, ID3D11DeviceContext* ctx);
void DrawOffscreen(ID3D11DeviceContext* ctx);
void MarkReload() { loaded_ = false; }
};

Expand All @@ -58,7 +58,7 @@ namespace GW2Radial
fVector4 color_;

public:
CustomElement(const CustomElementSettings& ces, ID3D11Device* dev);
CustomElement(const CustomElementSettings& ces);

protected:
fVector4 color() override;
Expand Down
4 changes: 2 additions & 2 deletions GW2Radial/include/Marker.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace GW2Radial
class Marker : public WheelElement
{
public:
Marker(MarkerType m, ID3D11Device* dev);
Marker(MarkerType m);

fVector4 color() override;
};

class ObjectMarker : public WheelElement
{
public:
ObjectMarker(MarkerType m, ID3D11Device* dev);
ObjectMarker(MarkerType m);

fVector4 color() override;
};
Expand Down
2 changes: 1 addition & 1 deletion GW2Radial/include/Mount.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GW2Radial
class Mount : public WheelElement
{
public:
Mount(MountType m, ID3D11Device* dev);
Mount(MountType m);

bool isActive() const override;

Expand Down
2 changes: 1 addition & 1 deletion GW2Radial/include/Novelty.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GW2Radial
class Novelty : public WheelElement
{
public:
Novelty(NoveltyType m, ID3D11Device* dev);
Novelty(NoveltyType m);

protected:
static const char* GetNoveltyNameFromType(NoveltyType m)
Expand Down
30 changes: 13 additions & 17 deletions GW2Radial/include/ShaderManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,23 @@ class ShaderId
class ConstantBufferBase
{
protected:
ConstantBufferBase(ComPtr<ID3D11DeviceContext> ctx, ComPtr<ID3D11Buffer> buf) : ctx(ctx), buf(buf) {}
ConstantBufferBase(ComPtr<ID3D11Buffer> buf) : buf(buf) {}
ComPtr<ID3D11Buffer> buf;
ComPtr<ID3D11DeviceContext> ctx;

friend class ShaderManager;

void Upload(void* data, size_t size);
void Upload(ID3D11DeviceContext* ctx, void* data, size_t size);

public:
ConstantBufferBase() = default;
bool IsValid() const { return buf != nullptr; }
const ComPtr<ID3D11Buffer> buffer() const { return buf; }
const ComPtr<ID3D11Buffer>& buffer() const { return buf; }
};

template<typename T>
class ConstantBuffer : public ConstantBufferBase
{
ConstantBuffer(ComPtr<ID3D11DeviceContext> ctx, ComPtr<ID3D11Buffer> buf, std::optional<T>&& data) : ConstantBufferBase(ctx, buf)
ConstantBuffer(ComPtr<ID3D11Buffer> buf, std::optional<T>&& data) : ConstantBufferBase(buf)
{
if (data.has_value())
this->data = *data;
Expand All @@ -61,9 +60,9 @@ class ConstantBuffer : public ConstantBufferBase
ConstantBuffer& operator=(ConstantBuffer&&) = default;

T* operator->() { return &data; }
void Update()
void Update(ID3D11DeviceContext* ctx)
{
Upload(&data, sizeof(T));
Upload(ctx, &data, sizeof(T));
}
};

Expand All @@ -72,25 +71,25 @@ class ShaderManager : public Singleton<ShaderManager, false>
public:
using AnyShaderComPtr = std::variant<ComPtr<ID3D11VertexShader>, ComPtr<ID3D11PixelShader>>;

ShaderManager(ID3D11Device* dev);
ShaderManager();

void SetShaders(ShaderId vs, ShaderId ps);
void SetShaders(ID3D11DeviceContext* ctx, ShaderId vs, ShaderId ps);
ShaderId GetShader(const std::wstring& filename, D3D11_SHADER_VERSION_TYPE st, const std::string& entrypoint);

template<typename T>
ConstantBuffer<T> MakeConstantBuffer(std::optional<T> data = std::nullopt)
{
auto buf = MakeConstantBuffer(sizeof(T), data.has_value() ? &data.value() : nullptr);
return { context_, buf, std::move(data) };
return { buf, std::move(data) };
}

template<typename... Args>
void SetConstantBuffers(Args& ...cbs)
void SetConstantBuffers(ID3D11DeviceContext* ctx, Args& ...cbs)
{
auto getbuf = [](auto& cb) { return cb.buf.get(); };
auto getbuf = [](auto& cb) { return cb.buf.Get(); };
ID3D11Buffer* cbPtrs[] = { getbuf(cbs)... };
context_->VSSetConstantBuffers(0, sizeof...(cbs), cbPtrs);
context_->PSSetConstantBuffers(0, sizeof...(cbs), cbPtrs);
ctx->VSSetConstantBuffers(0, sizeof...(cbs), cbPtrs);
ctx->PSSetConstantBuffers(0, sizeof...(cbs), cbPtrs);
}

void ReloadAll();
Expand All @@ -113,9 +112,6 @@ class ShaderManager : public Singleton<ShaderManager, false>

bool hotReloadFolderExists_ = false;

ComPtr<ID3D11Device> device_;
ComPtr<ID3D11DeviceContext> context_;

struct ShaderData
{
AnyShaderComPtr shader;
Expand Down
2 changes: 1 addition & 1 deletion GW2Radial/include/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ std::span<const wchar_t*> GetCommandLineArgs();

const wchar_t* GetCommandLineArg(const wchar_t* name);

void DrawScreenQuad(ComPtr<ID3D11DeviceContext>& ctx);
void DrawScreenQuad(ID3D11DeviceContext* ctx);

template<std::integral T, std::integral T2>
auto RoundUp(T numToRound, T2 multiple) -> std::common_type_t<T, T2>
Expand Down
14 changes: 7 additions & 7 deletions GW2Radial/include/Wheel.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ class Wheel : public SettingsMenu::Implementer
DIRECTION = 3
};

Wheel(std::shared_ptr<Texture2D> bgTexture, std::string nickname, std::string displayName, ID3D11Device* dev);
Wheel(std::shared_ptr<Texture2D> bgTexture, std::string nickname, std::string displayName);
virtual ~Wheel();

template<typename T>
static std::unique_ptr<Wheel> Create(std::shared_ptr<Texture2D> bgTexture, std::string nickname, std::string displayName, ID3D11Device* dev)
static std::unique_ptr<Wheel> Create(std::shared_ptr<Texture2D> bgTexture, std::string nickname, std::string displayName)
{
// TODO: Would be nice to somehow let wheel element .cpps determine these parameters as well
auto wheel = std::make_unique<Wheel>(bgTexture, std::move(nickname), std::move(displayName), dev);
wheel->Setup<T>(dev);
auto wheel = std::make_unique<Wheel>(bgTexture, std::move(nickname), std::move(displayName));
wheel->Setup<T>();
return std::move(wheel);
}

template<typename T>
void Setup(ID3D11Device* dev); // Requires implementation for each wheel element type
void Setup(); // Requires implementation for each wheel element type

void UpdateHover();
void AddElement(std::unique_ptr<WheelElement>&& we) { wheelElements_.push_back(std::move(we)); Sort(); }
void Draw(ComPtr<ID3D11DeviceContext> ctx);
void Draw(ID3D11DeviceContext* ctx);
void OnFocusLost();
void OnUpdate();
void OnMapChange(uint prevId, uint newId);
Expand All @@ -63,7 +63,7 @@ class Wheel : public SettingsMenu::Implementer

bool visible() override { return visibleInMenuOption_.value(); }

ID3D11Buffer* GetConstantBuffer() const { return cb_s.buffer().Get(); }
const ComPtr<ID3D11Buffer>& GetConstantBuffer() const { return cb_s.buffer(); }

protected:
void Sort();
Expand Down
7 changes: 3 additions & 4 deletions GW2Radial/include/WheelElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ namespace GW2Radial
class WheelElement
{
public:
WheelElement(uint id, const std::string &nickname, const std::string &category, const std::string &displayName,
ID3D11Device* dev, const Texture2D& tex = {});
WheelElement(uint id, const std::string &nickname, const std::string &category, const std::string &displayName, const Texture2D& tex = {});
virtual ~WheelElement() = default;

int DrawPriority(int extremumIndicator);

void SetShaderState(ID3D11DeviceContext* ctx);
void SetShaderState(ID3D11DeviceContext* ctx, const fVector4& spriteDimensions, ID3D11Buffer* wheelCb, bool shadow, float hoverRatio);
void Draw(ComPtr<ID3D11DeviceContext>& ctx, int n, fVector4 spriteDimensions, size_t activeElementsCount, const mstime& currentTime, const WheelElement* elementHovered, const class Wheel* parent);
void SetShaderState(ID3D11DeviceContext* ctx, const fVector4& spriteDimensions, const ComPtr<ID3D11Buffer>& wheelCb, bool shadow, float hoverRatio);
void Draw(ID3D11DeviceContext* ctx, int n, fVector4 spriteDimensions, size_t activeElementsCount, const mstime& currentTime, const WheelElement* elementHovered, const class Wheel* parent);

uint elementId() const { return elementId_; }

Expand Down
61 changes: 35 additions & 26 deletions GW2Radial/src/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,13 @@ LRESULT Core::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)

void Core::PreResizeSwapChain()
{
backBufferRTV_.Reset();

}

void Core::PostResizeSwapChain(uint w, uint h)
{
screenWidth_ = w;
screenHeight_ = h;

ComPtr<ID3D11Texture2D> backbuffer;
swc_->GetBuffer(0, IID_PPV_ARGS(backbuffer.GetAddressOf()));
device_->CreateRenderTargetView(backbuffer.Get(), nullptr, backBufferRTV_.ReleaseAndGetAddressOf());
}

void Core::PostCreateSwapChain(HWND hwnd, ID3D11Device* device, IDXGISwapChain* swc)
Expand All @@ -173,7 +169,6 @@ void Core::PostCreateSwapChain(HWND hwnd, ID3D11Device* device, IDXGISwapChain*
swc_ = swc;

#if _DEBUG
// At init, on windows
if (HMODULE mod = GetModuleHandleA("renderdoc.dll"))
{
pRENDERDOC_GetAPI RENDERDOC_GetAPI =
Expand All @@ -186,10 +181,6 @@ void Core::PostCreateSwapChain(HWND hwnd, ID3D11Device* device, IDXGISwapChain*

context_->QueryInterface(annotations_.ReleaseAndGetAddressOf());

ComPtr<ID3D11Texture2D> backbuffer;
swc_->GetBuffer(0, IID_PPV_ARGS(backbuffer.GetAddressOf()));
device_->CreateRenderTargetView(backbuffer.Get(), nullptr, backBufferRTV_.GetAddressOf());

DXGI_SWAP_CHAIN_DESC desc;
swc_->GetDesc(&desc);

Expand All @@ -198,16 +189,16 @@ void Core::PostCreateSwapChain(HWND hwnd, ID3D11Device* device, IDXGISwapChain*

firstFrame_ = true;

ShaderManager::i(std::make_unique<ShaderManager>(device_));
ShaderManager::i(std::make_unique<ShaderManager>());

UpdateCheck::i().CheckForUpdates();
MiscTab::i();

auto bgTex = std::make_shared<Texture2D>(std::move(CreateTextureFromResource(device_, Core::i().dllModule(), IDR_BG)));
wheels_.emplace_back(Wheel::Create<Mount>(bgTex, "mounts", "Mounts", device_));
wheels_.emplace_back(Wheel::Create<Novelty>(bgTex, "novelties", "Novelties", device_));
wheels_.emplace_back(Wheel::Create<Marker>(bgTex, "markers", "Markers", device_));
wheels_.emplace_back(Wheel::Create<ObjectMarker>(bgTex, "object_markers", "Object Markers", device_));
auto bgTex = std::make_shared<Texture2D>(std::move(CreateTextureFromResource(device_.Get(), Core::i().dllModule(), IDR_BG)));
wheels_.emplace_back(Wheel::Create<Mount>(bgTex, "mounts", "Mounts"));
wheels_.emplace_back(Wheel::Create<Novelty>(bgTex, "novelties", "Novelties"));
wheels_.emplace_back(Wheel::Create<Marker>(bgTex, "markers", "Markers"));
wheels_.emplace_back(Wheel::Create<ObjectMarker>(bgTex, "object_markers", "Object Markers"));

// Init ImGui
auto &imio = ImGui::GetIO();
Expand Down Expand Up @@ -236,9 +227,9 @@ void Core::PostCreateSwapChain(HWND hwnd, ID3D11Device* device, IDXGISwapChain*
imio.FontDefault = font_;

ImGui_ImplWin32_Init(gameWindow_);
ImGui_ImplDX11_Init(device_, context_);
ImGui_ImplDX11_Init(device_.Get(), context_.Get());

customWheels_ = std::make_unique<CustomWheelsManager>(device_, bgTex, wheels_, fontDraw_);
customWheels_ = std::make_unique<CustomWheelsManager>(bgTex, wheels_, fontDraw_);

firstMessageShown_ = std::make_unique<ConfigurationOption<bool>>("", "first_message_shown_v1", "Core", false);

Expand Down Expand Up @@ -291,16 +282,26 @@ void Core::OnUpdate()
}
}


#ifdef _DEBUG
#define DUMP_FIRST_RENDER
#endif
void Core::Draw()
{
#ifdef DUMP_FIRST_RENDER
static bool s_dumpRender = true;
bool isDumping = false;
if (GetAsyncKeyState(VK_F8) && rdoc())
{
isDumping = true;
rdoc()->StartFrameCapture(device_.Get(), nullptr);
}
#endif

if (annotations_)
annotations_->BeginEvent(L"GW2Radial");

StateBackupD3D11 d3dstate;
BackupD3D11State(context_, d3dstate);

context_->OMSetRenderTargets(1, backBufferRTV_.GetAddressOf(), nullptr);
BackupD3D11State(context_.Get(), d3dstate);

// This is the closest we have to a reliable "update" function, so use it as one
Input::i().OnUpdate();
Expand Down Expand Up @@ -347,9 +348,9 @@ void Core::Draw()
context_->RSSetViewports(1, &vp);

for (auto& wheel : wheels_)
wheel->Draw(context_);
wheel->Draw(context_.Get());

customWheels_->Draw(context_);
customWheels_->Draw(context_.Get());

SettingsMenu::i().Draw();
Log::i().Draw();
Expand Down Expand Up @@ -392,7 +393,7 @@ void Core::Draw()
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());

customWheels_->DrawOffscreen(device_, context_);
customWheels_->DrawOffscreen(context_.Get());
}

if(forceReloadWheels_)
Expand All @@ -401,10 +402,18 @@ void Core::Draw()
customWheels_->MarkReload();
}

RestoreD3D11State(context_, d3dstate);
RestoreD3D11State(context_.Get(), d3dstate);

if(annotations_)
annotations_->EndEvent();

#ifdef DUMP_FIRST_RENDER
if (isDumping)
{
s_dumpRender = false;
rdoc()->EndFrameCapture(device_.Get(), nullptr);
}
#endif
}

}
Loading

0 comments on commit 923559e

Please sign in to comment.