Skip to content

Commit

Permalink
Windows support and version bump (0.4.1)
Browse files Browse the repository at this point in the history
TensorFlow support WIP. This should addresses
#7 and
#93
  • Loading branch information
BachiLi committed Mar 17, 2020
1 parent d183971 commit 12ca1a8
Show file tree
Hide file tree
Showing 31 changed files with 231 additions and 193 deletions.
21 changes: 12 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(redner VERSION 0.0.2 DESCRIPTION "Differentiable Ray Tracer")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

if(WIN32)
find_package(PythonLibs 3.6 COMPONENTS Development REQUIRED)
find_package(Python 3.6 COMPONENTS Development REQUIRED)
add_subdirectory(pybind11)
else()
find_package(Python 3.6 COMPONENTS Development REQUIRED)
Expand Down Expand Up @@ -82,7 +82,6 @@ set(SRCS src/aabb.h
src/primary_contribution.h
src/primary_intersection.h
src/ptr.h
src/py_utils.h
src/ray.h
src/rebuild_topology.h
src/redner.h
Expand All @@ -104,7 +103,6 @@ set(SRCS src/aabb.h
src/channels.cpp
src/edge.cpp
src/edge_tree.cpp
src/envmap.cpp
src/load_serialized.cpp
src/material.cpp
src/miniz.c
Expand All @@ -131,7 +129,6 @@ if(REDNER_CUDA)
src/channels.cpp
src/edge.cpp
src/edge_tree.cpp
src/envmap.cpp
src/material.cpp
src/parallel.cpp
src/path_contribution.cpp
Expand Down Expand Up @@ -191,9 +188,15 @@ string( REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}"
string( REPLACE "/DNDEBUG" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
string( REPLACE "-DNDEBUG" "" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")

find_package(TensorFlow)
if(TensorFlow_FOUND)
add_subdirectory(pyredner_tensorflow/custom_ops)
else()
message(INFO " Building without TensorFlow support (not found)")
if(MSVC)
target_compile_options(redner PRIVATE /W3 /wd4244 /wd4146 /wd4244 /wd4305 /wd4334 /wd4996)
endif()

if(NOT WIN32)
find_package(TensorFlow)
if(TensorFlow_FOUND)
add_subdirectory(pyredner_tensorflow/custom_ops)
else()
message(INFO " Building without TensorFlow support (not found)")
endif()
endif()
15 changes: 10 additions & 5 deletions cmake/FindTensorFlow.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ if("${retcode}" STREQUAL "0")
list(GET TF_INFORMATION_LIST 1 TF_DETECTED_ABI)
list(GET TF_INFORMATION_LIST 2 TF_DETECTED_INCLUDE_DIR)
list(GET TF_INFORMATION_LIST 3 TF_DETECTED_LIBRARY_DIR)
# For some reason my tensorflow doesn't have a .so file
list(APPEND CMAKE_FIND_LIBRARY_SUFFIXES .so.1)
list(APPEND CMAKE_FIND_LIBRARY_SUFFIXES .so.2)
find_library(TF_DETECTED_LIBRARY NAMES tensorflow_framework PATHS
${TF_DETECTED_LIBRARY_DIR})
if(WIN32)
find_library(TF_DETECTED_LIBRARY NAMES _pywrap_tensorflow_internal PATHS
${TF_DETECTED_LIBRARY_DIR}/python)
else()
# For some reason my tensorflow doesn't have a .so file
list(APPEND CMAKE_FIND_LIBRARY_SUFFIXES .so.1)
list(APPEND CMAKE_FIND_LIBRARY_SUFFIXES .so.2)
find_library(TF_DETECTED_LIBRARY NAMES tensorflow_framework PATHS
${TF_DETECTED_LIBRARY_DIR})
endif()
set(TensorFlow_VERSION ${TF_DETECTED_VERSION})
set(TensorFlow_ABI ${TF_DETECTED_ABI})
set(TensorFlow_INCLUDE_DIR ${TF_DETECTED_INCLUDE_DIR})
Expand Down
8 changes: 3 additions & 5 deletions pyredner_tensorflow/custom_ops/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(rednerTFCustomOp)

# TODO: windows support
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fPIC --shared")
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

include_directories(SYSTEM ${TensorFlow_INCLUDE_DIR})

# Compile two versions of the library
add_library(redner_tf_data_ptr_cxx11_abi MODULE data_ptr.cc)
add_library(redner_tf_data_ptr_cxx11_abi SHARED data_ptr.cc)
set_target_properties(redner_tf_data_ptr_cxx11_abi PROPERTIES COMPILE_FLAGS -D_GLIBCXX_USE_CXX11_ABI=1)
set_target_properties(redner_tf_data_ptr_cxx11_abi PROPERTIES LINK_FLAGS -D_GLIBCXX_USE_CXX11_ABI=1)
target_link_libraries(redner_tf_data_ptr_cxx11_abi ${TensorFlow_LIBRARY})

add_library(redner_tf_data_ptr_no_cxx11_abi MODULE data_ptr.cc)
add_library(redner_tf_data_ptr_no_cxx11_abi SHARED data_ptr.cc)
set_target_properties(redner_tf_data_ptr_no_cxx11_abi PROPERTIES COMPILE_FLAGS -D_GLIBCXX_USE_CXX11_ABI=0)
set_target_properties(redner_tf_data_ptr_no_cxx11_abi PROPERTIES LINK_FLAGS -D_GLIBCXX_USE_CXX11_ABI=0)
target_link_libraries(redner_tf_data_ptr_no_cxx11_abi ${TensorFlow_LIBRARY})
5 changes: 5 additions & 0 deletions pyredner_tensorflow/custom_ops/data_ptr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
==============================================================================*/

#pragma warning(disable : 4003 4061 4100 4127 4242 4244 4267 4355 4365 4388 4464 4514 4574 4623 4625 4626 4647 4668 4710 4820 4946 5026 5027 5031 5039)

// For windows
#define NOMINMAX

#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/shape_inference.h"
#include "tensorflow/core/framework/op_kernel.h"
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ def build_extension(self, ext):
import torch
if torch.cuda.is_available():
build_with_cuda = True
if tf_spec is not None:
if tf_spec is not None and sys.platform != 'win32':
packages.append('pyredner_tensorflow')
if not build_with_cuda:
import tensorflow as tf
if tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None):
build_with_cuda = True
if len(packages) == 0:
print('Error: PyTorch or Tensorflow must be installed.')
print('Error: PyTorch or Tensorflow must be installed. For Windows platform only PyTorch is supported.')
exit()
# Override build_with_cuda with environment variable
if 'REDNER_CUDA' in os.environ:
Expand Down Expand Up @@ -204,7 +204,7 @@ def build_extension(self, ext):
if 'PROJECT_NAME' in os.environ:
project_name = os.environ['PROJECT_NAME']
setup(name = project_name,
version = '0.3.15',
version = '0.4.1',
description = 'Differentiable rendering without approximation.',
long_description = """redner is a differentiable renderer that can take the
derivatives of rendering output with respect to arbitrary
Expand Down
4 changes: 2 additions & 2 deletions src/active_pixels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void init_active_pixels(const BufferView<Ray> &rays,
auto new_end = DISPATCH_CACHED(use_gpu, thrust_alloc, thrust::remove_if,
active_pixels.begin(), active_pixels.end(),
active_pixels.begin(), op);
active_pixels.count = new_end - active_pixels.begin();
active_pixels.count = int(new_end - active_pixels.begin());
}

struct is_valid_intersection {
Expand All @@ -45,7 +45,7 @@ void update_active_pixels(const BufferView<int> &active_pixels,
auto new_end = DISPATCH(use_gpu, thrust::copy_if,
active_pixels.begin(), active_pixels.end(),
new_active_pixels.begin(), op);
new_active_pixels.count = new_end - new_active_pixels.begin();
new_active_pixels.count = int(new_end - new_active_pixels.begin());
}

void test_active_pixels(bool use_gpu) {
Expand Down
2 changes: 1 addition & 1 deletion src/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ inline T0 atomic_add(T0 &target, T1 source) {
T0 new_val;
do {
old_val = target;
new_val = old_val + source;
new_val = old_val + (T0)source;
#if defined(USE_GCC_INTRINSICS)
} while (!__atomic_compare_exchange(&target, &old_val, &new_val, true,
std::memory_order::memory_order_seq_cst,
Expand Down
17 changes: 9 additions & 8 deletions src/automatic_uv_map.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "automatic_uv_map.h"

#include <ctime>
#include <cstdio>
#include <cstdarg>
Expand All @@ -13,7 +14,7 @@ class Stopwatch {
clock_t m_start;
};

static int Print(const char *format, ...) {
int Print(const char *format, ...) {
va_list arg;
va_start(arg, format);
printf("\r"); // Clear progress text (PrintProgress).
Expand All @@ -22,7 +23,7 @@ static int Print(const char *format, ...) {
return result;
}

static void PrintProgress(const char *name, const char *indent1, const char *indent2, int progress, Stopwatch *stopwatch) {
void PrintProgress(const char *name, const char *indent1, const char *indent2, int progress, Stopwatch *stopwatch) {
if (progress == 0)
stopwatch->reset();
printf("\r%s%s [", indent1, name);
Expand All @@ -34,7 +35,7 @@ static void PrintProgress(const char *name, const char *indent1, const char *ind
printf("\n%s%.2f seconds (%g ms) elapsed\n", indent2, stopwatch->elapsed() / 1000.0, stopwatch->elapsed());
}

static bool ProgressCallback(xatlas::ProgressCategory::Enum category, int progress, void *userData) {
bool ProgressCallback(xatlas::ProgressCategory::Enum category, int progress, void *userData) {
Stopwatch *stopwatch = (Stopwatch *)userData;
PrintProgress(xatlas::StringForEnum(category), " ", " ", progress, stopwatch);
return true;
Expand All @@ -50,17 +51,17 @@ std::vector<int> automatic_uv_map(const std::vector<UVTriMesh> &meshes, TextureA
for (int i = 0; i < (int)meshes.size(); i++) {
const UVTriMesh &mesh = meshes[i];
xatlas::MeshDecl meshDecl;
meshDecl.vertexCount = mesh.num_vertices;
meshDecl.vertexCount = (uint32_t)mesh.num_vertices;
meshDecl.vertexPositionData = mesh.vertices.get();
meshDecl.vertexPositionStride = sizeof(float) * 3;
if (mesh.uvs.get()) {
meshDecl.vertexUvData = mesh.uvs.get();
meshDecl.vertexUvStride = sizeof(float) * 2;
}
meshDecl.indexCount = mesh.num_triangles * 3;
meshDecl.indexCount = (uint32_t)mesh.num_triangles * 3;
meshDecl.indexData = mesh.indices.get();
meshDecl.indexFormat = xatlas::IndexFormat::UInt32;
xatlas::AddMeshError::Enum error = xatlas::AddMesh(atlas.atlas, meshDecl, meshes.size());
xatlas::AddMeshError::Enum error = xatlas::AddMesh(atlas.atlas, meshDecl, (uint32_t)meshes.size());
if (error != xatlas::AddMeshError::Success) {
char buf[256];
sprintf(buf, "\rError adding mesh %d: %s\n", i, xatlas::StringForEnum(error));
Expand All @@ -78,7 +79,7 @@ std::vector<int> automatic_uv_map(const std::vector<UVTriMesh> &meshes, TextureA
std::vector<int> num_uv_vertices(meshes.size());
for (uint32_t i = 0; i < atlas.atlas->meshCount; i++) {
const xatlas::Mesh &mesh = atlas.atlas->meshes[i];
num_uv_vertices[i] = mesh.vertexCount;
num_uv_vertices[i] = (int)mesh.vertexCount;
}
return num_uv_vertices;
}
Expand All @@ -94,7 +95,7 @@ void copy_texture_atlas(const TextureAtlas &atlas, std::vector<UVTriMesh> &meshe
}
int *ind_target = meshes[i].uv_indices.get();
for (uint32_t f = 0; f < mesh.indexCount; f++) {
ind_target[f] = mesh.indexArray[f];
ind_target[f] = (int)mesh.indexArray[f];
}
}
}
4 changes: 2 additions & 2 deletions src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ struct Buffer {
}
}

int size() const { return count; }
int bytes() const { return count * sizeof(T); }
int size() const { return (int)count; }
int bytes() const { return (int)count * sizeof(T); }

T* begin() {
return data;
Expand Down
24 changes: 12 additions & 12 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void test_d_sample_primary_rays() {
auto finite_delta = Real(1e-6);
for (int i = 0; i < 3; i++) {
auto delta_pos = pos;
delta_pos[i] += finite_delta;
delta_pos[i] += float(finite_delta);
auto delta_camera = Camera{
1, 1,
&delta_pos[0],
Expand All @@ -140,7 +140,7 @@ void test_d_sample_primary_rays() {
CameraType::Perspective};
auto positive_ray =
sample_primary(delta_camera, Vector2{0.5, 0.5});
delta_pos[i] -= 2 * finite_delta;
delta_pos[i] -= float(2 * finite_delta);
delta_camera = Camera{
1, 1,
&delta_pos[0],
Expand All @@ -161,7 +161,7 @@ void test_d_sample_primary_rays() {
}
for (int i = 0; i < 3; i++) {
auto delta_look = look;
delta_look[i] += finite_delta;
delta_look[i] += float(finite_delta);
auto delta_camera = Camera{
1, 1,
&pos[0],
Expand All @@ -175,7 +175,7 @@ void test_d_sample_primary_rays() {
CameraType::Perspective};
auto positive_ray =
sample_primary(delta_camera, Vector2{0.5, 0.5});
delta_look[i] -= 2 * finite_delta;
delta_look[i] -= float(2 * finite_delta);
delta_camera = Camera{
1, 1,
&pos[0],
Expand All @@ -196,7 +196,7 @@ void test_d_sample_primary_rays() {
}
for (int i = 0; i < 3; i++) {
auto delta_up = up;
delta_up[i] += finite_delta;
delta_up[i] += float(finite_delta);
auto delta_camera = Camera{
1, 1,
&pos[0],
Expand All @@ -210,7 +210,7 @@ void test_d_sample_primary_rays() {
CameraType::Perspective};
auto positive_ray =
sample_primary(delta_camera, Vector2{0.5, 0.5});
delta_up[i] -= 2 * finite_delta;
delta_up[i] -= float(2 * finite_delta);
delta_camera = Camera{
1, 1,
&pos[0],
Expand Down Expand Up @@ -283,7 +283,7 @@ void test_d_camera_to_screen() {
auto finite_delta = Real(1e-6);
for (int i = 0; i < 3; i++) {
auto delta_pos = pos;
delta_pos[i] += finite_delta;
delta_pos[i] += float(finite_delta);
auto delta_camera = Camera{
1, 1,
&delta_pos[0],
Expand All @@ -296,7 +296,7 @@ void test_d_camera_to_screen() {
1e-2f,
CameraType::Perspective};
auto pxy = camera_to_screen(delta_camera, pt);
delta_pos[i] -= 2 * finite_delta;
delta_pos[i] -= float(2 * finite_delta);
delta_camera = Camera{
1, 1,
&delta_pos[0],
Expand All @@ -314,7 +314,7 @@ void test_d_camera_to_screen() {
}
for (int i = 0; i < 3; i++) {
auto delta_look = look;
delta_look[i] += finite_delta;
delta_look[i] += float(finite_delta);
auto delta_camera = Camera{
1, 1,
&pos[0],
Expand All @@ -327,7 +327,7 @@ void test_d_camera_to_screen() {
1e-2f,
CameraType::Perspective};
auto pxy = camera_to_screen(delta_camera, pt);
delta_look[i] -= 2 * finite_delta;
delta_look[i] -= float(2 * finite_delta);
delta_camera = Camera{
1, 1,
&pos[0],
Expand All @@ -345,7 +345,7 @@ void test_d_camera_to_screen() {
}
for (int i = 0; i < 3; i++) {
auto delta_up = up;
delta_up[i] += finite_delta;
delta_up[i] += float(finite_delta);
auto delta_camera = Camera{
1, 1,
&pos[0],
Expand All @@ -358,7 +358,7 @@ void test_d_camera_to_screen() {
1e-2f,
CameraType::Perspective};
auto pxy = camera_to_screen(delta_camera, pt);
delta_up[i] -= 2 * finite_delta;
delta_up[i] -= float(2 * finite_delta);
delta_camera = Camera{
1, 1,
&pos[0],
Expand Down
6 changes: 3 additions & 3 deletions src/edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ EdgeSampler::EdgeSampler(const std::vector<const Shape*> &shapes,
// No need to collect edges
return;
}
auto shapes_buffer = scene.shapes.view(0, shapes.size());
auto shapes_buffer = scene.shapes.view(0, (int)shapes.size());
// Conservatively allocate a big buffer for all edges
auto num_total_triangles = 0;
for (int shape_id = 0; shape_id < (int)shapes.size(); shape_id++) {
Expand Down Expand Up @@ -288,7 +288,7 @@ EdgeSampler::EdgeSampler(const std::vector<const Shape*> &shapes,
}, num_edges, scene.use_gpu);

DISPATCH(scene.use_gpu, thrust::copy, edges_buffer_begin, new_end, edges_begin);
current_num_edges += num_edges;
current_num_edges += (int)num_edges;
}
// Remove edges with 180 degree dihedral angles
auto edges_end = DISPATCH(scene.use_gpu, thrust::remove_if, edges.begin(),
Expand Down Expand Up @@ -1421,7 +1421,7 @@ struct secondary_edge_sampler {
}
auto diffuse_pmf = diffuse_weight / weight_sum;
auto specular_pmf = specular_weight / weight_sum;
auto m_pmf = 0.f;
auto m_pmf = Real(0);
auto n = shading_point.shading_frame.n;
if (material.two_sided) {
if (dot(wi, n) < 0.f) {
Expand Down
3 changes: 0 additions & 3 deletions src/envmap.cpp

This file was deleted.

4 changes: 4 additions & 0 deletions src/envmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
#include "transform.h"
#include "buffer.h"

#pragma warning(push)
#pragma warning(disable : 4324 4668)
#include <thrust/execution_policy.h>
#include <thrust/binary_search.h>
#pragma warning(pop)

struct Scene;

struct EnvironmentMap {
Expand Down
Loading

0 comments on commit 12ca1a8

Please sign in to comment.