GPU: Implement support for multisample antialiasing
This commit is contained in:
@ -4,7 +4,7 @@ Log_SetChannel(D3D11);
|
||||
|
||||
namespace D3D11 {
|
||||
|
||||
Texture::Texture() : m_width(0), m_height(0) {}
|
||||
Texture::Texture() : m_width(0), m_height(0), m_samples(0) {}
|
||||
|
||||
Texture::Texture(ComPtr<ID3D11Texture2D> texture, ComPtr<ID3D11ShaderResourceView> srv,
|
||||
ComPtr<ID3D11RenderTargetView> rtv)
|
||||
@ -13,6 +13,7 @@ Texture::Texture(ComPtr<ID3D11Texture2D> texture, ComPtr<ID3D11ShaderResourceVie
|
||||
const D3D11_TEXTURE2D_DESC desc = GetDesc();
|
||||
m_width = desc.Width;
|
||||
m_height = desc.Height;
|
||||
m_samples = desc.SampleDesc.Count;
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
@ -27,10 +28,10 @@ D3D11_TEXTURE2D_DESC Texture::GetDesc() const
|
||||
return desc;
|
||||
}
|
||||
|
||||
bool Texture::Create(ID3D11Device* device, u32 width, u32 height, DXGI_FORMAT format, u32 bind_flags,
|
||||
const void* initial_data, u32 initial_data_stride)
|
||||
bool Texture::Create(ID3D11Device* device, u32 width, u32 height, u32 samples, DXGI_FORMAT format, u32 bind_flags,
|
||||
const void* initial_data /* = nullptr */, u32 initial_data_stride /* = 0 */)
|
||||
{
|
||||
CD3D11_TEXTURE2D_DESC desc(format, width, height, 1, 1, bind_flags, D3D11_USAGE_DEFAULT, 0, 1, 0, 0);
|
||||
CD3D11_TEXTURE2D_DESC desc(format, width, height, 1, 1, bind_flags, D3D11_USAGE_DEFAULT, 0, samples, 0, 0);
|
||||
|
||||
D3D11_SUBRESOURCE_DATA srd;
|
||||
srd.pSysMem = initial_data;
|
||||
@ -48,8 +49,9 @@ bool Texture::Create(ID3D11Device* device, u32 width, u32 height, DXGI_FORMAT fo
|
||||
ComPtr<ID3D11ShaderResourceView> srv;
|
||||
if (bind_flags & D3D11_BIND_SHADER_RESOURCE)
|
||||
{
|
||||
const CD3D11_SHADER_RESOURCE_VIEW_DESC srv_desc(D3D11_SRV_DIMENSION_TEXTURE2D, desc.Format, 0, desc.MipLevels, 0,
|
||||
desc.ArraySize);
|
||||
const D3D11_SRV_DIMENSION srv_dimension =
|
||||
(desc.SampleDesc.Count > 1) ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D;
|
||||
const CD3D11_SHADER_RESOURCE_VIEW_DESC srv_desc(srv_dimension, desc.Format, 0, desc.MipLevels, 0, desc.ArraySize);
|
||||
const HRESULT hr = device->CreateShaderResourceView(texture.Get(), &srv_desc, srv.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
@ -61,7 +63,9 @@ bool Texture::Create(ID3D11Device* device, u32 width, u32 height, DXGI_FORMAT fo
|
||||
ComPtr<ID3D11RenderTargetView> rtv;
|
||||
if (bind_flags & D3D11_BIND_RENDER_TARGET)
|
||||
{
|
||||
const CD3D11_RENDER_TARGET_VIEW_DESC rtv_desc(D3D11_RTV_DIMENSION_TEXTURE2D, desc.Format, 0, 0, desc.ArraySize);
|
||||
const D3D11_RTV_DIMENSION rtv_dimension =
|
||||
(desc.SampleDesc.Count > 1) ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;
|
||||
const CD3D11_RENDER_TARGET_VIEW_DESC rtv_desc(rtv_dimension, desc.Format, 0, 0, desc.ArraySize);
|
||||
const HRESULT hr = device->CreateRenderTargetView(texture.Get(), &rtv_desc, rtv.GetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
@ -75,6 +79,7 @@ bool Texture::Create(ID3D11Device* device, u32 width, u32 height, DXGI_FORMAT fo
|
||||
m_rtv = std::move(rtv);
|
||||
m_width = desc.Width;
|
||||
m_height = desc.Height;
|
||||
m_samples = desc.SampleDesc.Count;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -86,8 +91,9 @@ bool Texture::Adopt(ID3D11Device* device, ComPtr<ID3D11Texture2D> texture)
|
||||
ComPtr<ID3D11ShaderResourceView> srv;
|
||||
if (desc.BindFlags & D3D11_BIND_SHADER_RESOURCE)
|
||||
{
|
||||
const CD3D11_SHADER_RESOURCE_VIEW_DESC srv_desc(D3D11_SRV_DIMENSION_TEXTURE2D, desc.Format, 0, desc.MipLevels, 0,
|
||||
desc.ArraySize);
|
||||
const D3D11_SRV_DIMENSION srv_dimension =
|
||||
(desc.SampleDesc.Count > 1) ? D3D11_SRV_DIMENSION_TEXTURE2DMS : D3D11_SRV_DIMENSION_TEXTURE2D;
|
||||
const CD3D11_SHADER_RESOURCE_VIEW_DESC srv_desc(srv_dimension, desc.Format, 0, desc.MipLevels, 0, desc.ArraySize);
|
||||
const HRESULT hr = device->CreateShaderResourceView(texture.Get(), &srv_desc, srv.ReleaseAndGetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
@ -99,7 +105,9 @@ bool Texture::Adopt(ID3D11Device* device, ComPtr<ID3D11Texture2D> texture)
|
||||
ComPtr<ID3D11RenderTargetView> rtv;
|
||||
if (desc.BindFlags & D3D11_BIND_RENDER_TARGET)
|
||||
{
|
||||
const CD3D11_RENDER_TARGET_VIEW_DESC rtv_desc(D3D11_RTV_DIMENSION_TEXTURE2D, desc.Format, 0, 0, desc.ArraySize);
|
||||
const D3D11_RTV_DIMENSION rtv_dimension =
|
||||
(desc.SampleDesc.Count > 1) ? D3D11_RTV_DIMENSION_TEXTURE2DMS : D3D11_RTV_DIMENSION_TEXTURE2D;
|
||||
const CD3D11_RENDER_TARGET_VIEW_DESC rtv_desc(rtv_dimension, desc.Format, 0, 0, desc.ArraySize);
|
||||
const HRESULT hr = device->CreateRenderTargetView(texture.Get(), &rtv_desc, rtv.ReleaseAndGetAddressOf());
|
||||
if (FAILED(hr))
|
||||
{
|
||||
@ -113,6 +121,7 @@ bool Texture::Adopt(ID3D11Device* device, ComPtr<ID3D11Texture2D> texture)
|
||||
m_rtv = std::move(rtv);
|
||||
m_width = desc.Width;
|
||||
m_height = desc.Height;
|
||||
m_samples = desc.SampleDesc.Count;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -123,6 +132,7 @@ void Texture::Destroy()
|
||||
m_texture.Reset();
|
||||
m_width = 0;
|
||||
m_height = 0;
|
||||
m_samples = 0;
|
||||
}
|
||||
|
||||
} // namespace D3D11
|
||||
@ -23,6 +23,8 @@ public:
|
||||
|
||||
ALWAYS_INLINE u32 GetWidth() const { return m_width; }
|
||||
ALWAYS_INLINE u32 GetHeight() const { return m_height; }
|
||||
ALWAYS_INLINE u32 GetSamples() const { return m_samples; }
|
||||
ALWAYS_INLINE bool IsMultisampled() const { return m_samples > 1; }
|
||||
ALWAYS_INLINE DXGI_FORMAT GetFormat() const { return GetDesc().Format; }
|
||||
D3D11_TEXTURE2D_DESC GetDesc() const;
|
||||
|
||||
@ -31,7 +33,7 @@ public:
|
||||
ALWAYS_INLINE operator ID3D11RenderTargetView*() const { return m_rtv.Get(); }
|
||||
ALWAYS_INLINE operator bool() const { return static_cast<bool>(m_texture); }
|
||||
|
||||
bool Create(ID3D11Device* device, u32 width, u32 height, DXGI_FORMAT format, u32 bind_flags,
|
||||
bool Create(ID3D11Device* device, u32 width, u32 height, u32 samples, DXGI_FORMAT format, u32 bind_flags,
|
||||
const void* initial_data = nullptr, u32 initial_data_stride = 0);
|
||||
bool Adopt(ID3D11Device* device, ComPtr<ID3D11Texture2D> texture);
|
||||
|
||||
@ -43,5 +45,6 @@ private:
|
||||
ComPtr<ID3D11RenderTargetView> m_rtv;
|
||||
u32 m_width;
|
||||
u32 m_height;
|
||||
u32 m_samples;
|
||||
};
|
||||
} // namespace D3D11
|
||||
@ -8,11 +8,13 @@ namespace GL {
|
||||
Texture::Texture() = default;
|
||||
|
||||
Texture::Texture(Texture&& moved)
|
||||
: m_id(moved.m_id), m_width(moved.m_width), m_height(moved.m_height), m_fbo_id(moved.m_fbo_id)
|
||||
: m_id(moved.m_id), m_width(moved.m_width), m_height(moved.m_height), m_samples(moved.m_samples),
|
||||
m_fbo_id(moved.m_fbo_id)
|
||||
{
|
||||
moved.m_id = 0;
|
||||
moved.m_width = 0;
|
||||
moved.m_height = 0;
|
||||
moved.m_samples = 0;
|
||||
moved.m_fbo_id = 0;
|
||||
}
|
||||
|
||||
@ -21,20 +23,32 @@ Texture::~Texture()
|
||||
Destroy();
|
||||
}
|
||||
|
||||
bool Texture::Create(u32 width, u32 height, GLenum internal_format, GLenum format, GLenum type, const void* data,
|
||||
bool linear_filter, bool wrap)
|
||||
bool Texture::Create(u32 width, u32 height, u32 samples, GLenum internal_format, GLenum format, GLenum type,
|
||||
const void* data, bool linear_filter, bool wrap)
|
||||
{
|
||||
glGetError();
|
||||
|
||||
const GLenum target = (samples > 1) ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
|
||||
|
||||
GLuint id;
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, data);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1);
|
||||
glBindTexture(target, id);
|
||||
|
||||
if (samples > 1)
|
||||
{
|
||||
glTexImage2DMultisample(target, samples, internal_format, width, height, GL_FALSE);
|
||||
}
|
||||
else
|
||||
{
|
||||
glTexImage2D(target, 0, internal_format, width, height, 0, format, type, data);
|
||||
|
||||
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
|
||||
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
|
||||
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE);
|
||||
}
|
||||
|
||||
glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, 1);
|
||||
|
||||
GLenum error = glGetError();
|
||||
if (error != GL_NO_ERROR)
|
||||
@ -50,15 +64,19 @@ bool Texture::Create(u32 width, u32 height, GLenum internal_format, GLenum forma
|
||||
m_id = id;
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
m_samples = samples;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Texture::SetLinearFilter(bool enabled)
|
||||
{
|
||||
Assert(!IsMultisampled());
|
||||
|
||||
Bind();
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, enabled ? GL_LINEAR : GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, enabled ? GL_LINEAR : GL_NEAREST);
|
||||
const GLenum target = GetGLTarget();
|
||||
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, enabled ? GL_LINEAR : GL_NEAREST);
|
||||
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, enabled ? GL_LINEAR : GL_NEAREST);
|
||||
}
|
||||
|
||||
bool Texture::CreateFramebuffer()
|
||||
@ -100,11 +118,12 @@ void Texture::Destroy()
|
||||
|
||||
m_width = 0;
|
||||
m_height = 0;
|
||||
m_samples = 0;
|
||||
}
|
||||
|
||||
void Texture::Bind()
|
||||
{
|
||||
glBindTexture(GL_TEXTURE_2D, m_id);
|
||||
glBindTexture(GetGLTarget(), m_id);
|
||||
}
|
||||
|
||||
void Texture::BindFramebuffer(GLenum target /*= GL_DRAW_FRAMEBUFFER*/)
|
||||
@ -125,11 +144,13 @@ Texture& Texture::operator=(Texture&& moved)
|
||||
m_id = moved.m_id;
|
||||
m_width = moved.m_width;
|
||||
m_height = moved.m_height;
|
||||
m_samples = moved.m_samples;
|
||||
m_fbo_id = moved.m_fbo_id;
|
||||
|
||||
moved.m_id = 0;
|
||||
moved.m_width = 0;
|
||||
moved.m_height = 0;
|
||||
moved.m_samples = 0;
|
||||
moved.m_fbo_id = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -10,8 +10,8 @@ public:
|
||||
Texture(Texture&& moved);
|
||||
~Texture();
|
||||
|
||||
bool Create(u32 width, u32 height, GLenum internal_format, GLenum format, GLenum type, const void* data = nullptr,
|
||||
bool linear_filter = false, bool wrap = false);
|
||||
bool Create(u32 width, u32 height, u32 samples, GLenum internal_format, GLenum format, GLenum type,
|
||||
const void* data = nullptr, bool linear_filter = false, bool wrap = false);
|
||||
bool CreateFramebuffer();
|
||||
|
||||
void Destroy();
|
||||
@ -19,11 +19,14 @@ public:
|
||||
void SetLinearFilter(bool enabled);
|
||||
|
||||
bool IsValid() const { return m_id != 0; }
|
||||
bool IsMultisampled() const { return m_samples > 1; }
|
||||
GLuint GetGLId() const { return m_id; }
|
||||
u32 GetWidth() const { return m_width; }
|
||||
u32 GetHeight() const { return m_height; }
|
||||
u32 GetSamples() const { return m_samples; }
|
||||
|
||||
GLuint GetGLFramebufferID() const { return m_fbo_id; }
|
||||
GLenum GetGLTarget() const { return IsMultisampled() ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D; }
|
||||
|
||||
void Bind();
|
||||
void BindFramebuffer(GLenum target = GL_DRAW_FRAMEBUFFER);
|
||||
@ -42,6 +45,7 @@ private:
|
||||
GLuint m_id = 0;
|
||||
u32 m_width = 0;
|
||||
u32 m_height = 0;
|
||||
u32 m_samples = 0;
|
||||
|
||||
GLuint m_fbo_id = 0;
|
||||
};
|
||||
|
||||
@ -248,6 +248,13 @@ void GraphicsPipelineBuilder::SetLineWidth(float width)
|
||||
m_rasterization_state.lineWidth = width;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetMultisamples(u32 multisamples, bool per_sample_shading)
|
||||
{
|
||||
m_multisample_state.rasterizationSamples = static_cast<VkSampleCountFlagBits>(multisamples);
|
||||
m_multisample_state.sampleShadingEnable = per_sample_shading;
|
||||
m_multisample_state.minSampleShading = (multisamples > 1) ? 0.0f : 1.0f;
|
||||
}
|
||||
|
||||
void GraphicsPipelineBuilder::SetNoCullRasterizationState()
|
||||
{
|
||||
SetRasterizationState(VK_POLYGON_MODE_FILL, VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE);
|
||||
|
||||
@ -81,6 +81,7 @@ public:
|
||||
|
||||
void SetRasterizationState(VkPolygonMode polygon_mode, VkCullModeFlags cull_mode, VkFrontFace front_face);
|
||||
void SetLineWidth(float width);
|
||||
void SetMultisamples(u32 multisamples, bool per_sample_shading);
|
||||
void SetNoCullRasterizationState();
|
||||
|
||||
void SetDepthState(bool depth_test, bool depth_write, VkCompareOp compare_op);
|
||||
|
||||
@ -472,6 +472,7 @@ bool Context::SelectDeviceFeatures(const VkPhysicalDeviceFeatures* required_feat
|
||||
|
||||
// Enable the features we use.
|
||||
m_device_features.dualSrcBlend = available_features.dualSrcBlend;
|
||||
m_device_features.sampleRateShading = available_features.sampleRateShading;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user