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