Home | History | Annotate | Download | only in source
      1 // Copyright (c) 2015-2016 The Khronos Group Inc.
      2 //
      3 // Permission is hereby granted, free of charge, to any person obtaining a
      4 // copy of this software and/or associated documentation files (the
      5 // "Materials"), to deal in the Materials without restriction, including
      6 // without limitation the rights to use, copy, modify, merge, publish,
      7 // distribute, sublicense, and/or sell copies of the Materials, and to
      8 // permit persons to whom the Materials are furnished to do so, subject to
      9 // the following conditions:
     10 //
     11 // The above copyright notice and this permission notice shall be included
     12 // in all copies or substantial portions of the Materials.
     13 //
     14 // MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
     15 // KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
     16 // SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
     17 //    https://www.khronos.org/registry/
     18 //
     19 // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     22 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
     23 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     24 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     25 // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
     26 
     27 #include "ext_inst.h"
     28 
     29 #include <cassert>
     30 #include <cstring>
     31 
     32 #include "spirv/1.0/GLSL.std.450.h"
     33 #include "spirv/1.0/OpenCL.std.h"
     34 #include "spirv_definition.h"
     35 
     36 #include "macro.h"
     37 
     38 static const spv_ext_inst_desc_t glslStd450Entries_1_0[] = {
     39 #include "glsl.std.450.insts-1.0.inc"
     40 };
     41 
     42 static const spv_ext_inst_desc_t openclEntries_1_0[] = {
     43 #include "opencl.std.insts-1.0.inc"
     44 };
     45 
     46 spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable,
     47                                 spv_target_env env) {
     48   if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
     49 
     50   static const spv_ext_inst_group_t groups_1_0[] = {
     51       {SPV_EXT_INST_TYPE_GLSL_STD_450, ARRAY_SIZE(glslStd450Entries_1_0),
     52        glslStd450Entries_1_0},
     53       {SPV_EXT_INST_TYPE_OPENCL_STD, ARRAY_SIZE(openclEntries_1_0),
     54        openclEntries_1_0},
     55   };
     56 
     57   static const spv_ext_inst_table_t table_1_0 = {ARRAY_SIZE(groups_1_0),
     58                                                  groups_1_0};
     59 
     60   switch (env) {
     61     // The extended instruction sets are all version 1.0 so far.
     62     case SPV_ENV_UNIVERSAL_1_0:
     63     case SPV_ENV_VULKAN_1_0:
     64     case SPV_ENV_UNIVERSAL_1_1:
     65       *pExtInstTable = &table_1_0;
     66       return SPV_SUCCESS;
     67     default:
     68       assert(0 && "Unknown spv_target_env in spvExtInstTableGet()");
     69       return SPV_ERROR_INVALID_TABLE;
     70   }
     71 }
     72 
     73 spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) {
     74   // The names are specified by the respective extension instruction
     75   // specifications.
     76   if (!strcmp("GLSL.std.450", name)) {
     77     return SPV_EXT_INST_TYPE_GLSL_STD_450;
     78   }
     79   if (!strcmp("OpenCL.std", name)) {
     80     return SPV_EXT_INST_TYPE_OPENCL_STD;
     81   }
     82   return SPV_EXT_INST_TYPE_NONE;
     83 }
     84 
     85 spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
     86                                        const spv_ext_inst_type_t type,
     87                                        const char* name,
     88                                        spv_ext_inst_desc* pEntry) {
     89   if (!table) return SPV_ERROR_INVALID_TABLE;
     90   if (!pEntry) return SPV_ERROR_INVALID_POINTER;
     91 
     92   for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
     93     const auto& group = table->groups[groupIndex];
     94     if (type != group.type) continue;
     95     for (uint32_t index = 0; index < group.count; index++) {
     96       const auto& entry = group.entries[index];
     97       if (!strcmp(name, entry.name)) {
     98         *pEntry = &entry;
     99         return SPV_SUCCESS;
    100       }
    101     }
    102   }
    103 
    104   return SPV_ERROR_INVALID_LOOKUP;
    105 }
    106 
    107 spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
    108                                         const spv_ext_inst_type_t type,
    109                                         const uint32_t value,
    110                                         spv_ext_inst_desc* pEntry) {
    111   if (!table) return SPV_ERROR_INVALID_TABLE;
    112   if (!pEntry) return SPV_ERROR_INVALID_POINTER;
    113 
    114   for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
    115     const auto& group = table->groups[groupIndex];
    116     if (type != group.type) continue;
    117     for (uint32_t index = 0; index < group.count; index++) {
    118       const auto& entry = group.entries[index];
    119       if (value == entry.ext_inst) {
    120         *pEntry = &entry;
    121         return SPV_SUCCESS;
    122       }
    123     }
    124   }
    125 
    126   return SPV_ERROR_INVALID_LOOKUP;
    127 }
    128