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"
|
|
|
|
|
|
|
|
struct vkd3d_glsl_generator
|
|
|
|
{
|
2023-11-17 09:39:48 -08:00
|
|
|
struct vsir_program *program;
|
2021-08-19 07:48:13 -07:00
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-08-19 07:48:16 -07:00
|
|
|
static void shader_glsl_ret(struct vkd3d_glsl_generator *generator,
|
|
|
|
const struct vkd3d_shader_instruction *ins)
|
|
|
|
{
|
2023-11-17 09:39:48 -08:00
|
|
|
const struct vkd3d_shader_version *version = &generator->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)
|
|
|
|
{
|
|
|
|
vkd3d_string_buffer_printf(&generator->buffer, "return;\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 07:48:13 -07:00
|
|
|
static void vkd3d_glsl_handle_instruction(struct vkd3d_glsl_generator *generator,
|
|
|
|
const struct vkd3d_shader_instruction *instruction)
|
|
|
|
{
|
2023-11-17 09:13:02 -08:00
|
|
|
generator->location = instruction->location;
|
|
|
|
|
2021-08-19 07:48:13 -07:00
|
|
|
switch (instruction->handler_idx)
|
|
|
|
{
|
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:
|
2021-08-19 07:48:14 -07:00
|
|
|
break;
|
2021-08-19 07:48:16 -07:00
|
|
|
case VKD3DSIH_RET:
|
|
|
|
shader_glsl_ret(generator, instruction);
|
|
|
|
break;
|
2021-08-19 07:48:13 -07:00
|
|
|
default:
|
|
|
|
vkd3d_glsl_compiler_error(generator,
|
|
|
|
VKD3D_SHADER_ERROR_GLSL_INTERNAL,
|
|
|
|
"Unhandled instruction %#x", instruction->handler_idx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 09:39:48 -08:00
|
|
|
static int vkd3d_glsl_generator_generate(struct vkd3d_glsl_generator *generator, struct vkd3d_shader_code *out)
|
2021-08-19 07:48:13 -07:00
|
|
|
{
|
2023-11-17 09:39:48 -08:00
|
|
|
const struct vkd3d_shader_instruction_array *instructions = &generator->program->instructions;
|
2023-01-19 18:24:20 -08:00
|
|
|
unsigned int i;
|
2021-08-19 07:48:13 -07:00
|
|
|
void *code;
|
|
|
|
|
2024-03-07 11:55:39 -08:00
|
|
|
ERR("Generating a GLSL shader. This is unsupported; you get to keep all the pieces if it breaks.\n");
|
|
|
|
|
2021-08-19 07:48:13 -07:00
|
|
|
vkd3d_string_buffer_printf(&generator->buffer, "#version 440\n\n");
|
|
|
|
vkd3d_string_buffer_printf(&generator->buffer, "void main()\n{\n");
|
|
|
|
|
2023-11-17 09:39:48 -08:00
|
|
|
for (i = 0; i < instructions->count; ++i)
|
2021-08-19 07:48:13 -07:00
|
|
|
{
|
2023-11-17 09:39:48 -08:00
|
|
|
vkd3d_glsl_handle_instruction(generator, &instructions->elements[i]);
|
2021-08-19 07:48:13 -07:00
|
|
|
}
|
|
|
|
|
2023-01-19 18:24:20 -08:00
|
|
|
if (generator->failed)
|
2021-10-06 08:11:45 -07:00
|
|
|
return VKD3D_ERROR_INVALID_SHADER;
|
2021-08-19 07:48:13 -07:00
|
|
|
|
|
|
|
vkd3d_string_buffer_printf(&generator->buffer, "}\n");
|
|
|
|
|
|
|
|
if ((code = vkd3d_malloc(generator->buffer.buffer_size)))
|
|
|
|
{
|
|
|
|
memcpy(code, generator->buffer.buffer, generator->buffer.content_size);
|
|
|
|
out->size = generator->buffer.content_size;
|
|
|
|
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
|
|
|
{
|
2023-11-17 09:36:33 -08:00
|
|
|
vkd3d_string_buffer_cleanup(&gen->buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-11-17 09:36:33 -08:00
|
|
|
vkd3d_string_buffer_init(&gen->buffer);
|
|
|
|
gen->message_context = message_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
int glsl_compile(struct vsir_program *program, struct vkd3d_shader_code *out,
|
|
|
|
struct vkd3d_shader_message_context *message_context)
|
|
|
|
{
|
|
|
|
struct vkd3d_glsl_generator generator;
|
|
|
|
int 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
|
|
|
}
|