Sign in to follow this  

D3d12 in C

This topic is 838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I am having trouble getting d3d12 working in C. I can't seem to manage to create a descriptor heap. Every time I tried to call a method of the heap, it crashes with a access violation exception. Am I doing something wrong ?

#include "stdafx.h"
#include <tchar.h>
#include <d3d12.h>
#include <dxgi1_4.h>

#define CALL(method, object, ... )  (object->lpVtbl->method(object, ##__VA_ARGS__))

#define __riid(x) (REFIID)(&_IID_ ## x)

typedef HRESULT(WINAPI* FN_CREATE_DXGI_FACTORY1)(REFIID, void **);
typedef HRESULT(WINAPI* FN_D3D12_CREATE_DEVICE)(IUnknown*, D3D_FEATURE_LEVEL, REFIID, void**);


static const IID _IID_IDXGIFactory4 = {0x1bc6ea02, 0xef36, 0x464f, { 0xbf,0x0c,0x21,0xca,0x39,0xe5,0x16,0x8a }};
static const IID _IID_ID3D12Device = {0x189819f1, 0x1db6, 0x4b57, { 0xbe,0x54,0x18,0x21,0x33,0x9b,0x85,0xf7 }};
static const IID _IID_IDXGIAdapter = {0x2411e7e1, 0x12ac, 0x4ccf, { 0xbd,0x14,0x97,0x98,0xe8,0x53,0x4d,0xc0 }};
static const IID _IID_ID3D12DescriptorHeap = {0x8efb471d, 0x616c, 0x4f49, { 0x90,0xf7,0x12,0x7b,0xb7,0x63,0xfa,0x51 }};

int main()
{

	HRESULT hr;
	HMODULE dxgi_dll;
	FN_CREATE_DXGI_FACTORY1 CreateFactory1;
	IDXGIFactory4* factory4;
	IDXGIAdapter *adapter;
	HMODULE d3d12_dll;
	FN_D3D12_CREATE_DEVICE CreateDevice;
	ID3D12Device * device;
	ID3D12DescriptorHeap *heap;

	dxgi_dll = LoadLibraryEx(_T("dxgi.dll"), NULL, 0);
	CreateFactory1 = (FN_CREATE_DXGI_FACTORY1)GetProcAddress(dxgi_dll, "CreateDXGIFactory1");

	CreateFactory1(__riid(IDXGIFactory4), (void**)&factory4);

	hr = CALL(EnumWarpAdapter, factory4, __riid(IDXGIAdapter), (void**)&adapter);
	
	d3d12_dll = LoadLibraryEx(_T("d3d12.dll"), NULL, 0);
	CreateDevice = (FN_D3D12_CREATE_DEVICE)GetProcAddress(d3d12_dll, "D3D12CreateDevice");
	
	hr = CreateDevice((IUnknown*)adapter, D3D_FEATURE_LEVEL_12_0, __riid(ID3D12Device), (void**)&device);

	D3D12_DESCRIPTOR_HEAP_DESC desc = {
		.NumDescriptors = 1,
		.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV,
		.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE,
		.NodeMask = 0
	};
	hr = CALL(CreateDescriptorHeap, device, &desc, __riid(ID3D12DescriptorHeap), (void**)&heap);

	CALL(GetDesc, heap); /*access violation exception*/

    return 0;
}

Share this post


Link to post
Share on other sites

I can't seem to manage to create a descriptor heap... I tried to call a method of the heap, it crashes with a access violation exception.


Are you sure you've created the descriptor heap at the time you want to use it? Check the HRESULT. Otherwise, you're using an uninitialized pointer... Edited by fastcall22

Share this post


Link to post
Share on other sites

You're not checking return values for success. Run it with the debugger, or check all return codes and print errors.

 

I have removed the error checking code for this post but the HRESULTs all SUCCEEDED().

Share this post


Link to post
Share on other sites

There's multiple things you have not told us here.

 

1. What exactly are you trying to do?  All I see is a program that leaks alot of memory just to call a few functions.

2. What is your OS?  I'm assuming that you are trying this on Windows 10, which is the only OS that supports Direct3D12 IIRC.

3. Is this your full program?  Can you post the entire source to your program if it's all in one source file?

 

It's been a month since I've touched D3D12, and even longer since I've done Direct3D in pure C.  Quite interesting.  When I get home I'll check my own code and see if you missed anying.

 

