chore: sync workspace state

This commit is contained in:
2026-03-29 01:36:53 +08:00
parent eb5de3e3d4
commit e5cb79f3ce
4935 changed files with 35593 additions and 360696 deletions

View File

@@ -28,10 +28,10 @@ public:
ID3D12Resource* GetResource() const { return m_resource.Get(); }
D3D12_RESOURCE_DESC GetDesc() const { return m_resource->GetDesc(); }
uint32_t GetWidth() const override { return static_cast<uint32_t>(GetDesc().Width); }
uint32_t GetHeight() const override { return GetDesc().Height; }
uint32_t GetDepth() const override { return GetDesc().DepthOrArraySize; }
uint32_t GetMipLevels() const override { return GetDesc().MipLevels; }
uint32_t GetWidth() const override { return m_resource ? static_cast<uint32_t>(GetDesc().Width) : 0; }
uint32_t GetHeight() const override { return m_resource ? GetDesc().Height : 0; }
uint32_t GetDepth() const override { return m_resource ? GetDesc().DepthOrArraySize : 0; }
uint32_t GetMipLevels() const override { return m_resource ? GetDesc().MipLevels : 0; }
void* GetNativeHandle() override { return m_resource.Get(); }
ResourceStates GetState() const override { return m_state; }
@@ -43,22 +43,19 @@ public:
const std::string& GetName() const override { return m_name; }
void SetName(const std::string& name) override { m_name = name; }
uint32_t GetArraySize() const { return GetDesc().DepthOrArraySize; }
Format GetFormat() const override { return FromD3D12(GetDesc().Format); }
TextureType GetTextureType() const override {
switch (GetDesc().Dimension) {
case D3D12_RESOURCE_DIMENSION_TEXTURE1D: return TextureType::Texture1D;
case D3D12_RESOURCE_DIMENSION_TEXTURE2D: return TextureType::Texture2D;
case D3D12_RESOURCE_DIMENSION_TEXTURE3D: return TextureType::Texture3D;
default: return TextureType::Texture2D;
}
}
uint32_t GetArraySize() const { return m_resource ? GetDesc().DepthOrArraySize : 0; }
Format GetFormat() const override { return m_format != Format::Unknown ? m_format : (m_resource ? FromD3D12(GetDesc().Format) : Format::Unknown); }
TextureType GetTextureType() const override { return m_textureType; }
void SetFormat(Format format) { m_format = format; }
void SetTextureType(TextureType type) { m_textureType = type; }
bool OwnsResource() const { return m_ownsResource; }
private:
ComPtr<ID3D12Resource> m_resource;
ResourceStates m_state = ResourceStates::Common;
Format m_format = Format::Unknown;
TextureType m_textureType = TextureType::Texture2D;
std::string m_name;
bool m_ownsResource = false;
};

View File

@@ -21,7 +21,7 @@ public:
CommandQueueType GetType() const override { return CommandQueueType::Direct; }
uint64_t GetTimestampFrequency() const override;
void* GetNativeHandle() override { return nullptr; }
void* GetNativeHandle() override { return this; }
void WaitForPreviousFrame() override {}
uint64_t GetCurrentFrame() const override { return 0; }
};

View File

