2021-08-19 07:48:13 -07:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Atharva Nimbalkar
|
|
|
|
*
|
|
|
|
* This library 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.
|
|
|
|
*
|
|
|
|
* This library 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 this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vkd3d_shader_private.h"
|
|
|
|
|
2021-07-13 15:11:32 -07:00
|
|
|
struct glsl_src
|
|
|
|
{
|
|
|
|
struct vkd3d_string_buffer *str;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct glsl_dst
|
|
|
|
{
|
|
|
|
const struct vkd3d_shader_dst_param *vsir;
|
|
|
|
struct vkd3d_string_buffer *register_name;
|
|
|
|
struct vkd3d_string_buffer *mask;
|
|
|
|
};
|
|
|
|
|
2021-08-19 07:48:13 -07:00
|
|
|
struct vkd3d_glsl_generator
|
|
|
|
{
|
2023-11-17 09:39:48 -08:00
|
|
|
struct vsir_program *program;
|
2021-07-13 15:11:32 -07:00
|
|
|
struct vkd3d_string_buffer_cache string_buffers;
|
|
|
|
struct vkd3d_string_buffer *buffer;
|
2023-02-25 19:04:58 -08:00
|
|
|
struct vkd3d_shader_location location;
|
2021-08-19 07:48:13 -07:00
|
|
|
struct vkd3d_shader_message_context *message_context;
|
2023-11-16 22:18:47 -08:00
|
|
|
unsigned int indent;
|
2021-08-19 07:48:13 -07:00
|
|
|
bool failed;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void VKD3D_PRINTF_FUNC(3, 4) vkd3d_glsl_compiler_error(
|
|
|
|
struct vkd3d_glsl_generator *generator,
|
|
|
|
enum vkd3d_shader_error error, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2023-02-25 19:04:58 -08:00
|
|
|
vkd3d_shader_verror(generator->message_context, &generator->location, error, fmt, args);
|
2021-08-19 07:48:13 -07:00
|
|
|
va_end(args);
|
|
|
|
generator->failed = true;
|
|
|
|
}
|
|
|
|
|
2023-11-16 22:18:47 -08:00
|
|
|
static void shader_glsl_print_indent(struct vkd3d_string_buffer *buffer, unsigned int indent)
|
|
|
|
{
|
|
|
|
vkd3d_string_buffer_printf(buffer, "%*s", 4 * indent, "");
|
|
|
|
}
|
|
|
|
|
2021-07-13 15:11:32 -07:00
|
|
|
static void shader_glsl_print_register_name(struct vkd3d_string_buffer *buffer,
|
|
|
|
struct vkd3d_glsl_generator *gen, const struct vkd3d_shader_register *reg)
|
|
|
|
{
|
2021-08-23 08:32:31 -07:00
|
|
|
switch (reg->type)
|
|
|
|
{
|
|
|
|
case VKD3DSPR_TEMP:
|
|
|
|
vkd3d_string_buffer_printf(buffer, "r[%u]", reg->idx[0].offset);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL,
|
|
|
|
"Internal compiler error: Unhandled register type %#x.", reg->type);
|
|
|
|
vkd3d_string_buffer_printf(buffer, "<unrecognised register %#x>", reg->type);
|
|
|
|
break;
|
|
|
|
}
|
2021-07-13 15:11:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void shader_glsl_print_swizzle(struct vkd3d_string_buffer *buffer, uint32_t swizzle, uint32_t mask)
|
|
|
|
{
|
|
|
|
const char swizzle_chars[] = "xyzw";
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
vkd3d_string_buffer_printf(buffer, ".");
|
|
|
|
for (i = 0; i < VKD3D_VEC4_SIZE; ++i)
|
|
|
|
{
|
|
|
|
if (mask & (VKD3DSP_WRITEMASK_0 << i))
|
|
|
|
vkd3d_string_buffer_printf(buffer, "%c", swizzle_chars[vsir_swizzle_get_component(swizzle, i)]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void shader_glsl_print_write_mask(struct vkd3d_string_buffer *buffer, uint32_t write_mask)
|
|
|
|
{
|
|
|
|
vkd3d_string_buffer_printf(buffer, ".");
|
|
|
|
if (write_mask & VKD3DSP_WRITEMASK_0)
|
|
|
|
vkd3d_string_buffer_printf(buffer, "x");
|
|
|
|
if (write_mask & VKD3DSP_WRITEMASK_1)
|
|
|
|
vkd3d_string_buffer_printf(buffer, "y");
|
|
|
|
if (write_mask & VKD3DSP_WRITEMASK_2)
|
|
|
|
vkd3d_string_buffer_printf(buffer, "z");
|
|
|
|
if (write_mask & VKD3DSP_WRITEMASK_3)
|
|
|
|
vkd3d_string_buffer_printf(buffer, "w");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void glsl_src_cleanup(struct glsl_src *src, struct vkd3d_string_buffer_cache *cache)
|
|
|
|
{
|
|
|
|
vkd3d_string_buffer_release(cache, src->str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void glsl_src_init(struct glsl_src *glsl_src, struct vkd3d_glsl_generator *gen,
|
|
|
|
const struct vkd3d_shader_src_param *vsir_src, uint32_t mask)
|
|
|
|
{
|
|
|
|
const struct vkd3d_shader_register *reg = &vsir_src->reg;
|
|
|
|
|
|
|
|
glsl_src->str = vkd3d_string_buffer_get(&gen->string_buffers);
|
|
|
|
|
|
|
|
if (reg->non_uniform)
|
|
|
|
vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL,
|
2024-08-16 15:23:51 -07:00
|
|
|
"Internal compiler error: Unhandled 'non-uniform' modifier.");
|
2021-07-13 15:11:32 -07:00
|
|
|
if (vsir_src->modifiers)
|
|
|
|
vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL,
|
|
|
|
"Internal compiler error: Unhandled source modifier(s) %#x.", vsir_src->modifiers);
|
|
|
|
|
|
|
|
shader_glsl_print_register_name(glsl_src->str, gen, reg);
|
|
|
|
if (reg->dimension == VSIR_DIMENSION_VEC4)
|
|
|
|
shader_glsl_print_swizzle(glsl_src->str, vsir_src->swizzle, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void glsl_dst_cleanup(struct glsl_dst *dst, struct vkd3d_string_buffer_cache *cache)
|
|
|
|
{
|
|
|
|
vkd3d_string_buffer_release(cache, dst->mask);
|
|
|
|
vkd3d_string_buffer_release(cache, dst->register_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t glsl_dst_init(struct glsl_dst *glsl_dst, struct vkd3d_glsl_generator *gen,
|
|
|
|
const struct vkd3d_shader_instruction *ins, const struct vkd3d_shader_dst_param *vsir_dst)
|
|
|
|
{
|
|
|
|
uint32_t write_mask = vsir_dst->write_mask;
|
|
|
|
|
|
|
|
if (ins->flags & VKD3DSI_PRECISE_XYZW)
|
|
|
|
vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL,
|
2024-08-16 15:23:51 -07:00
|
|
|
"Internal compiler error: Unhandled 'precise' modifier.");
|
2021-07-13 15:11:32 -07:00
|
|
|
if (vsir_dst->reg.non_uniform)
|
|
|
|
vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL,
|
2024-08-16 15:23:51 -07:00
|
|
|
"Internal compiler error: Unhandled 'non-uniform' modifier.");
|
2021-07-13 15:11:32 -07:00
|
|
|
|
|
|
|
glsl_dst->vsir = vsir_dst;
|
|
|
|
glsl_dst->register_name = vkd3d_string_buffer_get(&gen->string_buffers);
|
|
|
|
glsl_dst->mask = vkd3d_string_buffer_get(&gen->string_buffers);
|
|
|
|
|
|
|
|
shader_glsl_print_register_name(glsl_dst->register_name, gen, &vsir_dst->reg);
|
|
|
|
shader_glsl_print_write_mask(glsl_dst->mask, write_mask);
|
|
|
|
|
|
|
|
return write_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void VKD3D_PRINTF_FUNC(3, 4) shader_glsl_print_assignment(
|
|
|
|
struct vkd3d_glsl_generator *gen, struct glsl_dst *dst, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
if (dst->vsir->shift)
|
|
|
|
vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL,
|
|
|
|
"Internal compiler error: Unhandled destination shift %#x.", dst->vsir->shift);
|
|
|
|
if (dst->vsir->modifiers)
|
|
|
|
vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL,
|
|
|
|
"Internal compiler error: Unhandled destination modifier(s) %#x.", dst->vsir->modifiers);
|
|
|
|
|
|
|
|
shader_glsl_print_indent(gen->buffer, gen->indent);
|
|
|
|
vkd3d_string_buffer_printf(gen->buffer, "%s%s = ", dst->register_name->buffer, dst->mask->buffer);
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
vkd3d_string_buffer_vprintf(gen->buffer, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
vkd3d_string_buffer_printf(gen->buffer, ";\n");
|
|
|
|
}
|
|
|
|
|
2023-11-16 22:06:11 -08:00
|
|
|
static void shader_glsl_unhandled(struct vkd3d_glsl_generator *gen, const struct vkd3d_shader_instruction *ins)
|
|
|
|
{
|
2021-07-13 15:11:32 -07:00
|
|
|
shader_glsl_print_indent(gen->buffer, gen->indent);
|
|
|
|
vkd3d_string_buffer_printf(gen->buffer, "/* <unhandled instruction %#x> */\n", ins->opcode);
|
2023-11-16 22:06:11 -08:00
|
|
|
vkd3d_glsl_compiler_error(gen, VKD3D_SHADER_ERROR_GLSL_INTERNAL,
|
2024-04-24 11:34:42 -07:00
|
|
|
"Internal compiler error: Unhandled instruction %#x.", ins->opcode);
|
2023-11-16 22:06:11 -08:00
|
|
|
}
|
|
|
|
|
2021-07-13 15:11:32 -07:00
|
|
|
static void shader_glsl_mov(struct vkd3d_glsl_generator *gen, const struct vkd3d_shader_instruction *ins)
|
|
|
|
{
|
|
|
|
struct glsl_src src;
|
|
|
|
struct glsl_dst dst;
|
|
|
|
uint32_t mask;
|
|
|
|
|
|
|
|
mask = glsl_dst_init(&dst, gen, ins, &ins->dst[0]);
|
|
|
|
glsl_src_init(&src, gen, &ins->src[0], mask);
|
|
|
|
|
|
|
|
shader_glsl_print_assignment(gen, &dst, "%s", src.str->buffer);
|
|
|
|
|
|
|
|
glsl_src_cleanup(&src, &gen->string_buffers);
|
|
|
|
glsl_dst_cleanup(&dst, &gen->string_buffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void shader_glsl_ret(struct vkd3d_glsl_generator *gen, const struct vkd3d_shader_instruction *ins)
|
2021-08-19 07:48:16 -07:00
|
|
|
{
|
2021-07-13 15:11:32 -07:00
|
|
|
const struct vkd3d_shader_version *version = &gen->program->shader_version;
|
2021-08-19 07:48:16 -07:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: Implement in_subroutine
|
|
|
|
* TODO: shader_glsl_generate_shader_epilogue(generator);
|
|
|
|
*/
|
|
|
|
if (version->major >= 4)
|
|
|
|
{
|
2021-07-13 15:11:32 -07:00
|
|
|
shader_glsl_print_indent(gen->buffer, gen->indent);
|
|
|
|
vkd3d_string_buffer_printf(gen->buffer, "return;\n");
|
2021-08-19 07:48:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-13 15:11:32 -07:00
|
|
|
static void vkd3d_glsl_handle_instruction(struct vkd3d_glsl_generator *gen,
|
|
|
|
const struct vkd3d_shader_instruction *ins)
|
2021-08-19 07:48:13 -07:00
|
|
|
{
|
2021-07-13 15:11:32 -07:00
|
|
|
gen->location = ins->location;
|
2023-11-17 09:13:02 -08:00
|
|
|
|
2021-07-13 15:11:32 -07:00
|
|
|
switch (ins->opcode)
|
2021-08-19 07:48:13 -07:00
|
|
|
{
|
2021-08-19 07:48:14 -07:00
|
|
|
case VKD3DSIH_DCL_INPUT:
|
2021-08-19 07:48:17 -07:00
|
|
|
case VKD3DSIH_DCL_OUTPUT:
|
2021-08-19 07:48:15 -07:00
|
|
|
case VKD3DSIH_DCL_OUTPUT_SIV:
|
2024-02-24 07:28:41 -08:00
|
|
|
case VKD3DSIH_NOP:
|
2021-08-19 07:48:14 -07:00
|
|
|
break;
|
2021-07-13 15:11:32 -07:00
|
|
|
case VKD3DSIH_MOV:
|
|
|
|
shader_glsl_mov(gen, ins);
|
|
|
|
break;
|
2021-08-19 07:48:16 -07:00
|
|
|
case VKD3DSIH_RET:
|
2021-07-13 15:11:32 -07:00
|
|
|
shader_glsl_ret(gen, ins);
|
2021-08-19 07:48:16 -07:00
|
|
|
break;
|
2021-08-19 07:48:13 -07:00
|
|
|
default:
|
2021-07-13 15:11:32 -07:00
|
|
|
shader_glsl_unhandled(gen, ins);
|
2021-08-19 07:48:13 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 08:32:31 -07:00
|
|
|
static void shader_glsl_generate_declarations(struct vkd3d_glsl_generator *gen)
|
|
|
|
{
|
|
|
|
const struct vsir_program *program = gen->program;
|
|
|
|
struct vkd3d_string_buffer *buffer = gen->buffer;
|
|
|
|
|
|
|
|
if (program->temp_count)
|
|
|
|
vkd3d_string_buffer_printf(buffer, "vec4 r[%u];\n\n", program->temp_count);
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:28:22 -07:00
|
|
|
static int vkd3d_glsl_generator_generate(struct vkd3d_glsl_generator *gen, struct vkd3d_shader_code *out)
|
2021-08-19 07:48:13 -07:00
|
|
|
{
|
2021-10-21 10:28:22 -07:00
|
|
|
const struct vkd3d_shader_instruction_array *instructions = &gen->program->instructions;
|
2021-07-13 15:11:32 -07:00
|
|
|
struct vkd3d_string_buffer *buffer = gen->buffer;
|
2023-01-19 18:24:20 -08:00
|
|
|
unsigned int i;
|
2021-08-19 07:48:13 -07:00
|
|
|
void *code;
|
|
|
|
|
2024-07-18 06:17:33 -07:00
|
|
|
MESSAGE("Generating a GLSL shader. This is unsupported; you get to keep all the pieces if it breaks.\n");
|
2024-03-07 11:55:39 -08:00
|
|
|
|
2021-10-21 10:28:22 -07:00
|
|
|
vkd3d_string_buffer_printf(buffer, "#version 440\n\n");
|
2021-08-19 07:48:13 -07:00
|
|
|
|
2021-10-21 10:28:22 -07:00
|
|
|
vkd3d_string_buffer_printf(buffer, "/* Generated by %s. */\n\n", vkd3d_shader_get_version(NULL, NULL));
|
|
|
|
|
2021-08-23 08:32:31 -07:00
|
|
|
shader_glsl_generate_declarations(gen);
|
|
|
|
|
2021-10-21 10:28:22 -07:00
|
|
|
vkd3d_string_buffer_printf(buffer, "void main()\n{\n");
|
|
|
|
|
|
|
|
++gen->indent;
|
2023-11-17 09:39:48 -08:00
|
|
|
for (i = 0; i < instructions->count; ++i)
|
2021-08-19 07:48:13 -07:00
|
|
|
{
|
2021-10-21 10:28:22 -07:00
|
|
|
vkd3d_glsl_handle_instruction(gen, &instructions->elements[i]);
|
2021-08-19 07:48:13 -07:00
|
|
|
}
|
|
|
|
|
2021-10-21 10:28:22 -07:00
|
|
|
vkd3d_string_buffer_printf(buffer, "}\n");
|
2023-11-16 21:59:34 -08:00
|
|
|
|
|
|
|
if (TRACE_ON())
|
2021-10-21 10:28:22 -07:00
|
|
|
vkd3d_string_buffer_trace(buffer);
|
2023-11-16 21:59:34 -08:00
|
|
|
|
2021-10-21 10:28:22 -07:00
|
|
|
if (gen->failed)
|
2021-10-06 08:11:45 -07:00
|
|
|
return VKD3D_ERROR_INVALID_SHADER;
|
2021-08-19 07:48:13 -07:00
|
|
|
|
2021-10-21 10:28:22 -07:00
|
|
|
if ((code = vkd3d_malloc(buffer->buffer_size)))
|
2021-08-19 07:48:13 -07:00
|
|
|
{
|
2021-10-21 10:28:22 -07:00
|
|
|
memcpy(code, buffer->buffer, buffer->content_size);
|
|
|
|
out->size = buffer->content_size;
|
2021-08-19 07:48:13 -07:00
|
|
|
out->code = code;
|
|
|
|
}
|
|
|
|
else return VKD3D_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return VKD3D_OK;
|
|
|
|
}
|
|
|
|
|
2023-11-17 09:36:33 -08:00
|
|
|
static void vkd3d_glsl_generator_cleanup(struct vkd3d_glsl_generator *gen)
|
2021-08-19 07:48:13 -07:00
|
|
|
{
|
2021-07-13 15:11:32 -07:00
|
|
|
vkd3d_string_buffer_release(&gen->string_buffers, gen->buffer);
|
|
|
|
vkd3d_string_buffer_cache_cleanup(&gen->string_buffers);
|
2023-11-17 09:36:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vkd3d_glsl_generator_init(struct vkd3d_glsl_generator *gen,
|
2023-11-17 09:39:48 -08:00
|
|
|
struct vsir_program *program, struct vkd3d_shader_message_context *message_context)
|
2023-11-17 09:36:33 -08:00
|
|
|
{
|
|
|
|
memset(gen, 0, sizeof(*gen));
|
2023-11-17 09:39:48 -08:00
|
|
|
gen->program = program;
|
2021-07-13 15:11:32 -07:00
|
|
|
vkd3d_string_buffer_cache_init(&gen->string_buffers);
|
|
|
|
gen->buffer = vkd3d_string_buffer_get(&gen->string_buffers);
|
2023-11-17 09:36:33 -08:00
|
|
|
gen->message_context = message_context;
|
|
|
|
}
|
|
|
|
|
2024-02-24 06:13:06 -08:00
|
|
|
int glsl_compile(struct vsir_program *program, uint64_t config_flags,
|
|
|
|
const struct vkd3d_shader_compile_info *compile_info, struct vkd3d_shader_code *out,
|
2023-11-17 09:36:33 -08:00
|
|
|
struct vkd3d_shader_message_context *message_context)
|
|
|
|
{
|
|
|
|
struct vkd3d_glsl_generator generator;
|
|
|
|
int ret;
|
|
|
|
|
2024-09-05 11:20:03 -07:00
|
|
|
if ((ret = vsir_program_transform(program, config_flags, compile_info, message_context)) < 0)
|
2024-02-24 06:13:06 -08:00
|
|
|
return ret;
|
|
|
|
|
2023-11-17 09:39:48 -08:00
|
|
|
vkd3d_glsl_generator_init(&generator, program, message_context);
|
|
|
|
ret = vkd3d_glsl_generator_generate(&generator, out);
|
2023-11-17 09:36:33 -08:00
|
|
|
vkd3d_glsl_generator_cleanup(&generator);
|
|
|
|
|
|
|
|
return ret;
|
2021-08-19 07:48:13 -07:00
|
|
|
}
|