Path: blob/master/dep/winpixeventruntime/include/WinPixEventRuntime/PIXEvents.h
4261 views
// Copyright (c) Microsoft Corporation. All rights reserved.12/*==========================================================================;3*4* Copyright (C) Microsoft Corporation. All Rights Reserved.5*6* File: PIXEvents.h7* Content: PIX include file8* Don't include this file directly - use pix3.h9*10****************************************************************************/11#pragma once1213#ifndef _PixEvents_H_14#define _PixEvents_H_1516#ifndef _PIX3_H_17# error Do not include this file directly - use pix3.h18#endif1920#include "PIXEventsCommon.h"2122#if _MSC_VER < 180023# error This version of pix3.h is only supported on Visual Studio 2013 or higher24#elif _MSC_VER < 190025# ifndef constexpr // Visual Studio 2013 doesn't support constexpr26# define constexpr27# define PIX3__DEFINED_CONSTEXPR28# endif29#endif3031// Xbox does not support CPU events for retail scenarios32#if defined(USE_PIX) || !defined(PIX_XBOX)33#define PIX_CONTEXT_EMIT_CPU_EVENTS34#endif3536namespace PIXEventsDetail37{38template<typename... ARGS>39struct PIXEventTypeInferer40{41static constexpr PIXEventType Begin() { return PIXEvent_BeginEvent_VarArgs; }42static constexpr PIXEventType SetMarker() { return PIXEvent_SetMarker_VarArgs; }43static constexpr PIXEventType BeginOnContext() { return PIXEvent_BeginEvent_OnContext_VarArgs; }44static constexpr PIXEventType SetMarkerOnContext() { return PIXEvent_SetMarker_OnContext_VarArgs; }4546// Xbox and Windows store different types of events for context events.47// On Xbox these include a context argument, while on Windows they do48// not. It is important not to change the event types used on the49// Windows version as there are OS components (eg debug layer & DRED)50// that decode event structs.51#ifdef PIX_XBOX52static constexpr PIXEventType GpuBeginOnContext() { return PIXEvent_BeginEvent_OnContext_VarArgs; }53static constexpr PIXEventType GpuSetMarkerOnContext() { return PIXEvent_SetMarker_OnContext_VarArgs; }54#else55static constexpr PIXEventType GpuBeginOnContext() { return PIXEvent_BeginEvent_VarArgs; }56static constexpr PIXEventType GpuSetMarkerOnContext() { return PIXEvent_SetMarker_VarArgs; }57#endif58};5960template<>61struct PIXEventTypeInferer<>62{63static constexpr PIXEventType Begin() { return PIXEvent_BeginEvent_NoArgs; }64static constexpr PIXEventType SetMarker() { return PIXEvent_SetMarker_NoArgs; }65static constexpr PIXEventType BeginOnContext() { return PIXEvent_BeginEvent_OnContext_NoArgs; }66static constexpr PIXEventType SetMarkerOnContext() { return PIXEvent_SetMarker_OnContext_NoArgs; }6768#ifdef PIX_XBOX69static constexpr PIXEventType GpuBeginOnContext() { return PIXEvent_BeginEvent_OnContext_NoArgs; }70static constexpr PIXEventType GpuSetMarkerOnContext() { return PIXEvent_SetMarker_OnContext_NoArgs; }71#else72static constexpr PIXEventType GpuBeginOnContext() { return PIXEvent_BeginEvent_NoArgs; }73static constexpr PIXEventType GpuSetMarkerOnContext() { return PIXEvent_SetMarker_NoArgs; }74#endif75};7677inline void PIXCopyEventArguments(_Out_writes_to_ptr_(limit) UINT64*& destination, _In_ const UINT64* limit)78{79// nothing80UNREFERENCED_PARAMETER(destination);81UNREFERENCED_PARAMETER(limit);82}8384template<typename ARG, typename... ARGS>85void PIXCopyEventArguments(_Out_writes_to_ptr_(limit) UINT64*& destination, _In_ const UINT64* limit, ARG const& arg, ARGS const&... args)86{87PIXCopyEventArgument(destination, limit, arg);88PIXCopyEventArguments(destination, limit, args...);89}9091template<typename STR, typename... ARGS>92__declspec(noinline) void PIXBeginEventAllocate(PIXEventsThreadInfo* threadInfo, UINT64 color, STR formatString, ARGS... args)93{94#ifdef PIX_XBOX95UINT64 time = PIXEventsReplaceBlock(false);96#else97UINT64 time = PIXEventsReplaceBlock(threadInfo, false);98#endif99if (!time)100return;101102UINT64* destination = threadInfo->destination;103UINT64* limit = threadInfo->biasedLimit;104if (destination >= limit)105return;106107limit += PIXEventsSafeFastCopySpaceQwords;108*destination++ = PIXEncodeEventInfo(time, PIXEventTypeInferer<ARGS...>::Begin());109*destination++ = color;110111PIXCopyEventArguments(destination, limit, formatString, args...);112113*destination = PIXEventsBlockEndMarker;114threadInfo->destination = destination;115}116117template<typename STR, typename... ARGS>118void PIXBeginEvent(UINT64 color, STR formatString, ARGS... args)119{120PIXEventsThreadInfo* threadInfo = PIXGetThreadInfo();121UINT64* limit = threadInfo->biasedLimit;122if (limit != nullptr)123{124UINT64* destination = threadInfo->destination;125if (destination < limit)126{127limit += PIXEventsSafeFastCopySpaceQwords;128UINT64 time = PIXGetTimestampCounter();129*destination++ = PIXEncodeEventInfo(time, PIXEventTypeInferer<ARGS...>::Begin());130*destination++ = color;131132PIXCopyEventArguments(destination, limit, formatString, args...);133134*destination = PIXEventsBlockEndMarker;135threadInfo->destination = destination;136}137else138{139PIXBeginEventAllocate(threadInfo, color, formatString, args...);140}141}142}143144template<typename STR, typename... ARGS>145__declspec(noinline) void PIXSetMarkerAllocate(PIXEventsThreadInfo* threadInfo, UINT64 color, STR formatString, ARGS... args)146{147#ifdef PIX_XBOX148UINT64 time = PIXEventsReplaceBlock(false);149#else150UINT64 time = PIXEventsReplaceBlock(threadInfo, false);151#endif152if (!time)153return;154155UINT64* destination = threadInfo->destination;156UINT64* limit = threadInfo->biasedLimit;157158if (destination >= limit)159return;160161limit += PIXEventsSafeFastCopySpaceQwords;162*destination++ = PIXEncodeEventInfo(time, PIXEventTypeInferer<ARGS...>::SetMarker());163*destination++ = color;164165PIXCopyEventArguments(destination, limit, formatString, args...);166167*destination = PIXEventsBlockEndMarker;168threadInfo->destination = destination;169}170171template<typename STR, typename... ARGS>172void PIXSetMarker(UINT64 color, STR formatString, ARGS... args)173{174PIXEventsThreadInfo* threadInfo = PIXGetThreadInfo();175UINT64* limit = threadInfo->biasedLimit;176if (limit != nullptr)177{178UINT64* destination = threadInfo->destination;179if (destination < limit)180{181limit += PIXEventsSafeFastCopySpaceQwords;182UINT64 time = PIXGetTimestampCounter();183*destination++ = PIXEncodeEventInfo(time, PIXEventTypeInferer<ARGS...>::SetMarker());184*destination++ = color;185186PIXCopyEventArguments(destination, limit, formatString, args...);187188*destination = PIXEventsBlockEndMarker;189threadInfo->destination = destination;190}191else192{193PIXSetMarkerAllocate(threadInfo, color, formatString, args...);194}195}196}197198template<typename STR, typename... ARGS>199__declspec(noinline) void PIXBeginEventOnContextCpuAllocate(PIXEventsThreadInfo* threadInfo, void* context, UINT64 color, STR formatString, ARGS... args)200{201#ifdef PIX_XBOX202UINT64 time = PIXEventsReplaceBlock(false);203#else204UINT64 time = PIXEventsReplaceBlock(threadInfo, false);205#endif206if (!time)207return;208209UINT64* destination = threadInfo->destination;210UINT64* limit = threadInfo->biasedLimit;211212if (destination >= limit)213return;214215limit += PIXEventsSafeFastCopySpaceQwords;216*destination++ = PIXEncodeEventInfo(time, PIXEventTypeInferer<ARGS...>::BeginOnContext());217*destination++ = color;218219#ifdef PIX_XBOX220UNREFERENCED_PARAMETER(context);221PIXCopyEventArguments(destination, limit, formatString, args...);222#else223PIXCopyEventArguments(destination, limit, context, formatString, args...);224#endif225226*destination = PIXEventsBlockEndMarker;227threadInfo->destination = destination;228}229230template<typename STR, typename... ARGS>231void PIXBeginEventOnContextCpu(void* context, UINT64 color, STR formatString, ARGS... args)232{233PIXEventsThreadInfo* threadInfo = PIXGetThreadInfo();234UINT64* limit = threadInfo->biasedLimit;235if (limit != nullptr)236{237UINT64* destination = threadInfo->destination;238if (destination < limit)239{240limit += PIXEventsSafeFastCopySpaceQwords;241UINT64 time = PIXGetTimestampCounter();242*destination++ = PIXEncodeEventInfo(time, PIXEventTypeInferer<ARGS...>::BeginOnContext());243*destination++ = color;244245#ifdef PIX_XBOX246PIXCopyEventArguments(destination, limit, formatString, args...);247#else248PIXCopyEventArguments(destination, limit, context, formatString, args...);249#endif250251*destination = PIXEventsBlockEndMarker;252threadInfo->destination = destination;253}254else255{256PIXBeginEventOnContextCpuAllocate(threadInfo, context, color, formatString, args...);257}258}259}260261template<typename CONTEXT, typename STR, typename... ARGS>262void PIXBeginEvent(CONTEXT* context, UINT64 color, STR formatString, ARGS... args)263{264#ifdef PIX_CONTEXT_EMIT_CPU_EVENTS265PIXBeginEventOnContextCpu(context, color, formatString, args...);266#endif267268// TODO: we've already encoded this once for the CPU event - figure out way to avoid doing it again269UINT64 buffer[PIXEventsGraphicsRecordSpaceQwords];270UINT64* destination = buffer;271UINT64* limit = buffer + PIXEventsGraphicsRecordSpaceQwords - PIXEventsReservedTailSpaceQwords;272273*destination++ = PIXEncodeEventInfo(0, PIXEventTypeInferer<ARGS...>::GpuBeginOnContext());274*destination++ = color;275276PIXCopyEventArguments(destination, limit, formatString, args...);277*destination = 0ull;278279PIXBeginGPUEventOnContext(context, static_cast<void*>(buffer), static_cast<UINT>(reinterpret_cast<BYTE*>(destination) - reinterpret_cast<BYTE*>(buffer)));280}281282template<typename STR, typename... ARGS>283__declspec(noinline) void PIXSetMarkerOnContextCpuAllocate(PIXEventsThreadInfo* threadInfo, void* context, UINT64 color, STR formatString, ARGS... args)284{285#ifdef PIX_XBOX286UINT64 time = PIXEventsReplaceBlock(false);287#else288UINT64 time = PIXEventsReplaceBlock(threadInfo, false);289#endif290if (!time)291return;292293UINT64* destination = threadInfo->destination;294UINT64* limit = threadInfo->biasedLimit;295296if (destination >= limit)297return;298299limit += PIXEventsSafeFastCopySpaceQwords;300*destination++ = PIXEncodeEventInfo(time, PIXEventTypeInferer<ARGS...>::SetMarkerOnContext());301*destination++ = color;302303#ifdef PIX_XBOX304UNREFERENCED_PARAMETER(context);305PIXCopyEventArguments(destination, limit, formatString, args...);306#else307PIXCopyEventArguments(destination, limit, context, formatString, args...);308#endif309310*destination = PIXEventsBlockEndMarker;311threadInfo->destination = destination;312}313314template<typename STR, typename... ARGS>315void PIXSetMarkerOnContextCpu(void* context, UINT64 color, STR formatString, ARGS... args)316{317PIXEventsThreadInfo* threadInfo = PIXGetThreadInfo();318UINT64* limit = threadInfo->biasedLimit;319if (limit != nullptr)320{321UINT64* destination = threadInfo->destination;322if (destination < limit)323{324limit += PIXEventsSafeFastCopySpaceQwords;325UINT64 time = PIXGetTimestampCounter();326*destination++ = PIXEncodeEventInfo(time, PIXEventTypeInferer<ARGS...>::SetMarkerOnContext());327*destination++ = color;328329#ifdef PIX_XBOX330PIXCopyEventArguments(destination, limit, formatString, args...);331#else332PIXCopyEventArguments(destination, limit, context, formatString, args...);333#endif334335*destination = PIXEventsBlockEndMarker;336threadInfo->destination = destination;337}338else339{340PIXSetMarkerOnContextCpuAllocate(threadInfo, context, color, formatString, args...);341}342}343}344345template<typename CONTEXT, typename STR, typename... ARGS>346void PIXSetMarker(CONTEXT* context, UINT64 color, STR formatString, ARGS... args)347{348#ifdef PIX_CONTEXT_EMIT_CPU_EVENTS349PIXSetMarkerOnContextCpu(context, color, formatString, args...);350#endif351352UINT64 buffer[PIXEventsGraphicsRecordSpaceQwords];353UINT64* destination = buffer;354UINT64* limit = buffer + PIXEventsGraphicsRecordSpaceQwords - PIXEventsReservedTailSpaceQwords;355356*destination++ = PIXEncodeEventInfo(0, PIXEventTypeInferer<ARGS...>::GpuSetMarkerOnContext());357*destination++ = color;358359PIXCopyEventArguments(destination, limit, formatString, args...);360*destination = 0ull;361362PIXSetGPUMarkerOnContext(context, static_cast<void*>(buffer), static_cast<UINT>(reinterpret_cast<BYTE*>(destination) - reinterpret_cast<BYTE*>(buffer)));363}364365__declspec(noinline) inline void PIXEndEventAllocate(PIXEventsThreadInfo* threadInfo)366{367#ifdef PIX_XBOX368UINT64 time = PIXEventsReplaceBlock(true);369#else370UINT64 time = PIXEventsReplaceBlock(threadInfo, true);371#endif372if (!time)373return;374375UINT64* destination = threadInfo->destination;376UINT64* limit = threadInfo->biasedLimit;377378if (destination >= limit)379return;380381limit += PIXEventsSafeFastCopySpaceQwords;382*destination++ = PIXEncodeEventInfo(time, PIXEvent_EndEvent);383*destination = PIXEventsBlockEndMarker;384threadInfo->destination = destination;385}386387inline void PIXEndEvent()388{389PIXEventsThreadInfo* threadInfo = PIXGetThreadInfo();390UINT64* limit = threadInfo->biasedLimit;391if (limit != nullptr)392{393UINT64* destination = threadInfo->destination;394if (destination < limit)395{396limit += PIXEventsSafeFastCopySpaceQwords;397UINT64 time = PIXGetTimestampCounter();398*destination++ = PIXEncodeEventInfo(time, PIXEvent_EndEvent);399*destination = PIXEventsBlockEndMarker;400threadInfo->destination = destination;401}402else403{404PIXEndEventAllocate(threadInfo);405}406}407}408409__declspec(noinline) inline void PIXEndEventOnContextCpuAllocate(PIXEventsThreadInfo* threadInfo, void* context)410{411#ifdef PIX_XBOX412UINT64 time = PIXEventsReplaceBlock(true);413#else414UINT64 time = PIXEventsReplaceBlock(threadInfo, true);415#endif416if (!time)417return;418419UINT64* destination = threadInfo->destination;420UINT64* limit = threadInfo->biasedLimit;421422if (destination >= limit)423return;424425limit += PIXEventsSafeFastCopySpaceQwords;426*destination++ = PIXEncodeEventInfo(time, PIXEvent_EndEvent_OnContext);427#ifdef PIX_XBOX428UNREFERENCED_PARAMETER(context);429#else430PIXCopyEventArgument(destination, limit, context);431#endif432*destination = PIXEventsBlockEndMarker;433threadInfo->destination = destination;434}435436inline void PIXEndEventOnContextCpu(void* context)437{438PIXEventsThreadInfo* threadInfo = PIXGetThreadInfo();439UINT64* limit = threadInfo->biasedLimit;440if (limit != nullptr)441{442UINT64* destination = threadInfo->destination;443if (destination < limit)444{445limit += PIXEventsSafeFastCopySpaceQwords;446UINT64 time = PIXGetTimestampCounter();447*destination++ = PIXEncodeEventInfo(time, PIXEvent_EndEvent_OnContext);448#ifndef PIX_XBOX449PIXCopyEventArgument(destination, limit, context);450#endif451*destination = PIXEventsBlockEndMarker;452threadInfo->destination = destination;453}454else455{456PIXEndEventOnContextCpuAllocate(threadInfo, context);457}458}459}460461template<typename CONTEXT>462void PIXEndEvent(CONTEXT* context)463{464#ifdef PIX_CONTEXT_EMIT_CPU_EVENTS465PIXEndEventOnContextCpu(context);466#endif467PIXEndGPUEventOnContext(context);468}469}470471#if defined(USE_PIX)472473template<typename... ARGS>474void PIXBeginEvent(UINT64 color, PCWSTR formatString, ARGS... args)475{476PIXEventsDetail::PIXBeginEvent(color, formatString, args...);477}478479template<typename... ARGS>480void PIXBeginEvent(UINT64 color, PCSTR formatString, ARGS... args)481{482PIXEventsDetail::PIXBeginEvent(color, formatString, args...);483}484485template<typename... ARGS>486void PIXSetMarker(UINT64 color, PCWSTR formatString, ARGS... args)487{488PIXEventsDetail::PIXSetMarker(color, formatString, args...);489}490491template<typename... ARGS>492void PIXSetMarker(UINT64 color, PCSTR formatString, ARGS... args)493{494PIXEventsDetail::PIXSetMarker(color, formatString, args...);495}496497template<typename CONTEXT, typename... ARGS>498void PIXBeginEvent(CONTEXT* context, UINT64 color, PCWSTR formatString, ARGS... args)499{500PIXEventsDetail::PIXBeginEvent(context, color, formatString, args...);501}502503template<typename CONTEXT, typename... ARGS>504void PIXBeginEvent(CONTEXT* context, UINT64 color, PCSTR formatString, ARGS... args)505{506PIXEventsDetail::PIXBeginEvent(context, color, formatString, args...);507}508509template<typename CONTEXT, typename... ARGS>510void PIXSetMarker(CONTEXT* context, UINT64 color, PCWSTR formatString, ARGS... args)511{512PIXEventsDetail::PIXSetMarker(context, color, formatString, args...);513}514515template<typename CONTEXT, typename... ARGS>516void PIXSetMarker(CONTEXT* context, UINT64 color, PCSTR formatString, ARGS... args)517{518PIXEventsDetail::PIXSetMarker(context, color, formatString, args...);519}520521inline void PIXEndEvent()522{523PIXEventsDetail::PIXEndEvent();524}525526template<typename CONTEXT>527void PIXEndEvent(CONTEXT* context)528{529PIXEventsDetail::PIXEndEvent(context);530}531532#else // USE_PIX_RETAIL533534inline void PIXBeginEvent(UINT64, _In_ PCSTR, ...) {}535inline void PIXBeginEvent(UINT64, _In_ PCWSTR, ...) {}536inline void PIXBeginEvent(void*, UINT64, _In_ PCSTR, ...) {}537inline void PIXBeginEvent(void*, UINT64, _In_ PCWSTR, ...) {}538inline void PIXEndEvent() {}539inline void PIXEndEvent(void*) {}540inline void PIXSetMarker(UINT64, _In_ PCSTR, ...) {}541inline void PIXSetMarker(UINT64, _In_ PCWSTR, ...) {}542inline void PIXSetMarker(void*, UINT64, _In_ PCSTR, ...) {}543inline void PIXSetMarker(void*, UINT64, _In_ PCWSTR, ...) {}544545#endif // USE_PIX546547template<typename CONTEXT, typename... ARGS>548void PIXBeginRetailEvent(CONTEXT* context, UINT64 color, PCWSTR formatString, ARGS... args)549{550PIXEventsDetail::PIXBeginEvent(context, color, formatString, args...);551}552553template<typename CONTEXT, typename... ARGS>554void PIXBeginRetailEvent(CONTEXT* context, UINT64 color, PCSTR formatString, ARGS... args)555{556PIXEventsDetail::PIXBeginEvent(context, color, formatString, args...);557}558559template<typename CONTEXT, typename... ARGS>560void PIXSetRetailMarker(CONTEXT* context, UINT64 color, PCWSTR formatString, ARGS... args)561{562PIXEventsDetail::PIXSetMarker(context, color, formatString, args...);563}564565template<typename CONTEXT, typename... ARGS>566void PIXSetRetailMarker(CONTEXT* context, UINT64 color, PCSTR formatString, ARGS... args)567{568PIXEventsDetail::PIXSetMarker(context, color, formatString, args...);569}570571template<typename CONTEXT>572void PIXEndRetailEvent(CONTEXT* context)573{574PIXEventsDetail::PIXEndEvent(context);575}576577template<typename CONTEXT>578class PIXScopedEventObject579{580CONTEXT* m_context;581582public:583template<typename... ARGS>584PIXScopedEventObject(CONTEXT* context, UINT64 color, PCWSTR formatString, ARGS... args)585: m_context(context)586{587PIXBeginEvent(m_context, color, formatString, args...);588}589590template<typename... ARGS>591PIXScopedEventObject(CONTEXT* context, UINT64 color, PCSTR formatString, ARGS... args)592: m_context(context)593{594PIXBeginEvent(m_context, color, formatString, args...);595}596597~PIXScopedEventObject()598{599PIXEndEvent(m_context);600}601};602603template<typename CONTEXT>604class PIXScopedRetailEventObject605{606CONTEXT* m_context;607608public:609template<typename... ARGS>610PIXScopedRetailEventObject(CONTEXT* context, UINT64 color, PCWSTR formatString, ARGS... args)611: m_context(context)612{613PIXBeginRetailEvent(m_context, color, formatString, args...);614}615616template<typename... ARGS>617PIXScopedRetailEventObject(CONTEXT* context, UINT64 color, PCSTR formatString, ARGS... args)618: m_context(context)619{620PIXBeginRetailEvent(m_context, color, formatString, args...);621}622623~PIXScopedRetailEventObject()624{625PIXEndRetailEvent(m_context);626}627};628629template<>630class PIXScopedEventObject<void>631{632public:633template<typename... ARGS>634PIXScopedEventObject(UINT64 color, PCWSTR formatString, ARGS... args)635{636PIXBeginEvent(color, formatString, args...);637}638639template<typename... ARGS>640PIXScopedEventObject(UINT64 color, PCSTR formatString, ARGS... args)641{642PIXBeginEvent(color, formatString, args...);643}644645~PIXScopedEventObject()646{647PIXEndEvent();648}649};650651#define PIXConcatenate(a, b) a ## b652#define PIXGetScopedEventVariableName(a, b) PIXConcatenate(a, b)653#define PIXScopedEvent(context, ...) PIXScopedEventObject<PIXInferScopedEventType<decltype(context)>::Type> PIXGetScopedEventVariableName(pixEvent, __LINE__)(context, __VA_ARGS__)654655#ifdef PIX3__DEFINED_CONSTEXPR656#undef constexpr657#undef PIX3__DEFINED_CONSTEXPR658#endif659660#endif // _PIXEvents_H__661662663