Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbruyere committed Dec 28, 2017
0 parents commit 4e4b9e4
Show file tree
Hide file tree
Showing 32 changed files with 3,006 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
*.user
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vkh"]
path = vkh
url = https://github.com/jpbruyere/vkhelpers.git
100 changes: 100 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.9)

PROJECT(vkvg VERSION 0.1.0 DESCRIPTION "Vulkan Vector Graphic")

SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
SET(CMAKE_CXX_FLAGS "-W -Wall")
SET(CMAKE_EXE_LINKER_FLAGS "-lm")

IF(NOT CMAKE_BUILD_TYPE)
SET(CMAKE_BUILD_TYPE Release)
ENDIF()
MESSAGE(STATUS "${CMAKE_BUILD_TYPE} build.")

FIND_PACKAGE(Vulkan REQUIRED)
FIND_PACKAGE(GLFW3 REQUIRED)
FIND_PACKAGE(Freetype REQUIRED)
FIND_PACKAGE(FontConfig REQUIRED)

INCLUDE(FindPkgConfig)
INCLUDE(GNUInstallDirs)

PKG_CHECK_MODULES(PC_HARFBUZZ harfbuzz>=0.9.0)

FIND_PATH(HARFBUZZ_INCLUDE_DIRS NAMES hb.h
HINTS ${PC_HARFBUZZ_INCLUDE_DIRS} ${PC_HARFBUZZ_INCLUDEDIR}
)

FIND_LIBRARY(HARFBUZZ_LIBRARIES NAMES harfbuzz
HINTS ${PC_HARFBUZZ_LIBRARY_DIRS} ${PC_HARFBUZZ_LIBDIR}
)

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(HarfBuzz DEFAULT_MSG HARFBUZZ_INCLUDE_DIRS HARFBUZZ_LIBRARIES)


