Skip to content

Commit

Permalink
GL_EXT_spirv_intrinsics - Port extensions
Browse files Browse the repository at this point in the history
Add mechanism to use GL_EXT_spirv_intrinsics headers in glslang.
Ported GL_EXT_shader_realtime_clock as an example.
  • Loading branch information
AaronHaganAMD committed Oct 14, 2021
1 parent b9ba4c5 commit 5d5e196
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 10 deletions.
14 changes: 14 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ py_binary(
srcs = ["build_info.py"],
)

py_binary(
name = "gen_extension_headers",
srcs = ["gen_extension_headers.py"],
)

genrule(
name = "gen_build_info_h",
srcs = ["CHANGES.md", "build_info.h.tmpl"],
Expand All @@ -58,6 +63,14 @@ genrule(
tools = [":build_info"],
)

genrule(
name = "gen_extension_headers_h",
srcs = ["glslang/ExtensionHeaders", "gen_extension_headers.py"],
outs = ["glslang/glsl_intrinsic_header.h"],
cmd_bash = "$(location gen_extension_headers) -i $(location glslang/ExtensionHeaders) -o $(location glslang/glsl_intrinsic_header.h)",
tools = [":gen_extension_headers"],
)

COMMON_COPTS = select({
"@bazel_tools//src/conditions:windows": [""],
"//conditions:default": [
Expand Down Expand Up @@ -206,6 +219,7 @@ cc_binary(
srcs = [
"StandAlone/StandAlone.cpp",
"StandAlone/Worklist.h",
":glslang/glsl_intrinsic_header.h"
],
copts = COMMON_COPTS,
deps = [
Expand Down
20 changes: 20 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ action("glslang_build_info") {
]
}

action("glslang_extension_headers") {
script = "gen_extension_headers.py"

src_dir = "."
out_file = "${target_gen_dir}/include/glslang/glsl_intrinsic_header.h"

inputs = [
script
]
outputs = [ out_file ]
args = [
rebase_path(src_dir, root_build_dir),
"-i",
rebase_path("glslang/ExtensionHeaders", root_build_dir),
"-o",
rebase_path(out_file, root_build_dir),
]
}

spirv_tools_dir = glslang_spirv_tools_dir
if (!defined(glslang_angle)) {
glslang_angle = false
Expand Down Expand Up @@ -302,6 +321,7 @@ executable("glslang_validator") {
":glslang_build_info",
":glslang_default_resource_limits_sources",
":glslang_sources",
":glslang_extension_headers",
]
public_configs = [ ":glslang_hlsl" ]

Expand Down
20 changes: 18 additions & 2 deletions StandAlone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

find_host_package(PythonInterp 3 REQUIRED)

set(GLSLANG_INTRINSIC_H "${GLSLANG_GENERATED_INCLUDEDIR}/glslang/glsl_intrinsic_header.h")
set(GLSLANG_INTRINSIC_PY "${CMAKE_SOURCE_DIR}/gen_extension_headers.py")
set(GLSLANG_INTRINSIC_HEADER_DIR "${CMAKE_SOURCE_DIR}/glslang/ExtensionHeaders")

add_custom_command(
OUTPUT ${GLSLANG_INTRINSIC_H}
COMMAND ${PYTHON_EXECUTABLE} "${GLSLANG_INTRINSIC_PY}"
"-i" ${GLSLANG_INTRINSIC_HEADER_DIR}
"-o" ${GLSLANG_INTRINSIC_H}
DEPENDS ${GLSLANG_INTRINSIC_PY}
COMMENT "Generating ${GLSLANG_INTRINSIC_H}")

#add_custom_target(glslangValidator DEPENDS ${GLSLANG_INTRINSIC_H})

add_library(glslang-default-resource-limits
${CMAKE_CURRENT_SOURCE_DIR}/ResourceLimits.cpp
${CMAKE_CURRENT_SOURCE_DIR}/resource_limits_c.cpp)
Expand All @@ -41,7 +57,7 @@ target_include_directories(glslang-default-resource-limits
PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>)

set(SOURCES StandAlone.cpp DirStackFileIncluder.h)
set(SOURCES StandAlone.cpp DirStackFileIncluder.h ${GLSLANG_INTRINSIC_H})

add_executable(glslangValidator ${SOURCES})
set_property(TARGET glslangValidator PROPERTY FOLDER tools)
Expand Down Expand Up @@ -108,4 +124,4 @@ if(ENABLE_GLSLANG_INSTALL)
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
install(EXPORT glslang-default-resource-limitsTargets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake)
endif()
endif()
15 changes: 14 additions & 1 deletion StandAlone/StandAlone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
// Copyright (C) 2013-2016 LunarG, Inc.
// Copyright (C) 2016-2020 Google, Inc.
// Modifications Copyright(C) 2021 Advanced Micro Devices, Inc.All rights reserved.
//
// All rights reserved.
//
Expand Down Expand Up @@ -65,6 +66,8 @@
// Build-time generated includes
#include "glslang/build_info.h"

#include "glslang/glsl_intrinsic_header.h"

extern "C" {
GLSLANG_EXPORT void ShOutputHtml();
}
Expand Down Expand Up @@ -263,6 +266,7 @@ class TPreamble {

// Track the user's #define and #undef from the command line.
TPreamble UserPreamble;
std::string PreambleString;

//
// Create the default name for saving a binary if -o is not provided.
Expand Down Expand Up @@ -1204,8 +1208,17 @@ void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
"Use '-e <name>'.\n");
shader->setSourceEntryPoint(sourceEntryPointName);
}

std::string intrinsicString = getIntrinsic(compUnit.text, compUnit.count);

PreambleString = "";
if (UserPreamble.isSet())
shader->setPreamble(UserPreamble.get());
PreambleString.append(UserPreamble.get());

if (!intrinsicString.empty())
PreambleString.append(intrinsicString);

shader->setPreamble(PreambleString.c_str());
shader->addProcesses(Processes);

#ifndef GLSLANG_WEB
Expand Down
98 changes: 98 additions & 0 deletions gen_extension_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env python

# Copyright (c) 2020 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import glob
import sys
import os

def generate_main(glsl_files, output_header_file):
# Write commit ID to output header file
with open(output_header_file, "w", newline='\n') as header_file:
# Copyright Notice
header_string = '/***************************************************************************\n'
header_string += ' *\n'
header_string += ' * Copyright (c) 2015-2021 The Khronos Group Inc.\n'
header_string += ' * Copyright (c) 2015-2021 Valve Corporation\n'
header_string += ' * Copyright (c) 2015-2021 LunarG, Inc.\n'
header_string += ' * Copyright (c) 2015-2021 Google Inc.\n'
header_string += ' * Copyright (c) 2021 Advanced Micro Devices, Inc.All rights reserved.\n'
header_string += ' *\n'
header_string += ' ****************************************************************************/\n'
header_string += '#pragma once\n\n'
header_string += '#ifndef _INTRINSIC_EXTENSION_HEADER_H_\n'
header_string += '#define _INTRINSIC_EXTENSION_HEADER_H_\n\n'
header_file.write(header_string)

symbol_name_list = []

for i in glsl_files:
glsl_contents = open(i,"r").read()

filename = os.path.basename(i)
symbol_name = filename.split(".")[0]
symbol_name_list.append(symbol_name)
header_name = symbol_name + ".h"
header_str = 'std::string %s_GLSL = R"(\n%s\n)";\n' % (symbol_name, glsl_contents)
header_str += '\n'
header_file.write(header_str)

contents = ''
contents += '\n'
contents += 'std::string getIntrinsic(const char* const* shaders, int n) {\n'
contents += '\tstd::string shaderString = "";\n';

contents += '\tfor (int i = 0; i < n; i++) {\n'

for symbol_name in symbol_name_list:
contents += '\t\tif (strstr(shaders[i], "%s") != NULL) {\n' % (symbol_name)
contents += '\t\t shaderString.append(%s_GLSL);\n' % (symbol_name)
contents += '\t\t}\n'

contents += '\t}\n'
contents += '\treturn shaderString;\n';
contents += '}\n'

contents += '\n#endif\n'
header_file.write(contents)

def main():
if len(sys.argv) < 2:
raise Exception("Invalid number of arguments")

i = 0
while i < len(sys.argv):
opt = sys.argv[i]
i = i + 1

if opt == "-i" or opt == "-o":
if i == len(sys.argv):
raise Exception("Expected path after {}".format(opt))
val = sys.argv[i]
i = i + 1
if (opt == "-i"):
input_dir = val
elif (opt == "-o"):
output_file = val
else:
raise Exception("Unknown flag {}".format(opt))

glsl_files = glob.glob(input_dir + '/*.glsl')

# Generate main header
generate_main(glsl_files, output_file)

if __name__ == '__main__':
main()
16 changes: 16 additions & 0 deletions glslang/ExtensionHeaders/GL_EXT_shader_realtime_clock.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#extension GL_EXT_spirv_intrinsics : enable
#extension GL_ARB_gpu_shader_int64 : enable

uvec2 clockRealtime2x32EXT(void) {
spirv_instruction (extensions = ["SPV_KHR_shader_clock"], capabilities = [5055], id = 5056)
uvec2 clockRealtime2x32EXT_internal(uint scope);

return clockRealtime2x32EXT_internal(1 /*Device scope*/);
}

uint64_t clockRealtimeEXT(void) {
spirv_instruction (extensions = ["SPV_KHR_shader_clock"], capabilities = [5055], id = 5056)
uint64_t clockRealtimeEXT_internal(uint scope);

return clockRealtimeEXT_internal(1 /*Device scope*/);
}
9 changes: 2 additions & 7 deletions glslang/MachineIndependent/Initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (C) 2012-2016 LunarG, Inc.
// Copyright (C) 2015-2020 Google, Inc.
// Copyright (C) 2017 ARM Limited.
// Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
// Modifications Copyright (C) 2020-2021 Advanced Micro Devices, Inc. All rights reserved.
//
// All rights reserved.
//
Expand Down Expand Up @@ -4653,13 +4653,11 @@ void TBuiltIns::initialize(int version, EProfile profile, const SpvVersion& spvV
"\n");
}

// GL_ARB_shader_clock & GL_EXT_shader_realtime_clock
// GL_ARB_shader_clock
if (profile != EEsProfile && version >= 450) {
commonBuiltins.append(
"uvec2 clock2x32ARB();"
"uint64_t clockARB();"
"uvec2 clockRealtime2x32EXT();"
"uint64_t clockRealtimeEXT();"
"\n");
}

Expand Down Expand Up @@ -8408,9 +8406,6 @@ void TBuiltIns::identifyBuiltIns(int version, EProfile profile, const SpvVersion
symbolTable.setFunctionExtensions("clockARB", 1, &E_GL_ARB_shader_clock);
symbolTable.setFunctionExtensions("clock2x32ARB", 1, &E_GL_ARB_shader_clock);

symbolTable.setFunctionExtensions("clockRealtimeEXT", 1, &E_GL_EXT_shader_realtime_clock);
symbolTable.setFunctionExtensions("clockRealtime2x32EXT", 1, &E_GL_EXT_shader_realtime_clock);

if (profile == EEsProfile && version < 320) {
symbolTable.setVariableExtensions("gl_PrimitiveID", Num_AEP_geometry_shader, AEP_geometry_shader);
symbolTable.setVariableExtensions("gl_Layer", Num_AEP_geometry_shader, AEP_geometry_shader);
Expand Down

0 comments on commit 5d5e196

Please sign in to comment.