Branch data Line data Source code
1 : : /*
2 : : * This file is part of libplacebo.
3 : : *
4 : : * libplacebo is free software; you can redistribute it and/or
5 : : * modify it under the terms of the GNU Lesser General Public
6 : : * License as published by the Free Software Foundation; either
7 : : * version 2.1 of the License, or (at your option) any later version.
8 : : *
9 : : * libplacebo is distributed in the hope that it will be useful,
10 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : : * GNU Lesser General Public License for more details.
13 : : *
14 : : * You should have received a copy of the GNU Lesser General Public
15 : : * License along with libplacebo. If not, see <http://www.gnu.org/licenses/>.
16 : : */
17 : :
18 : : #include "hash.h"
19 : : #include "spirv.h"
20 : : #include "utils.h"
21 : : #include "glsl/glslang.h"
22 : :
23 : : // This header contains only preprocessor definitions
24 : : #include <glslang/build_info.h>
25 : :
26 : : // This is awkward, but we cannot use upstream macro, it was fixed in 11.11.0
27 : : #define PL_GLSLANG_VERSION_GREATER_THAN(major, minor, patch) \
28 : : ((GLSLANG_VERSION_MAJOR) > (major) || ((major) == GLSLANG_VERSION_MAJOR && \
29 : : ((GLSLANG_VERSION_MINOR) > (minor) || ((minor) == GLSLANG_VERSION_MINOR && \
30 : : (GLSLANG_VERSION_PATCH) > (patch)))))
31 : :
32 : : #if PL_GLSLANG_VERSION_GREATER_THAN(11, 8, 0)
33 : : #define GLSLANG_SPV_MAX PL_SPV_VERSION(1, 6)
34 : : #elif PL_GLSLANG_VERSION_GREATER_THAN(7, 13, 3496)
35 : : #define GLSLANG_SPV_MAX PL_SPV_VERSION(1, 5)
36 : : #elif PL_GLSLANG_VERSION_GREATER_THAN(6, 2, 2596)
37 : : #define GLSLANG_SPV_MAX PL_SPV_VERSION(1, 3)
38 : : #else
39 : : #define GLSLANG_SPV_MAX PL_SPV_VERSION(1, 0)
40 : : #endif
41 : :
42 : : const struct spirv_compiler pl_spirv_glslang;
43 : :
44 : 0 : static void glslang_destroy(pl_spirv spirv)
45 : : {
46 : 0 : pl_glslang_uninit();
47 : 0 : pl_free((void *) spirv);
48 : 0 : }
49 : :
50 : 0 : static pl_spirv glslang_create(pl_log log, struct pl_spirv_version spirv_ver)
51 : : {
52 [ # # ]: 0 : if (!pl_glslang_init()) {
53 : 0 : pl_fatal(log, "Failed initializing glslang SPIR-V compiler!");
54 : 0 : return NULL;
55 : : }
56 : :
57 : 0 : struct pl_spirv_t *spirv = pl_alloc_ptr(NULL, spirv);
58 : 0 : *spirv = (struct pl_spirv_t) {
59 : 0 : .signature = pl_str0_hash(pl_spirv_glslang.name),
60 : : .impl = &pl_spirv_glslang,
61 : : .version = spirv_ver,
62 : : .log = log,
63 : : };
64 : :
65 : 0 : PL_INFO(spirv, "glslang version: %d.%d.%d",
66 : : GLSLANG_VERSION_MAJOR,
67 : : GLSLANG_VERSION_MINOR,
68 : : GLSLANG_VERSION_PATCH);
69 : :
70 : : // Clamp to supported version by glslang
71 [ # # ]: 0 : if (GLSLANG_SPV_MAX < spirv->version.spv_version) {
72 : 0 : spirv->version.spv_version = GLSLANG_SPV_MAX;
73 : 0 : spirv->version.env_version = pl_spirv_version_to_vulkan(GLSLANG_SPV_MAX);
74 : : }
75 : :
76 : 0 : pl_hash_merge(&spirv->signature, (uint64_t) spirv->version.spv_version << 32 |
77 : 0 : spirv->version.env_version);
78 : : pl_hash_merge(&spirv->signature, (GLSLANG_VERSION_MAJOR & 0xFF) << 24 |
79 : : (GLSLANG_VERSION_MINOR & 0xFF) << 16 |
80 : : (GLSLANG_VERSION_PATCH & 0xFFFF));
81 : 0 : return spirv;
82 : : }
83 : :
84 : 0 : static pl_str glslang_compile(pl_spirv spirv, void *alloc,
85 : : struct pl_glsl_version glsl_ver,
86 : : enum glsl_shader_stage stage,
87 : : const char *shader)
88 : : {
89 : : struct pl_glslang_res *res;
90 : :
91 : 0 : res = pl_glslang_compile(glsl_ver, spirv->version, stage, shader);
92 [ # # # # ]: 0 : if (!res || !res->success) {
93 : 0 : PL_ERR(spirv, "glslang failed: %s", res ? res->error_msg : "(null)");
94 : 0 : pl_free(res);
95 : 0 : return (struct pl_str) {0};
96 : : }
97 : :
98 : : struct pl_str ret = {
99 : 0 : .buf = pl_steal(alloc, res->data),
100 : 0 : .len = res->size,
101 : : };
102 : :
103 : 0 : pl_free(res);
104 : 0 : return ret;
105 : : }
106 : :
107 : : const struct spirv_compiler pl_spirv_glslang = {
108 : : .name = "glslang",
109 : : .destroy = glslang_destroy,
110 : : .create = glslang_create,
111 : : .compile = glslang_compile,
112 : : };
|