svga: refactor some shader code

Put common code in new svga_shader.c file.  Considate separate vertex/
fragment shader ID generation.

Reviewed-by: Jose Fonseca <jfonseca@vmware.com>
This commit is contained in:
Brian Paul 2014-01-31 17:23:11 -07:00
parent 9bace99d77
commit 4686f610b1
11 changed files with 171 additions and 76 deletions

View File

@ -24,6 +24,7 @@ C_SOURCES := \
svga_pipe_vs.c \
svga_screen.c \
svga_screen_cache.c \
svga_shader.c \
svga_state.c \
svga_state_need_swtnl.c \
svga_state_constants.c \

View File

@ -68,8 +68,7 @@ static void svga_destroy( struct pipe_context *pipe )
svga_destroy_swtnl( svga );
util_bitmask_destroy( svga->vs_bm );
util_bitmask_destroy( svga->fs_bm );
util_bitmask_destroy( svga->shader_id_bm );
for(shader = 0; shader < PIPE_SHADER_TYPES; ++shader)
pipe_resource_reference( &svga->curr.cb[shader], NULL );
@ -124,13 +123,9 @@ struct pipe_context *svga_context_create( struct pipe_screen *screen,
svga->debug.no_line_width = debug_get_option_no_line_width();
svga->debug.force_hw_line_stipple = debug_get_option_force_hw_line_stipple();
svga->fs_bm = util_bitmask_create();
if (svga->fs_bm == NULL)
goto no_fs_bm;
svga->vs_bm = util_bitmask_create();
if (svga->vs_bm == NULL)
goto no_vs_bm;
svga->shader_id_bm = util_bitmask_create();
if (svga->shader_id_bm == NULL)
goto no_shader_bm;
svga->hwtnl = svga_hwtnl_create(svga);
if (svga->hwtnl == NULL)
@ -164,10 +159,8 @@ no_state:
no_swtnl:
svga_hwtnl_destroy( svga->hwtnl );
no_hwtnl:
util_bitmask_destroy( svga->vs_bm );
no_vs_bm:
util_bitmask_destroy( svga->fs_bm );
no_fs_bm:
util_bitmask_destroy( svga->shader_id_bm );
no_shader_bm:
svga->swc->destroy(svga->swc);
no_swc:
FREE(svga);

View File

@ -347,8 +347,7 @@ struct svga_context
} swtnl;
/* Bitmask of used shader IDs */
struct util_bitmask *fs_bm;
struct util_bitmask *vs_bm;
struct util_bitmask *shader_id_bm;
struct {
unsigned dirty[SVGA_STATE_MAX];

View File

@ -35,6 +35,7 @@
#include "svga_hw_reg.h"
#include "svga_cmd.h"
#include "svga_debug.h"
#include "svga_shader.h"
static void *
@ -98,17 +99,8 @@ svga_delete_fs_state(struct pipe_context *pipe, void *shader)
for (variant = fs->base.variants; variant; variant = tmp) {
tmp = variant->next;
ret = SVGA3D_DestroyShader(svga->swc, variant->id, SVGA3D_SHADERTYPE_PS);
if (ret != PIPE_OK) {
svga_context_flush(svga, NULL);
ret = SVGA3D_DestroyShader(svga->swc, variant->id,
SVGA3D_SHADERTYPE_PS);
assert(ret == PIPE_OK);
}
util_bitmask_clear(svga->fs_bm, variant->id);
svga_destroy_shader_variant(variant);
ret = svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_PS, variant);
(void) ret; /* PIPE_ERROR_ not handled yet */
/*
* Remove stale references to this variant to ensure a new variant on the

View File

@ -36,6 +36,7 @@
#include "svga_hw_reg.h"
#include "svga_cmd.h"
#include "svga_debug.h"
#include "svga_shader.h"
/**
@ -158,17 +159,8 @@ svga_delete_vs_state(struct pipe_context *pipe, void *shader)
for (variant = vs->base.variants; variant; variant = tmp) {
tmp = variant->next;
ret = SVGA3D_DestroyShader(svga->swc, variant->id, SVGA3D_SHADERTYPE_VS);
if (ret != PIPE_OK) {
svga_context_flush(svga, NULL);
ret = SVGA3D_DestroyShader(svga->swc, variant->id,
SVGA3D_SHADERTYPE_VS);
assert(ret == PIPE_OK);
}
util_bitmask_clear(svga->vs_bm, variant->id);
svga_destroy_shader_variant(variant);
ret = svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_VS, variant);
(void) ret; /* PIPE_ERROR_ not handled yet */
/*
* Remove stale references to this variant to ensure a new variant on the

View File

@ -0,0 +1,101 @@
/**********************************************************
* Copyright 2008-2012 VMware, Inc. All rights reserved.
*
* 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 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 "util/u_bitmask.h"
#include "util/u_memory.h"
#include "svga_context.h"
#include "svga_cmd.h"
#include "svga_shader.h"
/**
* Issue the SVGA3D commands to define a new shader.
* \param result contains the shader tokens, etc. The result->id field will
* be set here.
*/
enum pipe_error
svga_define_shader(struct svga_context *svga,
SVGA3dShaderType type,
struct svga_shader_variant *variant)
{
unsigned codeLen = variant->nr_tokens * sizeof(variant->tokens[0]);
{
enum pipe_error ret;
/* Allocate an integer ID for the shader */
variant->id = util_bitmask_add(svga->shader_id_bm);
if (variant->id == UTIL_BITMASK_INVALID_INDEX) {
return PIPE_ERROR_OUT_OF_MEMORY;
}
/* Issue SVGA3D device command to define the shader */
ret = SVGA3D_DefineShader(svga->swc,
variant->id,
type,
variant->tokens,
codeLen);
if (ret != PIPE_OK) {
/* free the ID */
assert(variant->id != UTIL_BITMASK_INVALID_INDEX);
util_bitmask_clear(svga->shader_id_bm, variant->id);
variant->id = UTIL_BITMASK_INVALID_INDEX;
return ret;
}
}
return PIPE_OK;
}
enum pipe_error
svga_destroy_shader_variant(struct svga_context *svga,
SVGA3dShaderType type,
struct svga_shader_variant *variant)
{
enum pipe_error ret = PIPE_OK;
/* first try */
if (variant->id != UTIL_BITMASK_INVALID_INDEX) {
ret = SVGA3D_DestroyShader(svga->swc, variant->id, type);
if (ret != PIPE_OK) {
/* flush and try again */
svga_context_flush(svga, NULL);
ret = SVGA3D_DestroyShader(svga->swc, variant->id, type);
assert(ret == PIPE_OK);
}
util_bitmask_clear(svga->shader_id_bm, variant->id);
}
FREE((unsigned *)variant->tokens);
FREE(variant);
return ret;
}

View File

@ -0,0 +1,44 @@
/**********************************************************
* Copyright 2008-2012 VMware, Inc. All rights reserved.
*
* 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 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 SVGA_SHADER_H
#define SVGA_SHADER_H
#include "svga3d_reg.h"
struct svga_shader_variant;
enum pipe_error
svga_define_shader(struct svga_context *svga,
SVGA3dShaderType type,
struct svga_shader_variant *variant);
enum pipe_error
svga_destroy_shader_variant(struct svga_context *svga,
SVGA3dShaderType type,
struct svga_shader_variant *variant);
#endif /* SVGA_SHADER_H */

View File

@ -33,6 +33,7 @@
#include "svga_context.h"
#include "svga_state.h"
#include "svga_cmd.h"
#include "svga_shader.h"
#include "svga_resource_texture.h"
#include "svga_tgsi.h"
@ -134,30 +135,21 @@ compile_fs(struct svga_context *svga,
}
}
variant->id = util_bitmask_add(svga->fs_bm);
if(variant->id == UTIL_BITMASK_INVALID_INDEX) {
ret = PIPE_ERROR_OUT_OF_MEMORY;
goto fail;
}
ret = SVGA3D_DefineShader(svga->swc,
variant->id,
SVGA3D_SHADERTYPE_PS,
variant->tokens,
variant->nr_tokens * sizeof variant->tokens[0]);
ret = svga_define_shader(svga, SVGA3D_SHADERTYPE_PS, variant);
if (ret != PIPE_OK)
goto fail;
*out_variant = variant;
/* insert variants at head of linked list */
variant->next = fs->base.variants;
fs->base.variants = variant;
return PIPE_OK;
fail:
if (variant) {
if (variant->id != UTIL_BITMASK_INVALID_INDEX)
util_bitmask_clear( svga->fs_bm, variant->id );
svga_destroy_shader_variant( variant );
svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_PS, variant);
}
return ret;
}

