Rendering a cube in DirectX 12 is relatively easy. The basic steps are:

  1. Create a Vertex Buffer Object (VBO) to store the cube's vertices.
  2. Create an Index Buffer Object (IBO) to store the cube's indices.
  3. Create a Vertex Shader to make sure the cube is rendered in the right place in 3D space.
  4. Create a Pixel Shader to make sure the cube is rendered with the correct colors and textures.
  5. Call the DrawIndexed function to draw the cube.
// Create a Vertex Buffer Object (VBO) to store the cube's vertices.
auto vertexBuffer = m_device->CreateCommittedResource(
  &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
  D3D12_HEAP_FLAG_NONE,
  &CD3DX12_RESOURCE_DESC::Buffer(sizeof(Vertex) * numVertices),
  D3D12_RESOURCE_STATE_GENERIC_READ,
  nullptr);

// Create an Index Buffer Object (IBO) to store the cube's indices.
auto indexBuffer = m_device->CreateCommittedResource(
  &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
  D3D12_HEAP_FLAG_NONE,
  &CD3DX12_RESOURCE_DESC::Buffer(sizeof(uint16_t) * numIndices),
  D3D12_RESOURCE_STATE_GENERIC_READ,
  nullptr);

// Create a Vertex Shader to make sure the cube is rendered in the right place in 3D space.
auto vertexShader = m_device->CreateCommittedResource(
  &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
  D3D12_HEAP_FLAG_NONE,
  &CD3DX12_RESOURCE_DESC::Buffer(vertexShaderSize),
  D3D12_RESOURCE_STATE_COPY_DEST,
  nullptr);

// Create a Pixel Shader to make sure the cube is rendered with the correct colors and textures.
auto pixelShader = m_device->CreateCommittedResource(
  &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
  D3D12_HEAP_FLAG_NONE,
  &CD3DX12_RESOURCE_DESC::Buffer(pixelShaderSize),
  D3D12_RESOURCE_STATE_COPY_DEST,
  nullptr);

// Call the `DrawIndexed` function to draw the cube.
m_commandList->DrawIndexedInstanced(numIndices, 1, 0, 0, 0);

Textures

Importing a Texture in DirectX 12

Importing a texture in DirectX 12 is quite simple. First, you will need to create a CommittedResource to store the texture in. This resource can be created using the CreateCommittedResource function which takes in a HeapProperties object, a HeapFlag, a ResourceDesc, and a ResourceState.

Once the committed resource is created, you will need to use the copyTextureRegion method to copy the texture data into the committed resource. This method takes in the committed resource, a ResourceDesc, a ResourceDesc, the texture data, and the size of the texture data.

Finally, you will need to call the SetShaderResourceView method to set the texture as a shader resource view. This method takes in the committed resource, the ResourceDesc, and the shader resource view descriptor.

Once these steps are complete, you will have successfully imported a texture in DirectX 12.

// Create a CommittedResource to store the texture in
auto textureResource = m_device->CreateCommittedResource(
  &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
  D3D12_HEAP_FLAG_NONE,
  &CD3DX12_RESOURCE_DESC::Texture2D(textureFormat, textureWidth, textureHeight),
  D3D12_RESOURCE_STATE_COPY_DEST,
  nullptr);

// Copy texture data into the committed resource
m_commandList->CopyTextureRegion(
  &CD3DX12_TEXTURE_COPY_LOCATION(textureResource, 0),
  0, 0, 0,
  &CD3DX12_TEXTURE_COPY_LOCATION(textureData, 0),
  nullptr);

// Set the texture as a shader resource view
m_commandList->SetShaderResourceView(
  0, textureResource,
  &CD3DX12_SHADER_RESOURCE_VIEW_DESC(textureFormat, textureWidth, textureHeight));

Texture data

Loading a texture from disk for DirectX 12 requires a few steps. First, you must create a CommittedResource to store the texture. This can be done by calling the CreateCommittedResource function which takes in a HeapProperties object, a HeapFlag, a ResourceDesc, and a ResourceState.

Once the resource has been created, you can use the CreateWICTextureFromFile function to create a texture from a file on disk. This function takes in the Device, the CommandList, the path to the file, and the CommittedResource created above.

Finally, you must call the SetShaderResourceView method to set the texture as a shader resource view. This method takes in the committed resource, the ResourceDesc, and the shader resource view descriptor.

// Create a CommittedResource to store the texture in
auto textureResource = m_device->CreateCommittedResource(
  &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
  D3D12_HEAP_FLAG_NONE,
  &CD3DX12_RESOURCE_DESC::Texture2D(textureFormat, textureWidth, textureHeight),
  D3D12_RESOURCE_STATE_COPY_DEST,
  nullptr);

// Create a texture from a file on disk
CreateWICTextureFromFile(m_device, m_commandList, pathToFile, textureResource);

// Set the texture as a shader resource view
m_commandList->SetShaderResourceView(
  0, textureResource,
  &CD3DX12_SHADER_RESOURCE_VIEW_DESC(textureFormat, textureWidth, textureHeight));