Path: blob/21.2-virgl/src/gallium/winsys/svga/drm/vmw_shader.c
4573 views
/**********************************************************1* Copyright 2009-2015 VMware, Inc. All rights reserved.2*3* Permission is hereby granted, free of charge, to any person4* obtaining a copy of this software and associated documentation5* files (the "Software"), to deal in the Software without6* restriction, including without limitation the rights to use, copy,7* modify, merge, publish, distribute, sublicense, and/or sell copies8* of the Software, and to permit persons to whom the Software is9* furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice shall be12* included in all copies or substantial portions of the Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,15* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF16* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND17* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS18* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN19* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN20* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21* SOFTWARE.22*23**********************************************************/242526#include "svga_cmd.h"27#include "util/u_debug.h"28#include "util/u_memory.h"2930#include "vmw_context.h"31#include "vmw_shader.h"32#include "vmw_buffer.h"33#include "vmw_screen.h"3435void36vmw_svga_winsys_shader_reference(struct vmw_svga_winsys_shader **pdst,37struct vmw_svga_winsys_shader *src)38{39struct pipe_reference *src_ref;40struct pipe_reference *dst_ref;41struct vmw_svga_winsys_shader *dst;4243if(pdst == NULL || *pdst == src)44return;4546dst = *pdst;4748src_ref = src ? &src->refcnt : NULL;49dst_ref = dst ? &dst->refcnt : NULL;5051if (pipe_reference(dst_ref, src_ref)) {52struct svga_winsys_screen *sws = &dst->screen->base;5354if (!sws->have_vgpu10)55vmw_ioctl_shader_destroy(dst->screen, dst->shid);56#ifdef DEBUG57/* to detect dangling pointers */58assert(p_atomic_read(&dst->validated) == 0);59dst->shid = SVGA3D_INVALID_ID;60#endif61sws->buffer_destroy(sws, dst->buf);62FREE(dst);63}6465*pdst = src;66}676869/**70* A helper function to create a shader object and upload the71* shader bytecode and signature if specified to the shader memory.72*/73struct vmw_svga_winsys_shader *74vmw_svga_shader_create(struct svga_winsys_screen *sws,75SVGA3dShaderType type,76const uint32 *bytecode,77uint32 bytecodeLen,78const SVGA3dDXShaderSignatureHeader *sgnInfo,79uint32 sgnLen)80{81struct vmw_svga_winsys_shader *shader;82void *map;8384shader = CALLOC_STRUCT(vmw_svga_winsys_shader);85if (!shader)86return NULL;8788pipe_reference_init(&shader->refcnt, 1);89p_atomic_set(&shader->validated, 0);90shader->screen = vmw_winsys_screen(sws);91shader->buf = sws->buffer_create(sws, 64,92SVGA_BUFFER_USAGE_SHADER,93bytecodeLen + sgnLen);94if (!shader->buf) {95FREE(shader);96return NULL;97}9899map = sws->buffer_map(sws, shader->buf, PIPE_MAP_WRITE);100if (!map) {101FREE(shader);102return NULL;103}104105/* copy the shader bytecode */106memcpy(map, bytecode, bytecodeLen);107108/* if shader signature is specified, append it to the bytecode. */109if (sgnLen) {110assert(sws->have_sm5);111map = (char *)map + bytecodeLen;112memcpy(map, sgnInfo, sgnLen);113}114sws->buffer_unmap(sws, shader->buf);115116return shader;117}118119120