Shogun.

Share this post


Link to post
Share on other sites

I have no experience with D3D12 but if its headers are like D2D's then [url=http://blog.airesoft.co.uk/2014/12/direct2d-scene-of-the-accident/]the C interface is broken[/url] in respect to the interface functions that return structures. And they're not really bothered about fixing it

Share this post


Link to post
Share on other sites

I have no experience with D3D12 but if its headers are like D2D's then the C interface is broken in respect to the interface functions that return structures. And they're not really bothered about fixing it

 

Hi adeyblue,

 

 Thank you for your help! I read the article and it does appear to be the same problem. I amended the code and it now works. I wonder if there's any way to recompile d3d12.h from d3d12.idl to fix this problem?

#include "stdafx.h"
#include <tchar.h>
#include <d3d12.h>
#include <dxgi1_4.h>

#define CALL(method, object, ... )  (object->lpVtbl->method(object, ##__VA_ARGS__))

#define __riid(x) (REFIID)(&_IID_ ## x)

typedef HRESULT(WINAPI* FN_CREATE_DXGI_FACTORY1)(REFIID, void **);
typedef HRESULT(WINAPI* FN_D3D12_CREATE_DEVICE)(IUnknown*, D3D_FEATURE_LEVEL, REFIID, void**);


static const IID _IID_IDXGIFactory4 = { 0x1bc6ea02, 0xef36, 0x464f,{ 0xbf,0x0c,0x21,0xca,0x39,0xe5,0x16,0x8a } };
static const IID _IID_ID3D12Device = { 0x189819f1, 0x1db6, 0x4b57,{ 0xbe,0x54,0x18,0x21,0x33,0x9b,0x85,0xf7 } };
static const IID _IID_IDXGIAdapter = { 0x2411e7e1, 0x12ac, 0x4ccf,{ 0xbd,0x14,0x97,0x98,0xe8,0x53,0x4d,0xc0 } };
static const IID _IID_ID3D12DescriptorHeap = { 0x8efb471d, 0x616c, 0x4f49,{ 0x90,0xf7,0x12,0x7b,0xb7,0x63,0xfa,0x51 } };

int main()
{

	HRESULT hr;
	HMODULE dxgi_dll;
	FN_CREATE_DXGI_FACTORY1 CreateFactory1;
	IDXGIFactory4* factory4;
	IDXGIAdapter *adapter;
	HMODULE d3d12_dll;
	FN_D3D12_CREATE_DEVICE CreateDevice;
	ID3D12Device * device;
	ID3D12DescriptorHeap *heap;

	dxgi_dll = LoadLibraryEx(_T("dxgi.dll"), NULL, 0);
	CreateFactory1 = (FN_CREATE_DXGI_FACTORY1)GetProcAddress(dxgi_dll, "CreateDXGIFactory1");

	CreateFactory1(__riid(IDXGIFactory4), (void**)&factory4);

	hr = CALL(EnumWarpAdapter, factory4, __riid(IDXGIAdapter), (void**)&adapter);

	d3d12_dll = LoadLibraryEx(_T("d3d12.dll"), NULL, 0);
	CreateDevice = (FN_D3D12_CREATE_DEVICE)GetProcAddress(d3d12_dll, "D3D12CreateDevice");

	hr = CreateDevice((IUnknown*)adapter, D3D_FEATURE_LEVEL_12_0, __riid(ID3D12Device), (void**)&device);

	D3D12_DESCRIPTOR_HEAP_DESC desc = {
		.NumDescriptors = 1,
		.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV,
		.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE,
		.NodeMask = 0
	};
	hr = CALL(CreateDescriptorHeap, device, &desc, __riid(ID3D12DescriptorHeap), (void**)&heap);


	typedef void (STDMETHODCALLTYPE *FN_GET_DESC)(ID3D12DescriptorHeap*, D3D12_DESCRIPTOR_HEAP_DESC*);

	FN_GET_DESC GetDesc = (FN_GET_DESC)heap->lpVtbl->GetDesc;

	D3D12_DESCRIPTOR_HEAP_DESC desc2 = { 0 };

	GetDesc(heap, &desc2);

	return 0;
}
Edited by duckweed

Share this post


Link to post
Share on other sites

This topic is 838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this