Skip to content

Commit

Permalink
Add DepthStencilBuffer class
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Dec 12, 2020
1 parent cedce85 commit ea7d864
Show file tree
Hide file tree
Showing 10 changed files with 640 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cmake/pomdog/PomdogGraphics.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ target_sources(pomdog_static PRIVATE
${POMDOG_SRC_DIR}/Graphics/ConstantBuffer.cpp
${POMDOG_INC_DIR}/Graphics/CullMode.hpp
${POMDOG_INC_DIR}/Graphics/DepthFormat.hpp
${POMDOG_INC_DIR}/Graphics/DepthStencilBuffer.hpp
${POMDOG_SRC_DIR}/Graphics/DepthStencilBuffer.cpp
${POMDOG_INC_DIR}/Graphics/DepthStencilDescription.hpp
${POMDOG_INC_DIR}/Graphics/DepthStencilOperation.hpp
${POMDOG_INC_DIR}/Graphics/EffectAnnotation.hpp
Expand Down Expand Up @@ -103,6 +105,8 @@ target_sources(pomdog_static PRIVATE
${POMDOG_SRC_DIR}/Graphics.GL4/BlendStateGL4.hpp
${POMDOG_SRC_DIR}/Graphics.GL4/BufferGL4.cpp
${POMDOG_SRC_DIR}/Graphics.GL4/BufferGL4.hpp
${POMDOG_SRC_DIR}/Graphics.GL4/DepthStencilBufferGL4.cpp
${POMDOG_SRC_DIR}/Graphics.GL4/DepthStencilBufferGL4.hpp
${POMDOG_SRC_DIR}/Graphics.GL4/DepthStencilStateGL4.cpp
${POMDOG_SRC_DIR}/Graphics.GL4/DepthStencilStateGL4.hpp
${POMDOG_SRC_DIR}/Graphics.GL4/EffectReflectionGL4.cpp
Expand Down Expand Up @@ -155,6 +159,8 @@ target_sources(pomdog_static PRIVATE
# NOTE: Graphics.Direct3D11
${POMDOG_SRC_DIR}/Graphics.Direct3D11/BufferDirect3D11.cpp
${POMDOG_SRC_DIR}/Graphics.Direct3D11/BufferDirect3D11.hpp
${POMDOG_SRC_DIR}/Graphics.Direct3D11/DepthStencilBufferDirect3D11.cpp
${POMDOG_SRC_DIR}/Graphics.Direct3D11/DepthStencilBufferDirect3D11.hpp
${POMDOG_SRC_DIR}/Graphics.Direct3D11/EffectReflectionDirect3D11.cpp
${POMDOG_SRC_DIR}/Graphics.Direct3D11/EffectReflectionDirect3D11.hpp
${POMDOG_SRC_DIR}/Graphics.Direct3D11/FormatHelper.cpp
Expand All @@ -181,6 +187,8 @@ target_sources(pomdog_static PRIVATE
${POMDOG_SRC_DIR}/Graphics.Metal/BufferMetal.hpp
${POMDOG_SRC_DIR}/Graphics.Metal/BufferMetal.mm
${POMDOG_SRC_DIR}/Graphics.Metal/ConstantsMetal.hpp
${POMDOG_SRC_DIR}/Graphics.Metal/DepthStencilBufferMetal.hpp
${POMDOG_SRC_DIR}/Graphics.Metal/DepthStencilBufferMetal.mm
${POMDOG_SRC_DIR}/Graphics.Metal/EffectReflectionMetal.hpp
${POMDOG_SRC_DIR}/Graphics.Metal/EffectReflectionMetal.mm
${POMDOG_SRC_DIR}/Graphics.Metal/GraphicsContextMetal.hpp
Expand Down
36 changes: 36 additions & 0 deletions include/Pomdog/Graphics/DepthStencilBuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2013-2020 mogemimi. Distributed under the MIT license.

#pragma once

#include "Pomdog/Basic/Export.hpp"
#include "Pomdog/Graphics/DepthFormat.hpp"
#include "Pomdog/Graphics/ForwardDeclarations.hpp"
#include "Pomdog/Graphics/Texture.hpp"
#include "Pomdog/Math/ForwardDeclarations.hpp"
#include <cstdint>
#include <memory>

namespace Pomdog {

class POMDOG_EXPORT DepthStencilBuffer : public Texture {
public:
DepthStencilBuffer() noexcept;
DepthStencilBuffer(const DepthStencilBuffer&) = delete;
DepthStencilBuffer& operator=(const DepthStencilBuffer&) = delete;

virtual ~DepthStencilBuffer();

/// Gets the width of the texture data, in pixels.
virtual std::int32_t GetWidth() const noexcept = 0;

/// Gets the height of the texture data, in pixels.
virtual std::int32_t GetHeight() const noexcept = 0;

/// Gets the format of the pixel data in the depth-stencil buffer.
virtual DepthFormat GetFormat() const noexcept = 0;

/// Gets the size of the texture resource.
virtual Rectangle GetBounds() const noexcept = 0;
};

} // namespace Pomdog
1 change: 1 addition & 0 deletions include/Pomdog/Graphics/ForwardDeclarations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Pomdog {

// Classes
class ConstantBuffer;
class DepthStencilBuffer;
class EffectReflection;
class GraphicsCommandList;
class GraphicsCommandQueue;
Expand Down
183 changes: 183 additions & 0 deletions src/Graphics.Direct3D11/DepthStencilBufferDirect3D11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Copyright (c) 2013-2020 mogemimi. Distributed under the MIT license.

#include "DepthStencilBufferDirect3D11.hpp"
#include "../Graphics.DXGI/DXGIFormatHelper.hpp"
#include "Pomdog/Graphics/DepthFormat.hpp"
#include "Pomdog/Math/Rectangle.hpp"
#include "Pomdog/Utility/Assert.hpp"

namespace Pomdog::Detail::Direct3D11 {
namespace {

using DXGI::DXGIFormatHelper;
using Microsoft::WRL::ComPtr;

[[nodiscard]] std::shared_ptr<Error>
BuildDepthBuffer(
ID3D11Device* device,
DepthFormat depthStencilFormat,
std::int32_t pixelWidth,
std::int32_t pixelHeight,
std::int32_t levelCount,
std::int32_t multiSampleCount,
ComPtr<ID3D11Texture2D>& depthStencil,
ComPtr<ID3D11DepthStencilView>& depthStencilView) noexcept
{
if (depthStencilFormat == DepthFormat::None) {
return Errors::New("depthStencilFormat must be != DepthFormat::None");
}
if (pixelWidth <= 0) {
return Errors::New("pixelWidth must be > 0");
}
if (pixelHeight <= 0) {
return Errors::New("pixelHeight must be > 0");
}
if (levelCount <= 0) {
return Errors::New("levelCount must be > 0");
}
if (multiSampleCount <= 0) {
return Errors::New("multiSampleCount must be > 0");
}

POMDOG_ASSERT(device != nullptr);
POMDOG_ASSERT(pixelWidth > 0);
POMDOG_ASSERT(pixelHeight > 0);
POMDOG_ASSERT(levelCount > 0);
POMDOG_ASSERT(multiSampleCount > 0);

// NOTE: Create depth stencil texture
D3D11_TEXTURE2D_DESC textureDesc;
textureDesc.Format = DXGIFormatHelper::ToDXGIFormat(depthStencilFormat);
textureDesc.Width = pixelWidth;
textureDesc.Height = pixelHeight;
textureDesc.ArraySize = 1;
textureDesc.MipLevels = levelCount;
textureDesc.SampleDesc.Count = 1;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;

if (auto hr = device->CreateTexture2D(&textureDesc, nullptr, &depthStencil); FAILED(hr)) {
return Errors::New("CreateTexture2D() failed");
}

// NOTE: Create the depth stencil view (DSV)
D3D11_DEPTH_STENCIL_VIEW_DESC dsvDesc;
ZeroMemory(&dsvDesc, sizeof(dsvDesc));
dsvDesc.Format = textureDesc.Format;
dsvDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
dsvDesc.Texture2D.MipSlice = 0;

POMDOG_ASSERT(depthStencil != nullptr);

if (auto hr = device->CreateDepthStencilView(depthStencil.Get(), &dsvDesc, &depthStencilView); FAILED(hr)) {
return Errors::New("CreateDepthStencilView() failed");
}

return nullptr;
}

} // namespace

std::shared_ptr<Error>
DepthStencilBufferDirect3D11::Initialize(
ID3D11Device* device,
std::int32_t pixelWidthIn,
std::int32_t pixelHeightIn,
DepthFormat depthStencilFormatIn,
std::int32_t multiSampleCount) noexcept
{
pixelWidth = pixelWidthIn;
pixelHeight = pixelHeightIn;
depthStencilFormat = depthStencilFormatIn;
multiSampleEnabled = (multiSampleCount > 1);

constexpr std::int32_t levelCount = 1;

if (auto err = BuildDepthBuffer(
device,
depthStencilFormat,
pixelWidth,
pixelHeight,
levelCount,
multiSampleCount,
depthStencil,
depthStencilView);
err != nullptr) {
return Errors::Wrap(std::move(err), "BuildDepthBuffer() failed");
}

return nullptr;
}

std::int32_t DepthStencilBufferDirect3D11::GetWidth() const noexcept
{
return pixelWidth;
}

std::int32_t DepthStencilBufferDirect3D11::GetHeight() const noexcept
{
return pixelHeight;
}

DepthFormat DepthStencilBufferDirect3D11::GetFormat() const noexcept
{
return depthStencilFormat;
}

Rectangle DepthStencilBufferDirect3D11::GetBounds() const noexcept
{
return Rectangle{0, 0, pixelWidth, pixelHeight};
}

ID3D11DepthStencilView*
DepthStencilBufferDirect3D11::GetDepthStencilView() const noexcept
{
return depthStencilView.Get();
}

std::shared_ptr<Error>
DepthStencilBufferDirect3D11::ResetBackBuffer(
ID3D11Device* device,
std::int32_t pixelWidthIn,
std::int32_t pixelHeightIn,
DepthFormat depthStencilFormatIn,
std::int32_t multiSampleCount) noexcept
{
POMDOG_ASSERT(device != nullptr);

pixelWidth = pixelWidthIn;
pixelHeight = pixelHeightIn;
depthStencilFormat = depthStencilFormatIn;
multiSampleEnabled = (multiSampleCount > 1);

depthStencilView.Reset();
depthStencil.Reset();

constexpr std::int32_t levelCount = 1;

if (auto err = BuildDepthBuffer(
device,
depthStencilFormat,
pixelWidth,
pixelHeight,
levelCount,
multiSampleCount,
depthStencil,
depthStencilView);
err != nullptr) {
return Errors::Wrap(std::move(err), "BuildDepthBuffer() failed");
}

return nullptr;
}

void DepthStencilBufferDirect3D11::ResetBackBuffer() noexcept
{
depthStencilView.Reset();
depthStencil.Reset();
}

} // namespace Pomdog::Detail::Direct3D11
58 changes: 58 additions & 0 deletions src/Graphics.Direct3D11/DepthStencilBufferDirect3D11.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2013-2020 mogemimi. Distributed under the MIT license.

#pragma once

#include "PrerequisitesDirect3D11.hpp"
#include "Pomdog/Graphics/DepthStencilBuffer.hpp"
#include "Pomdog/Graphics/ForwardDeclarations.hpp"
#include "Pomdog/Utility/Errors.hpp"
#include <wrl/client.h>

namespace Pomdog::Detail::Direct3D11 {

class DepthStencilBufferDirect3D11 final : public DepthStencilBuffer {
public:
[[nodiscard]] std::shared_ptr<Error>
Initialize(
ID3D11Device* device,
std::int32_t pixelWidth,
std::int32_t pixelHeight,
DepthFormat depthStencilFormat,
std::int32_t multiSampleCount) noexcept;

/// Gets the width of the texture data, in pixels.
std::int32_t GetWidth() const noexcept override;

/// Gets the height of the texture data, in pixels.
std::int32_t GetHeight() const noexcept override;

/// Gets the format of the pixel data in the depth-stencil buffer.
DepthFormat GetFormat() const noexcept override;

/// Gets the size of the texture resource.
Rectangle GetBounds() const noexcept override;

/// Gets the pointer of the depth-stencil-view.
[[nodiscard]] ID3D11DepthStencilView*
GetDepthStencilView() const noexcept;

[[nodiscard]] std::shared_ptr<Error>
ResetBackBuffer(
ID3D11Device* device,
std::int32_t pixelWidth,
std::int32_t pixelHeight,
DepthFormat depthStencilFormat,
std::int32_t multiSampleCount) noexcept;

void ResetBackBuffer() noexcept;

private:
Microsoft::WRL::ComPtr<ID3D11Texture2D> depthStencil;
Microsoft::WRL::ComPtr<ID3D11DepthStencilView> depthStencilView;
std::int32_t pixelWidth = 0;
std::int32_t pixelHeight = 0;
DepthFormat depthStencilFormat = DepthFormat::None;
bool multiSampleEnabled = false;
};

} // namespace Pomdog::Detail::Direct3D11
Loading

0 comments on commit ea7d864

Please sign in to comment.