@@ -161,7 +161,7 @@ Vector3 Quaternion::ToEulerAngles() const {
float cosyCosp = 1.0f - 2.0f * (y * y + z * z);
float yaw = std::atan2(sinyCosp, cosyCosp);
return Vector3(pitch, yaw, roll);
return Vector3(roll, pitch, yaw);
}
Matrix4 Quaternion::ToMatrix4x4() const {

View File

@@ -17,6 +17,7 @@
#include "XCEngine/RHI/D3D12/D3D12ResourceView.h"
#include "XCEngine/RHI/D3D12/D3D12RenderPass.h"
#include "XCEngine/RHI/D3D12/D3D12Framebuffer.h"
#include <algorithm>
#include <stdio.h>
#include <memory>
#include <string>
@@ -83,6 +84,75 @@ uint32_t GetFormatBytesPerPixel(Format format) {
}
}
TextureType ResolveTextureType(uint32_t rawType) {
return static_cast<TextureType>(rawType);
}
uint16_t ResolveDepthOrArraySize(const TextureDesc& desc, TextureType textureType) {
switch (textureType) {
case TextureType::Texture3D:
return static_cast<uint16_t>(std::max<uint32_t>(desc.depth, 1));
case TextureType::Texture2DArray:
return static_cast<uint16_t>(std::max<uint32_t>(desc.arraySize, 1));
case TextureType::TextureCube:
case TextureType::TextureCubeArray:
return static_cast<uint16_t>(std::max<uint32_t>(desc.arraySize, 6));
default:
return 1;
}
}
std::string ShaderModelToString(D3D_SHADER_MODEL shaderModel) {
switch (shaderModel) {
case D3D_SHADER_MODEL_6_7: return "6.7";
case D3D_SHADER_MODEL_6_6: return "6.6";
case D3D_SHADER_MODEL_6_5: return "6.5";
case D3D_SHADER_MODEL_6_4: return "6.4";
case D3D_SHADER_MODEL_6_3: return "6.3";
case D3D_SHADER_MODEL_6_2: return "6.2";
case D3D_SHADER_MODEL_6_1: return "6.1";
case D3D_SHADER_MODEL_6_0: return "6.0";
case D3D_SHADER_MODEL_5_1: return "5.1";
default: return {};
}
}
D3D12_RTV_DIMENSION ResolveRTVDimension(const ResourceViewDesc& desc, TextureType textureType) {
switch (desc.dimension) {
case ResourceViewDimension::Texture1D:
return D3D12_RTV_DIMENSION_TEXTURE1D;
case ResourceViewDimension::Texture1DArray:
return D3D12_RTV_DIMENSION_TEXTURE1DARRAY;
case ResourceViewDimension::Texture2DArray:
case ResourceViewDimension::TextureCube:
case ResourceViewDimension::TextureCubeArray:
return D3D12_RTV_DIMENSION_TEXTURE2DARRAY;
case ResourceViewDimension::Texture3D:
return D3D12_RTV_DIMENSION_TEXTURE3D;
case ResourceViewDimension::Texture2DMS:
return D3D12_RTV_DIMENSION_TEXTURE2DMS;
case ResourceViewDimension::Texture2DMSArray:
return D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY;
case ResourceViewDimension::Unknown:
break;
default:
return D3D12_RTV_DIMENSION_TEXTURE2D;
}
switch (textureType) {
case TextureType::Texture1D:
return D3D12_RTV_DIMENSION_TEXTURE1D;
case TextureType::Texture2DArray:
case TextureType::TextureCube:
case TextureType::TextureCubeArray:
return D3D12_RTV_DIMENSION_TEXTURE2DARRAY;
case TextureType::Texture3D:
return D3D12_RTV_DIMENSION_TEXTURE3D;
default:
return D3D12_RTV_DIMENSION_TEXTURE2D;
}
}
} // namespace
D3D12Device::D3D12Device()
@@ -226,6 +296,28 @@ void D3D12Device::QueryAdapterInfo() {
m_capabilities.bSupportsConservativeRasterization = options.ConservativeRasterizationTier != D3D12_CONSERVATIVE_RASTERIZATION_TIER_NOT_SUPPORTED;
}
m_capabilities.bSupportsComputeShaders = true;
D3D12_FEATURE_DATA_SHADER_MODEL shaderModel = {};
const D3D_SHADER_MODEL shaderModelCandidates[] = {
D3D_SHADER_MODEL_6_7,
D3D_SHADER_MODEL_6_6,
D3D_SHADER_MODEL_6_5,
D3D_SHADER_MODEL_6_4,
D3D_SHADER_MODEL_6_3,
D3D_SHADER_MODEL_6_2,
D3D_SHADER_MODEL_6_1,
D3D_SHADER_MODEL_6_0,
D3D_SHADER_MODEL_5_1
};
for (D3D_SHADER_MODEL candidate : shaderModelCandidates) {
shaderModel.HighestShaderModel = candidate;
if (SUCCEEDED(m_device->CheckFeatureSupport(D3D12_FEATURE_SHADER_MODEL, &shaderModel, sizeof(shaderModel)))) {
m_capabilities.shaderModel = ShaderModelToString(shaderModel.HighestShaderModel);
break;
}
}
D3D12_FEATURE_DATA_D3D12_OPTIONS3 options3 = {};
if (SUCCEEDED(m_device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS3, &options3, sizeof(options3)))) {
}
@@ -321,33 +413,35 @@ RHIBuffer* D3D12Device::CreateBuffer(const BufferDesc& desc) {
RHITexture* D3D12Device::CreateTexture(const TextureDesc& desc) {
auto* texture = new D3D12Texture();
D3D12_RESOURCE_DESC d3d12Desc = {};
d3d12Desc.Dimension = ToD3D12(static_cast<TextureType>(desc.textureType));
const TextureType textureType = ResolveTextureType(desc.textureType);
const Format format = static_cast<Format>(desc.format);
const bool isDepthFormat = format == Format::D24_UNorm_S8_UInt ||
format == Format::D32_Float ||
format == Format::D16_UNorm;
d3d12Desc.Dimension = ToD3D12(textureType);
d3d12Desc.Width = desc.width;
d3d12Desc.Height = desc.height;
d3d12Desc.DepthOrArraySize = desc.depth > 0 ? desc.depth : 1;
d3d12Desc.DepthOrArraySize = ResolveDepthOrArraySize(desc, textureType);
d3d12Desc.MipLevels = desc.mipLevels > 0 ? desc.mipLevels : 1;
d3d12Desc.Format = ToD3D12(static_cast<Format>(desc.format));
d3d12Desc.Format = ToD3D12(format);
d3d12Desc.SampleDesc.Count = desc.sampleCount > 0 ? desc.sampleCount : 1;
d3d12Desc.SampleDesc.Quality = desc.sampleQuality;
d3d12Desc.Flags = static_cast<D3D12_RESOURCE_FLAGS>(desc.flags);
Format format = static_cast<Format>(desc.format);
if (format == Format::D24_UNorm_S8_UInt || format == Format::D32_Float ||
format == Format::D16_UNorm) {
if (isDepthFormat) {
d3d12Desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
} else if (d3d12Desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D && desc.width > 0 && desc.height > 0) {
d3d12Desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
}
d3d12Desc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN;
const bool isDepthFormat = format == Format::D24_UNorm_S8_UInt ||
format == Format::D32_Float ||
format == Format::D16_UNorm;
const D3D12_RESOURCE_STATES initialState =
isDepthFormat ? D3D12_RESOURCE_STATE_DEPTH_WRITE : D3D12_RESOURCE_STATE_COMMON;
if (texture->Initialize(m_device.Get(), d3d12Desc, initialState)) {
texture->SetFormat(format);
texture->SetTextureType(textureType);
if (isDepthFormat) {
texture->SetState(ResourceStates::DepthWrite);
}
@@ -700,8 +794,23 @@ RHIResourceView* D3D12Device::CreateRenderTargetView(RHITexture* texture, const
}
D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = {};
rtvDesc.Format = ToD3D12(static_cast<Format>(desc.format));
rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
const Format format = desc.format != 0 ? static_cast<Format>(desc.format) : texture->GetFormat();
const TextureType textureType = texture->GetTextureType();
rtvDesc.Format = ToD3D12(format);
rtvDesc.ViewDimension = ResolveRTVDimension(desc, textureType);
if (rtvDesc.ViewDimension == D3D12_RTV_DIMENSION_TEXTURE2DARRAY) {
rtvDesc.Texture2DArray.MipSlice = desc.mipLevel;
rtvDesc.Texture2DArray.FirstArraySlice = desc.firstArraySlice;
rtvDesc.Texture2DArray.ArraySize = desc.arraySize > 0 ? desc.arraySize : 1;
rtvDesc.Texture2DArray.PlaneSlice = desc.planeSlice;
} else if (rtvDesc.ViewDimension == D3D12_RTV_DIMENSION_TEXTURE3D) {
rtvDesc.Texture3D.MipSlice = desc.mipLevel;
rtvDesc.Texture3D.FirstWSlice = desc.firstArraySlice;
rtvDesc.Texture3D.WSize = desc.arraySize > 0 ? desc.arraySize : 1;
} else {
rtvDesc.Texture2D.MipSlice = desc.mipLevel;
rtvDesc.Texture2D.PlaneSlice = desc.planeSlice;
}
auto heap = std::make_unique<D3D12DescriptorHeap>();
if (!heap->Initialize(m_device.Get(), DescriptorHeapType::RTV, 1, false)) {

View File

@@ -5,6 +5,63 @@
namespace XCEngine {
namespace RHI {
namespace {
ResourceViewDimension ToResourceViewDimension(D3D12_RTV_DIMENSION dimension) {
switch (dimension) {
case D3D12_RTV_DIMENSION_TEXTURE1D: return ResourceViewDimension::Texture1D;
case D3D12_RTV_DIMENSION_TEXTURE1DARRAY: return ResourceViewDimension::Texture1DArray;
case D3D12_RTV_DIMENSION_TEXTURE2D: return ResourceViewDimension::Texture2D;
case D3D12_RTV_DIMENSION_TEXTURE2DARRAY: return ResourceViewDimension::Texture2DArray;
case D3D12_RTV_DIMENSION_TEXTURE2DMS: return ResourceViewDimension::Texture2DMS;
case D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY: return ResourceViewDimension::Texture2DMSArray;
case D3D12_RTV_DIMENSION_TEXTURE3D: return ResourceViewDimension::Texture3D;
default: return ResourceViewDimension::Unknown;
}
}
ResourceViewDimension ToResourceViewDimension(D3D12_DSV_DIMENSION dimension) {
switch (dimension) {
case D3D12_DSV_DIMENSION_TEXTURE1D: return ResourceViewDimension::Texture1D;
case D3D12_DSV_DIMENSION_TEXTURE1DARRAY: return ResourceViewDimension::Texture1DArray;
case D3D12_DSV_DIMENSION_TEXTURE2D: return ResourceViewDimension::Texture2D;
case D3D12_DSV_DIMENSION_TEXTURE2DARRAY: return ResourceViewDimension::Texture2DArray;
case D3D12_DSV_DIMENSION_TEXTURE2DMS: return ResourceViewDimension::Texture2DMS;
case D3D12_DSV_DIMENSION_TEXTURE2DMSARRAY: return ResourceViewDimension::Texture2DMSArray;
default: return ResourceViewDimension::Unknown;
}
}
ResourceViewDimension ToResourceViewDimension(D3D12_SRV_DIMENSION dimension) {
switch (dimension) {
case D3D12_SRV_DIMENSION_TEXTURE1D: return ResourceViewDimension::Texture1D;
case D3D12_SRV_DIMENSION_TEXTURE1DARRAY: return ResourceViewDimension::Texture1DArray;
case D3D12_SRV_DIMENSION_TEXTURE2D: return ResourceViewDimension::Texture2D;
case D3D12_SRV_DIMENSION_TEXTURE2DARRAY: return ResourceViewDimension::Texture2DArray;
case D3D12_SRV_DIMENSION_TEXTURE2DMS: return ResourceViewDimension::Texture2DMS;
case D3D12_SRV_DIMENSION_TEXTURE2DMSARRAY: return ResourceViewDimension::Texture2DMSArray;
case D3D12_SRV_DIMENSION_TEXTURE3D: return ResourceViewDimension::Texture3D;
case D3D12_SRV_DIMENSION_TEXTURECUBE: return ResourceViewDimension::TextureCube;
case D3D12_SRV_DIMENSION_TEXTURECUBEARRAY: return ResourceViewDimension::TextureCubeArray;
case D3D12_SRV_DIMENSION_BUFFER: return ResourceViewDimension::Buffer;
default: return ResourceViewDimension::Unknown;
}
}
ResourceViewDimension ToResourceViewDimension(D3D12_UAV_DIMENSION dimension) {
switch (dimension) {
case D3D12_UAV_DIMENSION_TEXTURE1D: return ResourceViewDimension::Texture1D;
case D3D12_UAV_DIMENSION_TEXTURE1DARRAY: return ResourceViewDimension::Texture1DArray;
case D3D12_UAV_DIMENSION_TEXTURE2D: return ResourceViewDimension::Texture2D;
case D3D12_UAV_DIMENSION_TEXTURE2DARRAY: return ResourceViewDimension::Texture2DArray;
case D3D12_UAV_DIMENSION_TEXTURE3D: return ResourceViewDimension::Texture3D;
case D3D12_UAV_DIMENSION_BUFFER: return ResourceViewDimension::Buffer;
default: return ResourceViewDimension::Unknown;
}
}
} // namespace
D3D12ResourceView::D3D12ResourceView()
: m_viewType(ResourceViewType::ShaderResource)
, m_format(Format::Unknown)
@@ -63,8 +120,8 @@ void D3D12ResourceView::InitializeAsRenderTarget(ID3D12Device* device, ID3D12Res
const D3D12_RENDER_TARGET_VIEW_DESC* desc,
D3D12DescriptorHeap* heap, uint32_t slotIndex) {
m_viewType = ResourceViewType::RenderTarget;
m_format = desc ? static_cast<Format>(desc->Format) : Format::Unknown;
m_dimension = ResourceViewDimension::Texture2D;
m_format = desc ? FromD3D12(desc->Format) : Format::Unknown;
m_dimension = desc ? ToResourceViewDimension(desc->ViewDimension) : ResourceViewDimension::Unknown;
m_resource = resource;
m_heap = heap;
m_slotIndex = slotIndex;
@@ -78,8 +135,8 @@ void D3D12ResourceView::InitializeAsDepthStencil(ID3D12Device* device, ID3D12Res
const D3D12_DEPTH_STENCIL_VIEW_DESC* desc,
D3D12DescriptorHeap* heap, uint32_t slotIndex) {
m_viewType = ResourceViewType::DepthStencil;
m_format = desc ? static_cast<Format>(desc->Format) : Format::Unknown;
m_dimension = ResourceViewDimension::Texture2D;
m_format = desc ? FromD3D12(desc->Format) : Format::Unknown;
m_dimension = desc ? ToResourceViewDimension(desc->ViewDimension) : ResourceViewDimension::Unknown;
m_resource = resource;
m_heap = heap;
m_slotIndex = slotIndex;
@@ -93,8 +150,8 @@ void D3D12ResourceView::InitializeAsShaderResource(ID3D12Device* device, ID3D12R
const D3D12_SHADER_RESOURCE_VIEW_DESC* desc,
D3D12DescriptorHeap* heap, uint32_t slotIndex) {
m_viewType = ResourceViewType::ShaderResource;
m_format = desc ? static_cast<Format>(desc->Format) : Format::Unknown;
m_dimension = desc ? static_cast<ResourceViewDimension>(desc->ViewDimension) : ResourceViewDimension::Unknown;
m_format = desc ? FromD3D12(desc->Format) : Format::Unknown;
m_dimension = desc ? ToResourceViewDimension(desc->ViewDimension) : ResourceViewDimension::Unknown;
m_resource = resource;
m_heap = heap;
m_slotIndex = slotIndex;
@@ -108,8 +165,8 @@ void D3D12ResourceView::InitializeAsUnorderedAccess(ID3D12Device* device, ID3D12
const D3D12_UNORDERED_ACCESS_VIEW_DESC* desc,
D3D12DescriptorHeap* heap, uint32_t slotIndex) {
m_viewType = ResourceViewType::UnorderedAccess;
m_format = desc ? static_cast<Format>(desc->Format) : Format::Unknown;
m_dimension = desc ? static_cast<ResourceViewDimension>(desc->ViewDimension) : ResourceViewDimension::Unknown;
m_format = desc ? FromD3D12(desc->Format) : Format::Unknown;
m_dimension = desc ? ToResourceViewDimension(desc->ViewDimension) : ResourceViewDimension::Unknown;
m_resource = resource;
m_heap = heap;
m_slotIndex = slotIndex;

View File

@@ -33,12 +33,41 @@ bool D3D12Texture::Initialize(ID3D12Device* device, const D3D12_RESOURCE_DESC& d
return false;
}
m_format = FromD3D12(desc.Format);
switch (desc.Dimension) {
case D3D12_RESOURCE_DIMENSION_TEXTURE1D:
m_textureType = TextureType::Texture1D;
break;
case D3D12_RESOURCE_DIMENSION_TEXTURE3D:
m_textureType = TextureType::Texture3D;
break;
case D3D12_RESOURCE_DIMENSION_TEXTURE2D:
default:
m_textureType = TextureType::Texture2D;
break;
}
m_ownsResource = true;
return true;
}
bool D3D12Texture::InitializeFromExisting(ID3D12Resource* resource, bool ownsResource) {
m_resource = resource;
if (m_resource) {
const D3D12_RESOURCE_DESC desc = m_resource->GetDesc();
m_format = FromD3D12(desc.Format);
switch (desc.Dimension) {
case D3D12_RESOURCE_DIMENSION_TEXTURE1D:
m_textureType = TextureType::Texture1D;
break;
case D3D12_RESOURCE_DIMENSION_TEXTURE3D:
m_textureType = TextureType::Texture3D;
break;
case D3D12_RESOURCE_DIMENSION_TEXTURE2D:
default:
m_textureType = TextureType::Texture2D;
break;
}
}
m_ownsResource = ownsResource;
return true;
}
@@ -76,6 +105,8 @@ bool D3D12Texture::InitializeFromData(ID3D12Device* device, ID3D12GraphicsComman
return false;
}
m_ownsResource = true;
m_format = FromD3D12(format);
m_textureType = TextureType::Texture2D;
textureDesc = m_resource->GetDesc();
UINT64 memorySizeUsed = 0;
@@ -184,13 +215,22 @@ bool D3D12Texture::InitializeDepthStencil(ID3D12Device* device, uint32_t width,
IID_PPV_ARGS(&m_resource)
);
return SUCCEEDED(hResult);
if (SUCCEEDED(hResult)) {
m_format = FromD3D12(format);
m_textureType = TextureType::Texture2D;
m_ownsResource = true;
return true;
}
return false;
}
void D3D12Texture::Shutdown() {
if (m_ownsResource) {
m_resource.Reset();
}
m_resource.Reset();
m_state = ResourceStates::Common;
m_format = Format::Unknown;
m_textureType = TextureType::Texture2D;
m_ownsResource = false;
}
} // namespace RHI

View File

@@ -24,6 +24,8 @@
#include "XCEngine/RHI/OpenGL/OpenGLDescriptorSet.h"
#include "XCEngine/RHI/OpenGL/OpenGLPipelineLayout.h"
#include "XCEngine/Debug/Logger.h"
#include <algorithm>
#include <cctype>
typedef const char* (WINAPI* PFNWGLGETEXTENSIONSSTRINGARBPROC)(HDC hdc);
typedef BOOL (WINAPI* PFNWGLCHOOSEPIXELFORMATARBPROC)(HDC hdc, const int* piAttribIList, const FLOAT* pfAttribFList, UINT nMaxFormats, int* piFormats, UINT* nNumFormats);
@@ -55,6 +57,76 @@ std::string NarrowAscii(const std::wstring& value) {
return result;
}
TextureType ResolveTextureType(uint32_t rawType) {
return static_cast<TextureType>(rawType);
}
int ResolveTextureDepth(const TextureDesc& desc, TextureType textureType) {
switch (textureType) {
case TextureType::Texture3D:
return static_cast<int>(desc.depth > 0 ? desc.depth : 1);
case TextureType::Texture2DArray:
return static_cast<int>(desc.arraySize > 0 ? desc.arraySize : 1);
case TextureType::TextureCube:
case TextureType::TextureCubeArray:
return static_cast<int>(desc.arraySize > 0 ? desc.arraySize : 6);
default:
return static_cast<int>(desc.depth > 0 ? desc.depth : 1);
}
}
std::string ToLowerAscii(std::string value) {
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char ch) {
return static_cast<char>(std::tolower(ch));
});
return value;
}
uint32_t DetectVendorId(const char* vendor) {
const std::string lowerVendor = ToLowerAscii(vendor != nullptr ? vendor : "");
if (lowerVendor.find("nvidia") != std::string::npos) {
return 0x10DE;
}
if (lowerVendor.find("amd") != std::string::npos || lowerVendor.find("ati") != std::string::npos) {
return 0x1002;
}
if (lowerVendor.find("intel") != std::string::npos) {
return 0x8086;
}
if (lowerVendor.find("microsoft") != std::string::npos) {
return 0x1414;
}
return 1;
}
uint32_t HashRendererId(const char* renderer) {
const char* value = renderer != nullptr ? renderer : "";
uint32_t hash = 2166136261u;
while (*value != '\0') {
hash ^= static_cast<uint8_t>(*value++);
hash *= 16777619u;
}
return hash == 0 ? 1u : hash;
}
bool IsSoftwareRenderer(const char* vendor, const char* renderer) {
const std::string lowerVendor = ToLowerAscii(vendor != nullptr ? vendor : "");
const std::string lowerRenderer = ToLowerAscii(renderer != nullptr ? renderer : "");
return lowerVendor.find("microsoft") != std::string::npos ||
lowerRenderer.find("gdi generic") != std::string::npos ||
lowerRenderer.find("software") != std::string::npos ||
lowerRenderer.find("llvmpipe") != std::string::npos;
}
uint64_t QuerySharedSystemMemoryBytes() {
MEMORYSTATUSEX status = {};
status.dwLength = sizeof(status);
if (GlobalMemoryStatusEx(&status)) {
return status.ullTotalPhys;
}
return 0;
}
bool HasShaderPayload(const ShaderCompileDesc& desc) {
return !desc.source.empty() || !desc.fileName.empty();
}
@@ -423,6 +495,11 @@ bool OpenGLDevice::CreateContextForCurrentWindow() {
m_deviceInfo.vendor = std::wstring(vendor ? vendor : "", vendor ? vendor + strlen(vendor) : nullptr);
m_deviceInfo.renderer = std::wstring(renderer ? renderer : "", renderer ? renderer + strlen(renderer) : nullptr);
m_deviceInfo.version = std::wstring(version ? version : "", version ? version + strlen(version) : nullptr);
m_deviceInfo.vendorId = DetectVendorId(vendor);
m_deviceInfo.deviceId = HashRendererId(renderer);
m_deviceInfo.isSoftware = IsSoftwareRenderer(vendor, renderer);
m_deviceInfo.sharedSystemMemory = QuerySharedSystemMemoryBytes();
m_deviceInfo.dedicatedVideoMemory = m_deviceInfo.isSoftware ? 0ull : 1ull;
GLint majorVersion = 0, minorVersion = 0;
glGetIntegerv(GL_MAJOR_VERSION, &majorVersion);
@@ -432,14 +509,19 @@ bool OpenGLDevice::CreateContextForCurrentWindow() {
m_capabilities.majorVersion = majorVersion;
m_capabilities.minorVersion = minorVersion;
m_capabilities.shaderModel = "GLSL " + std::to_string(majorVersion) + "." + std::to_string(minorVersion);
m_capabilities.bSupportsGeometryShaders = true;
m_capabilities.bSupportsComputeShaders = GLVersion.major >= 4 && GLVersion.minor >= 3;
m_capabilities.bSupportsTessellation = GLVersion.major >= 4 && GLVersion.minor >= 1;
m_capabilities.bSupportsComputeShaders = (majorVersion > 4) || (majorVersion == 4 && minorVersion >= 3);
m_capabilities.bSupportsTessellation = (majorVersion > 4) || (majorVersion == 4 && minorVersion >= 1);
m_capabilities.bSupportsExplicitMultiThreading = false;
GLint maxTexSize = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
m_capabilities.maxTexture2DSize = static_cast<uint32_t>(maxTexSize);
GLint maxTexture3DSize = 0;
glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &maxTexture3DSize);
m_capabilities.maxTexture3DSize = static_cast<uint32_t>(maxTexture3DSize);
GLint maxCubeSize = 0;
glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &maxCubeSize);
@@ -462,6 +544,10 @@ bool OpenGLDevice::CreateContextForCurrentWindow() {
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxAttribs);
m_capabilities.maxVertexAttribs = static_cast<uint32_t>(maxAttribs);
GLint maxConstantBufferSize = 0;
glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &maxConstantBufferSize);
m_capabilities.maxConstantBufferSize = static_cast<uint32_t>(maxConstantBufferSize);
GLint maxTextureUnits = 0;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxTextureUnits);
m_textureUnitAllocator->Initialize(static_cast<uint32_t>(maxTextureUnits));
@@ -544,26 +630,31 @@ RHITexture* OpenGLDevice::CreateTexture(const TextureDesc& desc) {
auto* texture = new OpenGLTexture();
OpenGLTextureType type = OpenGLTextureType::Texture2D;
switch (desc.textureType) {
case 0:
switch (ResolveTextureType(desc.textureType)) {
case TextureType::Texture1D:
type = OpenGLTextureType::Texture1D;
break;
case 2:
case TextureType::Texture2DArray:
type = OpenGLTextureType::Texture2DArray;
break;
case 3:
case TextureType::Texture3D:
type = OpenGLTextureType::Texture3D;
break;
case 4:
case TextureType::TextureCube:
type = OpenGLTextureType::TextureCube;
break;
case TextureType::TextureCubeArray:
type = OpenGLTextureType::TextureCubeArray;
break;
case TextureType::Texture2D:
default:
type = OpenGLTextureType::Texture2D;
break;
}
OpenGLFormat format = ToOpenGLTextureFormat(static_cast<Format>(desc.format));
texture->Initialize(type, desc.width, desc.height, desc.depth, desc.mipLevels, format, nullptr);
const int depth = ResolveTextureDepth(desc, ResolveTextureType(desc.textureType));
texture->Initialize(type, desc.width, desc.height, depth, desc.mipLevels, format, nullptr);
texture->SetFormat(static_cast<Format>(desc.format));
return texture;
}
@@ -579,27 +670,32 @@ RHITexture* OpenGLDevice::CreateTexture(const TextureDesc& desc, const void* ini
auto* texture = new OpenGLTexture();
OpenGLTextureType type = OpenGLTextureType::Texture2D;
switch (desc.textureType) {
case 0:
switch (ResolveTextureType(desc.textureType)) {
case TextureType::Texture1D:
type = OpenGLTextureType::Texture1D;
break;
case 2:
case TextureType::Texture2DArray:
type = OpenGLTextureType::Texture2DArray;
break;
case 3:
case TextureType::Texture3D:
type = OpenGLTextureType::Texture3D;
break;
case 4:
case TextureType::TextureCube:
type = OpenGLTextureType::TextureCube;
break;
case TextureType::TextureCubeArray:
type = OpenGLTextureType::TextureCubeArray;
break;
case TextureType::Texture2D:
default:
type = OpenGLTextureType::Texture2D;
break;
}
OpenGLFormat format = ToOpenGLTextureFormat(static_cast<Format>(desc.format));
const int depth = ResolveTextureDepth(desc, ResolveTextureType(desc.textureType));
if (!texture->Initialize(type, desc.width, desc.height, desc.depth, desc.mipLevels, format, initialData)) {
if (!texture->Initialize(type, desc.width, desc.height, depth, desc.mipLevels, format, initialData)) {
delete texture;
return nullptr;
}
@@ -941,7 +1037,7 @@ void* OpenGLDevice::GetNativeDevice() {
}
void* OpenGLDevice::GetNativeHandle() const {
return nullptr;
return m_hglrc;
}
} // namespace RHI

View File

@@ -42,8 +42,10 @@ bool OpenGLTexture::Initialize(OpenGLTextureType type, int width, int height, in
}
} else if (type == OpenGLTextureType::Texture1D) {
glTexImage1D(GL_TEXTURE_1D, 0, internalFormat, width, 0, glFormat, glType, data);
} else if (type == OpenGLTextureType::Texture3D) {
glTexImage3D(GL_TEXTURE_3D, 0, internalFormat, width, height, depth, 0, glFormat, glType, data);
} else if (type == OpenGLTextureType::Texture3D ||
type == OpenGLTextureType::Texture2DArray ||
type == OpenGLTextureType::TextureCubeArray) {
glTexImage3D(target, 0, internalFormat, width, height, depth, 0, glFormat, glType, data);
} else {
glTexImage2D(target, 0, internalFormat, width, height, 0, glFormat, glType, data);
}
@@ -54,8 +56,17 @@ bool OpenGLTexture::Initialize(OpenGLTextureType type, int width, int height, in
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_REPEAT);
const GLint wrapMode = (type == OpenGLTextureType::TextureCube || type == OpenGLTextureType::TextureCubeArray)
? GL_CLAMP_TO_EDGE
: GL_REPEAT;
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrapMode);
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrapMode);
if (type == OpenGLTextureType::Texture3D ||
type == OpenGLTextureType::Texture2DArray ||
type == OpenGLTextureType::TextureCube ||
type == OpenGLTextureType::TextureCubeArray) {
glTexParameteri(target, GL_TEXTURE_WRAP_R, wrapMode);
}
glBindTexture(target, 0);
return true;
@@ -177,7 +188,11 @@ void OpenGLTexture::SetWrapping(int wrapS, int wrapT, int wrapR) {
glBindTexture(target, m_texture);
glTexParameteri(target, GL_TEXTURE_WRAP_S, wrapS);
glTexParameteri(target, GL_TEXTURE_WRAP_T, wrapT);
if (wrapR >= 0 && (m_type == OpenGLTextureType::Texture3D || m_type == OpenGLTextureType::TextureCube)) {
if (wrapR >= 0 &&
(m_type == OpenGLTextureType::Texture3D ||
m_type == OpenGLTextureType::Texture2DArray ||
m_type == OpenGLTextureType::TextureCube ||
m_type == OpenGLTextureType::TextureCubeArray)) {
glTexParameteri(target, GL_TEXTURE_WRAP_R, wrapR);
}
glBindTexture(target, 0);

View File

@@ -1,5 +1,4 @@
#include "fmpch.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#include "stb_image_write.h"