ilo: add ilo_state_vertex_buffer
[mesa.git] / src / gallium / drivers / ilo / ilo_gpgpu.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-2013 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "util/u_upload_mgr.h"
29 #include "ilo_context.h"
30 #include "ilo_render.h"
31 #include "ilo_shader.h"
32 #include "ilo_gpgpu.h"
33
34 static void
35 launch_grid(struct ilo_context *ilo,
36 const uint *block_layout, const uint *grid_layout,
37 const struct pipe_constant_buffer *input, uint32_t pc)
38 {
39 const unsigned grid_offset[3] = { 0, 0, 0 };
40 const unsigned thread_group_size =
41 block_layout[0] * block_layout[1] * block_layout[2];
42 int max_len, before_space;
43
44 ilo_cp_set_owner(ilo->cp, INTEL_RING_RENDER, NULL);
45
46 max_len = ilo_render_get_launch_grid_len(ilo->render, &ilo->state_vector);
47 max_len += ilo_render_get_flush_len(ilo->render) * 2;
48
49 if (max_len > ilo_cp_space(ilo->cp)) {
50 ilo_cp_submit(ilo->cp, "out of space");
51 assert(max_len <= ilo_cp_space(ilo->cp));
52 }
53
54 before_space = ilo_cp_space(ilo->cp);
55
56 while (true) {
57 struct ilo_builder_snapshot snapshot;
58
59 ilo_builder_batch_snapshot(&ilo->cp->builder, &snapshot);
60
61 ilo_render_emit_launch_grid(ilo->render, &ilo->state_vector,
62 grid_offset, grid_layout, thread_group_size, input, pc);
63
64 if (!ilo_builder_validate(&ilo->cp->builder, 0, NULL)) {
65 ilo_builder_batch_restore(&ilo->cp->builder, &snapshot);
66
67 /* flush and try again */
68 if (ilo_builder_batch_used(&ilo->cp->builder)) {
69 ilo_cp_submit(ilo->cp, "out of aperture");
70 continue;
71 }
72 }
73
74 break;
75 }
76
77 /* sanity check size estimation */
78 assert(before_space - ilo_cp_space(ilo->cp) <= max_len);
79 }
80
81 static void
82 ilo_launch_grid(struct pipe_context *pipe,
83 const uint *block_layout, const uint *grid_layout,
84 uint32_t pc, const void *input)
85 {
86 struct ilo_context *ilo = ilo_context(pipe);
87 struct ilo_shader_state *cs = ilo->state_vector.cs;
88 struct pipe_constant_buffer input_buf;
89
90 memset(&input_buf, 0, sizeof(input_buf));
91
92 input_buf.buffer_size =
93 ilo_shader_get_kernel_param(cs, ILO_KERNEL_CS_INPUT_SIZE);
94 if (input_buf.buffer_size) {
95 u_upload_data(ilo->uploader, 0, input_buf.buffer_size, input,
96 &input_buf.buffer_offset, &input_buf.buffer);
97 }
98
99 ilo_shader_cache_upload(ilo->shader_cache, &ilo->cp->builder);
100
101 launch_grid(ilo, block_layout, grid_layout, &input_buf, pc);
102
103 ilo_render_invalidate_hw(ilo->render);
104
105 if (ilo_debug & ILO_DEBUG_NOCACHE)
106 ilo_render_emit_flush(ilo->render);
107
108 if (input_buf.buffer_size)
109 pipe_resource_reference(&input_buf.buffer, NULL);
110 }
111
112 /**
113 * Initialize GPGPU-related functions.
114 */
115 void
116 ilo_init_gpgpu_functions(struct ilo_context *ilo)
117 {
118 ilo->base.launch_grid = ilo_launch_grid;
119 }