📚 d3d9 - Awesome Go Library for Windows
Go bindings for Direct3D9.
Detailed Description of d3d9
d3d9
This library is a pure Go wrapper for Microsoft's Direct3D9 API.
Games
Here you can see the library in action, it was used for two 48 hour game jams.
This is my entry to the GopherGala 2016:
And here is my entry for the Ludum Dare Game Jam 2016:
Installation
Get and build the library with:
go get -u github.com/gonutz/d3d9
To run a Direct3D9 application you need to have d3d9.dll
installed on your system. Luckily, all Windows versions starting with Windows XP have this pre-installed. This means that you will not need to ship any additional DLL with your application when using this library.
Obsolete CGo Version 1
This library does not use CGo anymore. With this change the API has incompatibly changed in some places. If you have a project using the old API and do not want to update everything right now, you can use a copy of version 1.0.0 of this library at github.com/gonutz/cgo/d3d9. All you have to do is go get that library and update your imports from github.com/gonutz/d3d9
to github.com/gonutz/cgo/d3d9
.
Usage
All Direct3D9 interfaces are translated to Go types and their methods are translated to functions on the types so the usage is very close to the C++ API.
There are some differences in the names in Go, since the package is named d3d9
, all names in that package drop the D3D
and 9
parts because they would be redundant. The changes are:
- Interfaces drop the
IDirect3D
prefix and the9
suffix, e.g.IDirect3DDevice9
becomesd3d9.Device
. The only exception isIDirect3D9
which in Go becomesDirect3D
. - Constants and enumerations drop the
D3D
prefix, otherwise they are the same and keep the upper case convention so users of Direct3D can easily find what they are looking for. For exampleD3DADAPTER_DEFAULT
becomesd3d9.ADAPTER_DEFAULT
,D3DFMT_R8G8B8
becomesd3d9.FMT_R8G8B8
etc. - Structs, like constants, only drop the
D3D
prefix, they too keep the upper case naming convention, soD3DRANGE
becomesd3d9.RANGE
. - Error constants also drop the
D3D
prefix soD3DERR_OUTOFVIDEOMEMORY
becomesERR_OUTOFVIDEOMEMORY
. However, the interface functions do not return these constants, they return Goerror
s instead ofHRESULT
s. - Instead of returning
HRESULT
, functions returnd3d9.Error
which implements the standard Go error interface and has an additional functionCode() int32
which returns the error code. This code can be checked against the defined error constants. If a function succeeds it returnsnil
(and notD3D_OK
) as the d3d9.Error.
Note that Direct3D9 needs a window handle for setting it up. This means that you need some way to create a native window and get the handle to pass it to d3d9. In the samples you can see how to do it using the SDL2 Go wrapper and Allen Dang's w32. You could also use other Windows wrapper libraries, like the walk library, or just write a little CGo code to set up a window yourself. This library does not provide window creation or event handling functionality, only the Direct3D9 wrapper.
All calls to Direct3D9 must happen from the same thread that creates the Direct3D9 object so make sure to add this code in your main package:
func init() {
runtime.LockOSThread()
}
There are some additional convenience functions. IndexBuffer
and VertexBuffer
have Lock
functions which return void*
pointers in the C API and would thus return uintptr
s in Go. You can use these pointers to read and write various types from/to that memory. However, using uintptr
or unsafe.Pointer
is not idiomatic Go so the Lock
functions return a wrapper around the uintptr
instead, providing functions of the form GetFloat32s
and SetFloat32s
which take a slice of []float32
and handle copying the data for you. See the documentation of these functions for further information.
Similarly, when locking a two-dimensional resource, like a texture, you will get a d3d9.LOCKED_RECT
. It too has a wrapper function for setting its 2D data. There is however no function to read the data, yet, and no functions for reading and writing sub-rectangles which could be useful. These functions can be added when needed by a real application to see the use case first and implement the functions according to that instead of guessing what might work. If you need such a function, create a pull request or an issue and it can be incorporated in this library.
Documentation
See the GoDoc for the Go API. The functions are only documented very generally, to get more detailed information about the Direct3D9 API see the MSDN documentation.
Examples
The samples folder contains some example programs that show the basics of setting up and using the library.
A real world usage of this library is my entry to the GopherGala 2016. It was first built using SDL2 and after the competition I ported it to DirectX, see main_windows.go
for the implementation.
Another real world example is my entry for the Ludum Dare Game Jam 2016 which is Windows only.
The prototype library also uses d3d9
on Windows.
Status
The code started out as a rough version generated from the MSDN online documentation and was then manually refined to work well with Go and its conventions. It wraps the whole Direct3D9 API right now and adds some additional convenience functions for easy Go usage. However, LOCKED_RECT
only has one function for setting the whole data rectangle at once, there is no simple way to set only sub-rectangles right now. Similarly the CubeTexture's LOCKED_BOX
has no wrapper functions for getting and setting its data, yet. For now the raw uintptr
memory pointers can be used along with the pitch values and in the future such functions can be added when needed.
Help improve this library
Only real world use and feedback can improve the usability of this library, so please use it, fork it, send pull requests and create issues to help improve this library.