summaryrefslogtreecommitdiffstats
path: root/Whisper/D3D/Binder.cpp
blob: 9caf7aab88e02b0f6a58176d6014e847dc88ab2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "stdafx.h"
#include "Binder.h"
#include <algorithm>
using namespace DirectCompute;

void Binder::bind( ID3D11ShaderResourceView* srv0, ID3D11UnorderedAccessView* uav0 )
{
	ID3D11DeviceContext* const ctx = context();
	ctx->CSSetUnorderedAccessViews( 0, 1, &uav0, nullptr );

	ctx->CSSetShaderResources( 0, 1, &srv0 );

	maxSrv = std::max( maxSrv, (uint8_t)1 );
	maxUav = std::max( maxUav, (uint8_t)1 );
}

void Binder::bind( ID3D11UnorderedAccessView* uav0 )
{
	context()->CSSetUnorderedAccessViews( 0, 1, &uav0, nullptr );
	maxUav = std::max( maxUav, (uint8_t)1 );
}

void Binder::bind( ID3D11ShaderResourceView* srv0, ID3D11ShaderResourceView* srv1, ID3D11UnorderedAccessView* uav0 )
{
	ID3D11DeviceContext* const ctx = context();
	ctx->CSSetUnorderedAccessViews( 0, 1, &uav0, nullptr );

	std::array< ID3D11ShaderResourceView*, 2> arr = { srv0, srv1 };
	ctx->CSSetShaderResources( 0, 2, arr.data() );

	maxSrv = std::max( maxSrv, (uint8_t)2 );
	maxUav = std::max( maxUav, (uint8_t)1 );
}

void Binder::bind( std::initializer_list<ID3D11ShaderResourceView*> srvs, std::initializer_list<ID3D11UnorderedAccessView*> uavs )
{
	ID3D11DeviceContext* const ctx = context();

	const size_t lengthResources = srvs.size();
	const size_t lengthUnordered = uavs.size();
	assert( lengthResources > 0 && lengthResources < D3D11_COMMONSHADER_INPUT_RESOURCE_REGISTER_COUNT );
	assert( lengthUnordered > 0 && lengthUnordered < D3D11_PS_CS_UAV_REGISTER_COUNT );

	ctx->CSSetUnorderedAccessViews( 0, (UINT)lengthUnordered, uavs.begin(), nullptr );
	ctx->CSSetShaderResources( 0, (UINT)lengthResources, srvs.begin() );

	maxSrv = std::max( maxSrv, (uint8_t)lengthResources );
	maxUav = std::max( maxUav, (uint8_t)lengthUnordered );
}

Binder::~Binder()
{
	uint8_t count = std::max( maxSrv, maxUav );
	if( 0 == count )
		return;
#pragma warning (disable: 6255) // Compiler doesn't know we have very few of these things
	size_t* arr = (size_t*)_alloca( count * sizeof( size_t ) );
	memset( arr, 0, count * sizeof( size_t ) );

	ID3D11DeviceContext* const ctx = context();
	ctx->CSSetShaderResources( 0, maxSrv, (ID3D11ShaderResourceView**)arr );
	ctx->CSSetUnorderedAccessViews( 0, maxUav, (ID3D11UnorderedAccessView**)arr, nullptr );
}