Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Vulkan/OpenGL enum name lookup  (Read 13886 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2016-03-27 16:41:55 »

Hello, everyone. I wrote this code to help me understand Vulkan. It searches through the Vulkan classes to convert enum values to enum names. Here's an example of it in action:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
      VkSurfaceFormatKHR.Buffer formats = ...;
      System.out.println("    Surface formats: " + formats.limit());
      for(int i = 0; i < formats.limit(); i++){
         System.out.println("    Surface format " + (i+1) + ":");
         VkSurfaceFormatKHR format = formats.get(i);
         System.out.println("        Format: " + getEnumName("VK_FORMAT", format.format()));
         System.out.println("        Colorspace: " + getEnumName("VK_COLORSPACE", format.colorSpace()));
      }
     
      VkSurfaceFormatKHR format = formats.get(0);

     
      IntBuffer presentModes = ...;
      System.out.println("    Surface present modes: " + presentModes.limit());
      for(int i = 0; i < presentModes.limit(); i++){
         int presentMode = presentModes.get(i);
         System.out.println("        Surface present mode " + (i+1) + ": " + getEnumName("VK_PRESENT_MODE", presentMode));
      }

Output:
Quote
    Surface formats: 2
    Surface format 1:
        Format: VK_FORMAT_B8G8R8A8_UNORM
        Colorspace: VK_COLORSPACE_SRGB_NONLINEAR_KHR
    Surface format 2:
        Format: VK_FORMAT_B8G8R8A8_SRGB
        Colorspace: VK_COLORSPACE_SRGB_NONLINEAR_KHR
    Surface present modes: 3
        Surface present mode 1: VK_PRESENT_MODE_FIFO_KHR
        Surface present mode 2: VK_PRESENT_MODE_FIFO_RELAXED_KHR
        Surface present mode 3: VK_PRESENT_MODE_MAILBOX_KHR
This is obviously only useful when debugging to make readable printouts, but it's extremely powerful (thank you, Java reflection!) and easy to use. Here's the code for getEnumName():
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
   private Class<?>[] enumClasses = new Class[]{
         VK10.class,
         KHRSurface.class,
         KHRSwapchain.class,
   };
   private String getEnumName(String prefix, int value){
      for(Class<?> c : enumClasses){
         for(Field f : c.getFields()){
            try {
               if(f.getName().startsWith(prefix) && (Integer)f.get(null) == value){
                  return f.getName();
               }
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      }
      return "Unknown enum " + value + " (0x" + Integer.toHexString(value) + ")";
   }

The enumClasses array can be tailor made so it only searches through the classes you use. Theoretically this function should work for OpenGL as well. In Vulkan each set of enums (formats, present modes, etc) all start at 0, but they're all prefixed with the set they belong to. In OpenGL the enums do not start with the name of the enum set, but I do think that they're unique in the entirety of OpenGL, so this function should still work for OpenGL too by just removing the prefix argument needed for Vulkan.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (439 views)
2019-09-04 15:33:17

hadezbladez (5293 views)
2018-11-16 13:46:03

hadezbladez (2213 views)
2018-11-16 13:41:33

hadezbladez (5562 views)
2018-11-16 13:35:35

hadezbladez (1156 views)
2018-11-16 13:32:03

EgonOlsen (4592 views)
2018-06-10 19:43:48

EgonOlsen (5467 views)
2018-06-10 19:43:44

EgonOlsen (3127 views)
2018-06-10 19:43:20

DesertCoockie (4023 views)
2018-05-13 18:23:11

nelsongames (4739 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!