xref: /aosp_15_r20/external/mesa3d/src/gallium/winsys/svga/drm/vmw_shader.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (c) 2009-2024 Broadcom. All Rights Reserved.
3  * The term “Broadcom” refers to Broadcom Inc.
4  * and/or its subsidiaries.
5  * SPDX-License-Identifier: MIT
6  */
7 
8 
9 #include "svga_cmd.h"
10 #include "util/u_debug.h"
11 #include "util/u_memory.h"
12 
13 #include "vmw_context.h"
14 #include "vmw_shader.h"
15 #include "vmw_buffer.h"
16 #include "vmw_screen.h"
17 
18 void
vmw_svga_winsys_shader_reference(struct vmw_svga_winsys_shader ** pdst,struct vmw_svga_winsys_shader * src)19 vmw_svga_winsys_shader_reference(struct vmw_svga_winsys_shader **pdst,
20                                  struct vmw_svga_winsys_shader *src)
21 {
22    struct pipe_reference *src_ref;
23    struct pipe_reference *dst_ref;
24    struct vmw_svga_winsys_shader *dst;
25 
26    if(pdst == NULL || *pdst == src)
27       return;
28 
29    dst = *pdst;
30 
31    src_ref = src ? &src->refcnt : NULL;
32    dst_ref = dst ? &dst->refcnt : NULL;
33 
34    if (pipe_reference(dst_ref, src_ref)) {
35       struct svga_winsys_screen *sws = &dst->screen->base;
36 
37       if (!sws->have_vgpu10)
38          vmw_ioctl_shader_destroy(dst->screen, dst->shid);
39 #if MESA_DEBUG
40       /* to detect dangling pointers */
41       assert(p_atomic_read(&dst->validated) == 0);
42       dst->shid = SVGA3D_INVALID_ID;
43 #endif
44       sws->buffer_destroy(sws, dst->buf);
45       FREE(dst);
46    }
47 
48    *pdst = src;
49 }
50 
51 
52 /**
53  * A helper function to create a shader object and upload the
54  * shader bytecode and signature if specified to the shader memory.
55  */
56 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)57 vmw_svga_shader_create(struct svga_winsys_screen *sws,
58                        SVGA3dShaderType type,
59                        const uint32 *bytecode,
60                        uint32 bytecodeLen,
61                        const SVGA3dDXShaderSignatureHeader *sgnInfo,
62                        uint32 sgnLen)
63 {
64    struct vmw_svga_winsys_shader *shader;
65    void *map;
66 
67    shader = CALLOC_STRUCT(vmw_svga_winsys_shader);
68    if (!shader)
69       return NULL;
70 
71    pipe_reference_init(&shader->refcnt, 1);
72    p_atomic_set(&shader->validated, 0);
73    shader->screen = vmw_winsys_screen(sws);
74    shader->buf = sws->buffer_create(sws, 64,
75                                     SVGA_BUFFER_USAGE_SHADER,
76                                     bytecodeLen + sgnLen);
77    if (!shader->buf) {
78       FREE(shader);
79       return NULL;
80    }
81 
82    map = sws->buffer_map(sws, shader->buf, PIPE_MAP_WRITE);
83    if (!map) {
84       FREE(shader);
85       return NULL;
86    }
87 
88    /* copy the shader bytecode */
89    memcpy(map, bytecode, bytecodeLen);
90 
91    /* if shader signature is specified, append it to the bytecode. */
92    if (sgnLen) {
93       assert(sws->have_sm5);
94       map = (char *)map + bytecodeLen;
95       memcpy(map, sgnInfo, sgnLen);
96    }
97    sws->buffer_unmap(sws, shader->buf);
98 
99    return shader;
100 }
101