Program Listing for File directx-range-repeat-cache.h
↰ Return to documentation for file (include\playback\directx-range-repeat-cache.h
)
/******************************************************************************
© Intel Corporation.
This software and the related documents are Intel copyrighted materials,
and your use of them is governed by the express license under which they
were provided to you ("License"). Unless the License provides otherwise,
you may not use, modify, copy, publish, distribute, disclose or transmit
this software or the related documents without Intel's prior written
permission.
This software and the related documents are provided as is, with no express
or implied warranties, other than those that are expressly stated in the
License.
******************************************************************************/
#pragma once
#include "playback/range-repeat-cache.h"
#include "serialization/apicallcontext.h"
#include "object-map/object-map.h"
#include "logger/logger.h"
#include "utility/directx11-utilities.h"
#include "playback-support/playback-support.h"
#include "dispatch-table/dispatch-table.h"
#include <d3d11_4.h>
#include <map>
#include <array>
#include "utility/assert.h"
namespace {
void AdjustRefCount(void* object, uint32_t refCount)
{
if (object == nullptr) {
GPA_ASSERT(false && "Argument required");
return;
}
IUnknown* unknown = (IUnknown*)object;
unknown->AddRef();
DWORD currentRefCount = unknown->Release();
while (currentRefCount < refCount) {
currentRefCount = unknown->AddRef();
}
// TODO : investigate very high reference counts ( 0xc0000001 )
/*while (currentRefCount > refCount) {
currentRefCount = unknown->Release();
}*/
}
} // namespace
namespace gpa {
namespace playback {
class Callable;
namespace repeat {
namespace directx {
template<class BaseClass>
class RangeRepeatCacheCOMInternal : public BaseClass
{
public:
typedef std::shared_ptr<RangeRepeatCacheCOMInternal<BaseClass>> Ptr;
RangeRepeatCacheCOMInternal(playback::Context* context, playback::Callable* callable, bool clearObjectMappingOnRelease = true)
: BaseClass(context, callable)
, mCreationCallable(*callable)
, mClearObjectMappingOnRelease(clearObjectMappingOnRelease)
{
}
virtual ~RangeRepeatCacheCOMInternal()
{
GPA_LOG_TRACE("RangeRepeatCacheCOM::dtor - obj %llx", mObjectKey);
if (mClearObjectMappingOnRelease) {
if (mContext->objMap->AcquireObject(mObjectKey) == mObject) {
mContext->objMap->SetObjectMapping(mObjectKey, nullptr);
}
}
};
virtual void OnRangeEnter(GPADispatchTable const*& table)
{
IUnknown* unknown = (IUnknown*)mObject;
unknown->AddRef();
mRefCount = unknown->Release();
BaseClass::OnRangeEnter(table);
}
virtual void OnRangeRepeatDestroyObject(GPADispatchTable const*& table, uint64_t repeatPass)
{
(void)table;
mDestroyingObjectCreatedInRange = true;
while (table->DirectX.ID3D12ResourceTable.Release((IUnknown*)mObject)) {
}
mDestroyingObjectCreatedInRange = false;
BaseClass::OnRangeRepeatDestroyObject(table, repeatPass);
}
virtual void OnRangeRepeatRecreateObject(GPADispatchTable const*& table, uint64_t repeatPass)
{
// Disable repeating flag to prevent from flagging the cache for mirror object as created during range
bool const isRepeating = mContext->repeating;
mContext->repeating = false;
mCreationCallable.PreExecute(mContext);
mCreationCallable.Execute(table, false);
mContext->repeating = isRepeating;
mObject = mContext->objMap->AcquireObject(mObjectKey);
GPA_ASSERT(mObject && "Could not obtain object from object map");
GPA_LOG_TRACE("OnRangeRepeatRecreateObject - recreated obj %llx key %llx with call %s", mObject, mObjectKey,
mCreationCallable.Name());
BaseClass::OnRangeRepeatRecreateObject(table, repeatPass);
}
virtual void OnRangeRepeatRestoreState(GPADispatchTable const*& table, uint64_t repeatPass)
{
GPA_LOG_TRACE("OnRangeRepeatRestoreState - obj %llx", mObjectKey);
if (!CreatedDuringRange()) {
AdjustRefCount(mObject, mRefCount);
}
BaseClass::OnRangeRepeatRestoreState(table, repeatPass);
}
virtual void OnRangeExit(GPADispatchTable const*& table)
{
GPA_LOG_TRACE("OnRangeExit - obj %llx", mObjectKey);
// We used to adjust ref count here
if (ReleasedDuringRange()) {
// This never happens, but will keep the code for now
// All the objects should be removed from the data manager when they die
repeat::RangeRepeatCache::Ptr existingCache =
std::static_pointer_cast<repeat::RangeRepeatCache>(mContext->dataManager->GetAssociatedDataRef((capture_support::DataManager::ObjectRef)mObject, playback::repeat::GetRangeRepeatCacheKey()));
if (existingCache && existingCache->CreationTimestamp() == CreationTimestamp()) {
GPA_LOG_TRACE("OnRangeExit - removing from DataMngr - obj %llx", mObjectKey);
mContext->dataManager->AssociateDataRefForObject((uint64_t)mObject, gpa::playback::repeat::GetRangeRepeatCacheKey(), nullptr);
}
}
BaseClass::OnRangeExit(table);
}
virtual void OnReleaseDuringRange(GPADispatchTable const*& table)
{
GPA_LOG_TRACE("OnReleaseDuringRange - obj %llx", mObjectKey);
RangeRepeatCache::OnReleaseDuringRange(table);
}
protected:
playback::Callable mCreationCallable;
private:
uint32_t mRefCount{};
bool mClearObjectMappingOnRelease{};
};
using RangeRepeatCacheCOM = RangeRepeatCacheCOMInternal<gpa::playback::repeat::RangeRepeatCache>;
using RangeRepeatCacheForCommandBuffersCOM = RangeRepeatCacheCOMInternal<gpa::playback::repeat::RangeRepeatCacheForCommandBuffers>;
template<typename ResourceType, typename ResourceDescType, typename CreateMethod>
ID3D11Resource* CreateResourceCopy(ID3D11Device* device, ID3D11DeviceContext* immediateContext,
CreateMethod createMethod, ID3D11Resource* resource)
{
ResourceDescType desc = {};
((ResourceType*)resource)->GetDesc(&desc);
ResourceType* copy = nullptr;
(device->*createMethod)(&desc, nullptr, ©);
GPA_ASSERT(copy && "Could not create resource copy");
immediateContext->CopyResource(copy, resource);
return copy;
}
class ID3D11ViewRangeRepeatCache : public RangeRepeatCacheCOM
{
public:
typedef std::shared_ptr<ID3D11ViewRangeRepeatCache> Ptr;
ID3D11ViewRangeRepeatCache(playback::Context* context, playback::Callable* callable);
virtual ~ID3D11ViewRangeRepeatCache();
void OnRangeEnter(GPADispatchTable const*& table) override;
void OnRangeRepeatRecreateObject(GPADispatchTable const*& table, uint64_t repeatPass) override;
};
class ID3D11ResourceRangeRepeatCache : public RangeRepeatCacheCOM
{
public:
typedef std::shared_ptr<ID3D11ResourceRangeRepeatCache> Ptr;
ID3D11ResourceRangeRepeatCache(playback::Context* context, playback::Callable* callable);
virtual ~ID3D11ResourceRangeRepeatCache();
void OnRangeEnter(GPADispatchTable const*& table) override;
void OnRangeRepeatRestoreState(GPADispatchTable const*& table, uint64_t repeatPass) override;
void OnRangeExit(GPADispatchTable const*& table) override;
void OnMapBeforeRepeatRange(ID3D11DeviceContext* context, UINT subresource, D3D11_MAP mapType);
void OnUnmapBeforeRepeatRange(ID3D11DeviceContext* context, UINT subresource);
IDXGISwapChain* GetSwapChain() const;
private:
struct SubresourceState
{
bool isMappedBeforeRepeat;
D3D11_MAP mapType;
};
std::map<uint32_t /*subresource index*/, SubresourceState> mSubresourceState;
ID3D11Resource* mCopy;
IDXGISwapChain* mSwapChain;
bool mIsBackBuffer;
};
class ID3D11ImmediateDeviceContextRangeRepeatCache : public RangeRepeatCacheCOM
{
std::unique_ptr<utility::directx::D3D11StateBlock> mStateBlock;
public:
typedef std::shared_ptr<ID3D11ImmediateDeviceContextRangeRepeatCache> Ptr;
ID3D11ImmediateDeviceContextRangeRepeatCache(playback::Context* context,
playback::Callable* callable);
virtual ~ID3D11ImmediateDeviceContextRangeRepeatCache();
void OnRangeEnter(GPADispatchTable const*& table) override;
void OnRangeRepeatRecreateObject(GPADispatchTable const*& table, uint64_t repeatPass) override;
void OnRangeRepeatRestoreState(GPADispatchTable const*& table, uint64_t repeatPass) override;
void OnRangeExit(GPADispatchTable const*& table) override;
};
} // namespace directx
} // namespace repeat
} // namespace playback
} // namespace gpa