1 /**************************************************************************
2 *
3 * Copyright 2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "lp_context.h"
29 #include "lp_state.h"
30 #include "lp_texture.h"
31
32 #include "util/u_memory.h"
33 #include "draw/draw_context.h"
34
35
36 static struct pipe_stream_output_target *
llvmpipe_create_so_target(struct pipe_context * pipe,struct pipe_resource * buffer,unsigned buffer_offset,unsigned buffer_size)37 llvmpipe_create_so_target(struct pipe_context *pipe,
38 struct pipe_resource *buffer,
39 unsigned buffer_offset,
40 unsigned buffer_size)
41 {
42 struct draw_so_target *t = CALLOC_STRUCT(draw_so_target);
43 if (!t)
44 return NULL;
45
46 t->target.context = pipe;
47 t->target.reference.count = 1;
48 pipe_resource_reference(&t->target.buffer, buffer);
49 t->target.buffer_offset = buffer_offset;
50 t->target.buffer_size = buffer_size;
51 return &t->target;
52 }
53
54
55 static void
llvmpipe_so_target_destroy(struct pipe_context * pipe,struct pipe_stream_output_target * target)56 llvmpipe_so_target_destroy(struct pipe_context *pipe,
57 struct pipe_stream_output_target *target)
58 {
59 pipe_resource_reference(&target->buffer, NULL);
60 FREE(target);
61 }
62
63
64 static uint32_t
llvmpipe_so_offset(struct pipe_stream_output_target * so_target)65 llvmpipe_so_offset(struct pipe_stream_output_target *so_target)
66 {
67 struct draw_so_target *target = (struct draw_so_target *)so_target;
68 return target->internal_offset;
69 }
70
71
72 static void
llvmpipe_set_so_targets(struct pipe_context * pipe,unsigned num_targets,struct pipe_stream_output_target ** targets,const unsigned * offsets)73 llvmpipe_set_so_targets(struct pipe_context *pipe,
74 unsigned num_targets,
75 struct pipe_stream_output_target **targets,
76 const unsigned *offsets)
77 {
78 struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
79 unsigned i;
80 for (i = 0; i < num_targets; i++) {
81 const bool append = (offsets[i] == (unsigned)-1);
82 /*
83 * Warn if the so target was created in another context.
84 * XXX Not entirely sure if mesa/st may rely on this?
85 * Otherwise should just assert.
86 */
87 if (targets[i] && targets[i]->context != pipe) {
88 debug_printf("Illegal setting of so target with target %d created "
89 "in another context\n", i);
90 }
91 pipe_so_target_reference((struct pipe_stream_output_target **)
92 &llvmpipe->so_targets[i], targets[i]);
93 /* If we're not appending then lets set the internal
94 offset to what was requested */
95 if (!append && llvmpipe->so_targets[i]) {
96 llvmpipe->so_targets[i]->internal_offset = offsets[i];
97 }
98
99 if (targets[i]) {
100 void *buf = llvmpipe_resource(targets[i]->buffer)->data;
101 llvmpipe->so_targets[i]->mapping = buf;
102 }
103 }
104
105 for (; i < llvmpipe->num_so_targets; i++) {
106 pipe_so_target_reference((struct pipe_stream_output_target **)
107 &llvmpipe->so_targets[i], NULL);
108 }
109 llvmpipe->num_so_targets = num_targets;
110
111 draw_set_mapped_so_targets(llvmpipe->draw, llvmpipe->num_so_targets,
112 llvmpipe->so_targets);
113 }
114
115
116 void
llvmpipe_init_so_funcs(struct llvmpipe_context * pipe)117 llvmpipe_init_so_funcs(struct llvmpipe_context *pipe)
118 {
119 pipe->pipe.create_stream_output_target = llvmpipe_create_so_target;
120 pipe->pipe.stream_output_target_destroy = llvmpipe_so_target_destroy;
121 pipe->pipe.set_stream_output_targets = llvmpipe_set_so_targets;
122 pipe->pipe.stream_output_target_offset = llvmpipe_so_offset;
123 }
124