Skip to content

Commit

Permalink
build: Add cube(pp) to install target
Browse files Browse the repository at this point in the history
- Modify cube and cubepp to obtain the texture image data from
  an include file instead of reading a PPM file at runtime.
  (This is the way it works for Android)
  This removes the need to install an image file or otherwise
  make an image file available in a repo build.
- Add cube and cubepp to the install target.

Note: The file handling code is left in place to make it easy
to add a "-texture_file <file>" option so a user can pass in
a texture file.

Fixes #5
  • Loading branch information
karl-lunarg committed May 29, 2018
1 parent 97af2fd commit d7e5993
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 530 deletions.
18 changes: 17 additions & 1 deletion BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ Start a build by selecting the Build->Build Solution menu item.
This solution copies the loader it built to each program's build directory
to ensure that the program uses the loader built from this solution.

#### Windows Install Target

The CMake project also generates an "install" target that you can use to
copy the primary build artifacts to a specific location using a
"bin, include, lib" style directory structure.
This may be useful for collecting the artifacts and providing them to
another project that is dependent on them.

The default location is `$CMAKE_BINARY_DIR\install`, but can be changed
with the `CMAKE_INSTALL_PREFIX` variable.
You can build the install target with:

cmake --build . --config Release --target install

or build the `INSTALL` target from the Visual Studio solution explorer.

### Windows Notes

#### CMake Visual Studio Generators
Expand Down Expand Up @@ -170,7 +186,7 @@ Assuming that you have built the code as described above and the current directo
This command installs files to:

- `/usr/local/lib`: Vulkan Tools shared objects (e.g., Mock ICD shared library)
- `/usr/local/bin`: vulkaninfo application
- `/usr/local/bin`: cube, cubepp, and vulkaninfo applications
- `/usr/local/share/vulkan/icd.d`: Mock ICD JSON file

You may need to run `ldconfig` in order to refresh the system loader search cache on some Linux systems.
Expand Down
18 changes: 18 additions & 0 deletions cube/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,15 @@ else()
target_link_libraries(cube ${LIBVK})
endif()

