svga: add a helper function for common shader creation

This patch refactors common shader creation code into a helper function.

Reviewed-by: Neha Bhende <bhenden@vmware.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16501>
This commit is contained in:
Charmaine Lee 2022-02-04 12:56:21 -08:00 committed by Marge Bot
parent 65fce0f813
commit ed77ac1eef
6 changed files with 93 additions and 135 deletions

View File

@ -1,5 +1,5 @@
/**********************************************************
* Copyright 2008-2009 VMware, Inc. All rights reserved.
* Copyright 2008-2022 VMware, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
@ -45,13 +45,13 @@ svga_create_fs_state(struct pipe_context *pipe,
struct svga_context *svga = svga_context(pipe);
struct svga_fragment_shader *fs;
fs = CALLOC_STRUCT(svga_fragment_shader);
if (!fs)
return NULL;
SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEFS);
fs->base.tokens = pipe_shader_state_to_tgsi_tokens(pipe->screen, templ);
fs = (struct svga_fragment_shader *)
svga_create_shader(pipe, templ, PIPE_SHADER_FRAGMENT,
sizeof(struct svga_fragment_shader));
if (!fs)
goto done;
/* Original shader IR could have been deleted if it is converted from
* NIR to TGSI. So need to explicitly set the shader state type to TGSI
@ -61,18 +61,12 @@ svga_create_fs_state(struct pipe_context *pipe,
tmp.type = PIPE_SHADER_IR_TGSI;
tmp.tokens = fs->base.tokens;
/* Collect basic info that we'll need later:
*/
tgsi_scan_shader(fs->base.tokens, &fs->base.info);
fs->base.id = svga->debug.shader_id++;
fs->generic_inputs = svga_get_generic_inputs_mask(&fs->base.info);
svga_remap_generics(fs->generic_inputs, fs->generic_remap_table);
fs->draw_shader = draw_create_fragment_shader(svga->swtnl.draw, &tmp);
done:
SVGA_STATS_TIME_POP(svga_sws(svga));
return fs;
}

View File

@ -1,5 +1,5 @@
/**********************************************************
* Copyright 2014 VMware, Inc. All rights reserved.
* Copyright 2014-2022 VMware, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
@ -42,18 +42,16 @@ svga_create_gs_state(struct pipe_context *pipe,
const struct pipe_shader_state *templ)
{
struct svga_context *svga = svga_context(pipe);
struct svga_geometry_shader *gs = CALLOC_STRUCT(svga_geometry_shader);
if (!gs)
return NULL;
struct svga_geometry_shader *gs;
SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEGS);
gs->base.tokens = pipe_shader_state_to_tgsi_tokens(pipe->screen, templ);
gs = (struct svga_geometry_shader *)
svga_create_shader(pipe, templ, PIPE_SHADER_GEOMETRY,
sizeof(struct svga_geometry_shader));
/* Collect basic info that we'll need later:
*/
tgsi_scan_shader(gs->base.tokens, &gs->base.info);
if (!gs)
goto done;
/* Original shader IR could have been deleted if it is converted from
* NIR to TGSI. So need to explicitly set the shader state type to TGSI
@ -65,16 +63,9 @@ svga_create_gs_state(struct pipe_context *pipe,
gs->draw_shader = draw_create_geometry_shader(svga->swtnl.draw, &tmp);
gs->base.id = svga->debug.shader_id++;
gs->generic_outputs = svga_get_generic_outputs_mask(&gs->base.info);
/* check for any stream output declarations */
if (templ->stream_output.num_outputs) {
gs->base.stream_output = svga_create_stream_output(svga, &gs->base,
&templ->stream_output);
}
done:
SVGA_STATS_TIME_POP(svga_sws(svga));
return gs;
}

View File

@ -1,5 +1,5 @@
/**********************************************************
* Copyright 2018-2020 VMware, Inc. All rights reserved.
* Copyright 2018-2022 VMware, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
@ -61,25 +61,19 @@ static void *
svga_create_tcs_state(struct pipe_context *pipe,
const struct pipe_shader_state *templ)
{
struct svga_context *svga = svga_context(pipe);
struct svga_tcs_shader *tcs;
tcs = CALLOC_STRUCT(svga_tcs_shader);
if (!tcs)
return NULL;
SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATETCS);
tcs->base.tokens = pipe_shader_state_to_tgsi_tokens(pipe->screen, templ);
/* Collect basic info that we'll need later:
*/
tgsi_scan_shader(tcs->base.tokens, &tcs->base.info);
tcs->base.id = svga->debug.shader_id++;
tcs = (struct svga_tcs_shader *)
svga_create_shader(pipe, templ, PIPE_SHADER_TESS_CTRL,
sizeof(struct svga_tcs_shader));
if (!tcs)
goto done;
tcs->generic_outputs = svga_get_generic_outputs_mask(&tcs->base.info);
done:
SVGA_STATS_TIME_POP(svga_sws(svga));
return tcs;
}
@ -145,25 +139,20 @@ static void *
svga_create_tes_state(struct pipe_context *pipe,
const struct pipe_shader_state *templ)
{
struct svga_context *svga = svga_context(pipe);
struct svga_tes_shader *tes;
tes = CALLOC_STRUCT(svga_tes_shader);
if (!tes)
return NULL;
SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATETES);
tes->base.tokens = pipe_shader_state_to_tgsi_tokens(pipe->screen, templ);
tes = (struct svga_tes_shader *)
svga_create_shader(pipe, templ, PIPE_SHADER_TESS_EVAL,
sizeof(struct svga_tes_shader));
/* Collect basic info that we'll need later:
*/
tgsi_scan_shader(tes->base.tokens, &tes->base.info);
tes->base.id = svga->debug.shader_id++;
if (!tes)
goto done;
tes->generic_inputs = svga_get_generic_inputs_mask(&tes->base.info);
done:
SVGA_STATS_TIME_POP(svga_sws(svga));
return tes;
}

View File

@ -1,5 +1,5 @@
/**********************************************************
* Copyright 2008-2009 VMware, Inc. All rights reserved.
* Copyright 2008-2022 VMware, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
@ -40,82 +40,22 @@
#include "svga_streamout.h"
/**
* Substitute a debug shader.
*/
static const struct tgsi_token *
substitute_vs(unsigned shader_id, const struct tgsi_token *old_tokens)
{
#if 0
FREE(old_tokens);
if (shader_id == 12) {
static struct tgsi_token tokens[300];
const char *text =
"VERT\n"
"DCL IN[0]\n"
"DCL IN[1]\n"
"DCL IN[2]\n"
"DCL OUT[0], POSITION\n"
"DCL TEMP[0..4]\n"
"IMM FLT32 { 1.0000, 1.0000, 1.0000, 1.0000 }\n"
"IMM FLT32 { 0.45, 1.0000, 1.0000, 1.0000 }\n"
"IMM FLT32 { 1.297863, 0.039245, 0.035993, 0.035976}\n"
"IMM FLT32 { -0.019398, 1.696131, -0.202151, -0.202050 }\n"
"IMM FLT32 { 0.051711, -0.348713, -0.979204, -0.978714 }\n"
"IMM FLT32 { 0.000000, 0.000003, 139.491577, 141.421356 }\n"
"DCL CONST[0..7]\n"
"DCL CONST[9..16]\n"
" MOV TEMP[2], IMM[0]\n"
" MOV TEMP[2].xyz, IN[2]\n"
" MOV TEMP[2].xyz, IN[0]\n"
" MOV TEMP[2].xyz, IN[1]\n"
" MUL TEMP[1], IMM[3], TEMP[2].yyyy\n"
" MAD TEMP[3], IMM[2], TEMP[2].xxxx, TEMP[1]\n"
" MAD TEMP[1], IMM[4], TEMP[2].zzzz, TEMP[3]\n"
" MAD TEMP[4], IMM[5], TEMP[2].wwww, TEMP[1]\n"
" MOV OUT[0], TEMP[4]\n"
" END\n";
if (!tgsi_text_translate(text,
tokens,
ARRAY_SIZE(tokens)))
{
assert(0);
return NULL;
}
return tokens;
}
#endif
return old_tokens;
}
static void *
svga_create_vs_state(struct pipe_context *pipe,
const struct pipe_shader_state *templ)
{
struct svga_context *svga = svga_context(pipe);
struct svga_vertex_shader *vs = CALLOC_STRUCT(svga_vertex_shader);
struct svga_vertex_shader *vs;
SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEVS);
vs = (struct svga_vertex_shader *)
svga_create_shader(pipe, templ, PIPE_SHADER_VERTEX,
sizeof(struct svga_vertex_shader));
if (!vs)
return NULL;
goto done;
SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATEVS);
/* substitute a debug shader?
*/
vs->base.tokens = substitute_vs(svga->debug.shader_id,
pipe_shader_state_to_tgsi_tokens(pipe->screen, templ));
/* Collect basic info that we'll need later:
*/
tgsi_scan_shader(vs->base.tokens, &vs->base.info);
vs->generic_outputs = svga_get_generic_outputs_mask(&vs->base.info);
{
/* Need to do construct a new template in case we substituted a
@ -129,16 +69,7 @@ svga_create_vs_state(struct pipe_context *pipe,
vs->draw_shader = draw_create_vertex_shader(svga->swtnl.draw, &tmp2);
}
vs->base.id = svga->debug.shader_id++;
vs->generic_outputs = svga_get_generic_outputs_mask(&vs->base.info);
/* check for any stream output declarations */
if (templ->stream_output.num_outputs) {
vs->base.stream_output = svga_create_stream_output(svga, &vs->base,
&templ->stream_output);
}
done:
SVGA_STATS_TIME_POP(svga_sws(svga));
return vs;
}

View File

@ -1,5 +1,5 @@
/**********************************************************
* Copyright 2008-2012 VMware, Inc. All rights reserved.
* Copyright 2008-2022 VMware, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
@ -32,6 +32,9 @@
#include "svga_shader.h"
#include "svga_resource_texture.h"
#include "VGPU10ShaderTokens.h"
#include "tgsi/tgsi_parse.h"
#include "tgsi/tgsi_text.h"
#include "nir/nir_to_tgsi.h"
/**
@ -901,3 +904,43 @@ svga_rebind_shaders(struct svga_context *svga)
return PIPE_OK;
}
/**
* Helper function to create a shader object.
*/
struct svga_shader *
svga_create_shader(struct pipe_context *pipe,
const struct pipe_shader_state *templ,
enum pipe_shader_type stage,
unsigned shader_structlen)
{
struct svga_context *svga = svga_context(pipe);
struct svga_shader *shader = CALLOC(1, shader_structlen);
if (shader == NULL)
return NULL;
shader->id = svga->debug.shader_id++;
shader->type = templ->type;
shader->stage = stage;
shader->tokens = pipe_shader_state_to_tgsi_tokens(pipe->screen, templ);
if (shader->type == PIPE_SHADER_IR_TGSI) {
/* Collect basic info that we'll need later */
tgsi_scan_shader(shader->tokens, &shader->info);
}
else {
debug_printf("Unexpected nir shader\n");
assert(0);
}
/* check for any stream output declarations */
if (templ->stream_output.num_outputs) {
shader->stream_output = svga_create_stream_output(svga, shader,
&templ->stream_output);
}
return shader;
}

View File

@ -1,5 +1,5 @@
/**********************************************************
* Copyright 2008-2012 VMware, Inc. All rights reserved.
* Copyright 2008-2022 VMware, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
@ -298,6 +298,10 @@ struct svga_cs_variant
struct svga_shader
{
enum pipe_shader_ir type; /* IR type */
enum pipe_shader_type stage; /* shader stage */
/* TGSI */
const struct tgsi_token *tokens;
struct svga_token_key token_key; /* token key for the token string */
struct tgsi_shader_info info;
@ -422,6 +426,12 @@ struct svga_shader *
svga_search_shader_token_key(struct svga_shader *shader,
const struct svga_token_key *key);
struct svga_shader *
svga_create_shader(struct pipe_context *pipe,
const struct pipe_shader_state *templ,
enum pipe_shader_type stage,
unsigned len);
enum pipe_error
svga_define_shader(struct svga_context *svga,
struct svga_shader_variant *variant);