View File

@ -35,6 +35,7 @@
#include "svga_context.h"
#include "svga_state.h"
#include "svga_cmd.h"
#include "svga_shader.h"
#include "svga_tgsi.h"
#include "svga_hw_reg.h"
@ -128,30 +129,21 @@ compile_vs(struct svga_context *svga,
}
}
variant->id = util_bitmask_add(svga->vs_bm);
if(variant->id == UTIL_BITMASK_INVALID_INDEX) {
ret = PIPE_ERROR_OUT_OF_MEMORY;
goto fail;
}
ret = SVGA3D_DefineShader(svga->swc,
variant->id,
SVGA3D_SHADERTYPE_VS,
variant->tokens,
variant->nr_tokens * sizeof variant->tokens[0]);
ret = svga_define_shader(svga, SVGA3D_SHADERTYPE_VS, variant);
if (ret != PIPE_OK)
goto fail;
*out_variant = variant;
/* insert variants at head of linked list */
variant->next = vs->base.variants;
vs->base.variants = variant;
return PIPE_OK;
fail:
if (variant) {
if (variant->id != UTIL_BITMASK_INVALID_INDEX)
util_bitmask_clear( svga->vs_bm, variant->id );
svga_destroy_shader_variant( variant );
svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_VS, variant);
}
return ret;
}

View File

@ -381,11 +381,3 @@ svga_translate_vertex_program(const struct svga_vertex_shader *vs,
return svga_tgsi_translate(&vs->base, &key, PIPE_SHADER_VERTEX);
}
void
svga_destroy_shader_variant(struct svga_shader_variant *variant)
{
FREE((unsigned *) variant->tokens);
FREE(variant);
}

View File

@ -154,9 +154,6 @@ svga_translate_vertex_program( const struct svga_vertex_shader *fs,
const struct svga_vs_compile_key *vkey );
void
svga_destroy_shader_variant(struct svga_shader_variant *variant);
unsigned
svga_get_generic_inputs_mask(const struct tgsi_shader_info *info);