-
-
Save robertsdionne/9d703b3eb787115556387c2e3bf8fb89 to your computer and use it in GitHub Desktop.
gonutz-d3d9-zbuffer-repro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module github.com/robertsdionne/gonutz-d3d9-zbuffer-repro | |
go 1.18 | |
require ( | |
github.com/gonutz/d3d9 v1.2.1 // indirect | |
github.com/gonutz/d3dmath v1.0.0 // indirect | |
github.com/gonutz/w32/v2 v2.7.0 // indirect | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
github.com/gonutz/d3d9 v1.2.1 h1:xcQLMKrAf+/hvFtsyc9LuLJ+qs6TPtbzuKYONRERpYo= | |
github.com/gonutz/d3d9 v1.2.1/go.mod h1:q74g3QbR280b+qYauwEV0N9SVadszWPLZ4l/wHiD/AA= | |
github.com/gonutz/d3dmath v1.0.0 h1:Va5RdqfBcj+H9V91UiEzRJ/L+A8qvBiHF8kkMWhokDY= | |
github.com/gonutz/d3dmath v1.0.0/go.mod h1:ybIqtVMWSAcf3qsyIaG5v2RrKRtU3mw5zoWN5rkVC04= | |
github.com/gonutz/w32/v2 v2.7.0 h1:O9lx/5tZ+WjZdxdvQFTh+/kP/7y95YCaL5MHqPrIHn4= | |
github.com/gonutz/w32/v2 v2.7.0/go.mod h1:MgtHx0AScDVNKyB+kjyPder4xIi3XAcHS6LDDU2DmdE= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <d3d9.h> | |
#include <d3dx9.h> | |
#include <windows.h> | |
#pragma comment(lib, "d3d9.lib") | |
LRESULT __stdcall windowProc(HWND, UINT, WPARAM, LPARAM); | |
struct Vertex { | |
float x, y, z; | |
UINT32 color; | |
}; | |
int main() { | |
const char* className = "gonutz-d3d9-zbuffer-repro"; | |
const int width = 1024; | |
const int height = 768; | |
WNDCLASSA windowClass = {}; | |
windowClass.hCursor = LoadCursor(0, MAKEINTRESOURCE(IDC_ARROW)); | |
windowClass.lpfnWndProc = windowProc; | |
windowClass.lpszClassName = className; | |
windowClass.hInstance = GetModuleHandle(nullptr); | |
auto windowClassAtom = RegisterClassA(&windowClass); | |
if (windowClassAtom == 0) { | |
return E_FAIL; | |
} | |
auto windowHandle = CreateWindowA(className, className, WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, width, height, nullptr, nullptr, 0, nullptr); | |
if (windowHandle == 0) { | |
return E_FAIL; | |
} | |
auto d3d = Direct3DCreate9(D3D_SDK_VERSION); | |
if (d3d == nullptr) { | |
return E_FAIL; | |
} | |
D3DPRESENT_PARAMETERS dpp = {}; | |
dpp.hDeviceWindow = windowHandle; | |
dpp.Windowed = true; | |
dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; | |
dpp.BackBufferFormat = D3DFMT_UNKNOWN; | |
dpp.EnableAutoDepthStencil = true; | |
dpp.AutoDepthStencilFormat = D3DFMT_D24X8; | |
LPDIRECT3DDEVICE9 device = nullptr; | |
auto result = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, windowHandle, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dpp, &device); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
result = device->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
result = device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
Vertex vertices[] = { | |
{-0.5, -0.5, 0.0, 0xFFFF0000}, | |
{0.0, 0.5, 0.0, 0xFF00FF00}, | |
{0.5, -0.5, 0.0, 0xFF0000FF}, | |
}; | |
LPDIRECT3DVERTEXBUFFER9 vertexBuffer = nullptr; | |
result = device->CreateVertexBuffer(3 * sizeof(Vertex), D3DUSAGE_WRITEONLY, D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &vertexBuffer, nullptr); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
void* vertexBufferMemory; | |
result = vertexBuffer->Lock(0, 0, (void**)&vertexBufferMemory, D3DLOCK_DISCARD); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
memcpy(vertexBufferMemory, vertices, sizeof(vertices)); | |
result = vertexBuffer->Unlock(); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
result = device->SetRenderState(D3DRS_LIGHTING, 0); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
MSG message = {}; | |
while (message.message != WM_QUIT) { | |
if (PeekMessage(&message, 0, 0, 0, PM_REMOVE) != 0) { | |
TranslateMessage(&message); | |
DispatchMessage(&message); | |
} | |
else { | |
device->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1, 0); | |
result = device->BeginScene(); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
D3DXMATRIXA16 world; | |
D3DXMatrixTranslation(&world, 0, 0, 4); | |
result = device->SetTransform(D3DTS_WORLD, &world); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
D3DXVECTOR3 eye(0, 0, 0); | |
D3DXVECTOR3 lookat(0, 0, 1); | |
D3DXVECTOR3 up(0, 1, 0); | |
D3DXMATRIXA16 view; | |
D3DXMatrixLookAtLH(&view, &eye, &lookat, &up); | |
result = device->SetTransform(D3DTS_VIEW, &view); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
D3DXMATRIXA16 proj; | |
D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, (float)width / height, 0.1f, 1000.0f); | |
result = device->SetTransform(D3DTS_PROJECTION, &proj); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
result = device->SetStreamSource(0, vertexBuffer, 0, sizeof(Vertex)); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
result = device->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
result = device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
result = device->EndScene(); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
result = device->Present(nullptr, nullptr, nullptr, nullptr); | |
if (result != D3D_OK) { | |
return E_FAIL; | |
} | |
} | |
} | |
vertexBuffer->Release(); | |
device->Release(); | |
d3d->Release(); | |
UnregisterClassA("spaceship", GetModuleHandle(0)); | |
return 0; | |
} | |
LRESULT __stdcall windowProc(HWND handle, UINT message, WPARAM wParam, LPARAM lParam) { | |
switch (message) { | |
case WM_DESTROY: | |
PostQuitMessage(0); | |
break; | |
default: | |
return DefWindowProcA(handle, message, wParam, lParam); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
Microsoft Visual Studio Solution File, Format Version 12.00 | |
# Visual Studio Version 17 | |
VisualStudioVersion = 17.5.33530.505 | |
MinimumVisualStudioVersion = 10.0.40219.1 | |
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "gonutz-d3d9-zbuffer-repro", "gonutz-d3d9-zbuffer-repro.vcxproj", "{7A11E4E4-7418-4A31-B642-C87D0346F831}" | |
EndProject | |
Global | |
GlobalSection(SolutionConfigurationPlatforms) = preSolution | |
Debug|x64 = Debug|x64 | |
Debug|x86 = Debug|x86 | |
Release|x64 = Release|x64 | |
Release|x86 = Release|x86 | |
EndGlobalSection | |
GlobalSection(ProjectConfigurationPlatforms) = postSolution | |
{7A11E4E4-7418-4A31-B642-C87D0346F831}.Debug|x64.ActiveCfg = Debug|x64 | |
{7A11E4E4-7418-4A31-B642-C87D0346F831}.Debug|x64.Build.0 = Debug|x64 | |
{7A11E4E4-7418-4A31-B642-C87D0346F831}.Debug|x86.ActiveCfg = Debug|Win32 | |
{7A11E4E4-7418-4A31-B642-C87D0346F831}.Debug|x86.Build.0 = Debug|Win32 | |
{7A11E4E4-7418-4A31-B642-C87D0346F831}.Release|x64.ActiveCfg = Release|x64 | |
{7A11E4E4-7418-4A31-B642-C87D0346F831}.Release|x64.Build.0 = Release|x64 | |
{7A11E4E4-7418-4A31-B642-C87D0346F831}.Release|x86.ActiveCfg = Release|Win32 | |
{7A11E4E4-7418-4A31-B642-C87D0346F831}.Release|x86.Build.0 = Release|Win32 | |
EndGlobalSection | |
GlobalSection(SolutionProperties) = preSolution | |
HideSolutionNode = FALSE | |
EndGlobalSection | |
GlobalSection(ExtensibilityGlobals) = postSolution | |
SolutionGuid = {89A57A13-5E19-47DE-8C56-064E6EEF0C9D} | |
EndGlobalSection | |
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<ItemGroup Label="ProjectConfigurations"> | |
<ProjectConfiguration Include="Debug|Win32"> | |
<Configuration>Debug</Configuration> | |
<Platform>Win32</Platform> | |
</ProjectConfiguration> | |
<ProjectConfiguration Include="Release|Win32"> | |
<Configuration>Release</Configuration> | |
<Platform>Win32</Platform> | |
</ProjectConfiguration> | |
<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> | |
<Keyword>Win32Proj</Keyword> | |
<ProjectGuid>{7a11e4e4-7418-4a31-b642-c87d0346f831}</ProjectGuid> | |
<RootNamespace>gonutzd3d9zbufferrepro</RootNamespace> | |
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> | |
</PropertyGroup> | |
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | |
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | |
<ConfigurationType>Application</ConfigurationType> | |
<UseDebugLibraries>true</UseDebugLibraries> | |
<PlatformToolset>v143</PlatformToolset> | |
<CharacterSet>Unicode</CharacterSet> | |
</PropertyGroup> | |
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | |
<ConfigurationType>Application</ConfigurationType> | |
<UseDebugLibraries>false</UseDebugLibraries> | |
<PlatformToolset>v143</PlatformToolset> | |
<WholeProgramOptimization>true</WholeProgramOptimization> | |
<CharacterSet>Unicode</CharacterSet> | |
</PropertyGroup> | |
<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="Shared"> | |
</ImportGroup> | |
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | |
<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|Win32'"> | |
<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)'=='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|Win32'"> | |
<ClCompile> | |
<WarningLevel>Level3</WarningLevel> | |
<SDLCheck>true</SDLCheck> | |
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |
<ConformanceMode>true</ConformanceMode> | |
</ClCompile> | |
<Link> | |
<SubSystem>Console</SubSystem> | |
<GenerateDebugInformation>true</GenerateDebugInformation> | |
</Link> | |
</ItemDefinitionGroup> | |
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | |
<ClCompile> | |
<WarningLevel>Level3</WarningLevel> | |
<FunctionLevelLinking>true</FunctionLevelLinking> | |
<IntrinsicFunctions>true</IntrinsicFunctions> | |
<SDLCheck>true</SDLCheck> | |
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |
<ConformanceMode>true</ConformanceMode> | |
</ClCompile> | |
<Link> | |
<SubSystem>Console</SubSystem> | |
<EnableCOMDATFolding>true</EnableCOMDATFolding> | |
<OptimizeReferences>true</OptimizeReferences> | |
<GenerateDebugInformation>true</GenerateDebugInformation> | |
</Link> | |
</ItemDefinitionGroup> | |
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | |
<ClCompile> | |
<WarningLevel>Level3</WarningLevel> | |
<SDLCheck>true</SDLCheck> | |
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |
<ConformanceMode>true</ConformanceMode> | |
</ClCompile> | |
<Link> | |
<SubSystem>Console</SubSystem> | |
<GenerateDebugInformation>true</GenerateDebugInformation> | |
</Link> | |
</ItemDefinitionGroup> | |
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | |
<ClCompile> | |
<WarningLevel>Level3</WarningLevel> | |
<FunctionLevelLinking>true</FunctionLevelLinking> | |
<IntrinsicFunctions>true</IntrinsicFunctions> | |
<SDLCheck>true</SDLCheck> | |
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | |
<ConformanceMode>true</ConformanceMode> | |
</ClCompile> | |
<Link> | |
<SubSystem>Console</SubSystem> | |
<EnableCOMDATFolding>true</EnableCOMDATFolding> | |
<OptimizeReferences>true</OptimizeReferences> | |
<GenerateDebugInformation>true</GenerateDebugInformation> | |
</Link> | |
</ItemDefinitionGroup> | |
<ItemGroup> | |
<ClCompile Include="gonutz-d3d9-zbuffer-repro.cpp" /> | |
</ItemGroup> | |
<ItemGroup> | |
<None Include="packages.config" /> | |
</ItemGroup> | |
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | |
<ImportGroup Label="ExtensionTargets"> | |
<Import Project="packages\Microsoft.DXSDK.D3DX.9.29.952.8\build\native\Microsoft.DXSDK.D3DX.targets" Condition="Exists('packages\Microsoft.DXSDK.D3DX.9.29.952.8\build\native\Microsoft.DXSDK.D3DX.targets')" /> | |
</ImportGroup> | |
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> | |
<PropertyGroup> | |
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> | |
</PropertyGroup> | |
<Error Condition="!Exists('packages\Microsoft.DXSDK.D3DX.9.29.952.8\build\native\Microsoft.DXSDK.D3DX.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.DXSDK.D3DX.9.29.952.8\build\native\Microsoft.DXSDK.D3DX.targets'))" /> | |
</Target> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<ItemGroup> | |
<Filter Include="Source Files"> | |
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> | |
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions> | |
</Filter> | |
<Filter Include="Header Files"> | |
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> | |
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions> | |
</Filter> | |
<Filter Include="Resource Files"> | |
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> | |
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> | |
</Filter> | |
</ItemGroup> | |
<ItemGroup> | |
<ClCompile Include="gonutz-d3d9-zbuffer-repro.cpp"> | |
<Filter>Source Files</Filter> | |
</ClCompile> | |
</ItemGroup> | |
<ItemGroup> | |
<None Include="packages.config" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |
<PropertyGroup /> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/binary" | |
"errors" | |
"log" | |
"math" | |
"runtime" | |
"syscall" | |
"github.com/gonutz/d3d9" | |
"github.com/gonutz/d3dmath" | |
"github.com/gonutz/w32/v2" | |
) | |
const sizeofVertex = 16 | |
type Vertex struct { | |
x, y, z float32 | |
color uint32 | |
} | |
func main() { | |
runtime.LockOSThread() | |
err := run() | |
if err != nil { | |
log.Fatalln(err) | |
} | |
} | |
func run() (err error) { | |
const width, height = 1024, 768 | |
const className = "gonutz-d3d9-zbuffer-repro" | |
classNamePtr, err := syscall.UTF16PtrFromString(className) | |
if err != nil { | |
return | |
} | |
windowClass := w32.WNDCLASSEX{ | |
Cursor: w32.LoadCursor(0, w32.MakeIntResource(w32.IDC_ARROW)), | |
WndProc: syscall.NewCallback(windowProc), | |
ClassName: classNamePtr, | |
Instance: w32.GetModuleHandle(""), | |
} | |
windowClassAtom := w32.RegisterClassEx(&windowClass) | |
if windowClassAtom == 0 { | |
err = errors.New("failed to register window class") | |
return | |
} | |
defer w32.UnregisterClassAtom(windowClassAtom, windowClass.Instance) | |
windowHandle := w32.CreateWindow(classNamePtr, classNamePtr, w32.WS_OVERLAPPEDWINDOW|w32.WS_VISIBLE, w32.CW_USEDEFAULT, w32.CW_USEDEFAULT, width, height, 0, 0, 0, nil) | |
d3d, err := d3d9.Create(d3d9.SDK_VERSION) | |
if err != nil { | |
return | |
} | |
defer d3d.Release() | |
dpp := d3d9.PRESENT_PARAMETERS{ | |
HDeviceWindow: d3d9.HWND(windowHandle), | |
Windowed: 1, | |
SwapEffect: d3d9.SWAPEFFECT_DISCARD, | |
BackBufferFormat: d3d9.FMT_UNKNOWN, | |
EnableAutoDepthStencil: 1, | |
AutoDepthStencilFormat: d3d9.FMT_D24X8, | |
} | |
device, _, err := d3d.CreateDevice(d3d9.ADAPTER_DEFAULT, d3d9.DEVTYPE_HAL, d3d9.HWND(windowHandle), d3d9.CREATE_HARDWARE_VERTEXPROCESSING, dpp) | |
if err != nil { | |
return | |
} | |
defer device.Release() | |
err = device.SetRenderState(d3d9.RS_ZENABLE, d3d9.ZB_TRUE) | |
if err != nil { | |
return | |
} | |
err = device.SetRenderState(d3d9.RS_CULLMODE, d3d9.CULL_NONE) | |
if err != nil { | |
return | |
} | |
vertices := []Vertex{ | |
{-0.5, -0.5, 0.0, 0xFFF00000}, | |
{0.0, 0.5, 0.0, 0xFF00FF00}, | |
{0.5, -0.5, 0.0, 0xFF0000FF}, | |
} | |
vertexBuffer, err := device.CreateVertexBuffer(uint(len(vertices)*sizeofVertex), d3d9.USAGE_WRITEONLY, d3d9.FVF_XYZ|d3d9.FVF_DIFFUSE, d3d9.POOL_DEFAULT, 0) | |
if err != nil { | |
return | |
} | |
defer vertexBuffer.Release() | |
vertexBufferMemory, err := vertexBuffer.Lock(0, 0, 0) | |
if err != nil { | |
return | |
} | |
var buffer bytes.Buffer | |
err = binary.Write(&buffer, binary.LittleEndian, vertices) | |
if err != nil { | |
return | |
} | |
vertexBufferMemory.SetBytes(0, buffer.Bytes()) | |
err = vertexBuffer.Unlock() | |
if err != nil { | |
return | |
} | |
err = device.SetRenderState(d3d9.RS_LIGHTING, 0) | |
if err != nil { | |
return | |
} | |
var message w32.MSG | |
for message.Message != w32.WM_QUIT { | |
if w32.PeekMessage(&message, 0, 0, 0, w32.PM_REMOVE) { | |
w32.TranslateMessage(&message) | |
w32.DispatchMessage(&message) | |
} else { | |
err = device.Clear(nil, d3d9.CLEAR_TARGET|d3d9.CLEAR_ZBUFFER, d3d9.ColorXRGB(0, 0, 0), 1, 0) | |
if err != nil { | |
return | |
} | |
err = device.BeginScene() | |
if err != nil { | |
return | |
} | |
world := d3dmath.Translate(0, 0, 4) | |
err = device.SetTransform(d3d9.TSWorldMatrix(0), d3d9.MATRIX(world)) | |
if err != nil { | |
return | |
} | |
eye := d3dmath.Vec3{0, 0, 0} | |
lookat := d3dmath.Vec3{0, 0, 1} | |
up := d3dmath.Vec3{0, 1, 1} | |
view := d3dmath.LookAt(eye, lookat, up) | |
err = device.SetTransform(d3d9.TS_VIEW, d3d9.MATRIX(view)) | |
if err != nil { | |
return | |
} | |
proj := d3dmath.Perspective(math.Pi/4, width/height, 0.1, 1000) | |
err = device.SetTransform(d3d9.TS_PROJECTION, d3d9.MATRIX(proj)) | |
if err != nil { | |
return | |
} | |
err = device.SetStreamSource(0, vertexBuffer, 0, sizeofVertex) | |
if err != nil { | |
return | |
} | |
err = device.SetFVF(d3d9.FVF_XYZ | d3d9.FVF_DIFFUSE) | |
if err != nil { | |
return | |
} | |
err = device.DrawPrimitive(d3d9.PT_TRIANGLELIST, 0, 1) | |
if err != nil { | |
return | |
} | |
err = device.EndScene() | |
if err != nil { | |
return | |
} | |
err = device.Present(nil, nil, 0, nil) | |
if err != nil { | |
return | |
} | |
} | |
} | |
return | |
} | |
func windowProc(window w32.HWND, message uint32, w, l uintptr) uintptr { | |
switch message { | |
case w32.WM_DESTROY: | |
w32.PostQuitMessage(0) | |
return 0 | |
default: | |
return w32.DefWindowProc(window, message, w, l) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="Microsoft.DXSDK.D3DX" version="9.29.952.8" targetFramework="native" /> | |
</packages> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment