Skip to content

Commit

Permalink
Menu shows up on D3D12
Browse files Browse the repository at this point in the history
  • Loading branch information
quadpixels committed Nov 8, 2023
1 parent 68e3e3e commit 5372e4c
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 33 deletions.
2 changes: 1 addition & 1 deletion chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ChunkPass {
ID3D12PipelineState* pipeline_state_default_palette;
ID3D12PipelineState* pipeline_state_depth_only;

// 给不同的Chunk共享的资源
// Per-Object CBs
int num_max_chunks;
ID3D12Resource* d_per_object_cbs;

Expand Down
18 changes: 8 additions & 10 deletions dx12helloworld/DX12TextScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,9 @@ extern ID3D12CommandQueue* g_command_queue;
extern IDXGISwapChain3* g_swapchain;
extern void GlmMat4ToDirectXMatrix(DirectX::XMMATRIX* out, const glm::mat4& m);

// From textrender.cpp
struct TextCbPerScene {
DirectX::XMVECTOR screensize; // Assume alignment at float4 boundary
DirectX::XMMATRIX transform;
DirectX::XMMATRIX projection;
DirectX::XMVECTOR textcolor;
};

void WaitForPreviousFrame();
// Dummy
void do_RenderText_D3D12(const std::wstring& text, float x, float y, float scale, glm::vec3 color, glm::mat4 transform) {}

DX12TextScene::DX12TextScene() {
InitCommandList();
Expand All @@ -40,8 +34,6 @@ DX12TextScene::DX12TextScene() {
text_pass->InitD3D12();
text_pass->InitFreetype();
text_pass->AllocateConstantBuffers(1024);
text_pass->AddText(L"Hello world", WIN_W / 2.0f, WIN_H / 2.0f + 20, 1.0f, glm::vec3(1.0f, 0.5f, 0.5f), glm::mat4(1));
text_pass->AddText(L"Hello world", WIN_W / 2.0f, WIN_H / 2.0f + 40, 1.5f, glm::vec3(1.0f, 0.8f, 0.5f), glm::mat4(1));
}

void DX12TextScene::InitCommandList() {
Expand Down Expand Up @@ -251,6 +243,7 @@ void DX12TextScene::Render() {
command_list->OMSetBlendFactor(blend_factor);
command_list->RSSetViewports(1, &viewport);
command_list->RSSetScissorRects(1, &scissor);
command_list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
for (size_t i = 0; i < text_pass->characters_to_display.size(); i++) {
const TextPass::CharacterToDisplay& ctd = text_pass->characters_to_display[i];
command_list->IASetVertexBuffers(0, 1, &ctd.vbv);
Expand Down Expand Up @@ -280,6 +273,11 @@ void DX12TextScene::Update(float secs) {
::localtime_s(&tm, &t1);
wss << std::put_time(&tm, L"%F %T");
AddText(wss.str(), glm::vec2(WIN_W / 2.0f, WIN_H / 2.0f));

text_pass->StartPass();
text_pass->AddText(L"Hello world", WIN_W / 2.0f, WIN_H / 2.0f + 20, 1.0f, glm::vec3(1.0f, 0.5f, 0.5f), glm::mat4(1));
text_pass->AddText(L"Hello world", WIN_W / 2.0f, WIN_H / 2.0f + 48, 1.5f, glm::vec3(1.0f, 0.8f, 0.5f), glm::mat4(1));
text_pass->AddText(wss.str(), WIN_W / 2.0f, WIN_H / 2.0f + 90, 1.5f, glm::vec3(0.3f, 0.3f, 1.0f), glm::mat4(1));
}

void DX12TextScene::AddText(const std::wstring& txt, glm::vec2 pos) {
Expand Down
30 changes: 24 additions & 6 deletions game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ void MainMenu::DrawHelpScreen() {
}

void MainMenu::Render_D3D11(const glm::mat4& uitransform) {
do_Render(ClimbD3D11, uitransform);
}

void MainMenu::Render_D3D12(const glm::mat4& uitransform) {
do_Render(ClimbD3D12, uitransform);
}

void MainMenu::do_Render(GraphicsAPI api, const glm::mat4& uitransform) {
// Copy & paste from Render()
const int ymin = 200, textsize = 24;
int y = ymin;
Expand All @@ -390,7 +398,7 @@ void MainMenu::Render_D3D11(const glm::mat4& uitransform) {
float w;
MeasureTextWidth(line, &w);
glm::vec3 c = glm::vec3(1.0f, 1.0f, 0.5f);
RenderText(ClimbD3D11, line, WIN_W / 2 - w / 2, y, 1.0f, c, uitransform);
RenderText(api, line, WIN_W / 2 - w / 2, y, 1.0f, c, uitransform);
y += textsize;
}

Expand All @@ -406,25 +414,35 @@ void MainMenu::Render_D3D11(const glm::mat4& uitransform) {
glm::vec3 c;
if (i == curr_selection.back()) c = glm::vec3(1.0f, 0.2f, 0.2f);
else c = glm::vec3(1.0f, 1.0f, 0.1f);
RenderText(ClimbD3D11, line, WIN_W / 2 - w / 2, y, 1.0f, c, uitransform);
RenderText(api, line, WIN_W / 2 - w / 2, y, 1.0f, c, uitransform);
y += textsize;
}

// Background
#ifdef WIN32
UpdateSimpleTexturePerSceneCB(0, 0, fade_alpha);
fsquad->Render_D3D11();
if (IsD3D11()) {
UpdateSimpleTexturePerSceneCB(0, 0, fade_alpha);
fsquad->Render_D3D11();
}
else {
// TODO
}
#endif
if (is_in_help) {
DrawHelpScreen();
if (IsD3D11()) {
DrawHelpScreen();
}
else {
// TODO
}
}

// DBG
#ifdef WIN32
{
std::wstringstream ws;
ws << std::to_wstring(curr_menu.size()) << " | " << std::to_wstring(curr_selection.size());
RenderText(ClimbD3D11, ws.str(), 20, 700, 1, glm::vec3(0, 1, 1), uitransform);
RenderText(api, ws.str(), 20, 700, 1, glm::vec3(0, 1, 1), uitransform);
}
#endif
}
Expand Down
3 changes: 3 additions & 0 deletions game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class MainMenu {

void Render(const glm::mat4& uitransform);
void Render_D3D11(const glm::mat4& uitransform);
void Render_D3D12(const glm::mat4& uitransform);
void OnUpDownPressed(int delta); // -1: up; +1: down
void OnLeftRightPressed(int delta); // -1: left; +1: right
void OnEscPressed();
Expand Down Expand Up @@ -128,6 +129,8 @@ class MainMenu {

static void InitStatic(unsigned p) { program = p; }
static unsigned program;
private:
void do_Render(GraphicsAPI api, const glm::mat4& uitransform);
};

class TextMessage {
Expand Down
55 changes: 51 additions & 4 deletions main_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ extern Camera* GetCurrentSceneCamera();
extern GameScene* GetCurrentGameScene();
extern DirectX::XMMATRIX g_projection_d3d11;
extern Particles* g_particles;
extern float g_cam_rot_x, g_cam_rot_y;

static ChunkPass* chunk_pass_depth, * chunk_pass_normal;
const static int NUM_SPRITES = 1024;
Expand All @@ -44,9 +45,9 @@ static int g_frame_index;
static ID3D12Fence* g_fence;
static int g_fence_value = 0;
static HANDLE g_fence_event;
static ID3D12CommandQueue* g_command_queue;
static ID3D12CommandAllocator* g_command_allocator;
static ID3D12GraphicsCommandList* g_command_list;
ID3D12CommandQueue* g_command_queue; // Shared with textrender.cpp
ID3D12CommandAllocator* g_command_allocator; // Shared with textrender.cpp
ID3D12GraphicsCommandList* g_command_list; // Shared with textrender.cpp
static IDXGISwapChain3* g_swapchain;
static ID3D12DescriptorHeap* g_rtv_heap;
static ID3D12Resource* g_rendertargets[FRAME_COUNT];
Expand All @@ -56,6 +57,8 @@ static ID3D12DescriptorHeap* g_dsv_heap;
static unsigned g_dsv_descriptor_size;
static ID3D12Resource* g_gbuffer12;
static ID3D12Resource* g_shadow_map;
static TextPass* text_pass;
const static int NUM_CHARS = 1024;

// For ChunkPass
static ID3D12Resource* d_per_scene_cb;
Expand Down Expand Up @@ -284,7 +287,12 @@ void UpdatePerSceneCB_D3D12(const DirectX::XMVECTOR* dir_light, const DirectX::X
d_per_scene_cb->Unmap(0, nullptr);
}

void do_RenderText_D3D12(const std::wstring& text, float x, float y, float scale, glm::vec3 color, glm::mat4 transform) {
text_pass->AddText(text, x, y, scale, color, transform);
}

void Render_D3D12() {
text_pass->StartPass();
GetCurrentGameScene()->PreRender();
GetCurrentGameScene()->PrepareSpriteListForRender();
UpdatePerSceneCB_D3D12(&(g_dir_light->GetDir_D3D11()), &(g_dir_light->GetPV_D3D11()), &(GetCurrentSceneCamera()->GetPos_D3D11()));
Expand Down Expand Up @@ -401,14 +409,49 @@ void Render_D3D12() {
}
}

CE(g_command_list->Close());
g_command_queue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&g_command_list);

{
glm::mat4 uitransform(1);
uitransform *= glm::rotate(uitransform, g_cam_rot_x, glm::vec3(0.0f, 1.0f, 0.0f));
uitransform *= glm::rotate(uitransform, g_cam_rot_y, glm::vec3(1.0f, 0.0f, 0.0f));
if (g_main_menu_visible) {
g_mainmenu->Render_D3D12(uitransform);
}
}

// TextPass's rendering procedure
CE(g_command_list->Reset(g_command_allocator, text_pass->pipeline_state));
g_command_list->SetGraphicsRootSignature(text_pass->root_signature);
ID3D12DescriptorHeap* ppHeaps_textpass[] = { text_pass->srv_heap };
g_command_list->SetDescriptorHeaps(_countof(ppHeaps_textpass), ppHeaps_textpass);
g_command_list->RSSetViewports(1, &main_viewport);
g_command_list->RSSetScissorRects(1, &main_scissor);
g_command_list->OMSetRenderTargets(2, rtvs, FALSE, &main_dsv_handle);
float blend_factor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
g_command_list->OMSetBlendFactor(blend_factor);

g_command_list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
for (size_t i = 0; i < text_pass->characters_to_display.size(); i++) {
const TextPass::CharacterToDisplay& ctd = text_pass->characters_to_display[i];
g_command_list->IASetVertexBuffers(0, 1, &ctd.vbv);
g_command_list->SetGraphicsRootConstantBufferView(0, text_pass->per_scene_cbs->GetGPUVirtualAddress() + sizeof(TextCbPerScene) * ctd.per_scene_cb_index);
CD3DX12_GPU_DESCRIPTOR_HANDLE srv_handle(
text_pass->srv_heap->GetGPUDescriptorHandleForHeapStart(),
ctd.character->offset_in_srv_heap, text_pass->srv_descriptor_size);
g_command_list->SetGraphicsRootDescriptorTable(1, srv_handle);
g_command_list->DrawInstanced(6, 1, 0, 0);
}

g_command_list->ResourceBarrier(1, &keep(CD3DX12_RESOURCE_BARRIER::Transition(
render_target,
D3D12_RESOURCE_STATE_RENDER_TARGET,
D3D12_RESOURCE_STATE_PRESENT
)));

CE(g_command_list->Close());
g_command_queue->ExecuteCommandLists(1, (ID3D12CommandList* const*)&g_command_list);

CE(g_swapchain->Present(1, 0));
WaitForPreviousFrame();
}
Expand Down Expand Up @@ -436,6 +479,10 @@ void MyInit_D3D12() {
chunk_pass_normal = new ChunkPass();
chunk_pass_normal->AllocateConstantBuffers(NUM_SPRITES);
chunk_pass_normal->InitD3D12DefaultPalette();
text_pass = new TextPass(g_device12, g_command_queue, g_command_list, g_command_allocator);
text_pass->AllocateConstantBuffers(NUM_CHARS);
text_pass->InitD3D12();
text_pass->InitFreetype();

g_projection_d3d11 = DirectX::XMMatrixPerspectiveFovLH(60.0f * 3.14159f / 180.0f, WIN_W * 1.0f / WIN_H, 0.01f, 499.0f);
Particles::InitStatic(g_chunkgrid[3]);
Expand Down
2 changes: 1 addition & 1 deletion shaders_hlsl/textrender.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ VSOutput VSMain(VSInput input) {
const float half_yext = Z * tan(fovy);
float half_xext = half_yext / screensize.y * screensize.x;

float2 xy = input.position.xy / screensize;
float2 xy = input.position.xy / screensize.xy;
xy.y = 1.0 - xy.y;
xy = xy * 2.0f - float2(1.0f, 1.0f);
xy = xy * float2(half_xext, half_yext);
Expand Down
19 changes: 8 additions & 11 deletions textrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern ID3D11VertexShader* g_vs_textrender;
extern ID3D11PixelShader* g_ps_textrender;
extern ID3DBlob *g_vs_textrender_blob;
extern void GlmMat4ToDirectXMatrix(DirectX::XMMATRIX* out, const glm::mat4& m);
extern void do_RenderText_D3D12(const std::wstring& text, float x, float y, float scale, glm::vec3 color, glm::mat4 transform);
ID3D11InputLayout *input_layout11;

#endif
Expand All @@ -32,12 +33,6 @@ std::map<wchar_t, Character_D3D11> g_characters_d3d11;
FT_Face g_face;

#ifdef WIN32
struct TextCbPerScene {
DirectX::XMVECTOR screensize; // Assume alignment at float4 boundary
DirectX::XMMATRIX transform;
DirectX::XMMATRIX projection;
DirectX::XMVECTOR textcolor;
};
static ID3D11Buffer* vertex_buffer11;
static ID3D11Buffer* textcb_perscene11;
extern ID3D11BlendState* g_blendstate11;
Expand Down Expand Up @@ -357,18 +352,18 @@ void TextPass::InitD3D12() {
ID3DBlob* error = nullptr;
unsigned compile_flags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
const wchar_t* filenames[] = {
L"shaders/textrender.hlsl",
L"../shaders/textrender.hlsl",
L"shaders_hlsl/textrender.hlsl",
L"../shaders_hlsl/textrender.hlsl",
};
for (size_t i = 0; i < 2; i++) {
D3DCompileFromFile(filenames[i], nullptr, nullptr,
HRESULT hr = D3DCompileFromFile(filenames[i], nullptr, nullptr,
"VSMain", "vs_5_0", compile_flags, 0, &VS, &error);
if (error) printf("Error compiling VS: %s\n", (char*)(error->GetBufferPointer()));

D3DCompileFromFile(filenames[i], nullptr, nullptr,
hr = D3DCompileFromFile(filenames[i], nullptr, nullptr,
"PSMain", "ps_5_0", compile_flags, 0, &PS, &error);
if (error) printf("Error compiling PS: %s\n", (char*)(error->GetBufferPointer()));
else break;
if (error == nullptr && hr == 0) break;
}
}

Expand Down Expand Up @@ -475,6 +470,7 @@ void TextPass::InitFreetype() {
}
}
FT_Set_Pixel_Sizes(face, 0, 20);
g_face = face;
}

Character_D3D12* TextPass::CreateOrGetChar(wchar_t ch) {
Expand Down Expand Up @@ -633,6 +629,7 @@ void RenderText(GraphicsAPI api, std::wstring text, GLfloat x, GLfloat y, GLfloa
switch (api) {
#ifdef WIN32
case ClimbD3D11: do_RenderText_D3D11(text, x, y, scale, color, transform); break;
case ClimbD3D12: do_RenderText_D3D12(text, x, y, scale, color, transform); break;
#endif
case ClimbOpenGL: do_RenderText(g_programs[6], text, x, y, scale, color, transform); break;
default: break;
Expand Down
7 changes: 7 additions & 0 deletions textrender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ struct Character {
};

#ifdef WIN32
struct TextCbPerScene {
DirectX::XMVECTOR screensize; // Assume alignment at float4 boundary
DirectX::XMMATRIX transform;
DirectX::XMMATRIX projection;
DirectX::XMVECTOR textcolor;
};

struct Character_D3D11 {
ID3D11Texture2D *texture;
ID3D11ShaderResourceView *srv;
Expand Down

0 comments on commit 5372e4c

Please sign in to comment.