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.

Fixes #5
  • Loading branch information
karl-lunarg committed May 29, 2018
1 parent 97af2fd commit 2cb03b4
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 526 deletions.
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()
55 changes: 2 additions & 53 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 */
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 */
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) {
#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)) {
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.

File renamed without changes.
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 2cb03b4

Please sign in to comment.