st/glsl: move linking code to the same st file

Since they call one another this makes it easier to see what is
going on without looking in multiple files.

Reviewed-by: Marek Olšák <marek.olsak@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/23541>
This commit is contained in:
Timothy Arceri 2023-06-09 14:49:01 +10:00 committed by Marge Bot
parent 92dcaf7deb
commit a337a0c807
8 changed files with 93 additions and 170 deletions

View file

@ -69,6 +69,7 @@
#include "api_exec_decl.h"
#include "state_tracker/st_context.h"
#include "state_tracker/st_glsl_to_ir.h"
#include "state_tracker/st_program.h"
#ifdef ENABLE_SHADER_CACHE
@ -1332,7 +1333,7 @@ link_program(struct gl_context *ctx, struct gl_shader_program *shProg,
ensure_builtin_types(ctx);
FLUSH_VERTICES(ctx, 0, 0);
_mesa_glsl_link_shader(ctx, shProg);
st_link_shader(ctx, shProg);
/* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
*

View file

@ -29,7 +29,6 @@
#include "util/glheader.h"
#include "compiler/shader_enums.h"
#include "program/link_program.h"
#include "util/macros.h"

View file

@ -138,7 +138,6 @@
#include <mesa/math/m_eval.h>
#include <mesa/math/m_matrix.h>
#include <mesa/program/arbprogparse.h>
#include <mesa/program/link_program.h>
#include <mesa/program/program.h>
#include <mesa/program/program_parser.h>
#include <mesa/program/prog_cache.h>

View file

@ -262,8 +262,6 @@ files_libmesa = files(
'math/m_matrix.h',
'program/arbprogparse.c',
'program/arbprogparse.h',
'program/link_program.cpp',
'program/link_program.h',
'program/prog_cache.c',
'program/prog_cache.h',
'program/prog_instruction.c',

View file

@ -1,122 +0,0 @@
/*
* Copyright (C) 2005-2007 Brian Paul All Rights Reserved.
* Copyright (C) 2008 VMware, Inc. All Rights Reserved.
* Copyright © 2010 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "main/shaderapi.h"
#include "main/shaderobj.h"
#include "main/glspirv.h"
#include "compiler/glsl/glsl_parser_extras.h"
#include "compiler/glsl_types.h"
#include "compiler/glsl/linker.h"
#include "compiler/glsl/program.h"
#include "compiler/glsl/shader_cache.h"
#include "util/perf/cpu_trace.h"
#include "state_tracker/st_glsl_to_ir.h"
extern "C" {
/**
* Link a GLSL shader program. Called via glLinkProgram().
*/
void
_mesa_glsl_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
{
unsigned int i;
bool spirv = false;
MESA_TRACE_FUNC();
_mesa_clear_shader_program_data(ctx, prog);
prog->data = _mesa_create_shader_program_data();
prog->data->LinkStatus = LINKING_SUCCESS;
for (i = 0; i < prog->NumShaders; i++) {
if (!prog->Shaders[i]->CompileStatus) {
linker_error(prog, "linking with uncompiled/unspecialized shader");
}
if (!i) {
spirv = (prog->Shaders[i]->spirv_data != NULL);
} else if (spirv && !prog->Shaders[i]->spirv_data) {
/* The GL_ARB_gl_spirv spec adds a new bullet point to the list of
* reasons LinkProgram can fail:
*
* "All the shader objects attached to <program> do not have the
* same value for the SPIR_V_BINARY_ARB state."
*/
linker_error(prog,
"not all attached shaders have the same "
"SPIR_V_BINARY_ARB state");
}
}
prog->data->spirv = spirv;
if (prog->data->LinkStatus) {
if (!spirv)
link_shaders(ctx, prog);
else
_mesa_spirv_link_shaders(ctx, prog);
}
/* If LinkStatus is LINKING_SUCCESS, then reset sampler validated to true.
* Validation happens via the LinkShader call below. If LinkStatus is
* LINKING_SKIPPED, then SamplersValidated will have been restored from the
* shader cache.
*/
if (prog->data->LinkStatus == LINKING_SUCCESS) {
prog->SamplersValidated = GL_TRUE;
}
if (prog->data->LinkStatus && !st_link_shader(ctx, prog)) {
prog->data->LinkStatus = LINKING_FAILURE;
}
if (prog->data->LinkStatus != LINKING_FAILURE)
_mesa_create_program_resource_hash(prog);
/* Return early if we are loading the shader from on-disk cache */
if (prog->data->LinkStatus == LINKING_SKIPPED)
return;
if (ctx->_Shader->Flags & GLSL_DUMP) {
if (!prog->data->LinkStatus) {
fprintf(stderr, "GLSL shader program %d failed to link\n", prog->Name);
}
if (prog->data->InfoLog && prog->data->InfoLog[0] != 0) {
fprintf(stderr, "GLSL shader program %d info log:\n", prog->Name);
fprintf(stderr, "%s\n", prog->data->InfoLog);
}
}
#ifdef ENABLE_SHADER_CACHE
if (prog->data->LinkStatus)
shader_cache_write_program_metadata(ctx, prog);
#endif
}
} /* extern "C" */

View file

@ -1,40 +0,0 @@
/*
* Copyright © 2010 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef IR_TO_MESA_H
#define IR_TO_MESA_H
#ifdef __cplusplus
extern "C" {
#endif
struct gl_context;
struct gl_shader_program;
void _mesa_glsl_link_shader(struct gl_context *ctx, struct gl_shader_program *prog);
#ifdef __cplusplus
}
#endif
#endif /* IR_TO_MESA_H */

View file

@ -26,12 +26,18 @@
#include "compiler/glsl/glsl_parser_extras.h"
#include "compiler/glsl/ir_optimization.h"
#include "compiler/glsl/linker_util.h"
#include "compiler/glsl/program.h"
#include "compiler/glsl/shader_cache.h"
#include "st_nir.h"
#include "st_shader_cache.h"
#include "st_program.h"
#include "main/glspirv.h"
#include "main/shaderapi.h"
#include "main/shaderobj.h"
static GLboolean
link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
{
@ -100,8 +106,8 @@ extern "C" {
/**
* Link a shader.
*/
GLboolean
st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
static bool
st_link_glsl_to_nir(struct gl_context *ctx, struct gl_shader_program *prog)
{
struct pipe_context *pctx = st_context(ctx)->pipe;
@ -130,4 +136,86 @@ st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
return ret;
}
/**
* Link a GLSL shader program. Called via glLinkProgram().
*/
void
st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog)
{
unsigned int i;
bool spirv = false;
MESA_TRACE_FUNC();
_mesa_clear_shader_program_data(ctx, prog);
prog->data = _mesa_create_shader_program_data();
prog->data->LinkStatus = LINKING_SUCCESS;
for (i = 0; i < prog->NumShaders; i++) {
if (!prog->Shaders[i]->CompileStatus) {
linker_error(prog, "linking with uncompiled/unspecialized shader");
}
if (!i) {
spirv = (prog->Shaders[i]->spirv_data != NULL);
} else if (spirv && !prog->Shaders[i]->spirv_data) {
/* The GL_ARB_gl_spirv spec adds a new bullet point to the list of
* reasons LinkProgram can fail:
*
* "All the shader objects attached to <program> do not have the
* same value for the SPIR_V_BINARY_ARB state."
*/
linker_error(prog,
"not all attached shaders have the same "
"SPIR_V_BINARY_ARB state");
}
}
prog->data->spirv = spirv;
if (prog->data->LinkStatus) {
if (!spirv)
link_shaders(ctx, prog);
else
_mesa_spirv_link_shaders(ctx, prog);
}
/* If LinkStatus is LINKING_SUCCESS, then reset sampler validated to true.
* Validation happens via the LinkShader call below. If LinkStatus is
* LINKING_SKIPPED, then SamplersValidated will have been restored from the
* shader cache.
*/
if (prog->data->LinkStatus == LINKING_SUCCESS) {
prog->SamplersValidated = GL_TRUE;
}
if (prog->data->LinkStatus && !st_link_glsl_to_nir(ctx, prog)) {
prog->data->LinkStatus = LINKING_FAILURE;
}
if (prog->data->LinkStatus != LINKING_FAILURE)
_mesa_create_program_resource_hash(prog);
/* Return early if we are loading the shader from on-disk cache */
if (prog->data->LinkStatus == LINKING_SKIPPED)
return;
if (ctx->_Shader->Flags & GLSL_DUMP) {
if (!prog->data->LinkStatus) {
fprintf(stderr, "GLSL shader program %d failed to link\n", prog->Name);
}
if (prog->data->InfoLog && prog->data->InfoLog[0] != 0) {
fprintf(stderr, "GLSL shader program %d info log:\n", prog->Name);
fprintf(stderr, "%s\n", prog->data->InfoLog);
}
}
#ifdef ENABLE_SHADER_CACHE
if (prog->data->LinkStatus)
shader_cache_write_program_metadata(ctx, prog);
#endif
}
} /* extern "C" */

View file

@ -28,7 +28,7 @@
extern "C" {
#endif
GLboolean
void
st_link_shader(struct gl_context *ctx, struct gl_shader_program *prog);
#ifdef __cplusplus