1# Copyright (c) 2018 The Android Open Source Project 2# Copyright (c) 2018 Google Inc. 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16from .common.codegen import CodeGen 17from .common.vulkantypes import \ 18 VulkanAPI, makeVulkanTypeSimple, iterateVulkanType 19 20from .wrapperdefs import VulkanWrapperGenerator 21from .wrapperdefs import STRUCT_EXTENSION_PARAM 22from .wrapperdefs import STRUCT_EXTENSION_PARAM_FOR_WRITE 23from .wrapperdefs import EXTENSION_SIZE_API_NAME 24from .wrapperdefs import EXTENSION_SIZE_WITH_STREAM_FEATURES_API_NAME 25from .wrapperdefs import STRUCT_TYPE_API_NAME 26 27class VulkanExtensionStructs(VulkanWrapperGenerator): 28 29 def __init__(self, module, typeInfo): 30 VulkanWrapperGenerator.__init__(self, module, typeInfo) 31 32 self.codegen = CodeGen() 33 34 self.structTypeRetType = \ 35 makeVulkanTypeSimple(False, "uint32_t", 0) 36 37 self.rootTypeVarName = "rootType" 38 self.rootTypeParam = \ 39 makeVulkanTypeSimple(False, "VkStructureType", 40 0, self.rootTypeVarName) 41 self.structTypePrototype = \ 42 VulkanAPI(STRUCT_TYPE_API_NAME, 43 self.structTypeRetType, 44 [STRUCT_EXTENSION_PARAM]) 45 46 self.extensionStructSizeRetType = \ 47 makeVulkanTypeSimple(False, "size_t", 0) 48 self.extensionStructSizePrototype = \ 49 VulkanAPI(EXTENSION_SIZE_API_NAME, 50 self.extensionStructSizeRetType, 51 [self.rootTypeParam, STRUCT_EXTENSION_PARAM]) 52 53 self.streamFeaturesType = makeVulkanTypeSimple(False, "uint32_t", 0, "streamFeatures") 54 55 self.extensionStructSizeWithStreamFeaturesPrototype = \ 56 VulkanAPI(EXTENSION_SIZE_WITH_STREAM_FEATURES_API_NAME, 57 self.extensionStructSizeRetType, 58 [self.streamFeaturesType, self.rootTypeParam, STRUCT_EXTENSION_PARAM]) 59 def onBegin(self,): 60 VulkanWrapperGenerator.onBegin(self) 61 self.module.appendHeader(self.codegen.makeFuncDecl( 62 self.structTypePrototype)) 63 self.module.appendHeader(self.codegen.makeFuncDecl( 64 self.extensionStructSizePrototype)) 65 self.module.appendHeader(self.codegen.makeFuncDecl( 66 self.extensionStructSizeWithStreamFeaturesPrototype)) 67 68 def onGenType(self, typeXml, name, alias): 69 VulkanWrapperGenerator.onGenType(self, typeXml, name, alias) 70 71 def onGenCmd(self, cmdinfo, name, alias): 72 VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias) 73 74 def onEnd(self,): 75 VulkanWrapperGenerator.onEnd(self) 76 77 def castAsStruct(varName, typeName, const=True): 78 return "reinterpret_cast<%s%s*>(%s)" % \ 79 ("const " if const else "", typeName, varName) 80 81 def structTypeImpl(cgen): 82 cgen.stmt( 83 "const uint32_t asStructType = *(%s)" % 84 (castAsStruct(STRUCT_EXTENSION_PARAM.paramName, "uint32_t"))) 85 cgen.stmt("return asStructType") 86 87 self.module.appendImpl( 88 self.codegen.makeFuncImpl( 89 self.structTypePrototype, structTypeImpl)) 90 91 def forEachExtensionReturnSize(ext, _, cgen): 92 cgen.stmt("return sizeof(%s)" % ext.name) 93 94 def forEachExtensionReturnSizeProtectedByFeature(ext, _, cgen): 95 featureProtected = (ext.optionalStr is not None) and (ext.optionalStr.startswith("streamFeature:")) 96 if featureProtected: 97 splitted = ext.optionalStr.split(":") 98 cgen.beginIf("%s & %s" % ("streamFeatures", splitted[1])) 99 cgen.stmt("return sizeof(%s)" % ext.name) 100 cgen.endIf() 101 cgen.beginElse() 102 cgen.stmt("return 0") 103 cgen.endIf() 104 else: 105 cgen.stmt("return sizeof(%s)" % ext.name) 106 107 self.module.appendImpl( 108 self.codegen.makeFuncImpl( 109 self.extensionStructSizePrototype, 110 lambda cgen: self.emitForEachStructExtension( 111 cgen, 112 self.extensionStructSizeRetType, 113 STRUCT_EXTENSION_PARAM, 114 forEachExtensionReturnSize, autoBreak=False, 115 rootTypeVar=self.rootTypeParam))) 116 117 self.module.appendImpl( 118 self.codegen.makeFuncImpl( 119 self.extensionStructSizeWithStreamFeaturesPrototype, 120 lambda cgen: self.emitForEachStructExtension( 121 cgen, 122 self.extensionStructSizeRetType, 123 STRUCT_EXTENSION_PARAM, 124 forEachExtensionReturnSizeProtectedByFeature, autoBreak=False, 125 rootTypeVar=self.rootTypeParam))) 126