1
0
This commit is contained in:
2026-01-08 20:25:33 +08:00
parent 886e0caab2
commit 55ed60c643
7 changed files with 50 additions and 55 deletions

View File

@@ -1,5 +1,5 @@
#include <basalt/export_macro.hpp> #include <basalt/export_macro.hpp>
#include <basalt/kernel.hpp> #include <basalt/engine.hpp>
#include <windows.h> #include <windows.h>
#include <d3d11.h> #include <d3d11.h>
#include <d3dcompiler.h> #include <d3dcompiler.h>
@@ -28,10 +28,9 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
} }
// Create a render window for DirectX // Create a render window for DirectX
static HWND CreateRenderWindow(std::uint32_t width, std::uint32_t height, const std::wstring_view& title) { static HWND CreateRenderWindow(std::uint32_t width, std::uint32_t height) {
static bool g_CLSREG = false; static bool g_CLSREG = false;
constexpr wchar_t class_name[] = L"DirectXRenderWindowClass"; constexpr wchar_t class_name[] = L"DirectXRenderWindowClass";
std::wstring c_title(title);
if (!g_CLSREG) { if (!g_CLSREG) {
WNDCLASSEXW wc = {0}; WNDCLASSEXW wc = {0};
@@ -55,7 +54,7 @@ static HWND CreateRenderWindow(std::uint32_t width, std::uint32_t height, const
HWND hwnd = CreateWindowExW(0, HWND hwnd = CreateWindowExW(0,
class_name, class_name,
c_title.c_str(), L"DirectXRenderWindow",
WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME ^ WS_MAXIMIZEBOX, WS_OVERLAPPEDWINDOW ^ WS_THICKFRAME ^ WS_MAXIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
@@ -161,8 +160,9 @@ const char* g_PS = R"(
} }
)"; )";
using ::Basalt::Shared::Kernel::EngineConfig; namespace engine = ::basalt::shared::engine;
using ::Basalt::Shared::Kernel::IEngine; using engine::EngineConfig;
using engine::IEngine;
class DirectX11Engine : public IEngine { class DirectX11Engine : public IEngine {
public: public:
@@ -197,7 +197,7 @@ public:
if (this->config.headless) { if (this->config.headless) {
window = NULL; window = NULL;
} else { } else {
window = CreateRenderWindow(this->config.width, this->config.height, this->config.title); window = CreateRenderWindow(this->config.width, this->config.height);
ShowWindow(window, SW_SHOW); ShowWindow(window, SW_SHOW);
UpdateWindow(window); UpdateWindow(window);
} }
@@ -314,6 +314,17 @@ public:
// return; // return;
//} //}
// 设置管线
context->OMSetRenderTargets(1, rtv.GetAddressOf(), dsv.Get());
context->OMSetDepthStencilState(depth_state.Get(), 1);
context->RSSetState(rasterizer_state.Get());
context->VSSetShader(vs.Get(), nullptr, 0);
context->PSSetShader(ps.Get(), nullptr, 0);
context->IASetInputLayout(input_layout.Get());
UINT stride = sizeof(Vertex), offset = 0;
context->IASetVertexBuffers(0, 1, vertex_buffer.GetAddressOf(), &stride, &offset);
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// 缩放深度数据数组到指定大小 // 缩放深度数据数组到指定大小
depth_data.resize(this->config.width * this->config.height * sizeof(float)); depth_data.resize(this->config.width * this->config.height * sizeof(float));
@@ -331,17 +342,6 @@ public:
context->ClearRenderTargetView(rtv.Get(), clear_color); context->ClearRenderTargetView(rtv.Get(), clear_color);
context->ClearDepthStencilView(dsv.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0); context->ClearDepthStencilView(dsv.Get(), D3D11_CLEAR_DEPTH, 1.0f, 0);
// 设置管线
context->OMSetRenderTargets(1, rtv.GetAddressOf(), dsv.Get());
context->OMSetDepthStencilState(depth_state.Get(), 1);
context->RSSetState(rasterizer_state.Get());
context->VSSetShader(vs.Get(), nullptr, 0);
context->PSSetShader(ps.Get(), nullptr, 0);
context->IASetInputLayout(input_layout.Get());
UINT stride = sizeof(Vertex), offset = 0;
context->IASetVertexBuffers(0, 1, vertex_buffer.GetAddressOf(), &stride, &offset);
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// 绘制立方体 // 绘制立方体
context->Draw(sizeof(CubeVertices) / sizeof(Vertex), 0); // 自动计算顶点数 context->Draw(sizeof(CubeVertices) / sizeof(Vertex), 0); // 自动计算顶点数

View File

@@ -11,7 +11,7 @@ namespace basalt::presenter::cmd_client {
DATA_READY = 0x01, ///< Presenter -> Trainer DATA_READY = 0x01, ///< Presenter -> Trainer
DATA_RECEIVED = 0x02, ///< Trainer -> Presenter DATA_RECEIVED = 0x02, ///< Trainer -> Presenter
ACTIVELY_STOP = 0x21, ///< Presenter-->Trainer ACTIVELY_STOP = 0x21, ///< Presenter-->Trainer
STOP_REQUEST = 0X71, ///< Presenter<--Trainer STOP_REQUEST = 0x71, ///< Presenter<--Trainer
STOP_RESPONSE = 0x72, ///< Presenter-->Trainer STOP_RESPONSE = 0x72, ///< Presenter-->Trainer
}; };

View File

@@ -77,7 +77,7 @@ namespace basalt::presenter::dll_loader {
} }
} }
void *DllLoader::GetFunctionPointer(const char *name) { void *DllLoader::get_function_pointer(const char *name) {
if (!m_Handle) throw std::runtime_error("Can not fetch function pointer on not loaded dynamic library."); if (!m_Handle) throw std::runtime_error("Can not fetch function pointer on not loaded dynamic library.");
#if defined(BASALT_OS_WINDOWS) #if defined(BASALT_OS_WINDOWS)
return (void *) GetProcAddress(m_Handle, name); return (void *) GetProcAddress(m_Handle, name);

View File

@@ -28,21 +28,21 @@ namespace basalt::presenter::dll_loader {
~DllLoader(); ~DllLoader();
private: private:
void* GetFunctionPointer(const char* name); void* get_function_pointer(const char* name);
public: public:
template<typename T> template<typename T>
T* CreateInstance() { T* create_instance() {
using Fct = T* (*) (); using Fct = T* (*) ();
constexpr char EXPOSE_FUNC_NAME[] = "BSCreateInstance"; constexpr char EXPOSE_FUNC_NAME[] = "BSCreateInstance";
auto fct = (Fct) GetFunctionPointer(EXPOSE_FUNC_NAME); auto fct = (Fct) get_function_pointer(EXPOSE_FUNC_NAME);
return fct(); return fct();
} }
template<typename T> template<typename T>
void DestroyInstance(T* instance) { void destroy_instance(T* instance) {
using Fct = void (*)(T*); using Fct = void (*)(T*);
constexpr char EXPOSE_FUNC_NAME[] = "BSDestroyInstance"; constexpr char EXPOSE_FUNC_NAME[] = "BSDestroyInstance";
auto fct = (Fct) GetFunctionPointer(EXPOSE_FUNC_NAME); auto fct = (Fct) get_function_pointer(EXPOSE_FUNC_NAME);
fct(instance); fct(instance);
} }

View File

@@ -1,6 +1,15 @@
#include "dll_loader.hpp" #include "dll_loader.hpp"
#include "cmd_client.hpp" #include "cmd_client.hpp"
#include <basalt/char_types.hpp> #include <basalt/char_types.hpp>
#include <basalt/engine.hpp>
#include <basalt/deliver.hpp>
namespace engine = ::basalt::shared::engine;
using engine::EngineConfig;
using engine::IEngine;
namespace deliver = ::basalt::shared::deliver;
using deliver::DeliverConfig;
using deliver::IDeliver;
namespace dll_loader = ::basalt::presenter::dll_loader; namespace dll_loader = ::basalt::presenter::dll_loader;
using dll_loader::DllKind; using dll_loader::DllKind;
@@ -15,18 +24,18 @@ int main(int argc, char* argv[]) {
auto client = CmdClient(); auto client = CmdClient();
auto payload = client.wait_handshake(); auto payload = client.wait_handshake();
//auto* engine = engine_dll.CreateInstance<Kernel::IEngine>(); auto* engine = engine_dll.create_instance<IEngine>();
//auto* deliver = deliver_dll.CreateInstance<Kernel::IDeliver>(); //auto* deliver = deliver_dll.create_instance<IDeliver>();
//Kernel::EngineConfig engine_config{.headless = false, .title = BSTEXT("Fuck You"), .width = payload.width, .height = payload.height}; EngineConfig engine_config{.headless = false, .width = payload.width, .height = payload.height};
//engine->startup(std::move(engine_config)); engine->startup(std::move(engine_config));
while (true) { while (true) {
auto req_stop = false; //engine->tick(); auto req_stop = engine->tick();
auto can_stop = client.tick(req_stop); auto can_stop = client.tick(req_stop);
if (can_stop) break; if (can_stop) break;
} }
//engine->shutdown(); engine->shutdown();
//engine_dll.DestroyInstance(engine); engine_dll.destroy_instance(engine);
} }

View File

@@ -92,10 +92,10 @@ class CmdServer:
if self.__status != ServerStatus.Running: if self.__status != ServerStatus.Running:
raise RuntimeError("unexpected server status") raise RuntimeError("unexpected server status")
# If there is stop requested, we post it first and return # If there is stop requested from us,
# we order Presenter exit and enter next step.
if request_stop: if request_stop:
self.__wait_stop() self.__pipe_operator.write(CODE_PACKER.pack(ProtocolCode.STOP_REQUEST))
return True
while True: while True:
# Wait for code from Presenter # Wait for code from Presenter
@@ -115,26 +115,12 @@ class CmdServer:
case ProtocolCode.ACTIVELY_STOP: case ProtocolCode.ACTIVELY_STOP:
# Presenter requested stop. # Presenter requested stop.
# Agree with it, send code and wait response # Agree with it, send code and wait response
self.__wait_stop() self.__pipe_operator.write(
return True CODE_PACKER.pack(ProtocolCode.STOP_REQUEST)
case _: )
raise RuntimeError("unexpected protocol code when running")
def __wait_stop(self) -> None:
# Send stop request code
self.__pipe_operator.write(CODE_PACKER.pack(ProtocolCode.STOP_REQUEST))
# Wait stop response code
while True:
# Accept code
code_bytes = self.__pipe_operator.read(CODE_PACKER.size)
(code,) = CODE_PACKER.unpack(code_bytes)
# Check whether it is stop response
match ProtocolCode(code):
case ProtocolCode.STOP_RESPONSE: case ProtocolCode.STOP_RESPONSE:
# Set self status and return # Set self status and return
self.__status = ServerStatus.Stop self.__status = ServerStatus.Stop
return return True
case _: case _:
raise RuntimeError("unexpected protocol code when waiting quit") raise RuntimeError("unexpected protocol code when running")

View File

@@ -27,4 +27,4 @@ url = "https://download.pytorch.org/whl/cu126"
explicit = true explicit = true
[too.ruff] [too.ruff]
line-length = 88 line-length = 130