# Find glslc shader compiler.
# On Android, the NDK includes the binary, so no external dependency.
if(ANDROID)
file(GLOB glslc-folders ${ANDROID_NDK}/shader-tools/*)
else()
file(GLOB glslc-folders ${VULKAN_SDK}/bin)
endif()
FIND_PROGRAM(GLSLC glslc HINTS ${glslc-folders})
if(NOT GLSLC)
message(FATAL_ERROR "glslc compiler not found!")
endif()

SET(SHADER_DIR "shaders")
SET(SHADER_FILES ${SHADER_DIR}/*.frag ${SHADER_DIR}/*.vert ${SHADER_DIR}/*.geom ${SHADER_DIR}/*.comp)
FILE(GLOB_RECURSE SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${SHADER_FILES})
FOREACH(SHADER ${SHADERS})
SET(shader-input ${CMAKE_CURRENT_SOURCE_DIR}/${SHADER})
SET(shader-output ${CMAKE_CURRENT_BINARY_DIR}/${SHADER}.spv)
ADD_CUSTOM_COMMAND (
OUTPUT ${shader-output}
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/${SHADER_DIR}"
COMMAND ${GLSLC} ${shader-input} -o ${shader-output}
COMMENT "Compiling ${shader-input}"
DEPENDS ${SHADER}
VERBATIM
)
SET(SHADER_OUTPUTS ${SHADER_OUTPUTS} ${shader-output})
ENDFOREACH()
ADD_CUSTOM_TARGET(CompileShaders ALL DEPENDS ${SHADER_OUTPUTS})

#add_definitions( -DDEBUG_VK_PERF=true )

FILE(GLOB VKVG_SRC src/*.c vkh/*.c)

ADD_LIBRARY(${PROJECT_NAME} SHARED ${VKVG_SRC} ${SHADERS})

SET_TARGET_PROPERTIES(vkvg PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION 1
PUBLIC_HEADER include/vkvg.h
)

TARGET_INCLUDE_DIRECTORIES(vkvg PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/vkh
${CMAKE_CURRENT_SOURCE_DIR}/src
${FREETYPE_INCLUDE_DIRS}
${HARFBUZZ_INCLUDE_DIRS}
${FONTCONFIG_INCLUDE_DIR}
)

TARGET_LINK_LIBRARIES(${PROJECT_NAME}
${Vulkan_LIBRARY}
${GLFW3_LIBRARY}
${FREETYPE_LIBRARY}
${HARFBUZZ_LIBRARIES}
${FONTCONFIG_LIBRARIES}
)

CONFIGURE_FILE(vkvg.pc.in vkvg.pc @ONLY)
INSTALL(FILES ${CMAKE_BINARY_DIR}/vkvg.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
INSTALL(TARGETS vkvg
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# vkvg
35 changes: 35 additions & 0 deletions cmake/FindFontConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# - Find FontConfig library
# Find the FontConfig includes and library
# This module defines
# FONTCONFIG_INCLUDE_DIR, where to find fontconfig.h
# FONTCONFIG_LIBRARIES, libraries to link against to use the FontConfig API.
# FONTCONFIG_FOUND, If false, do not try to use FontConfig.

#=============================================================================
# Copyright 2012 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of VTK, substitute the full
# License text for the above reference.)

find_path(FONTCONFIG_INCLUDE_DIR fontconfig/fontconfig.h)

find_library(FONTCONFIG_LIBRARY NAMES fontconfig)

# handle the QUIETLY and REQUIRED arguments and set FONTCONFIG_FOUND to TRUE if
# all listed variables are TRUE
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(FontConfig DEFAULT_MSG
FONTCONFIG_LIBRARY FONTCONFIG_INCLUDE_DIR)

if(FONTCONFIG_FOUND)
set( FONTCONFIG_LIBRARIES ${FONTCONFIG_LIBRARY} )
endif()

mark_as_advanced(FONTCONFIG_INCLUDE_DIR FONTCONFIG_LIBRARY FONTCONFIG_LIBRARIES)
49 changes: 49 additions & 0 deletions cmake/FindGLFW3.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Locate the glfw3 library
#
# This module defines the following variables:
#
# GLFW3_LIBRARY the name of the library;
# GLFW3_INCLUDE_DIR where to find glfw include files.
# GLFW3_FOUND true if both the GLFW3_LIBRARY and GLFW3_INCLUDE_DIR have been found.
#
# To help locate the library and include file, you can define a
# variable called GLFW3_ROOT which points to the root of the glfw library
# installation.
#
# default search dirs
#
# Cmake file from: https://github.com/daw42/glslcookbook

set( _glfw3_HEADER_SEARCH_DIRS
"/usr/include"
"/usr/local/include"
"${CMAKE_SOURCE_DIR}/includes"
"C:/Program Files (x86)/glfw/include" )
set( _glfw3_LIB_SEARCH_DIRS
"/usr/lib"
"/usr/local/lib"
"${CMAKE_SOURCE_DIR}/lib"
"C:/Program Files (x86)/glfw/lib-msvc110" )

# Check environment for root search directory
set( _glfw3_ENV_ROOT $ENV{GLFW3_ROOT} )
if( NOT GLFW3_ROOT AND _glfw3_ENV_ROOT )
set(GLFW3_ROOT ${_glfw3_ENV_ROOT} )
endif()

# Put user specified location at beginning of search
if( GLFW3_ROOT )
list( INSERT _glfw3_HEADER_SEARCH_DIRS 0 "${GLFW3_ROOT}/include" )
list( INSERT _glfw3_LIB_SEARCH_DIRS 0 "${GLFW3_ROOT}/lib" )
endif()

# Search for the header
FIND_PATH(GLFW3_INCLUDE_DIR "GLFW/glfw3.h"
PATHS ${_glfw3_HEADER_SEARCH_DIRS} )

# Search for the library
FIND_LIBRARY(GLFW3_LIBRARY NAMES glfw3 glfw
PATHS ${_glfw3_LIB_SEARCH_DIRS} )
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(GLFW3 DEFAULT_MSG
GLFW3_LIBRARY GLFW3_INCLUDE_DIR)
95 changes: 95 additions & 0 deletions include/vkvg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifndef VKVG_H
#define VKVG_H

#include <vulkan/vulkan.h>
#include "vectors.h"
#include "math.h"

#define VKVG_SAMPLES VK_SAMPLE_COUNT_8_BIT

typedef enum VkvgDirection {
VKVG_HORIZONTAL = 0,
VKVG_VERTICAL = 1
}VkvgDirection;

typedef struct _vkvg_context_t* VkvgContext;
typedef struct _vkvg_surface_t* VkvgSurface;
typedef struct _vkvg_device_t* VkvgDevice;
typedef struct _vkvg_pattern_t* VkvgPattern;

VkvgDevice vkvg_device_create (VkDevice vkdev, VkQueue queue, uint32_t qFam,
VkPhysicalDeviceMemoryProperties memprops);
void vkvg_device_destroy (VkvgDevice dev);

VkvgSurface vkvg_surface_create (VkvgDevice dev, int32_t width, uint32_t height);
void vkvg_surface_destroy (VkvgSurface surf);
VkImage vkvg_surface_get_vk_image (VkvgSurface surf);
VkImage vkvg_surface_get_vkh_image (VkvgSurface surf);

//mimic from cairo, to facilitate usage of vkvg as cairo vulkan backend
typedef enum _vkvg_operator {
VKVG_OPERATOR_CLEAR,

VKVG_OPERATOR_SOURCE,
VKVG_OPERATOR_OVER,
VKVG_OPERATOR_IN,
VKVG_OPERATOR_OUT,
VKVG_OPERATOR_ATOP,

VKVG_OPERATOR_DEST,
VKVG_OPERATOR_DEST_OVER,
VKVG_OPERATOR_DEST_IN,
VKVG_OPERATOR_DEST_OUT,
VKVG_OPERATOR_DEST_ATOP,

VKVG_OPERATOR_XOR,
VKVG_OPERATOR_ADD,
VKVG_OPERATOR_SATURATE,

VKVG_OPERATOR_MULTIPLY,
VKVG_OPERATOR_SCREEN,
VKVG_OPERATOR_OVERLAY,
VKVG_OPERATOR_DARKEN,
VKVG_OPERATOR_LIGHTEN,
VKVG_OPERATOR_COLOR_DODGE,
VKVG_OPERATOR_COLOR_BURN,
VKVG_OPERATOR_HARD_LIGHT,
VKVG_OPERATOR_SOFT_LIGHT,
VKVG_OPERATOR_DIFFERENCE,
VKVG_OPERATOR_EXCLUSION,
VKVG_OPERATOR_HSL_HUE,
VKVG_OPERATOR_HSL_SATURATION,
VKVG_OPERATOR_HSL_COLOR,
VKVG_OPERATOR_HSL_LUMINOSITY
} vkvg_operator_t;

/*Context*/
VkvgContext vkvg_create (VkvgSurface surf);
void vkvg_destroy (VkvgContext ctx);

void vkvg_flush (VkvgContext ctx);

void vkvg_close_path (VkvgContext ctx);
void vkvg_line_to (VkvgContext ctx, float x, float y);
void vkvg_move_to (VkvgContext ctx, float x, float y);
void vkvg_arc (VkvgContext ctx, float xc, float yc, float radius, float a1, float a2);
void vkvg_stroke (VkvgContext ctx);
void vkvg_stroke_preserve (VkvgContext ctx);
void vkvg_fill (VkvgContext ctx);
void vkvg_fill_preserve (VkvgContext ctx);
void vkvg_paint (VkvgContext ctx);
void vkvg_reset_clip (VkvgContext ctx);
void vkvg_clip (VkvgContext ctx);
void vkvg_clip_preserve (VkvgContext ctx);
void vkvg_set_rgba (VkvgContext ctx, float r, float g, float b, float a);
void vkvg_set_linewidth (VkvgContext ctx, float width);
void vkvg_set_source_surface(VkvgContext ctx, VkvgSurface surf, float x, float y);

void vkvg_select_font_face (VkvgContext ctx, const char* name);
void vkvg_set_font_size (VkvgContext ctx, uint32_t size);
void vkvg_show_text (VkvgContext ctx, const char* text);

void vkvg_save (VkvgContext ctx);
void vkvg_restore (VkvgContext ctx);

#endif
103 changes: 103 additions & 0 deletions shaders/deferred.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#version 450

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (binding = 1) uniform sampler2DMS samplerPosition;
layout (binding = 2) uniform sampler2DMS samplerNormal;
layout (binding = 3) uniform sampler2DMS samplerAlbedo;

layout (location = 0) in vec2 inUV;

layout (location = 0) out vec4 outFragcolor;

struct Light {
vec4 position;
vec3 color;
float radius;
};

layout (binding = 4) uniform UBO
{
Light lights[6];
vec4 viewPos;
ivec2 windowSize;
} ubo;

layout (constant_id = 0) const int NUM_SAMPLES = 8;

#define NUM_LIGHTS 6

// Manual resolve for MSAA samples
vec4 resolve(sampler2DMS tex, ivec2 uv)
{
vec4 result = vec4(0.0);
for (int i = 0; i < NUM_SAMPLES; i++)
{
vec4 val = texelFetch(tex, uv, i);
result += val;
}
// Average resolved samples
return result / float(NUM_SAMPLES);
}

vec3 calculateLighting(vec3 pos, vec3 normal, vec4 albedo)
{
vec3 result = vec3(0.0);

for(int i = 0; i < NUM_LIGHTS; ++i)
{
// Vector to light
vec3 L = ubo.lights[i].position.xyz - pos;
// Distance from light to fragment position
float dist = length(L);

// Viewer to fragment
vec3 V = ubo.viewPos.xyz - pos;
V = normalize(V);

// Light to fragment
L = normalize(L);

// Attenuation
float atten = ubo.lights[i].radius / (pow(dist, 2.0) + 1.0);

// Diffuse part
vec3 N = normalize(normal);
float NdotL = max(0.0, dot(N, L));
vec3 diff = ubo.lights[i].color * albedo.rgb * NdotL * atten;

// Specular part
vec3 R = reflect(-L, N);
float NdotR = max(0.0, dot(R, V));
vec3 spec = ubo.lights[i].color * albedo.a * pow(NdotR, 8.0) * atten;

result += diff + spec;
}
return result;
}

void main()
{
ivec2 attDim = textureSize(samplerPosition);
ivec2 UV = ivec2(inUV * attDim);

#define ambient 0.15

// Ambient part
vec4 alb = resolve(samplerAlbedo, UV);
vec3 fragColor = vec3(0.0);

// Calualte lighting for every MSAA sample
for (int i = 0; i < NUM_SAMPLES; i++)
{
vec3 pos = texelFetch(samplerPosition, UV, i).rgb;
vec3 normal = texelFetch(samplerNormal, UV, i).rgb;
vec4 albedo = texelFetch(samplerAlbedo, UV, i);
fragColor += calculateLighting(pos, normal, albedo);
}

fragColor = (alb.rgb * ambient) + fragColor / float(NUM_SAMPLES);

outFragcolor = vec4(fragColor, 1.0);
}
Loading

0 comments on commit 4e4b9e4

Please sign in to comment.