if(APPLE)
set_target_properties(cube PROPERTIES
INSTALL_RPATH "@loader_path/../lib"
)
install(TARGETS cube DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
else()
install(TARGETS cube RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()

######################################################################################
# cubepp

Expand All @@ -259,3 +268,12 @@ else()
add_executable(cubepp WIN32 cube.cpp ${PROJECT_SOURCE_DIR}/cube/cube.vert ${PROJECT_SOURCE_DIR}/cube/cube.frag cube.vert.inc cube.frag.inc)
target_link_libraries(cubepp ${LIBVK})
endif()

if(APPLE)
set_target_properties(cubepp PROPERTIES
INSTALL_RPATH "@loader_path/../lib"
)
install(TARGETS cubepp DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
else()
install(TARGETS cubepp RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
57 changes: 3 additions & 54 deletions cube/cube.c
Original file line number Diff line number Diff line change
Expand Up @@ -1450,14 +1450,10 @@ static void demo_prepare_depth(struct demo *demo) {
assert(!err);
}

/* Load a ppm file into memory */
/* Convert ppm image data from header file into RGBA texture image */
#include "lunarg.ppm.h"
bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *layout, int32_t *width, int32_t *height) {
#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
filename = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@(filename)].UTF8String;
#endif

#ifdef __ANDROID__
#include <lunarg.ppm.h>
(void)filename;
char *cPtr;
cPtr = (char *)lunarg_ppm;
if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
Expand All @@ -1476,7 +1472,6 @@ bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *
}
while (strncmp(cPtr++, "\n", 1))
;

for (int y = 0; y < *height; y++) {
uint8_t *rowPtr = rgba_data;
for (int x = 0; x < *width; x++) {
Expand All @@ -1487,53 +1482,7 @@ bool loadTexture(const char *filename, uint8_t *rgba_data, VkSubresourceLayout *
}
rgba_data += layout->rowPitch;
}

return true;
#else
FILE *fPtr = fopen(filename, "rb");
char header[256], *cPtr, *tmp;

if (!fPtr) return false;

cPtr = fgets(header, 256, fPtr); // P6
if (cPtr == NULL || strncmp(header, "P6\n", 3)) {
fclose(fPtr);
return false;
}

do {
cPtr = fgets(header, 256, fPtr);
if (cPtr == NULL) {
fclose(fPtr);
return false;
}
} while (!strncmp(header, "#", 1));

sscanf(header, "%u %u", width, height);
if (rgba_data == NULL) {
fclose(fPtr);
return true;
}
tmp = fgets(header, 256, fPtr); // Format
(void)tmp;
if (cPtr == NULL || strncmp(header, "255\n", 3)) {
fclose(fPtr);
return false;
}

for (int y = 0; y < *height; y++) {
uint8_t *rowPtr = rgba_data;
for (int x = 0; x < *width; x++) {
size_t s = fread(rowPtr, 3, 1, fPtr);
(void)s;
rowPtr[3] = 255; /* Alpha of 1 */
rowPtr += 4;
}
rgba_data += layout->rowPitch;
}
fclose(fPtr);
return true;
#endif
}

static void demo_prepare_texture_image(struct demo *demo, const char *filename, struct texture_object *tex_obj,
Expand Down
54 changes: 17 additions & 37 deletions cube/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2301,58 +2301,38 @@ void Demo::update_data_buffer() {
device.unmapMemory(swapchain_image_resources[current_buffer].uniform_memory);
}

/* Convert ppm image data from header file into RGBA texture image */
#include "lunarg.ppm.h"
bool Demo::loadTexture(const char *filename, uint8_t *rgba_data, vk::SubresourceLayout *layout, int32_t *width, int32_t *height) {
#if (defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK))
filename = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@(filename)].UTF8String;
#endif

FILE *fPtr = fopen(filename, "rb");
if (!fPtr) {
(void)filename;
char *cPtr;
cPtr = (char *)lunarg_ppm;
if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "P6\n", 3)) {
return false;
}

char header[256];
char *cPtr = fgets(header, 256, fPtr); // P6
if (cPtr == nullptr || strncmp(header, "P6\n", 3)) {
fclose(fPtr);
return false;
}

do {
cPtr = fgets(header, 256, fPtr);
if (cPtr == nullptr) {
fclose(fPtr);
return false;
}
} while (!strncmp(header, "#", 1));

sscanf(header, "%" SCNd32 " %" SCNd32, width, height);
if (rgba_data == nullptr) {
fclose(fPtr);
while (strncmp(cPtr++, "\n", 1))
;
sscanf(cPtr, "%u %u", width, height);
if (rgba_data == NULL) {
return true;
}

char *result = fgets(header, 256, fPtr); // Format
VERIFY(result != nullptr);
if (cPtr == nullptr || strncmp(header, "255\n", 3)) {
fclose(fPtr);
while (strncmp(cPtr++, "\n", 1))
;
if ((unsigned char *)cPtr >= (lunarg_ppm + lunarg_ppm_len) || strncmp(cPtr, "255\n", 4)) {
return false;
}

while (strncmp(cPtr++, "\n", 1))
;
for (int y = 0; y < *height; y++) {
uint8_t *rowPtr = rgba_data;

for (int x = 0; x < *width; x++) {
size_t s = fread(rowPtr, 3, 1, fPtr);
(void)s;
memcpy(rowPtr, cPtr, 3);
rowPtr[3] = 255; /* Alpha of 1 */
rowPtr += 4;
cPtr += 3;
}

rgba_data += layout->rowPitch;
}

fclose(fPtr);
return true;
}

Expand Down
434 changes: 0 additions & 434 deletions cube/lunarg.ppm

This file was deleted.

4 changes: 2 additions & 2 deletions cube/android/include/lunarg.ppm.h → cube/lunarg.ppm.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
unsigned char lunarg_ppm[] = {
static unsigned char lunarg_ppm[] = {
0x50, 0x36, 0x0a, 0x32, 0x35, 0x36, 0x20, 0x32, 0x35, 0x36, 0x0a, 0x32, 0x35, 0x35, 0x0a, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75,
0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75,
0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75,
Expand Down Expand Up @@ -9362,4 +9362,4 @@ unsigned char lunarg_ppm[] = {
0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75,
0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75,
0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75, 0x75};
unsigned int lunarg_ppm_len = 196623;
static unsigned int lunarg_ppm_len = 196623;
1 change: 0 additions & 1 deletion cube/macOS/cube/cube.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ set(cube_HDRS
${CMAKE_CURRENT_SOURCE_DIR}/macOS/cube/DemoViewController.h
)
set(cube_RESOURCES
${CMAKE_CURRENT_SOURCE_DIR}/lunarg.ppm
${CMAKE_BINARY_DIR}/staging-json/MoltenVK_icd.json
${CMAKE_CURRENT_SOURCE_DIR}/macOS/cube/Resources/LunarGIcon.icns
)
Expand Down
1 change: 0 additions & 1 deletion cube/macOS/cubepp/cubepp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ set(cubepp_HDRS
${CMAKE_CURRENT_SOURCE_DIR}/macOS/cubepp/DemoViewController.h
)
set(cubepp_RESOURCES
${CMAKE_CURRENT_SOURCE_DIR}/lunarg.ppm
${CMAKE_BINARY_DIR}/staging-json/MoltenVK_icd.json
${CMAKE_CURRENT_SOURCE_DIR}/macOS/cubepp/Resources/LunarGIcon.icns
)
Expand Down

0 comments on commit d7e5993

Please sign in to comment.