From 0987464cf99120b6728949496ee41bd14a35b225 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 24 Dec 2022 17:08:15 +0800 Subject: Make it compile with MSVC. --- .gitignore | 6 +++++- CMakeLists.txt | 19 ++++++++++++++----- README.md | 4 ++-- aa.h | 8 ++++---- bitmap.h | 6 +++--- main_final_render.cpp | 3 +++ main_simple_scanner.cpp | 5 +++++ threading.h | 7 ++++--- timer.h | 4 ++-- vec.h | 16 ++++++++-------- 10 files changed, 50 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 8a8a9e2..d7131fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ cmake-build* +build .code -.idea \ No newline at end of file +.idea +.vs + +CMakeSettings.json \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 22b29e8..332f10e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,8 +5,15 @@ set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) -set(common_compiler_args "-Wall -Werror -Wno-unused -std=c++11") -set(CMAKE_CXX_FLAGS_DEBUG "${common_compiler_args} -g -ggdb -fsanitize=address -DDEBUG") +set(common_compiler_args "-Wall") +if (!MSVC) + set(common_compiler_args "${common_compiler_args} -Werror") +endif () +set(common_compiler_args, "${common_compiler_args} -Wno-unused -std=c++11") + +if (!MSVC) + set(CMAKE_CXX_FLAGS_DEBUG "${common_compiler_args} -g -ggdb -fsanitize=address -DDEBUG") +endif () set(CMAKE_CXX_FLAGS_RELEASE "${common_compiler_args} -O2") set(CMAKE_VERBOSE_MAKEFILE on) @@ -14,10 +21,12 @@ set(CMAKE_VERBOSE_MAKEFILE on) # Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24: if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0") cmake_policy(SET CMP0135 NEW) -endif() +endif () -link_libraries(pthread) -link_libraries(atomic) +if (!MSVC) + link_libraries(pthread) + link_libraries(atomic) +endif () # main executable diff --git a/README.md b/README.md index e0e96de..2ae8234 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ ## Tested platforms -- Linux -- Win32 (Windows 10) +- Linux (GCC/Clang) +- Win32 (Windows 10/11, GCC(MinGW)/Clang(MinGW)/MSVC14) ## Ray-Trace logging diff --git a/aa.h b/aa.h index 2be5f2c..7484bfd 100644 --- a/aa.h +++ b/aa.h @@ -93,13 +93,13 @@ public: uint64_t diffuse_seed; }; - thread_pool pool{thread_count, *this, images, samples}; + thread_pool pool{thread_count, *this, images, samples}; timer tim{true}; std::cerr << "Seeding tasks..." << std::endl; tim.start_measure(); - for (typeof(samples) i = 0; i < samples; ++i) { - pool.submit_task([](size_t tid, s_render_task &task, const aa_viewport &ctx, typeof(images) &images) { + for (decltype(samples) i = 0; i < samples; ++i) { + pool.submit_task([](size_t tid, s_render_task &task, const aa_viewport &ctx, decltype(images) &images) { basic_viewport vp{ ctx.cxyz, ctx.screen_center, ctx.image_width, ctx.image_height, @@ -110,7 +110,7 @@ public: bokeh_ctx bokeh{task.diffuse_seed + 6543210987ULL}; images[tid] = vp.render(task.diffuse_seed, bc, bokeh); }, s_render_task{ - .bias_seed=seedgen(), .diffuse_seed=seedgen() + seedgen(), seedgen() }); } tim.stop_measure(); diff --git a/bitmap.h b/bitmap.h index a544b03..ea64c3c 100644 --- a/bitmap.h +++ b/bitmap.h @@ -47,7 +47,7 @@ struct pixel { * For example: Set color depth to 8bit, for normalized color (1, 0.5, 0.25), we get: (255, 127, 63). */ static inline pixel from_normalized(double r, double g, double b) { - return pixel{.r = (T) (mod * r), .g = (T) (mod * g), .b = (T) (mod * b)}; + return pixel{(T) (mod * r), (T) (mod * g), (T) (mod * b)}; } // v3d must be a normalized vector @@ -120,12 +120,12 @@ template< typename = typename std::enable_if::value, S>::type > pixel operator*(S scale, const pixel &pixel) { - return ::pixel < T > {.r=(T) (pixel.r * scale), .g=(T) (pixel.g * scale), .b=(T) (pixel.b * scale)}; + return ::pixel < T > {(T) (pixel.r * scale), (T) (pixel.g * scale), (T) (pixel.b * scale)}; } template pixel operator*(const vec3 &scale, const pixel &pixel) { - return ::pixel < T > {.r=(T) (pixel.r * scale.x), .g=(T) (pixel.g * scale.y), .b=(T) (pixel.b * scale.z)}; + return ::pixel < T > {(T) (pixel.r * scale.x), (T) (pixel.g * scale.y), (T) (pixel.b * scale.z)}; } // Mix two colors a and b. Returns a*u + b*v diff --git a/main_final_render.cpp b/main_final_render.cpp index 277ffd9..c2c44ca 100644 --- a/main_final_render.cpp +++ b/main_final_render.cpp @@ -2,6 +2,9 @@ // Created by Keuin on 2022/4/21. // +// include M_PI from cmath when using MSVC +#define _USE_MATH_DEFINES + #include "viewport.h" #include "hitlist.h" #include "aa.h" diff --git a/main_simple_scanner.cpp b/main_simple_scanner.cpp index 16a39a0..28e9ef3 100644 --- a/main_simple_scanner.cpp +++ b/main_simple_scanner.cpp @@ -2,11 +2,16 @@ // Created by Keuin on 2022/4/11. // +// include M_PI from cmath when using MSVC +#define _USE_MATH_DEFINES + #include #include #include #include #include +#include +#include #include "vec.h" #include "timer.h" diff --git a/threading.h b/threading.h index 249a115..3fbe0b5 100644 --- a/threading.h +++ b/threading.h @@ -12,6 +12,7 @@ #include #include #include +#include // A simple once-usage thread pool and task queue. // Using lock-free atomic counter to avoid expensive queue or synchronization mechanism. @@ -19,7 +20,7 @@ // Once the task queue is empty, threads quit. template -using task_func_t = void (*)(size_t, T_Args &, const T_ImmuCtx &, T_MutCtx &); +using task_func_t = std::function; // internal usage template @@ -61,7 +62,7 @@ public: template void thread_pool::start() { if (workers.empty()) { - for (typeof(thread_count) i = 0; i < thread_count; ++i) { + for (decltype(thread_count) i = 0; i < thread_count; ++i) { workers.emplace_back(std::thread{&thread_pool::worker_main, this}); } } else { @@ -83,7 +84,7 @@ void thread_pool::worker_main() { // Do not submit after starting. template void thread_pool::submit_task(task_func_t f, T &&t) { - tasks.push_back(s_task{.f=f, .arg=std::move(t)}); + tasks.push_back(s_task{f, std::move(t)}); } template diff --git a/timer.h b/timer.h index 68774c5..71a9e3b 100644 --- a/timer.h +++ b/timer.h @@ -10,8 +10,8 @@ class timer { private: - typeof(std::chrono::system_clock::now()) start_time; - typeof(std::chrono::system_clock::now()) end_time; + decltype(std::chrono::system_clock::now()) start_time; + decltype(std::chrono::system_clock::now()) end_time; bool silent; public: timer() : silent{false} {} diff --git a/vec.h b/vec.h index 99f5f46..a9b72c3 100644 --- a/vec.h +++ b/vec.h @@ -53,11 +53,11 @@ struct vec3 { } vec3 operator+(const vec3 &b) const { - return vec3{.x=x + b.x, .y=y + b.y, .z=z + b.z}; + return vec3{x + b.x, y + b.y, z + b.z}; } vec3 operator-() const { - return vec3{.x = -x, .y = -y, .z = -z}; + return vec3{-x, -y, -z}; } vec3 operator-(const vec3 &b) const { @@ -75,12 +75,12 @@ struct vec3 { // cross product (aka outer product, or vector product, producing a vector) vec3 cross(const vec3 &b) const { - return vec3{.x=y * b.z - z * b.y, .y=z * b.x - x * b.z, .z=x * b.y - y * b.x}; + return vec3{y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x}; } // Multiply with b on every dimension. vec3 scale(const vec3 &b) const { - return vec3{.x=x * b.x, .y=y * b.y, .z=z * b.z}; + return vec3{x * b.x, y * b.y, z * b.z}; } // norm value @@ -140,7 +140,7 @@ inline vec3 operator*(const vec3 &vec, const S &b) { if (std::is_floating_point::value) { assert(std::isfinite(b));; } - return vec3{.x=(T) (vec.x * b), .y=(T) (vec.y * b), .z=(T) (vec.z * b)}; + return vec3{(T) (vec.x * b), (T) (vec.y * b), (T) (vec.z * b)}; } // product vec3 by a scalar, with fp assertions @@ -152,7 +152,7 @@ inline vec3 operator*(const S &b, const vec3 &vec) { if (std::is_floating_point::value) { assert(std::isfinite(b)); } - return vec3{.x=(T) (vec.x * b), .y=(T) (vec.y * b), .z=(T) (vec.z * b)}; + return vec3{(T) (vec.x * b), (T) (vec.y * b), (T) (vec.z * b)}; } // product vec3 by the inversion of a scalar (div by a scalar), with fp assertions @@ -166,7 +166,7 @@ inline vec3 operator/(const vec3 &vec, const S &b) { assert(std::isfinite(b)); assert(b != 0); } - return vec3{.x=(T) (vec.x / b), .y=(T) (vec.y / b), .z=(T) (vec.z / b)}; + return vec3{(T) (vec.x / b), (T) (vec.y / b), (T) (vec.z / b)}; } // scalar product (inner product) @@ -208,7 +208,7 @@ public: inline vec3 range01() { while (true) { const auto x = uni(mt), y = uni(mt), z = uni(mt); - const auto vec = vec3{.x=x, .y=y, .z=z}; + const auto vec = vec3{x, y, z}; if (vec.mod2() <= 1.0) return vec.unit_vec(); } } -- cgit v1.2.3