Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootstrap/dx11 debug shapes #4

Merged
merged 10 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion code/config.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#define RT_LOG_FILE "log.txt"

#define TEMP_MEM_SIZE RT_MEGABYTES(8)

namespace rt {
char const static *WIN_NAME = "Raytracing Engine";
char const static *WIN_CLASS = "rt_engine";
s64 constexpr static WIN_WIDTH = 1366;
s64 constexpr static WIN_HEIGHT = 768;
bool constexpr static VSYNC = true;

s64 constexpr static TEMP_MEM_SIZE = RT_MEGABYTES(8);
s64 constexpr static IM_TRIS_COUNT = 1024;
} // namespace rt
9 changes: 9 additions & 0 deletions code/first.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
#include "math/math.hpp"
#include "os/os.hpp"
#include "window/window.hpp"
#include "gfx/gfx.hpp"

#include "base/base.cpp"
#include "math/math.cpp"
#include "os/os.cpp"
#include "window/window.cpp"
#include "gfx/gfx.cpp"


using namespace rt;
Expand Down Expand Up @@ -55,6 +57,7 @@ main(void) {
os_start_app_timer();
os_init_filesystem();
window_create_or_panic();
gfx_init_or_panic();

String_Builder sb;

Expand All @@ -71,6 +74,12 @@ main(void) {

while(!window_is_closed()) {
win32_message_loop();

gfx_im_rect({.x = 650, .y = 400}, {.width = 50, .height = 100}, COLOR_RED);
gfx_im_rect({.x = 600, .y = 300}, {.width = 50, .height = 100}, COLOR_GREEN);
gfx_im_rect({.x = 700, .y = 300}, {.width = 50, .height = 100}, COLOR_BLUE);

gfx_render();
}

logf("Goodbye :)\n");
Expand Down
91 changes: 91 additions & 0 deletions code/gfx/gfx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
namespace rt {
struct D3d {
::ID3D11Device *device = NULL;
::ID3D11DeviceContext *device_context = NULL;
::IDXGISwapChain *swap_chain = NULL;
::ID3D11RenderTargetView *render_target_view = NULL;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider creating typedefs of ID3D types, ex. typedef ID3D11Device IDevice to avoid interface versioning. D3D have various updates with increasing numbers as sufixes (from previous example, newest device is ID3D11Device5 from d3d11_4.h) to help in maintaining code. Also consider using COM smart pointers to avoid memory leaks and manual managment of interface counters (Microsoft way of handling lifetime of the interface).

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


struct XXC_Pipeline *xxc_pipeline = NULL;
} static gD3d;

void
gfx_init_or_panic() {
::DXGI_SWAP_CHAIN_DESC const swap_chain_descr = {
.BufferDesc = {
.RefreshRate = {
.Numerator = 0,
.Denominator = 1
},
.Format = DXGI_FORMAT_B8G8R8A8_UNORM,
},

.SampleDesc = {
.Count = 1,
.Quality = 0
},

.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT,
.BufferCount = 1,
.OutputWindow = gWin32_Window.hWnd,
.Windowed = true, // @Note: for fullscreen, set to false & set width and height
.SwapEffect = DXGI_SWAP_EFFECT_DISCARD
};

::UINT flags = D3D11_CREATE_DEVICE_SINGLETHREADED;

#if HANDMADE_INTERNAL
flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

::D3D_FEATURE_LEVEL feature_level;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On all current hardware you can set to 11_1.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

::HRESULT hr;
hr = ::D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
flags,
NULL,
0,
D3D11_SDK_VERSION,
&swap_chain_descr,
&gD3d.swap_chain,
&gD3d.device,
&feature_level,
&gD3d.device_context
);

check_(gD3d.swap_chain);
check_(gD3d.device);
check_(gD3d.device_context);
d3d_check_hresult_(hr);

::ID3D11Texture2D *framebuffer;
hr = gD3d.swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&framebuffer);
d3d_check_hresult_(hr);

hr = gD3d.device->CreateRenderTargetView(framebuffer, 0, &gD3d.render_target_view);
d3d_check_hresult_(hr);

d3d_safe_release_(framebuffer);

gfx_im_init_or_panic();
}

void
gfx_render() {
/* clear the back buffer to cornflower blue for the new frame */
float background_colour[4] = { 0x64 / 255.0f, 0x95 / 255.0f, 0xED / 255.0f, 1.0f };
gD3d.device_context->ClearRenderTargetView(gD3d.render_target_view, background_colour);


// Tell the output merger to use our render target
gD3d.device_context->OMSetRenderTargets(1, &gD3d.render_target_view, NULL);

gfx_im_flush();

// Present
gD3d.swap_chain->Present( VSYNC?1:0, 0 );
}
} // namespace rt

#include "im_pipeline.cxx"
46 changes: 46 additions & 0 deletions code/gfx/gfx.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "im_pipeline.hxx"


#define d3d_safe_release_(x) \
do { \
if (x) { \
x->Release(); \
x = NULL; \
} \
} while(false)

#define d3d_check_hresult_(hr) \
do { \
if (FAILED(hr)) { \
String const error_str = os_error_to_string(hr); \
\
logf("[D3D] !!! %s:%d -- ", __FILE__, (s32)__LINE__); \
logf("%.*s\n", (s32)error_str.count, error_str.data); \
\
errf("Failed to initialize DirectX 11."); \
} \
} while(false)

namespace rt {
u32 constexpr COLOR_RED = 0xff0000ff;
u32 constexpr COLOR_GREEN = 0x00ff00ff;
u32 constexpr COLOR_BLUE = 0x0000ffff;

// XX - x and y position
// C - 32-bit color
struct Vertex_XXC {
Vec2 position;
u32 color;
};

struct alignas(16) Constants {
Mat4x4 projection;
Mat4x4 camera;
};

void
gfx_init_or_panic();

void
gfx_render();
} // namespace rt;
Loading