Skip to content

EffectTextureFactory

Chuck Walbourn edited this page Jul 17, 2016 · 25 revisions

This is a helper object primarily used by the Model loader implementations to provide sharing of texture resources. This can be used standalone as well, and allows access to any existing textures definitions already loaded.

It uses a simple case-sensitive string-based (wide-character) map for finding effect instances that have already been created by the factory, which avoid duplication of resources in complex models and scenes.

Header

#include <Effects.h>

Initialization

The EffectTextureFactory constructor requires a Direct3D 12 device, a ResourceUploadBatch, and will create the required resource heap automatically.

std::unique_ptr<EffectTextureFactory> modelResources;

ResourceUploadBatch resourceUpload(device);

resourceUpload.Begin();

modelResources = std::make_unique<EffectTextureFactory>( device,
    resourceUploadBatch,
    numberOfDescriptors,
    D3D12_DESCRIPTOR_HEAP_FLAG_NONE);

CreateTexture(...);

auto uploadResourcesFinished = resourceUpload.End(m_deviceResources->GetCommandQueue());

// Wait for the command list to finish executing
m_deviceResources->WaitForGpu();

// Wait for the upload thread to terminate
uploadResourcesFinished.wait();

If you already have created the required heaps, you can provide your own:

states = std::make_unique<CommonStates>(device);

resourceDescriptors = std::make_unique<DescriptorHeap>(device,
    D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
    D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE,
    Descriptors::Count);

modelResources = std::make_unique<EffectFactory>( device,
    resourceUploadBatch,
    resourceDescriptors->Heap() );

For exception safety, it is recommended you make use of the C++ RAII pattern and use a std::unique_ptr or std::shared_ptr

Creating textures

UNDER CONSTRUCTION

Drawing

When drawing with texture resources, if you used the first constructor that create it's own heap, you must set it:

ID3D12DescriptorHeap* heaps[] = { modelResources->Heap(), states->Heap() };
commandList->SetDescriptorHeaps(_countof(heaps), heaps);

Otherwise you need to use the usual method of setting a DescriptorHeap:

ID3D12DescriptorHeap* heaps[] = { resourceDescriptors->Heap(), states->Heap() };
commandList->SetDescriptorHeaps(_countof(heaps), heaps);

Directories

The CreateTexture method assumes the name given is the filename. By default, this is a relative path to the current directory. To cause the factory to look in a specific directory path, use SetDirectory.

modelResources->SetDirectory( L".\\Assets" );

Interfaces

The EffectTextureFactory is a concrete implementation of the IEffectTextureFactory interface, and provides a default implementation and caching policy. This allows the developer to create their own custom version of the factory by deriving from IEffectTextureFactory, which can be used with Model loaders. This could be used for alternative caching policies, locating textures in packed archives, etc.

Sharing

You can control the sharing cache with two methods that are implemented for EffectTextureFactory.

This method sets the sharing mode which defaults to true. By setting it to false, CreateTexture will always return a new instance rather than returning a cached instance.

modelResources->SetSharing( false );

This method clears the sharing cache, which might not release all the instances if they are referenced by other objects.

modelResources->ReleaseCache();

For Use

  • Universal Windows Platform apps
  • Windows desktop apps
  • Windows 11
  • Windows 10
  • Xbox One
  • Xbox Series X|S

Architecture

  • x86
  • x64
  • ARM64

For Development

  • Visual Studio 2022
  • Visual Studio 2019 (16.11)
  • clang/LLVM v12 - v19
  • MinGW 12.2, 13.2
  • CMake 3.20

Related Projects

DirectX Tool Kit for DirectX 11

DirectXMesh

DirectXTex

DirectXMath

Tools

Test Suite

Model Viewer

Content Exporter

DxCapsViewer

See also

DirectX Landing Page

Clone this wiki locally