Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ LogNull g_null_log;
LogStdout g_stdout_log;
RENDERDOC_DevicePointer g_rdoc_device = {};

extern const std::pair<uint32_t, const char *> KnownGPUVendors[] = {
extern const std::pair<uint32_t, std::string_view> KnownGPUVendors[] = {
{0x1002, "AMD"}, {0x10DE, "NVIDIA"}, {0x8086, "INTEL"}, {0x13B5, "ARM"}};
extern const int KnownGPUVendorsCount = 4;
extern const int KnownGPUVendorsCount = std::size(KnownGPUVendors);
} // namespace Ray

Ray::RendererBase *Ray::CreateRenderer(const settings_t &s, ILog *log,
Expand Down Expand Up @@ -137,9 +137,9 @@ int Ray::QueryAvailableGPUDevices(ILog *log, gpu_device_t out_devices[], const i
return count;
}

bool Ray::MatchDeviceNames(const char *name, const char *pattern) {
std::regex match_name(pattern);
return std::regex_search(name, match_name) || strcmp(name, pattern) == 0;
bool Ray::MatchDeviceNames(std::string_view name, std::string_view pattern) {
std::regex match_name(pattern.data());
return std::regex_search(name.data(), match_name) || name == pattern;
}

const char *Ray::Version() { return "v0.4.0-unknown-commit"; }
Expand Down
2 changes: 1 addition & 1 deletion Ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ int QueryAvailableGPUDevices(ILog *log, gpu_device_t out_devices[], int capacity
@param pattern regex pattern
@return true if name matches pattern
*/
bool MatchDeviceNames(const char *name, const char *pattern);
bool MatchDeviceNames(std::string_view name, std::string_view pattern);

/** @brief Get version string
@return version string
Expand Down
22 changes: 11 additions & 11 deletions RendererBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <cstring>

namespace Ray {
const char *RendererTypeName(const eRendererType rt) {
std::string_view RendererTypeName(const eRendererType rt) {
switch (rt) {
case eRendererType::Reference:
return "REF";
Expand All @@ -28,24 +28,24 @@ const char *RendererTypeName(const eRendererType rt) {
}
}

eRendererType RendererTypeFromName(const char *name) {
if (strcmp(name, "REF") == 0) {
eRendererType RendererTypeFromName(std::string_view name) {
if (name == "REF") {
return eRendererType::Reference;
} else if (strcmp(name, "SSE2") == 0) {
} else if (name == "SSE2") {
return eRendererType::SIMD_SSE2;
} else if (strcmp(name, "SSE41") == 0) {
} else if (name == "SSE41") {
return eRendererType::SIMD_SSE41;
} else if (strcmp(name, "AVX") == 0) {
} else if (name == "AVX") {
return eRendererType::SIMD_AVX;
} else if (strcmp(name, "AVX2") == 0) {
} else if (name == "AVX2") {
return eRendererType::SIMD_AVX2;
} else if (strcmp(name, "AVX512") == 0) {
} else if (name == "AVX512") {
return eRendererType::SIMD_AVX512;
} else if (strcmp(name, "NEON") == 0) {
} else if (name == "NEON") {
return eRendererType::SIMD_NEON;
} else if (strcmp(name, "VK") == 0) {
} else if (name == "VK") {
return eRendererType::Vulkan;
} else if (strcmp(name, "DX") == 0) {
} else if (name == "DX") {
return eRendererType::DirectX12;
}
return eRendererType::Reference;
Expand Down
9 changes: 5 additions & 4 deletions RendererBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstring>
#include <memory>
#include <string_view>

#include "Config.h"
#include "SceneBase.h"
Expand Down Expand Up @@ -40,8 +41,8 @@ const Bitmask<eRendererType> RendererCPU = Bitmask<eRendererType>{eRendererType:
// All GPU renderers
const Bitmask<eRendererType> RendererGPU = Bitmask<eRendererType>{eRendererType::Vulkan} | eRendererType::DirectX12;

const char *RendererTypeName(eRendererType rt);
eRendererType RendererTypeFromName(const char *name);
std::string_view RendererTypeName(eRendererType rt);
eRendererType RendererTypeFromName(std::string_view name);

/// Returns whether it is safe to call Render function for non-overlapping regions from different threads
bool RendererSupportsMultithreading(eRendererType rt);
Expand All @@ -51,7 +52,7 @@ bool RendererSupportsHWRT(eRendererType rt);
/// Renderer settings
struct settings_t {
int w = 0, h = 0;
const char *preferred_device = nullptr;
std::string_view preferred_device;
bool use_tex_compression = true;
bool use_hwrt = true;
bool use_bindless = true;
Expand Down Expand Up @@ -141,7 +142,7 @@ class RendererBase {
virtual ILog *log() const = 0;

/// Name of the device
virtual const char *device_name() const = 0;
virtual std::string_view device_name() const = 0;

/// Tells whether this is a hardware accelerated renderer
virtual bool is_hwrt() const { return false; }
Expand Down
4 changes: 2 additions & 2 deletions SceneBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ struct vtx_attribute_t {

/// Mesh description
struct mesh_desc_t {
const char *name = nullptr; ///< Mesh name (for debugging)
std::string_view name; ///< Mesh name (for debugging)
ePrimType prim_type; ///< Primitive type
vtx_attribute_t vtx_positions; ///< Vertex positions
vtx_attribute_t vtx_normals; ///< Vertex normals
Expand Down Expand Up @@ -178,7 +178,7 @@ struct tex_desc_t {
eTextureFormat format; ///< Texture data format
eTextureConvention convention =
eTextureConvention::OGL; ///< Texture convention (affects normalmaps and BC-compressed textures)
const char *name = nullptr; ///< Debug name
std::string_view name; ///< Debug name
Span<const uint8_t> data; ///< Texture data
int w, ///< Texture width
h; ///< Texture height
Expand Down
6 changes: 4 additions & 2 deletions internal/CDFUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

namespace Ray {
// This mostly taken from Cycles source code
template <typename Func> inline std::vector<float> CDFEvaluate(const int res, const float from, const float to, Func func) {
template <typename Func>
inline std::vector<float> CDFEvaluate(const int res, const float from, const float to, Func func) {
const int cdf_count = res + 1;
const float range = to - from;

Expand All @@ -30,7 +31,8 @@ template <typename Func> inline std::vector<float> CDFEvaluate(const int res, co
std::vector<float> CDFInvert(int res, float from, float to, const std::vector<float> &cdf, bool make_symmetric);

template <typename Func>
inline std::vector<float> CDFInverted(const int res, const float from, const float to, Func func, const bool make_symmetric) {
inline std::vector<float> CDFInverted(const int res, const float from, const float to, Func func,
const bool make_symmetric) {
const std::vector<float> cdf = CDFEvaluate(res - 1, from, to, func);
return CDFInvert(res, from, to, cdf, make_symmetric);
}
Expand Down
2 changes: 1 addition & 1 deletion internal/Dx/BufferDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ eResState GetInitialDxResourceState(const eBufType type) {

int Ray::Dx::Buffer::g_GenCounter = 0;

Ray::Dx::Buffer::Buffer(const char *name, Context *ctx, const eBufType type, const uint32_t initial_size)
Ray::Dx::Buffer::Buffer(std::string_view name, Context *ctx, const eBufType type, const uint32_t initial_size)
: ctx_(ctx), name_(name), type_(type), size_(0) {
Resize(initial_size);
}
Expand Down
2 changes: 1 addition & 1 deletion internal/Dx/BufferDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Buffer {

public:
Buffer() = default;
explicit Buffer(const char *name, Context *ctx, eBufType type, uint32_t initial_size);
explicit Buffer(std::string_view name, Context *ctx, eBufType type, uint32_t initial_size);
Buffer(const Buffer &rhs) = delete;
Buffer(Buffer &&rhs) noexcept { (*this) = std::move(rhs); }
~Buffer();
Expand Down
48 changes: 33 additions & 15 deletions internal/Dx/ContextDX.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "ContextDX.h"

#include <codecvt>

#include "../../Log.h"
#include "../../Types.h"
#include "../ScopeExit.h"
Expand All @@ -23,9 +21,9 @@
#include <dxgi1_4.h>

namespace Ray {
bool MatchDeviceNames(const char *name, const char *pattern);
bool MatchDeviceNames(std::string_view name, std::string_view pattern);

extern const std::pair<uint32_t, const char *> KnownGPUVendors[];
extern const std::pair<uint32_t, std::string_view> KnownGPUVendors[];
extern const int KnownGPUVendorsCount;

extern RENDERDOC_DevicePointer g_rdoc_device;
Expand Down Expand Up @@ -109,7 +107,7 @@ void Ray::Dx::Context::Destroy() {
#undef SAFE_RELEASE
}

bool Ray::Dx::Context::Init(ILog *log, const char *preferred_device, const int validation_level) {
bool Ray::Dx::Context::Init(ILog *log, std::string_view preferred_device, const int validation_level) {
log_ = log;
validation_level_ = validation_level;

Expand All @@ -120,8 +118,6 @@ bool Ray::Dx::Context::Init(ILog *log, const char *preferred_device, const int v
}
}

std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> str_converter;

IDXGIFactory4 *dxgi_factory = nullptr;
HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(&dxgi_factory));
if (FAILED(hr)) {
Expand All @@ -141,7 +137,19 @@ bool Ray::Dx::Context::Init(ILog *log, const char *preferred_device, const int v
adapter_score += 1000;
}

if (preferred_device && MatchDeviceNames(str_converter.to_bytes(desc.Description).c_str(), preferred_device)) {
const int utf8_len = WideCharToMultiByte(CP_UTF8, // Code page
0, // Flags
desc.Description, // Wide char string to convert
-1, // Null-terminated input
nullptr, // Output buffer
0, // Output buffer size (0 means we want the size)
nullptr, nullptr);

std::string device_name(utf8_len, '\0');
WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, device_name.data(), utf8_len, nullptr, nullptr);
device_name.pop_back();

if (!preferred_device.empty() && MatchDeviceNames(device_name, preferred_device)) {
adapter_score += 100000;
}

Expand Down Expand Up @@ -232,15 +240,21 @@ bool Ray::Dx::Context::Init(ILog *log, const char *preferred_device, const int v
DXGI_ADAPTER_DESC1 desc;
best_adapter->GetDesc1(&desc);

device_name_ = str_converter.to_bytes(desc.Description);
{ // Get device name
const int utf8_len = WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, nullptr, 0, nullptr, nullptr);

device_name_.resize(utf8_len, '\0');
WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, device_name_.data(), utf8_len, nullptr, nullptr);
device_name_.pop_back();
}

log_->Info("============================================================================");
log_->Info("Device info:");

auto it = find_if(KnownGPUVendors, KnownGPUVendors + KnownGPUVendorsCount,
[&](std::pair<uint32_t, const char *> v) { return desc.VendorId == v.first; });
[&](std::pair<uint32_t, std::string_view> v) { return desc.VendorId == v.first; });
if (it != KnownGPUVendors + KnownGPUVendorsCount) {
log_->Info("\tVendor\t\t: %s", it->second);
log_->Info("\tVendor\t\t: %s", it->second.data());
}

log_->Info("\tName\t\t: %s", device_name_.c_str());
Expand Down Expand Up @@ -510,8 +524,6 @@ int Ray::Dx::Context::QueryAvailableDevices(ILog *log, gpu_device_t out_devices[
return 0;
}

std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> str_converter;

IDXGIAdapter1 *adapter = nullptr;
int adapter_index = 0;

Expand All @@ -525,8 +537,14 @@ int Ray::Dx::Context::QueryAvailableDevices(ILog *log, gpu_device_t out_devices[
continue;
}

strncpy_s(out_devices[count].name, sizeof(out_devices[count].name),
str_converter.to_bytes(desc.Description).c_str(), sizeof(out_devices[count].name) - 1);
const int utf8_len = WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, nullptr, 0, nullptr, nullptr);

std::string device_name(utf8_len, '\0');
WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, device_name.data(), utf8_len, nullptr, nullptr);
device_name.pop_back();

strncpy_s(out_devices[count].name, sizeof(out_devices[count].name), device_name.c_str(),
sizeof(out_devices[count].name) - 1);
++count;

++adapter_index;
Expand Down
6 changes: 3 additions & 3 deletions internal/Dx/ContextDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ class Context {
Context();
~Context();

bool Init(ILog *log, const char *preferred_device, int validation_level);
bool Init(ILog *log, std::string_view preferred_device, int validation_level);
void Destroy();

ID3D12Device *device() const { return device_; }
ID3D12Device5 *device5() const { return device5_; }
const std::string &device_name() const { return device_name_; }
std::string_view device_name() const { return device_name_; }

ILog *log() const { return log_; }
void *api() const { return nullptr; }
Expand Down Expand Up @@ -148,7 +148,7 @@ class Context {
bool frame_cpu_synced[MaxFramesInFlight] = {};

Buffer uniform_data_bufs[MaxFramesInFlight];
uint32_t uniform_data_buf_offs[MaxFramesInFlight];
uint32_t uniform_data_buf_offs[MaxFramesInFlight] = {};

// resources scheduled for deferred destruction
SmallVector<MemAllocation, 128> allocs_to_free[MaxFramesInFlight];
Expand Down
12 changes: 9 additions & 3 deletions internal/Dx/DebugMarkerDX.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "DebugMarkerDX.h"

#include <codecvt>
#include <cassert>
#include <string>

#ifndef NOMINMAX
Expand All @@ -17,13 +17,19 @@
#include <WinPixEventRuntime/pix3.h>
#endif

Ray::Dx::DebugMarker::DebugMarker(Context *, ID3D12GraphicsCommandList *_cmd_buf, const char *name)
Ray::Dx::DebugMarker::DebugMarker(Context *, ID3D12GraphicsCommandList *_cmd_buf, std::string_view name)
: cmd_buf_(_cmd_buf) {
#ifdef ENABLE_GPU_DEBUG
#ifdef ENABLE_PIX
PIXBeginEvent(cmd_buf_, 0, name);
#else
std::wstring wstr(name, name + strlen(name));
const int req_size = MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, nullptr, 0);
assert(req_size > 0);

std::wstring wstr(req_size, 0);
MultiByteToWideChar(CP_UTF8, 0, name.data(), -1, wstr.data(), req_size);
wstr.pop_back();

cmd_buf_->BeginEvent(0, wstr.c_str(), UINT(wstr.length() * sizeof(wchar_t)));
#endif
#endif
Expand Down
4 changes: 3 additions & 1 deletion internal/Dx/DebugMarkerDX.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#include <string_view>

struct ID3D12GraphicsCommandList;

namespace Ray::Dx {
class Context;
struct DebugMarker {
explicit DebugMarker(Context *ctx, ID3D12GraphicsCommandList *cmd_buf, const char *name);
explicit DebugMarker(Context *ctx, ID3D12GraphicsCommandList *cmd_buf, std::string_view name);
~DebugMarker();

ID3D12GraphicsCommandList *cmd_buf_ = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion internal/Dx/MemoryAllocatorDX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void Ray::Dx::MemAllocation::Release() {
}
}

Ray::Dx::MemAllocator::MemAllocator(const char *name, Context *ctx, const uint32_t initial_pool_size,
Ray::Dx::MemAllocator::MemAllocator(std::string_view name, Context *ctx, const uint32_t initial_pool_size,
D3D12_HEAP_TYPE heap_type, const float growth_factor, const uint32_t max_pool_size)
: name_(name), ctx_(ctx), growth_factor_(growth_factor), max_pool_size_(max_pool_size), heap_type_(heap_type) {
assert(growth_factor_ > 1.0f);
Expand Down
6 changes: 3 additions & 3 deletions internal/Dx/MemoryAllocatorDX.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class MemAllocator {
bool AllocateNewPool(uint32_t size);

public:
MemAllocator(const char *name, Context *ctx, uint32_t initial_pool_size, D3D12_HEAP_TYPE heap_type,
MemAllocator(std::string_view name, Context *ctx, uint32_t initial_pool_size, D3D12_HEAP_TYPE heap_type,
float growth_factor, uint32_t max_pool_size);
~MemAllocator();

Expand Down Expand Up @@ -95,7 +95,7 @@ class MemAllocators {
std::unique_ptr<MemAllocator> allocators_[8];

public:
MemAllocators(const char *name, Context *ctx, const uint32_t initial_pool_size, const float growth_factor,
MemAllocators(std::string_view name, Context *ctx, const uint32_t initial_pool_size, const float growth_factor,
const uint32_t max_pool_size)
: name_(name), ctx_(ctx), initial_pool_size_(initial_pool_size), growth_factor_(growth_factor),
max_pool_size_(max_pool_size) {}
Expand All @@ -104,7 +104,7 @@ class MemAllocators {
if (!allocators_[heap_type]) {
std::string name = name_;
name += " (type " + std::to_string(heap_type) + ")";
allocators_[heap_type] = std::make_unique<MemAllocator>(name.c_str(), ctx_, initial_pool_size_, heap_type,
allocators_[heap_type] = std::make_unique<MemAllocator>(name, ctx_, initial_pool_size_, heap_type,
growth_factor_, max_pool_size_);
}
return allocators_[heap_type]->Allocate(alignment, size);
Expand Down
Loading