Unity Dx9 Occlusion Query plugin

//Occlusion Unity plugin

#include "UnityPluginInterface.h"

#include <math.h>
#include <stdio.h>


// --------------------------------------------------------------------------
// Include headers for the graphics APIs we support
#if SUPPORT_D3D9
#include <d3d9.h>
#endif

// --------------------------------------------------------------------------
// Helper utilities

// COM-like Release macro
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(a) if (a) { a->Release(); a = NULL; }
#endif

// --------------------------------------------------------------------------
// SetTimeFromUnity, an example function we export which is called by one of the scripts.
static int g_pixelsVisible = 0;

extern "C" int EXPORT_API Result() { return g_pixelsVisible; }

// --------------------------------------------------------------------------
// UnitySetGraphicsDevice

static int g_DeviceType = -1;

// Actual setup/teardown functions defined below
#if SUPPORT_D3D9
static IDirect3DDevice9* g_D3D9Device = NULL;

// A D3dQuery Interface Pointer
static IDirect3DQuery9 * g_D3DQuery = NULL;

static void SetGraphicsDeviceD3D9 (IDirect3DDevice9* device, GfxDeviceEventType eventType)
{
g_D3D9Device = device;

// Create or Reset release g_D3DQuery
switch (eventType) {
case kGfxDeviceEventInitialize:
case kGfxDeviceEventAfterReset:
// After device is initialized or was just reset, create the g_D3DQuery.
if (!g_D3DQuery)
g_D3D9Device->CreateQuery( D3DQUERYTYPE_OCCLUSION, &g_D3DQuery );
break;
case kGfxDeviceEventBeforeReset:
case kGfxDeviceEventShutdown:
// Before device is reset or being shut down, release the g_D3DQuery.
SAFE_RELEASE(g_D3DQuery);
break;
}
}

#endif // #if SUPPORT_D3D9

extern "C" void EXPORT_API UnitySetGraphicsDevice (void* device, int deviceType, int eventType)
{
// Set device type to -1, i.e. "not recognized by our plugin"
g_DeviceType = -1;

#if SUPPORT_D3D9
// D3D9 device, remember device pointer and device type.
// The pointer we get is IDirect3DDevice9.
if (deviceType == kGfxRendererD3D9)
{
g_DeviceType = deviceType;
SetGraphicsDeviceD3D9 ((IDirect3DDevice9*)device, (GfxDeviceEventType)eventType);
}
#endif
}

// --------------------------------------------------------------------------
// OcclusionBegin

extern "C" void EXPORT_API OcclusionBegin ()
{
#if SUPPORT_D3D9
if(g_D3DQuery != NULL)
g_D3DQuery->Issue( D3DISSUE_BEGIN );
#endif
}


// --------------------------------------------------------------------------
// OcclusionEnd
extern "C" void EXPORT_API OcclusionEnd ()
{
#if SUPPORT_D3D9
// End the query, get the data
if(g_D3DQuery != NULL)
{
g_D3DQuery->Issue( D3DISSUE_END );

while (g_D3DQuery->GetData((void *) &g_pixelsVisible,
sizeof(int), D3DGETDATA_FLUSH) == S_FALSE);
}
#endif
}

原文地址:https://www.cnblogs.com/bearworks/p/3493867.html