summaryrefslogtreecommitdiffstats
path: root/src/glsl/spirv_glslang.c
blob: ffb8f55be2ff3c610ea144e8b432dd2fca17202b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * This file is part of libplacebo.
 *
 * libplacebo is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libplacebo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with libplacebo. If not, see <http://www.gnu.org/licenses/>.
 */

#include "hash.h"
#include "spirv.h"
#include "utils.h"
#include "glsl/glslang.h"

// This header contains only preprocessor definitions
#include <glslang/build_info.h>

// This is awkward, but we cannot use upstream macro, it was fixed in 11.11.0
#define PL_GLSLANG_VERSION_GREATER_THAN(major, minor, patch) \
    ((GLSLANG_VERSION_MAJOR) > (major) || ((major) == GLSLANG_VERSION_MAJOR && \
    ((GLSLANG_VERSION_MINOR) > (minor) || ((minor) == GLSLANG_VERSION_MINOR && \
     (GLSLANG_VERSION_PATCH) > (patch)))))

#if PL_GLSLANG_VERSION_GREATER_THAN(11, 8, 0)
#define GLSLANG_SPV_MAX PL_SPV_VERSION(1, 6)
#elif PL_GLSLANG_VERSION_GREATER_THAN(7, 13, 3496)
#define GLSLANG_SPV_MAX PL_SPV_VERSION(1, 5)
#elif PL_GLSLANG_VERSION_GREATER_THAN(6, 2, 2596)
#define GLSLANG_SPV_MAX PL_SPV_VERSION(1, 3)
#else
#define GLSLANG_SPV_MAX PL_SPV_VERSION(1, 0)
#endif

const struct spirv_compiler pl_spirv_glslang;

static void glslang_destroy(pl_spirv spirv)
{
    pl_glslang_uninit();
    pl_free((void *) spirv);
}

static pl_spirv glslang_create(pl_log log, struct pl_spirv_version spirv_ver)
{
    if (!pl_glslang_init()) {
        pl_fatal(log, "Failed initializing glslang SPIR-V compiler!");
        return NULL;
    }

    struct pl_spirv_t *spirv = pl_alloc_ptr(NULL, spirv);
    *spirv = (struct pl_spirv_t) {
        .signature = pl_str0_hash(pl_spirv_glslang.name),
        .impl      = &pl_spirv_glslang,
        .version   = spirv_ver,
        .log       = log,
    };

    PL_INFO(spirv, "glslang version: %d.%d.%d",
            GLSLANG_VERSION_MAJOR,
            GLSLANG_VERSION_MINOR,
            GLSLANG_VERSION_PATCH);

    // Clamp to supported version by glslang
    if (GLSLANG_SPV_MAX < spirv->version.spv_version) {
        spirv->version.spv_version = GLSLANG_SPV_MAX;
        spirv->version.env_version = pl_spirv_version_to_vulkan(GLSLANG_SPV_MAX);
    }

    pl_hash_merge(&spirv->signature, (uint64_t) spirv->version.spv_version << 32 |
                                                spirv->version.env_version);
    pl_hash_merge(&spirv->signature, (GLSLANG_VERSION_MAJOR & 0xFF) << 24 |
                                     (GLSLANG_VERSION_MINOR & 0xFF) << 16 |
                                     (GLSLANG_VERSION_PATCH & 0xFFFF));
    return spirv;
}

static pl_str glslang_compile(pl_spirv spirv, void *alloc,
                              struct pl_glsl_version glsl_ver,
                              enum glsl_shader_stage stage,
                              const char *shader)
{
    struct pl_glslang_res *res;

    res = pl_glslang_compile(glsl_ver, spirv->version, stage, shader);
    if (!res || !res->success) {
        PL_ERR(spirv, "glslang failed: %s", res ? res->error_msg : "(null)");
        pl_free(res);
        return (struct pl_str) {0};
    }

    struct pl_str ret = {
        .buf = pl_steal(alloc, res->data),
        .len = res->size,
    };

    pl_free(res);
    return ret;
}

const struct spirv_compiler pl_spirv_glslang = {
    .name       = "glslang",
    .destroy    = glslang_destroy,
    .create     = glslang_create,
    .compile    = glslang_compile,
};