PacktPublishing / Vulkan-Cookbook

Code repository for Vulkan Cookbook by Packt
MIT License
816 stars 109 forks source link

Platforms specific extensions may behave weird on preprocessing of VS2017 #8

Closed thesmallcreeper closed 6 years ago

thesmallcreeper commented 6 years ago

If on main.cpp I include and define with this order:

#include <iostream>
#include <vector>

// Platform related options
#define VK_USE_PLATFORM_WIN32_KHR

#define LoadFunction GetProcAddress
#define LIBRARY_TYPE HMODULE

// Prepare for loading Vulkan functions

#define VK_NO_PROTOTYPES
#include "VulkanFunctions.h"

using namespace VulkanFunctions;

int main()
{
// Load vulkan-1.ddl file

    LIBRARY_TYPE vulkan_library = LoadLibrary("vulkan-1.dll");
...

and VulkanFunctions.h is like this:

#ifndef VULKAN_FUNCTIONS
#define VULKAN_FUNCTIONS

#include "vulkan/vulkan.h"

namespace VulkanFunctions{

#define EXPORTED_VULKAN_FUNCTION( name ) extern PFN_##name name;
#define GLOBAL_LEVEL_VULKAN_FUNCTION( name ) extern PFN_##name name;
#define INSTANCE_LEVEL_VULKAN_FUNCTION( name ) extern PFN_##name name;
#define INSTANCE_LEVEL_VULKAN_FUNCTION_FROM_EXTENSION( name, extension ) extern PFN_##name name;
#define DEVICE_LEVEL_VULKAN_FUNCTION( name ) extern PFN_##name name;
#define DEVICE_LEVEL_VULKAN_FUNCTION_FROM_EXTENSION( name, extension ) extern PFN_##name name;

#include "ListOfVulkanFunctions.inl"

}

#endif

and on my ListOfVulkanFunctions.inl there is a platform specific function from extension , example:

INSTANCE_LEVEL_VULKAN_FUNCTION_FROM_EXTENSION(vkCreateWin32SurfaceKHR, VK_KHR_WIN32_SURFACE_EXTENSION_NAME)

Then on compiling I get syntax errors (errors: C4430 , C2146) on the lines of ListOfVulkanFunctions.inl that platform specific functions exist. By the way, manually using vkGetInstanceProcAddr works.

However!, if I write #define VK_USE_PLATFORM_WIN32_KHR just above #include "vulkan/vulkan.h" on VulkanFunctions.h everything compile (and work) fine!

I just write this because I find it strange that this happen since in main.cpp I already define #define VK_USE_PLATFORM_WIN32_KHR and then include #include "VulkanFunctions.h", but I have to redefine the platform again at VulkanFunctions.h.

Is there a explanation? Even though I have a poor experience of #define(s) and #include(s) this look strange to me and so I make this "issue"

Using Visual Studio 2017 (15.6.4). SDK: 10.0.16299.0 Platform Toolset: Visual Studio 2017 (v141)

P.S: Might be a better place to share this piece of information since this is not a issue-bug of your code.

Ekzuzy commented 6 years ago

Thanks for the info! Maybe I will try to change the repository so the problem does not occur with VS2017.

Ekzuzy commented 6 years ago

Hi. I'd like to have a better understanding what You are trying to achieve. Does this problem occur while compiling samples from the Cookbook or Your own?

In Cookbook samples VulkanFunctions.h file is included in two places. So if You are creating Your own project, but use files from the Cookbook and if You define VK_USE_PLATFORM_WIN32_KHR just before the one of these inclusions but not before the other, this may generate compilation errors (depending on the order of compiled files).

That's why in Vulkan Cookbook samples, this definition is specified not in code but globally through project properties:

Project -> Properties -> C++ -> Preprocessor -> Preprocessor Definitions

If You are compiling Your own project try adding VK_USE_PLATFORM_WIN32_KHR in the above option. This should help.

But if the problem concerns compilation of Vulkan Cookbook samples with VS2017, then I will need to have a look at it when I finally install this version of Visual Studio.

thesmallcreeper commented 6 years ago

On something on my own.

Thinking twice about this issue I think it was unnecessary. I could not get why defining VK_USE_PLATFORM_WIN32_KHR on such a way could not work.

Thank you for your time and sorry if you feel like I wasted some of it.

Ekzuzy commented 6 years ago

I'm not sure that I helped here but an open conversation is always good ;-). If You have any other problems, do not hesitate to write.