Skip to content

Commit

Permalink
Make it compile with MSVC.
Browse files Browse the repository at this point in the history
  • Loading branch information
keuin committed Dec 24, 2022
1 parent da03ccd commit 0987464
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 28 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
cmake-build*
build

.code
.idea
.idea
.vs

CMakeSettings.json
19 changes: 14 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@ 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)

# 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

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions aa.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ class aa_viewport {
uint64_t diffuse_seed;
};

thread_pool<s_render_task, typeof(*this), typeof(images)> pool{thread_count, *this, images, samples};
thread_pool<s_render_task, decltype(*this), decltype(images)> 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<U, V> &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<U, V> &ctx, decltype(images) &images) {
basic_viewport<U, V> vp{
ctx.cxyz, ctx.screen_center,
ctx.image_width, ctx.image_height,
Expand All @@ -110,7 +110,7 @@ class aa_viewport {
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();
Expand Down
6 changes: 3 additions & 3 deletions bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> from_normalized(double r, double g, double b) {
return pixel<T>{.r = (T) (mod * r), .g = (T) (mod * g), .b = (T) (mod * b)};
return pixel<T>{(T) (mod * r), (T) (mod * g), (T) (mod * b)};
}

// v3d must be a normalized vector
Expand Down Expand Up @@ -120,12 +120,12 @@ template<
typename = typename std::enable_if<std::is_arithmetic<S>::value, S>::type
>
pixel<T> operator*(S scale, const pixel <T> &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<typename S, typename T>
pixel<T> operator*(const vec3<S> &scale, const pixel <T> &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
Expand Down
3 changes: 3 additions & 0 deletions main_final_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions main_simple_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
// Created by Keuin on 2022/4/11.
//

// include M_PI from cmath when using MSVC
#define _USE_MATH_DEFINES

#include <cstdint>
#include <iostream>
#include <vector>
#include <memory>
#include <cstdlib>
#include <string>
#include <cmath>

#include "vec.h"
#include "timer.h"
Expand Down
7 changes: 4 additions & 3 deletions threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
#include <mutex>
#include <atomic>
#include <iostream>
#include <functional>

// A simple once-usage thread pool and task queue.
// Using lock-free atomic counter to avoid expensive queue or synchronization mechanism.
// Tasks should be added into the queue before starting.
// Once the task queue is empty, threads quit.

template<typename T_Args, typename T_ImmuCtx, typename T_MutCtx>
using task_func_t = void (*)(size_t, T_Args &, const T_ImmuCtx &, T_MutCtx &);
using task_func_t = std::function<void(size_t, T_Args&, const T_ImmuCtx&, T_MutCtx&)>;

// internal usage
template<typename T, typename U, typename V>
Expand Down Expand Up @@ -61,7 +62,7 @@ class thread_pool {
template<typename T, typename U, typename V>
void thread_pool<T, U, V>::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<T, U, V>::worker_main, this});
}
} else {
Expand All @@ -83,7 +84,7 @@ void thread_pool<T, U, V>::worker_main() {
// Do not submit after starting.
template<typename T, typename U, typename V>
void thread_pool<T, U, V>::submit_task(task_func_t<T, U, V> f, T &&t) {
tasks.push_back(s_task<T, U, V>{.f=f, .arg=std::move(t)});
tasks.push_back(s_task<T, U, V>{f, std::move(t)});
}

template<typename T, typename U, typename V>
Expand Down
4 changes: 2 additions & 2 deletions timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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} {}
Expand Down
16 changes: 8 additions & 8 deletions vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -140,7 +140,7 @@ inline vec3<T> operator*(const vec3<T> &vec, const S &b) {
if (std::is_floating_point<S>::value) {
assert(std::isfinite(b));;
}
return vec3<T>{.x=(T) (vec.x * b), .y=(T) (vec.y * b), .z=(T) (vec.z * b)};
return vec3<T>{(T) (vec.x * b), (T) (vec.y * b), (T) (vec.z * b)};
}

// product vec3 by a scalar, with fp assertions
Expand All @@ -152,7 +152,7 @@ inline vec3<T> operator*(const S &b, const vec3<T> &vec) {
if (std::is_floating_point<S>::value) {
assert(std::isfinite(b));
}
return vec3<T>{.x=(T) (vec.x * b), .y=(T) (vec.y * b), .z=(T) (vec.z * b)};
return vec3<T>{(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
Expand All @@ -166,7 +166,7 @@ inline vec3<T> operator/(const vec3<T> &vec, const S &b) {
assert(std::isfinite(b));
assert(b != 0);
}
return vec3<T>{.x=(T) (vec.x / b), .y=(T) (vec.y / b), .z=(T) (vec.z / b)};
return vec3<T>{(T) (vec.x / b), (T) (vec.y / b), (T) (vec.z / b)};
}

// scalar product (inner product)
Expand Down Expand Up @@ -208,7 +208,7 @@ class rand_vec_gen {
inline vec3<T> range01() {
while (true) {
const auto x = uni(mt), y = uni(mt), z = uni(mt);
const auto vec = vec3<T>{.x=x, .y=y, .z=z};
const auto vec = vec3<T>{x, y, z};
if (vec.mod2() <= 1.0) return vec.unit_vec();
}
}
Expand Down

0 comments on commit 0987464

Please sign in to comment.