Skip to content

Commit

Permalink
Vulkan
Browse files Browse the repository at this point in the history
* add vulkan support
* add tooltip setting
* fix ReaderBox crash
* fix picture load abort memory leak
  • Loading branch information
karwler committed Feb 4, 2023
1 parent 090fb62 commit 348331c
Show file tree
Hide file tree
Showing 53 changed files with 78,481 additions and 2,156 deletions.
49 changes: 40 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ option(DOWNLOADER "Build with downloader. (currently mostly broken)" OFF)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
option(NATIVE "Build for the current CPU." OFF)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
if(WIN32)
option(DIRECTX "Build with DirectX 11 support." ON)
elseif(UNIX)
option(OPENGLES "Build for OpenGL ES 3.0 instead of OpenGL 3.0." OFF)
option(APPIMAGE "Package as an AppImage." OFF)
endif()
option(OPENGL "Build with OpenGL 3.0 support." ON)
option(VULKAN "Build with Vulkan 1.0 support." ON)

string(TOLOWER ${PROJECT_NAME} PROJECT_NAME_LOWER)

Expand All @@ -30,12 +32,34 @@ set(SRC_FILES
"src/engine/inputSys.h"
"src/engine/renderer.cpp"
"src/engine/renderer.h"
"src/engine/rendererDx.cpp"
"src/engine/rendererDx.h"
"src/engine/rendererGl.cpp"
"src/engine/rendererGl.h"
"src/engine/rendererVk.cpp"
"src/engine/rendererVk.h"
"src/engine/scene.cpp"
"src/engine/scene.h"
"src/engine/windowSys.cpp"
"src/engine/windowSys.h"
"src/engine/world.cpp"
"src/engine/world.h"
"src/engine/shaders/dx.gui.pixl.dbg.h"
"src/engine/shaders/dx.gui.pixl.rel.h"
"src/engine/shaders/dx.gui.vert.dbg.h"
"src/engine/shaders/dx.gui.vert.rel.h"
"src/engine/shaders/dx.sel.pixl.dbg.h"
"src/engine/shaders/dx.sel.pixl.rel.h"
"src/engine/shaders/dx.sel.vert.dbg.h"
"src/engine/shaders/dx.sel.vert.rel.h"
"src/engine/shaders/vk.gui.frag.dbg.h"
"src/engine/shaders/vk.gui.frag.rel.h"
"src/engine/shaders/vk.gui.vert.dbg.h"
"src/engine/shaders/vk.gui.vert.rel.h"
"src/engine/shaders/vk.sel.frag.dbg.h"
"src/engine/shaders/vk.sel.frag.rel.h"
"src/engine/shaders/vk.sel.vert.dbg.h"
"src/engine/shaders/vk.sel.vert.rel.h"
"src/prog/browser.cpp"
"src/prog/browser.h"
"src/prog/downloader.cpp"
Expand All @@ -54,13 +78,13 @@ set(SRC_FILES
"src/utils/widgets.cpp"
"src/utils/widgets.h")

if(CMAKE_SYSTEM_NAME MATCHES "Windows")
if(WIN32)
list(APPEND SRC_FILES "rsc/resource.rc")
endif()

# dependencies
set(VER_ARC "3.6.1")
set(VER_SDL "2.26.1")
set(VER_ARC "3.6.2")
set(VER_SDL "2.26.2")
set(VER_IMG "2.6.2")
set(VER_TTF "2.20.1")
set(VER_GLM "0.9.9.8")
Expand Down Expand Up @@ -125,11 +149,16 @@ function(compileLib URL LIBDIR CMDV WDIR ORIG_LIBDIR)
endif()
endfunction()

if(VULKAN)
find_package(Vulkan REQUIRED)
string(REPLACE "/Include" "" VULKAN_PATH "${Vulkan_INCLUDE_DIRS}")
endif()
file(MAKE_DIRECTORY "${DIR_LIB}")
downloadLib("https://github.com/g-truc/glm/releases/download/${VER_GLM}/glm-${VER_GLM}.zip" "${DIR_LIB}/glm" "")
include_directories("${CMAKE_SOURCE_DIR}/src" "${DIR_LIB}/glm")
include_directories("${CMAKE_SOURCE_DIR}/src" "${DIR_LIB}/glm" "$<$<BOOL:${VULKAN}>:${Vulkan_INCLUDE_DIRS}>")
link_directories("$<$<BOOL:${VULKAN}>:${VULKAN_PATH}/Lib>")

if(CMAKE_SYSTEM_NAME MATCHES "Windows")
if(WIN32)
set(PBOUT_DIR "${CMAKE_BINARY_DIR}/${PROJECT_NAME}")
set(TBIN_DIR "${PBOUT_DIR}")
set(DATA_DIR "${PBOUT_DIR}")
Expand Down Expand Up @@ -237,7 +266,8 @@ endif()
# compiler flags
add_compile_definitions($<$<BOOL:${DOWNLOADER}>:DOWNLOADER>
$<$<BOOL:${DIRECTX}>:WITH_DIRECTX>
$<$<BOOL:${OPENGLES}>:OPENGLES>
"$<$<BOOL:${OPENGL}>:WITH_OPENGL;$<$<BOOL:${OPENGLES}>:OPENGLES>>"
$<$<BOOL:${VULKAN}>:WITH_VULKAN>
"$<$<BOOL:${WIN32}>:UNICODE;_UNICODE;_CRT_SECURE_NO_WARNINGS;NOMINMAX;$<$<NOT:$<BOOL:${MSVC}>>:_WIN32_WINNT=0x600>>")

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Expand All @@ -262,8 +292,9 @@ endif()
add_executable(${PROJECT_NAME} WIN32 ${SRC_FILES})
target_link_libraries(${PROJECT_NAME} SDL2 SDL2_image SDL2_ttf archive
"$<$<BOOL:${UNIX}>:pthread;dl>"
"$<IF:$<BOOL:${WIN32}>,opengl32,$<IF:$<BOOL:${OPENGLES}>,GLESv2,GL>>"
"$<$<BOOL:${DIRECTX}>:d3d11.lib;d3dcompiler.lib;dxgi.lib>"
"$<$<BOOL:${DIRECTX}>:d3d11.lib;dxgi.lib>"
"$<$<BOOL:${OPENGL}>:$<IF:$<BOOL:${WIN32}>,opengl32,$<IF:$<BOOL:${OPENGLES}>,GLESv2,GL>>>"
"$<$<BOOL:${VULKAN}>:$<IF:$<BOOL:${WIN32}>,vulkan-1,vulkan>>"
"$<$<BOOL:${DOWNLOADER}>:$<IF:$<BOOL:${WIN32}>,libcurl;libxml2,curl;xml2>>")

set_target_properties(${PROJECT_NAME} PROPERTIES
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2022 karwler <[email protected]>
Copyright (C) 2023 karwler <[email protected]>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
Expand Down
33 changes: 25 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,39 @@ A simple comic/manga reader for Linux and Windows.
It's basically just an image viewer that shows all pictures of a directory/archive.
Currently supported file formats are whatever SDL2_image and libarchive support.

Used libraries are SDL2, SDL2_image, SDL2_ttf, libarchive and by extension FreeType, HarfBuzz, libtiff, libwebp, stb_image and zlib and the included default font is BrisaSans.
The CMakeLists.txt is written for at least CMake 3.12.4 with Clang, GCC or MSVC which need to support C++17. You also need some version of Python 3 with Wand which requires ImageMagick to be installed.
You can create a Makefile for a debug build by running CMake with the "-DCMAKE_BUILD_TYPE=Debug" option. Otherwise it'll default to a release build.
## Build
Used libraries are SDL2, SDL2_image, SDL2_ttf, libarchive, glm and by extension FreeType, HarfBuzz, libtiff, libwebp, stb_image and zlib. The included default font is BrisaSans.
The CMakeLists.txt is written for at least CMake 3.12.4 with Clang, GCC or MSVC which need to support C++17.
You can generate project files for a debug build by running CMake with the "-DCMAKE_BUILD_TYPE=Debug" option. Otherwise it'll default to a release build.
By default the Program uses OpenGL 3.0, which can be switched to OpenGL ES 3.0 with "-DOPENGLES=1" or entirely disabled with "-DOPENGL=0".
Support for DirectX 11 and Vulkan 1.0 can be enabled by setting the options "-DDIRECTX=1" and "-DVULKAN=1".

## Linux
All dependencies need to be installed manually. Installing the development packages for libsdl2 libsdl2-image libsdl2-ttf and libarchive should do the trick.
### Linux
Most dependencies need to be installed manually. Installing the development packages for libsdl2 libsdl2-image libsdl2-ttf and libarchive should do the trick.
To build an AppImage run CMake with the "-DAPPIMAGE=1" option.
Settings files are being saved in "~/.vertiread".
Settings files are being saved in "~/.local/share/vertiread".

There’s a launcher file, which is copied to the build directory after compilation.
If you want a menu entry for the program, just set the executable’s and icon’s path in the .desktop file and move it to either "/usr/share/applications" or "~/.local/share/applications".
If you want a menu entry for the program, set the executable’s and icon’s path in the .desktop file and move it to either "/usr/share/applications" or "~/.local/share/applications".

## Windows
```bash
mkdir build
cd build
cmake .. -DVULKAN=1
make
```

### Windows
Only MS Visual Studio and MinGW are supported. All necessary libraries are downloaded while running CMake.
Settings files are being saved in "%AppData%\VertiRead".

```batch
mkdir build
cd build
cmake .. -G "NMake Makefiles" -DDIRECTX=1
nmake
```

## How to use it
The idea is that you have a library directory in which you have your comics saved in form of pictures or archives of pictures. The location of this directory can be changed in the settings.
Left clicking on a book in the book list will take you to the file explorer, while right clicking on a book will show you further options, like going to the last viewed page.
Expand Down
135 changes: 135 additions & 0 deletions rsc/dx_shaders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import os
import shutil
import subprocess

vsGui = '''
struct VertOut {
float4 pos : SV_POSITION;
float2 tuv : TEXCOORD0;
};
cbuffer Pview : register(b0) {
float4 pview;
};
cbuffer Instance : register(b1) {
int4 rect;
int4 frame;
};
static const float2 vposs[] = {
float2(0.f, 0.f),
float2(1.f, 0.f),
float2(0.f, 1.f),
float2(1.f, 1.f)
};
VertOut main(uint vid : SV_VertexId) {
float4 dst = float4(0.f, 0.f, 0.f, 0.f);
if (rect[2] > 0 && rect[3] > 0 && frame[2] > 0 && frame[3] > 0) {
dst.xy = max(rect.xy, frame.xy);
dst.zw = min(rect.xy + rect.zw, frame.xy + frame.zw) - dst.xy;
}
VertOut vout;
if (dst[2] > 0.f && dst[3] > 0.f) {
float4 uvrc = float4(dst.xy - rect.xy, dst.zw) / float4(rect.zwzw);
vout.tuv = vposs[vid] * uvrc.zw + uvrc.xy;
float2 loc = vposs[vid] * dst.zw + dst.xy;
vout.pos = float4((loc.x - pview.x) / pview[2] - 1.f, -(loc.y - pview.y) / pview[3] + 1.0, 0.f, 1.f);
} else {
vout.tuv = float2(0.f, 0.f);
vout.pos = float4(-2.f, -2.f, 0.f, 1.f);
}
return vout;
}'''

psGui = '''
Texture2D textureView : register(t0);
SamplerState sampleState : register(s0);
cbuffer InstanceColor : register(b0) {
float4 color;
};
float4 main(float4 pos : SV_POSITION, float2 tuv : TEXCOORD0) : SV_TARGET {
return textureView.Sample(sampleState, tuv) * color;
}'''

vsSel = '''
cbuffer Pview : register(b0) {
float4 pview;
};
cbuffer Instance : register(b1) {
int4 rect;
int4 frame;
};
static const float2 vposs[] = {
float2(0.f, 0.f),
float2(1.f, 0.f),
float2(0.f, 1.f),
float2(1.f, 1.f)
};
float4 main(uint vid : SV_VertexId) : SV_POSITION {
float4 dst = float4(0.f, 0.f, 0.f, 0.f);
if (rect[2] > 0 && rect[3] > 0 && frame[2] > 0 && frame[3] > 0) {
dst.xy = max(rect.xy, frame.xy);
dst.zw = min(rect.xy + rect.zw, frame.xy + frame.zw) - dst.xy;
}
if (dst[2] > 0.f && dst[3] > 0.f) {
float2 loc = vposs[vid] * dst.zw + dst.xy;
return float4((loc.x - pview.x) / pview[2] - 1.0, -(loc.y - pview.y) / pview[3] + 1.0, 0.f, 1.f);
}
return float4(-2.f, -2.f, 0.f, 1.f);
}'''

psSel = '''
cbuffer InstanceAddr : register(b1) {
uint2 addr;
};
uint2 main(float4 pos : SV_POSITION) : SV_TARGET {
if (addr.x != 0 || addr.y != 0)
return addr;
discard;
return uint2(0, 0);
}'''

def compile_source(fxc: str, code: str, name: str, ver: str):
with open(name, 'w') as fh:
fh.write(code)

for dbg in [ True, False ]:
try:
opt = [ '/Od', '/Zi' ] if dbg else [ '/O1' ]
ext = 'dbg' if dbg else 'rel'
fxcFile = f'{name}.{ext}.fxc'
cppFile = f'{name}.{ext}.h'

ret = subprocess.run([ fxc, '/Ges', '/Gis', '/T', ver, '/Fo', fxcFile, name ] + opt)
if ret.stdout:
print(f'stdout: {ret.stdout}')
if ret.stderr:
print(f'stderr: {ret.stderr}')
if ret.returncode != 0:
print(f'returned: {ret.returncode}')

with open(fxcFile, "rb") as fh:
data = fh.read()
with open(cppFile, 'w') as fh:
fh.write(',\n'.join(f'0x{c:X}' for c in data))
fh.write('\n')
os.remove(fxcFile)
except Exception as e:
print(e)
os.remove(name)

if __name__ == '__main__':
fxc = shutil.which('fxc')
os.chdir(os.path.join(os.path.dirname(__file__), os.pardir, 'src', 'engine', 'shaders'))
for it in [ (vsGui, 'dx.gui.vert', 'vs_5_0'), (psGui, 'dx.gui.pixl', 'ps_5_0'), (vsSel, 'dx.sel.vert', 'vs_5_0'), (psSel, 'dx.sel.pixl', 'ps_5_0') ]:
compile_source(fxc, it[0], it[1], it[2])
Loading

0 comments on commit 348331c

Please sign in to comment.