summaryrefslogtreecommitdiff
path: root/materialsystem/occlusionquerymgr.h
blob: 2820e51c403cc2a565595f481bc6e1e9fe5ccc0a (plain) (blame)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================

#ifndef OCCLUSIONQUERY_H
#define OCCLUSIONQUERY_H

#include "tier1/utlsymbol.h"
#include "tier1/utlrbtree.h"

#ifndef MATSYS_INTERNAL
#error "This file is private to the implementation of IMaterialSystem/IMaterialSystemInternal"
#endif

#if defined( _WIN32 )
#pragma once
#endif


#include "tier1/utllinkedlist.h"
#include "shaderapi/ishaderapi.h"


class IMatRenderContextInternal;

// because the GPU/driver can buffer frames we need to allow several queries to be in flight.
// The game wants to reiusse the queries every frame so we buffer them here to avoid
// having to block waiting for a query to be available for reissue.
#define COUNT_OCCLUSION_QUERY_STACK 4

//-----------------------------------------------------------------------------
// Dictionary of all known materials
//-----------------------------------------------------------------------------
class COcclusionQueryMgr
{
public:
	COcclusionQueryMgr();

	// Allocate and delete query objects.
	OcclusionQueryObjectHandle_t CreateOcclusionQueryObject( );
	void OnCreateOcclusionQueryObject( OcclusionQueryObjectHandle_t h );
	void DestroyOcclusionQueryObject( OcclusionQueryObjectHandle_t h );

	// Bracket drawing with begin and end so that we can get counts next frame.
	void BeginOcclusionQueryDrawing( OcclusionQueryObjectHandle_t h );
	void EndOcclusionQueryDrawing( OcclusionQueryObjectHandle_t h );

	// Used to make the handle think it's never had a successful query before
	void ResetOcclusionQueryObject( OcclusionQueryObjectHandle_t );

	// Get the number of pixels rendered between begin and end on an earlier frame.
	// Calling this in the same frame is a huge perf hit!
	int OcclusionQuery_GetNumPixelsRendered( OcclusionQueryObjectHandle_t h, bool bDoQuery );
	void OcclusionQuery_IssueNumPixelsRenderedQuery( OcclusionQueryObjectHandle_t h );

	// Internal stuff for occlusion query
	void AllocOcclusionQueryObjects( void );
	void FreeOcclusionQueryObjects( void );

	// Advance frame
	void AdvanceFrame();

private:
	//-----------------------------------------------------------------------------
	// Occlusion query objects
	//-----------------------------------------------------------------------------
	struct OcclusionQueryObject_t 
	{
		ShaderAPIOcclusionQuery_t m_QueryHandle[COUNT_OCCLUSION_QUERY_STACK];
		int m_LastResult;
		int m_nFrameIssued;
		int m_nCurrentIssue;
		bool m_bHasBeenIssued[COUNT_OCCLUSION_QUERY_STACK];

		OcclusionQueryObject_t(void)
		{
			for ( int i = 0; i < COUNT_OCCLUSION_QUERY_STACK; i++ )
			{
				m_QueryHandle[i] = INVALID_SHADERAPI_OCCLUSION_QUERY_HANDLE;
				m_bHasBeenIssued[i] = false;
			}
			m_LastResult = -1;
			m_nFrameIssued = -1;
			m_nCurrentIssue = 0;
		}
	};

	// Flushes an outstanding query
	void FlushQuery( OcclusionQueryObjectHandle_t hOcclusionQuery, int nIndex );

	// Occlusion query objects
	CUtlFixedLinkedList<OcclusionQueryObject_t>	m_OcclusionQueryObjects;
	CThreadFastMutex m_Mutex;
	int m_nFrameCount;
};


//-----------------------------------------------------------------------------
// Singleton
//-----------------------------------------------------------------------------
extern COcclusionQueryMgr *g_pOcclusionQueryMgr;


#endif // OCCLUSIONQUERY_H