From 44f7f07c2eb348d14b5a68ce721c90c9339d99b7 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Tue, 30 Dec 2025 15:01:41 -0600 Subject: [PATCH] initial --- .gitignore | 7 ++ Debug/.keep | 0 build.bat | 9 ++ compile.rsp | 19 ++++ environment.bat | 15 +++ link.rsp | 22 ++++ main.cpp | 270 ++++++++++++++++++++++++++++++++++++++++++++++++ main.fx | 19 ++++ notes.txt | 3 + 9 files changed, 364 insertions(+) create mode 100644 .gitignore create mode 100644 Debug/.keep create mode 100644 build.bat create mode 100644 compile.rsp create mode 100644 environment.bat create mode 100644 link.rsp create mode 100644 main.cpp create mode 100644 main.fx create mode 100644 notes.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f8a2440 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.fxo +*.exe +*.ilk +*.pdb +*.manifest +*.idb +*.obj diff --git a/Debug/.keep b/Debug/.keep new file mode 100644 index 0000000..e69de29 diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..e9e1c12 --- /dev/null +++ b/build.bat @@ -0,0 +1,9 @@ +cl.exe @"compile.rsp" "main.cpp" + +link.exe @"link.rsp" /OUT:"Debug\d3d10.exe" /PDB:"Debug\d3d10.pdb" ".\Debug\main.obj" + +fxc /Od /Zi /T fx_4_0 /nologo /Fo main.fxo main.fx + +@if %errorlevel% neq 0 exit /b %errorlevel% + +Debug\d3d10.exe diff --git a/compile.rsp b/compile.rsp new file mode 100644 index 0000000..f0186c6 --- /dev/null +++ b/compile.rsp @@ -0,0 +1,19 @@ +/Od +/D "WIN32" +/D "_DEBUG" +/D "_WINDOWS" +/D "_UNICODE" +/D "UNICODE" +/Gm +/EHsc +/RTC1 +/MDd +/Fo"Debug\\" +/Fd"Debug\vc80.pdb" +/W3 +/c +/Wp64 +/ZI +/TP +/nologo +/errorReport:prompt \ No newline at end of file diff --git a/environment.bat b/environment.bat new file mode 100644 index 0000000..f33bea7 --- /dev/null +++ b/environment.bat @@ -0,0 +1,15 @@ +@Set DXSDK_DIR=C:\Program Files\Microsoft DirectX SDK (June 2010) + +@Set Lib=%DXSDK_DIR%\Lib\x86;%Lib% +@Set Include=%DXSDK_DIR%\Include;%Include% +@Set Path=%DXSDK_DIR%\Utilities\Bin\x86;%Path% + +@Set VSINSTALLDIR=C:\Program Files\Microsoft Visual Studio 8 +@Set VCINSTALLDIR=C:\Program Files\Microsoft Visual Studio 8\VC + +@Set Path=%VCINSTALLDIR%\BIN;%Path% +@Set Path=%VSINSTALLDIR%\Common7\IDE;%Path% +@Set Include=%VCINSTALLDIR%\include;%Include% +@Set Include=%VCINSTALLDIR%\PlatformSDK\Include;%Include% +@Set Lib=%VCINSTALLDIR%\LIB;%Lib% +@Set Lib=%VCINSTALLDIR%\PlatformSDK\Lib;%Lib% diff --git a/link.rsp b/link.rsp new file mode 100644 index 0000000..8acf14a --- /dev/null +++ b/link.rsp @@ -0,0 +1,22 @@ +/INCREMENTAL +/DEBUG +/SUBSYSTEM:WINDOWS +/MACHINE:X86 +/NOLOGO +/ERRORREPORT:PROMPT +d3d10.lib +d3dx10.lib +kernel32.lib +user32.lib +gdi32.lib +winspool.lib +comdlg32.lib +advapi32.lib +shell32.lib +ole32.lib +oleaut32.lib +uuid.lib +odbc32.lib +odbccp32.lib + + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..c27e83b --- /dev/null +++ b/main.cpp @@ -0,0 +1,270 @@ +#define _WIN32_WINNT 0x0501 +#include +#include +#include +#include + +HINSTANCE g_hInstance = NULL; +HWND g_hWnd = NULL; +ID3D10Device * g_pd3dDevice = NULL; +IDXGISwapChain * g_pSwapChain = NULL; +ID3D10RenderTargetView * g_pRenderTargetView = NULL; +ID3D10Effect * g_pEffect = NULL; +ID3D10EffectTechnique * g_pTechnique = NULL; +ID3D10InputLayout * g_pVertexLayout = NULL; +ID3D10Buffer * g_pVertexBuffer = NULL; + +HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow); +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); +HRESULT InitDirect3DDevice(); +void Render(); + +struct SimpleVertex { + D3DXVECTOR3 Pos; +}; + +void print(LPCSTR fmt, ...) +{ + va_list args; + va_start(args, fmt); + char buf[512]; + STRSAFE_LPSTR end; + + StringCbVPrintfExA(buf, + (sizeof (buf)), + &end, + NULL, + STRSAFE_NULL_ON_FAILURE, + fmt, + args); + va_end(args); + size_t length = end - &buf[0]; + HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE); + WriteConsoleA(hOutput, buf, (DWORD)length, NULL, NULL); +} + +int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) +{ + FreeConsole(); + AllocConsole(); + AttachConsole(GetCurrentProcessId()); + print("WinMain\n"); + if (FAILED(InitWindow(hInstance, nCmdShow))) { + print("InitWindow\n"); + return 0; + } + + if (FAILED(InitDirect3DDevice())) { + print("InitDirect3DDevice\n"); + return 0; + } + + MSG msg = {}; + while (msg.message != WM_QUIT) { + if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } else { + Render(); + } + } + + return 0; +} + +HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow) +{ + g_hInstance = hInstance; + + // window class + WNDCLASSEX wcex = {}; + wcex.cbSize = (sizeof (WNDCLASSEX)); + wcex.style = CS_HREDRAW | CS_VREDRAW; + wcex.lpfnWndProc = WndProc; + wcex.hInstance = hInstance; + wcex.lpszClassName = L"d3d10wc"; + if (!RegisterClassEx(&wcex)) + return E_FAIL; + + // create window + RECT rc = { 0, 0, 640, 480 }; + AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE); + g_hWnd = CreateWindow(L"d3d10wc", + L"d3d10", WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, + rc.right - rc.left, rc.bottom - rc.top, + NULL, + NULL, + hInstance, + NULL); + if(!g_hWnd) + return E_FAIL; + + ShowWindow(g_hWnd, nCmdShow); + + return S_OK; +} + +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + PAINTSTRUCT ps; + HDC hdc; + + switch(message) { + case WM_PAINT: + hdc = BeginPaint(hWnd, &ps); + EndPaint(hWnd, &ps); + break; + + case WM_DESTROY: + PostQuitMessage( 0 ); + break; + + default: + return DefWindowProc(hWnd, message, wParam, lParam); + } + + return 0; +} + +HRESULT InitDirect3DDevice() +{ + DXGI_SWAP_CHAIN_DESC sd = {}; + sd.BufferCount = 1; + sd.BufferDesc.Width = 640; + sd.BufferDesc.Height = 480; + sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + sd.BufferDesc.RefreshRate.Numerator = 60; + sd.BufferDesc.RefreshRate.Denominator = 1; + sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; + sd.OutputWindow = g_hWnd; + sd.SampleDesc.Count = 1; + sd.SampleDesc.Quality = 0; + sd.Windowed = TRUE; + + HRESULT hr; + hr = D3D10CreateDeviceAndSwapChain(NULL, + D3D10_DRIVER_TYPE_REFERENCE, + NULL, + D3D10_CREATE_DEVICE_DEBUG, + D3D10_SDK_VERSION, + &sd, + &g_pSwapChain, + &g_pd3dDevice); + if (FAILED(hr)) { + print("D3D10CreateDeviceAndSwapChain\n"); + return hr; + } + + ID3D10Texture2D * pBackBuffer; + hr = g_pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID *)&pBackBuffer); + if (FAILED(hr)) { + print("g_pSwapChain->GetBuffer\n"); + return hr; + } + hr = g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_pRenderTargetView); + pBackBuffer->Release(); + if (FAILED(hr)) { + print("g_pSwapChain->CreateRenderTargetView\n"); + return hr; + } + + g_pd3dDevice->OMSetRenderTargets(1, &g_pRenderTargetView, NULL); + + // viewport + D3D10_VIEWPORT vp; + vp.Width = 640; + vp.Height = 480; + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + vp.TopLeftX = 0; + vp.TopLeftY = 0; + g_pd3dDevice->RSSetViewports(1, &vp); + + // effect + + ID3D10Blob * pBlobErrors = NULL; + DWORD dwShaderFlags = D3D10_SHADER_ENABLE_STRICTNESS | D3D10_SHADER_DEBUG; + hr = D3DX10CreateEffectFromFile(L"c:/Users/dot/d3d10/main.fx", + NULL, + NULL, + "fx_4_0", + dwShaderFlags, + 0, + g_pd3dDevice, + NULL, + NULL, + &g_pEffect, + &pBlobErrors, + NULL); + if (FAILED(hr)) { + print("D3DX10CreateEffectFromFile\n"); + const char * pError = (const char *)pBlobErrors->GetBufferPointer(); + (void)pError; + return hr; + } + + g_pTechnique = g_pEffect->GetTechniqueByName("Render"); + + // input layout + D3D10_INPUT_ELEMENT_DESC layout[] = { + {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0}, + }; + UINT numElements = (sizeof (layout)) / (sizeof (layout[0])); + + D3D10_PASS_DESC passDesc; + g_pTechnique->GetPassByIndex(0)->GetDesc(&passDesc); + print("pass desc: `%s`\n", passDesc.Name); + + hr = g_pd3dDevice->CreateInputLayout(layout, numElements, + passDesc.pIAInputSignature, + passDesc.IAInputSignatureSize, + &g_pVertexLayout); + if (FAILED(hr)) { + print("CreateInputLayout\n"); + return hr; + } + g_pd3dDevice->IASetInputLayout(g_pVertexLayout); + + // vertex buffer + SimpleVertex vertices[] = { + D3DXVECTOR3( 0.0f, 0.5f, 0.5f), + D3DXVECTOR3( 0.5f, -0.5f, 0.5f), + D3DXVECTOR3(-0.5f, -0.5f, 0.5f), + }; + D3D10_BUFFER_DESC bd; + bd.Usage = D3D10_USAGE_DEFAULT; + bd.ByteWidth = (sizeof (SimpleVertex)) * 3; + bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; + bd.CPUAccessFlags = 0; + bd.MiscFlags = 0; + D3D10_SUBRESOURCE_DATA InitData; + InitData.pSysMem = vertices; + hr = g_pd3dDevice->CreateBuffer(&bd, &InitData, &g_pVertexBuffer); + if (FAILED(hr)) { + print("CreateBuffer\n"); + return hr; + } + UINT stride = (sizeof (SimpleVertex)); + UINT offset = 0; + g_pd3dDevice->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); + + g_pd3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + return S_OK; +} + +void Render() +{ + float ClearColor[4] = { 0.0f, 0.125f, 0.6f, 1.0f }; + g_pd3dDevice->ClearRenderTargetView(g_pRenderTargetView, ClearColor); + + D3D10_TECHNIQUE_DESC techDesc; + g_pTechnique->GetDesc( &techDesc ); + for(UINT p = 0; p < techDesc.Passes; p++) { + g_pTechnique->GetPassByIndex(p)->Apply(0); + g_pd3dDevice->Draw(3, 0); + } + + g_pSwapChain->Present(0, 0); +} diff --git a/main.fx b/main.fx new file mode 100644 index 0000000..0c7f212 --- /dev/null +++ b/main.fx @@ -0,0 +1,19 @@ +float4 VS(float4 Pos : POSITION) : SV_POSITION +{ + return Pos; +} + +float4 PS(float4 Pos : SV_POSITION) : SV_Target +{ + return float4(1.0f, 1.0f, 0.0f, 1.0f); +} + +technique10 Render +{ + pass P0 + { + SetVertexShader(CompileShader(vs_4_0, VS())); + SetGeometryShader(NULL); + SetPixelShader(CompileShader(ps_4_0, PS())); + } +} diff --git a/notes.txt b/notes.txt new file mode 100644 index 0000000..4265622 --- /dev/null +++ b/notes.txt @@ -0,0 +1,3 @@ +VC shell: + +dumpbin /dependents a.exe