1 /**********************************************************
2  * Copyright 2009-2015 VMware, Inc.  All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  **********************************************************/
25 
26 
27 #include "svga_cmd.h"
28 #include "util/u_debug.h"
29 #include "util/u_memory.h"
30 
31 #include "vmw_context.h"
32 #include "vmw_shader.h"
33 #include "vmw_buffer.h"
34 #include "vmw_screen.h"
35 
36 void
vmw_svga_winsys_shader_reference(struct vmw_svga_winsys_shader ** pdst,struct vmw_svga_winsys_shader * src)37 vmw_svga_winsys_shader_reference(struct vmw_svga_winsys_shader **pdst,
38                                  struct vmw_svga_winsys_shader *src)
39 {
40    struct pipe_reference *src_ref;
41    struct pipe_reference *dst_ref;
42    struct vmw_svga_winsys_shader *dst;
43 
44    if(pdst == NULL || *pdst == src)
45       return;
46 
47    dst = *pdst;
48 
49    src_ref = src ? &src->refcnt : NULL;
50    dst_ref = dst ? &dst->refcnt : NULL;
51 
52    if (pipe_reference(dst_ref, src_ref)) {
53       struct svga_winsys_screen *sws = &dst->screen->base;
54 
55       if (!sws->have_vgpu10)
56          vmw_ioctl_shader_destroy(dst->screen, dst->shid);
57 #ifdef DEBUG
58       /* to detect dangling pointers */
59       assert(p_atomic_read(&dst->validated) == 0);
60       dst->shid = SVGA3D_INVALID_ID;
61 #endif
62       sws->buffer_destroy(sws, dst->buf);
63       FREE(dst);
64    }
65 
66    *pdst = src;
67 }
68 
69 
70 /**
71  * A helper function to create a shader object and upload the
72  * shader bytecode and signature if specified to the shader memory.
73  */
74 struct vmw_svga_winsys_shader *
vmw_svga_shader_create(struct svga_winsys_screen * sws,SVGA3dShaderType type,const uint32 * bytecode,uint32 bytecodeLen,const SVGA3dDXShaderSignatureHeader * sgnInfo,uint32 sgnLen)75 vmw_svga_shader_create(struct svga_winsys_screen *sws,
76                        SVGA3dShaderType type,
77                        const uint32 *bytecode,
78                        uint32 bytecodeLen,
79                        const SVGA3dDXShaderSignatureHeader *sgnInfo,
80                        uint32 sgnLen)
81 {
82    struct vmw_svga_winsys_shader *shader;
83    void *map;
84 
85    shader = CALLOC_STRUCT(vmw_svga_winsys_shader);
86    if (!shader)
87       return NULL;
88 
89    pipe_reference_init(&shader->refcnt, 1);
90    p_atomic_set(&shader->validated, 0);
91    shader->screen = vmw_winsys_screen(sws);
92    shader->buf = sws->buffer_create(sws, 64,
93                                     SVGA_BUFFER_USAGE_SHADER,
94                                     bytecodeLen + sgnLen);
95    if (!shader->buf) {
96       FREE(shader);
97       return NULL;
98    }
99 
100    map = sws->buffer_map(sws, shader->buf, PIPE_MAP_WRITE);
101    if (!map) {
102       FREE(shader);
103       return NULL;
104    }
105 
106    /* copy the shader bytecode */
107    memcpy(map, bytecode, bytecodeLen);
108 
109    /* if shader signature is specified, append it to the bytecode. */
110    if (sgnLen) {
111       assert(sws->have_sm5);
112       map = (char *)map + bytecodeLen;
113       memcpy(map, sgnInfo, sgnLen);
114    }
115    sws->buffer_unmap(sws, shader->buf);
116 
117    return shader;
118 }
119