MiMoEngine ASM: x86-64 MASM汇编+C混合架构游戏引擎
核心热路径(数学/内存/字符串/性能分析)用MASM汇编(SSE/AVX2) 外围系统(窗口/渲染/ECS/音频/UI/脚本)用C语言 含自研编辑器、VisualScript积木编程、中文渲染(glyph_atlas)
This commit is contained in:
commit
2d56cb3bc9
91 files changed
+9988
No files matched your search
@@ -0,0 +1,87 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<ProjectGuid>{A5A5A5A5-5A5A-5A5A-5A5A-5A5A5A5A5A5A}</ProjectGuid>
|
||||
<RootNamespace>Demo</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdc17</LanguageStandard>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<EnableEnhancedInstructionSet>AdvancedVectorExtensions2</EnableEnhancedInstructionSet>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)Engine\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>d3d11.lib;dxgi.lib;d3dcompiler.lib;xaudio2.lib;xinput.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<WarningLevel>Level4</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;WIN32_LEAN_AND_MEAN;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdc17</LanguageStandard>
|
||||
<AdditionalIncludeDirectories>$(ProjectDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<EnableEnhancedInstructionSet>AdvancedVectorExtensions2</EnableEnhancedInstructionSet>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalLibraryDirectories>$(SolutionDir)Engine\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>d3d11.lib;dxgi.lib;d3dcompiler.lib;xaudio2.lib;xinput.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.c" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
</Project>
|
||||
@@ -0,0 +1,179 @@
|
||||
#include "../Engine/Core/engine.h"
|
||||
#include "../Engine/Core/logger.h"
|
||||
#include "../Engine/Core/timer.h"
|
||||
#include "../Engine/Window/window.h"
|
||||
#include "../Engine/Rendering/graphics_device.h"
|
||||
#include "../Engine/Rendering/shader.h"
|
||||
#include "../Engine/Rendering/mesh.h"
|
||||
#include "../Engine/include/engine_api.h"
|
||||
#include "../Engine/ASM/math_c_fallback.h"
|
||||
#include "../Engine/Editor/glyph_atlas.h"
|
||||
#include <d3dcompiler.h>
|
||||
|
||||
static GraphicsDevice* g_dev = NULL;
|
||||
static Shader* g_shader = NULL;
|
||||
static Mesh* g_tri = NULL;
|
||||
static GlyphAtlas g_atlas;
|
||||
static ID3D11VertexShader* g_tvs = NULL;
|
||||
static ID3D11PixelShader* g_tps = NULL;
|
||||
static ID3D11InputLayout* g_tlayout = NULL;
|
||||
static ID3D11BlendState* g_tblend = NULL;
|
||||
static ID3D11SamplerState* g_tsampler = NULL;
|
||||
static ID3D11Buffer* g_tcb = NULL;
|
||||
|
||||
typedef struct { float p[3], n[3], uv[2], c[4]; } V;
|
||||
|
||||
static void draw_quad(ID3D11DeviceContext* ctx, float x, float y, float w, float h, float r, float g, float b, float a) {
|
||||
V v[4] = {
|
||||
{{x,y,0},{0,0,-1},{0,0},{r,g,b,a}},
|
||||
{{x+w,y,0},{0,0,-1},{1,0},{r,g,b,a}},
|
||||
{{x+w,y+h,0},{0,0,-1},{1,1},{r,g,b,a}},
|
||||
{{x,y+h,0},{0,0,-1},{0,1},{r,g,b,a}},
|
||||
};
|
||||
uint32_t idx[6] = {0,1,2, 0,2,3};
|
||||
ID3D11Buffer *vb = NULL, *ib = NULL;
|
||||
D3D11_BUFFER_DESC bd = {0}; D3D11_SUBRESOURCE_DATA sd = {0};
|
||||
bd.ByteWidth = sizeof(V)*4; bd.Usage = D3D11_USAGE_DEFAULT; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||
sd.pSysMem = v; g_dev->device->lpVtbl->CreateBuffer(g_dev->device, &bd, &sd, &vb);
|
||||
bd.ByteWidth = sizeof(uint32_t)*6; bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
||||
sd.pSysMem = idx; g_dev->device->lpVtbl->CreateBuffer(g_dev->device, &bd, &sd, &ib);
|
||||
UINT stride = sizeof(V), offset = 0;
|
||||
ctx->lpVtbl->IASetVertexBuffers(ctx, 0, 1, &vb, &stride, &offset);
|
||||
ctx->lpVtbl->IASetIndexBuffer(ctx, ib, DXGI_FORMAT_R32_UINT, 0);
|
||||
ctx->lpVtbl->DrawIndexed(ctx, 6, 0, 0);
|
||||
ib->lpVtbl->Release(ib); vb->lpVtbl->Release(vb);
|
||||
}
|
||||
|
||||
static void on_render(Engine* engine) {
|
||||
if (!g_dev) return;
|
||||
ID3D11DeviceContext* ctx = g_dev->context;
|
||||
float sw = (float)g_dev->width;
|
||||
float sh = (float)g_dev->height;
|
||||
|
||||
graphics_device_clear(g_dev, 0.0f, 0.0f, 0.0f, 1.0f);
|
||||
|
||||
float proj[64] = {0};
|
||||
proj[0] = 2.0f / sw; proj[5] = -2.0f / sh;
|
||||
proj[10] = 1.0f; proj[12] = -1.0f; proj[13] = 1.0f; proj[15] = 1.0f;
|
||||
Mat4 proj_m, proj_t;
|
||||
memcpy(&proj_m, proj, sizeof(Mat4));
|
||||
mat4_transpose(&proj_m, &proj_t);
|
||||
|
||||
D3D11_VIEWPORT vp = {0};
|
||||
vp.Width = sw; vp.Height = sh; vp.MaxDepth = 1.0f;
|
||||
ctx->lpVtbl->RSSetViewports(ctx, 1, &vp);
|
||||
|
||||
D3D11_RASTERIZER_DESC rdesc = {0};
|
||||
rdesc.FillMode = D3D11_FILL_SOLID;
|
||||
rdesc.CullMode = D3D11_CULL_NONE;
|
||||
ID3D11RasterizerState* rstate = NULL;
|
||||
g_dev->device->lpVtbl->CreateRasterizerState(g_dev->device, &rdesc, &rstate);
|
||||
ctx->lpVtbl->RSSetState(ctx, rstate);
|
||||
|
||||
ctx->lpVtbl->IASetPrimitiveTopology(ctx, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
shader_bind(g_shader, ctx);
|
||||
shader_set_matrix(g_shader, ctx, (float*)&proj_t);
|
||||
|
||||
mesh_draw(g_tri, ctx);
|
||||
draw_quad(ctx, sw - 200, 10, 180, 60, 1, 1, 1, 1);
|
||||
draw_quad(ctx, sw - 100, 20, 50, 30, 0, 1, 0, 1);
|
||||
|
||||
|
||||
|
||||
if (rstate) rstate->lpVtbl->Release(rstate);
|
||||
graphics_device_present(g_dev);
|
||||
}
|
||||
|
||||
static void on_key(Engine* engine, int key) {
|
||||
if (key == VK_ESCAPE) engine_request_quit(engine);
|
||||
}
|
||||
|
||||
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
|
||||
(void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
|
||||
LOG_INFO("=== Compatibility Test ===");
|
||||
|
||||
EngineConfig config = engine_default_config();
|
||||
config.app_name = "Compatibility Test";
|
||||
config.vsync = 1;
|
||||
config.window_width = 1280;
|
||||
config.window_height = 720;
|
||||
|
||||
Engine* engine = engine_create();
|
||||
if (!engine_init(engine, &config)) return -1;
|
||||
|
||||
g_dev = graphics_device_create();
|
||||
if (!graphics_device_init(g_dev, window_get_handle(engine->window), config.window_width, config.window_height, 1)) {
|
||||
LOG_FATAL("D3D11 init failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_shader = shader_create();
|
||||
shader_compile(g_shader, g_dev->device, NULL, NULL, 0, 0);
|
||||
|
||||
glyph_atlas_init(&g_atlas, g_dev->device);
|
||||
|
||||
const char* tvs_src = "cbuffer CB:register(b0){float4x4 WVP;};struct VS_IN{float4 pos:POSITION;float3 n:NORMAL;float2 uv:TEXCOORD;float4 col:COLOR;};struct PS_IN{float4 pos:SV_POSITION;float2 uv:TEXCOORD;float4 col:COLOR;};PS_IN main(VS_IN i){PS_IN o;o.pos=mul(i.pos,WVP);o.uv=i.uv;o.col=i.col;return o;}";
|
||||
const char* tps_src = "struct PS_IN{float4 pos:SV_POSITION;float2 uv:TEXCOORD;float4 col:COLOR;};float4 main(PS_IN i):SV_TARGET{return i.col;}";
|
||||
ID3DBlob *vsb = NULL, *psb = NULL, *err = NULL;
|
||||
D3DCompile(tvs_src, (UINT)strlen(tvs_src), NULL, NULL, NULL, "main", "vs_5_0", 0, 0, &vsb, &err);
|
||||
if (err) { LOG_ERROR("Text VS error: %s", (const char*)err->lpVtbl->GetBufferPointer(err)); err->lpVtbl->Release(err); err = NULL; }
|
||||
D3DCompile(tps_src, (UINT)strlen(tps_src), NULL, NULL, NULL, "main", "ps_5_0", 0, 0, &psb, &err);
|
||||
if (err) { LOG_ERROR("Text PS error: %s", (const char*)err->lpVtbl->GetBufferPointer(err)); err->lpVtbl->Release(err); err = NULL; }
|
||||
LOG_INFO("Text shaders: vsb=%p psb=%p", (void*)vsb, (void*)psb);
|
||||
if (vsb && psb) {
|
||||
g_dev->device->lpVtbl->CreateVertexShader(g_dev->device, vsb->lpVtbl->GetBufferPointer(vsb), vsb->lpVtbl->GetBufferSize(vsb), NULL, &g_tvs);
|
||||
g_dev->device->lpVtbl->CreatePixelShader(g_dev->device, psb->lpVtbl->GetBufferPointer(psb), psb->lpVtbl->GetBufferSize(psb), NULL, &g_tps);
|
||||
D3D11_INPUT_ELEMENT_DESC layout[] = {
|
||||
{"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0},
|
||||
{"NORMAL",0,DXGI_FORMAT_R32G32B32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0},
|
||||
{"TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,24,D3D11_INPUT_PER_VERTEX_DATA,0},
|
||||
{"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT,0,32,D3D11_INPUT_PER_VERTEX_DATA,0},
|
||||
};
|
||||
g_dev->device->lpVtbl->CreateInputLayout(g_dev->device, layout, 4, vsb->lpVtbl->GetBufferPointer(vsb), vsb->lpVtbl->GetBufferSize(vsb), &g_tlayout);
|
||||
vsb->lpVtbl->Release(vsb); psb->lpVtbl->Release(psb);
|
||||
}
|
||||
D3D11_SAMPLER_DESC sdesc = {0};
|
||||
sdesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
|
||||
sdesc.AddressU = sdesc.AddressV = sdesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
|
||||
g_dev->device->lpVtbl->CreateSamplerState(g_dev->device, &sdesc, &g_tsampler);
|
||||
D3D11_BLEND_DESC bdesc = {0};
|
||||
bdesc.RenderTarget[0].BlendEnable = TRUE;
|
||||
bdesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
|
||||
bdesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
|
||||
bdesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
|
||||
bdesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
|
||||
bdesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
|
||||
bdesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
|
||||
bdesc.RenderTarget[0].RenderTargetWriteMask = 0x0F;
|
||||
HRESULT hr_blend = g_dev->device->lpVtbl->CreateBlendState(g_dev->device, &bdesc, &g_tblend);
|
||||
LOG_INFO("CreateBlendState hr=0x%08x blend=%p", (unsigned)hr_blend, (void*)g_tblend);
|
||||
D3D11_BUFFER_DESC cbd = {0};
|
||||
cbd.ByteWidth = 256; cbd.Usage = D3D11_USAGE_DEFAULT; cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
|
||||
g_dev->device->lpVtbl->CreateBuffer(g_dev->device, &cbd, NULL, &g_tcb);
|
||||
|
||||
V tri[3] = {
|
||||
{{640, 300, 0}, {0,0,-1}, {0,0}, {1, 0.2f, 0.2f, 1}},
|
||||
{{500, 550, 0}, {0,0,-1}, {0,1}, {0.2f, 1, 0.2f, 1}},
|
||||
{{780, 550, 0}, {0,0,-1}, {1,0}, {0.2f, 0.2f, 1, 1}},
|
||||
};
|
||||
uint32_t idx[3] = {0, 1, 2};
|
||||
g_tri = (Mesh*)calloc(1, sizeof(Mesh));
|
||||
g_tri->stride = sizeof(V); g_tri->vcount = 3; g_tri->icount = 3;
|
||||
D3D11_BUFFER_DESC bd = {0}; D3D11_SUBRESOURCE_DATA sd = {0};
|
||||
bd.ByteWidth = sizeof(V)*3; bd.Usage = D3D11_USAGE_DEFAULT; bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
|
||||
sd.pSysMem = tri; g_dev->device->lpVtbl->CreateBuffer(g_dev->device, &bd, &sd, &g_tri->vb);
|
||||
bd.ByteWidth = sizeof(uint32_t)*3; bd.BindFlags = D3D11_BIND_INDEX_BUFFER;
|
||||
sd.pSysMem = idx; g_dev->device->lpVtbl->CreateBuffer(g_dev->device, &bd, &sd, &g_tri->ib);
|
||||
|
||||
engine_set_render_callback(engine, on_render);
|
||||
engine_set_key_callback(engine, on_key);
|
||||
LOG_INFO("Running...");
|
||||
engine_run(engine);
|
||||
|
||||
mesh_destroy(g_tri);
|
||||
shader_destroy(g_shader);
|
||||
glyph_atlas_destroy(&g_atlas);
|
||||
graphics_device_destroy(g_dev);
|
||||
engine_shutdown(engine);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// 独立 D3D11 测试 - 不依赖引擎框架
|
||||
#include <windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#pragma comment(lib, "d3d11.lib")
|
||||
#pragma comment(lib, "dxgi.lib")
|
||||
|
||||
static ID3D11Device* device = NULL;
|
||||
static ID3D11DeviceContext* context = NULL;
|
||||
static IDXGISwapChain* swap_chain = NULL;
|
||||
static ID3D11RenderTargetView* rtv = NULL;
|
||||
|
||||
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
||||
if (msg == WM_DESTROY) { PostQuitMessage(0); return 0; }
|
||||
return DefWindowProcW(hwnd, msg, wParam, lParam);
|
||||
}
|
||||
|
||||
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
|
||||
(void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
|
||||
|
||||
// 注册窗口类
|
||||
WNDCLASSEXW wc = {0};
|
||||
wc.cbSize = sizeof(WNDCLASSEXW);
|
||||
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
|
||||
wc.lpfnWndProc = WndProc;
|
||||
wc.hInstance = hInstance;
|
||||
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
||||
wc.lpszClassName = L"D3D11Test";
|
||||
RegisterClassExW(&wc);
|
||||
|
||||
// 创建窗口
|
||||
HWND hwnd = CreateWindowExW(0, L"D3D11Test", L"D3D11 Test",
|
||||
WS_OVERLAPPEDWINDOW, 100, 100, 800, 600,
|
||||
NULL, NULL, hInstance, NULL);
|
||||
ShowWindow(hwnd, SW_SHOW);
|
||||
UpdateWindow(hwnd);
|
||||
|
||||
// 创建 D3D11 设备和交换链
|
||||
DXGI_SWAP_CHAIN_DESC scd = {0};
|
||||
scd.BufferCount = 1;
|
||||
scd.BufferDesc.Width = 800;
|
||||
scd.BufferDesc.Height = 600;
|
||||
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
scd.BufferDesc.RefreshRate.Numerator = 60;
|
||||
scd.BufferDesc.RefreshRate.Denominator = 1;
|
||||
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
scd.OutputWindow = hwnd;
|
||||
scd.SampleDesc.Count = 1;
|
||||
scd.Windowed = TRUE;
|
||||
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
|
||||
|
||||
D3D_FEATURE_LEVEL levels[] = {D3D_FEATURE_LEVEL_11_0};
|
||||
HRESULT hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, levels, 1, D3D11_SDK_VERSION, &scd, &swap_chain, &device, NULL, &context);
|
||||
|
||||
if (FAILED(hr)) {
|
||||
MessageBoxW(hwnd, L"D3D11 CreateDevice Failed!", L"Error", MB_OK);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 创建渲染目标
|
||||
ID3D11Texture2D* back_buffer = NULL;
|
||||
swap_chain->lpVtbl->GetBuffer(swap_chain, 0, &IID_ID3D11Texture2D, (void**)&back_buffer);
|
||||
device->lpVtbl->CreateRenderTargetView(device, (ID3D11Resource*)back_buffer, NULL, &rtv);
|
||||
back_buffer->lpVtbl->Release(back_buffer);
|
||||
|
||||
// 设置渲染目标
|
||||
context->lpVtbl->OMSetRenderTargets(context, 1, &rtv, NULL);
|
||||
|
||||
// 主循环
|
||||
MSG msg = {0};
|
||||
while (msg.message != WM_QUIT) {
|
||||
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
||||
if (msg.message == WM_QUIT) break;
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
|
||||
// 清屏 - 蓝色
|
||||
float color[4] = {0.2f, 0.5f, 0.8f, 1.0f};
|
||||
context->lpVtbl->ClearRenderTargetView(context, rtv, color);
|
||||
|
||||
// 呈现
|
||||
swap_chain->lpVtbl->Present(swap_chain, 0, 0);
|
||||
}
|
||||
|
||||
// 清理
|
||||
rtv->lpVtbl->Release(rtv);
|
||||
swap_chain->lpVtbl->Release(swap_chain);
|
||||
context->lpVtbl->Release(context);
|
||||
device->lpVtbl->Release(device);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
#include "../Engine/Core/engine.h"
|
||||
#include "../Engine/Core/logger.h"
|
||||
#include "../Engine/Core/timer.h"
|
||||
#include "../Engine/Window/window.h"
|
||||
#include "../Engine/Rendering/graphics_device.h"
|
||||
#include "../Engine/include/engine_api.h"
|
||||
#include "../Engine/ASM/math_c_fallback.h"
|
||||
#include "../Engine/Editor/editor.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static GraphicsDevice* g_dev = NULL;
|
||||
static Editor g_editor;
|
||||
|
||||
static void on_render(Engine* engine) {
|
||||
if (!g_dev) return;
|
||||
editor_update(&g_editor, (float)engine->delta_time);
|
||||
editor_render(&g_editor);
|
||||
graphics_device_present(g_dev);
|
||||
}
|
||||
|
||||
static void on_key(Engine* engine, int key) {
|
||||
if (key == VK_ESCAPE) engine_request_quit(engine);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
(void)argc; (void)argv;
|
||||
|
||||
LOG_INFO("=== MiMo Engine Editor ===");
|
||||
|
||||
EngineConfig config = engine_default_config();
|
||||
config.app_name = "MiMo Engine Editor";
|
||||
config.vsync = 1;
|
||||
config.window_width = 1440;
|
||||
config.window_height = 900;
|
||||
|
||||
Engine* engine = engine_create();
|
||||
if (!engine_init(engine, &config)) {
|
||||
LOG_FATAL("Engine init failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_dev = graphics_device_create();
|
||||
if (!graphics_device_init(g_dev, window_get_handle(engine->window), config.window_width, config.window_height, 1)) {
|
||||
LOG_FATAL("D3D11 init failed");
|
||||
return -1;
|
||||
}
|
||||
LOG_INFO("D3D11 device ready: %dx%d", config.window_width, config.window_height);
|
||||
|
||||
if (!editor_init(&g_editor, g_dev)) {
|
||||
LOG_FATAL("Editor init failed");
|
||||
return -1;
|
||||
}
|
||||
LOG_INFO("Editor initialized");
|
||||
|
||||
engine_set_render_callback(engine, on_render);
|
||||
engine_set_key_callback(engine, on_key);
|
||||
|
||||
LOG_INFO("Running editor...");
|
||||
engine_run(engine);
|
||||
|
||||
editor_shutdown(&g_editor);
|
||||
graphics_device_destroy(g_dev);
|
||||
engine_shutdown(engine);
|
||||
return 0;
|
||||
}
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
#include "../Engine/Core/engine.h"
|
||||
#include "../Engine/Core/logger.h"
|
||||
#include "../Engine/Core/timer.h"
|
||||
#include "../Engine/Window/window.h"
|
||||
#include "../Engine/Rendering/graphics_device.h"
|
||||
#include "../Engine/Rendering/shader.h"
|
||||
#include "../Engine/Rendering/mesh.h"
|
||||
#include "../Engine/Rendering/camera.h"
|
||||
#include "../Engine/include/engine_api.h"
|
||||
#include "../Engine/ASM/math_c_fallback.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static GraphicsDevice* g_dev = NULL;
|
||||
static Shader* g_shader = NULL;
|
||||
static Mesh* g_cube = NULL;
|
||||
static Camera g_camera;
|
||||
static float g_angle = 0.0f;
|
||||
|
||||
static void mat4_transpose(const Mat4* in, Mat4* out) {
|
||||
for (int i = 0; i < 4; i++)
|
||||
for (int j = 0; j < 4; j++)
|
||||
out->m[i][j] = in->m[j][i];
|
||||
}
|
||||
|
||||
static void on_render(Engine* engine) {
|
||||
if (!g_dev || !g_shader || !g_cube) return;
|
||||
|
||||
float w = (float)window_get_width(engine->window);
|
||||
float h = (float)window_get_height(engine->window);
|
||||
|
||||
graphics_device_clear(g_dev, 0.15f, 0.15f, 0.2f, 1.0f);
|
||||
|
||||
g_dev->context->lpVtbl->IASetPrimitiveTopology(g_dev->context, D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
|
||||
Mat4 model, wvp, wvp_t;
|
||||
mat4_rotate_y(g_angle, &model);
|
||||
Mat4* view = (Mat4*)camera_get_view(&g_camera);
|
||||
Mat4* proj = (Mat4*)camera_get_projection(&g_camera);
|
||||
|
||||
Mat4 vp;
|
||||
mat4_multiply(view, proj, &vp);
|
||||
mat4_multiply(&model, &vp, &wvp);
|
||||
mat4_transpose(&wvp, &wvp_t);
|
||||
|
||||
shader_bind(g_shader, g_dev->context);
|
||||
shader_set_matrix(g_shader, g_dev->context, (float*)&wvp_t);
|
||||
|
||||
ID3D11DepthStencilView* dsv = g_dev->depth_stencil;
|
||||
g_dev->context->lpVtbl->OMSetRenderTargets(g_dev->context, 1, &g_dev->render_target, dsv);
|
||||
D3D11_VIEWPORT vp2 = {0};
|
||||
vp2.Width = w; vp2.Height = h; vp2.MaxDepth = 1.0f;
|
||||
g_dev->context->lpVtbl->RSSetViewports(g_dev->context, 1, &vp2);
|
||||
g_dev->context->lpVtbl->RSSetState(g_dev->context, NULL);
|
||||
|
||||
mesh_draw(g_cube, g_dev->context);
|
||||
graphics_device_present(g_dev);
|
||||
|
||||
g_angle += 0.02f;
|
||||
}
|
||||
|
||||
static void on_key(Engine* engine, int key) {
|
||||
if (key == VK_ESCAPE) engine_request_quit(engine);
|
||||
}
|
||||
|
||||
static void on_resize(Engine* engine, int width, int height) {
|
||||
if (g_dev && width > 0 && height > 0) {
|
||||
graphics_device_resize(g_dev, width, height);
|
||||
g_camera.aspect = (float)width / (float)height;
|
||||
g_camera.dirty = 1;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
(void)argc; (void)argv;
|
||||
|
||||
EngineConfig config = engine_default_config();
|
||||
config.app_name = "MiMo Engine - Cube Demo";
|
||||
config.vsync = 0;
|
||||
config.window_width = 1280;
|
||||
config.window_height = 720;
|
||||
|
||||
Engine* engine = engine_create();
|
||||
if (!engine_init(engine, &config)) {
|
||||
LOG_FATAL("Engine init failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
g_dev = graphics_device_create();
|
||||
if (!graphics_device_init(g_dev, window_get_handle(engine->window), config.window_width, config.window_height, 0)) {
|
||||
LOG_FATAL("D3D11 init failed");
|
||||
return -1;
|
||||
}
|
||||
LOG_INFO("D3D11 device ready");
|
||||
|
||||
g_shader = shader_create();
|
||||
if (!shader_compile(g_shader, g_dev->device, NULL, NULL, 0, 0)) {
|
||||
LOG_FATAL("Shader compile failed");
|
||||
return -1;
|
||||
}
|
||||
LOG_INFO("Shader compiled");
|
||||
|
||||
g_cube = mesh_create_cube(g_dev->device);
|
||||
LOG_INFO("Cube mesh created");
|
||||
|
||||
float aspect = (float)config.window_width / (float)config.window_height;
|
||||
g_camera = camera_perspective(1.0472f, aspect, 0.1f, 100.0f);
|
||||
g_camera.position = (Vec3){0, 1.5f, -4.0f};
|
||||
g_camera.target = (Vec3){0, 0, 0};
|
||||
g_camera.dirty = 1;
|
||||
|
||||
engine_set_render_callback(engine, on_render);
|
||||
engine_set_key_callback(engine, on_key);
|
||||
engine_set_resize_callback(engine, on_resize);
|
||||
|
||||
LOG_INFO("Running cube demo...");
|
||||
engine_run(engine);
|
||||
|
||||
mesh_destroy(g_cube);
|
||||
shader_destroy(g_shader);
|
||||
graphics_device_destroy(g_dev);
|
||||
engine_shutdown(engine);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user