1d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell/**************************************************************************
2d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell *
3d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * All Rights Reserved.
5d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell *
6d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
7d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * copy of this software and associated documentation files (the
8d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * "Software"), to deal in the Software without restriction, including
9d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * without limitation the rights to use, copy, modify, merge, publish,
10d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * distribute, sub license, and/or sell copies of the Software, and to
11d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * permit persons to whom the Software is furnished to do so, subject to
12d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * the following conditions:
13d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell *
14d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * The above copyright notice and this permission notice (including the
15d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * next paragraph) shall be included in all copies or substantial portions
16d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * of the Software.
17d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell *
18d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell *
26d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell **************************************************************************/
27d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
2829858e1b553cee1fd7e3380ea62c69d2a6b91b95José Fonseca /**
2929858e1b553cee1fd7e3380ea62c69d2a6b91b95José Fonseca  * @file
30076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul  *
3129858e1b553cee1fd7e3380ea62c69d2a6b91b95José Fonseca  * Wrap the cso cache & hash mechanisms in a simplified
32d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell  * pipe-driver-specific interface.
33d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell  *
3429858e1b553cee1fd7e3380ea62c69d2a6b91b95José Fonseca  * @author Zack Rusin <zack@tungstengraphics.com>
3529858e1b553cee1fd7e3380ea62c69d2a6b91b95José Fonseca  * @author Keith Whitwell <keith@tungstengraphics.com>
36d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell  */
37d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
38d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell#include "pipe/p_state.h"
3976eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák#include "util/u_draw.h"
409b02f41cac36286d6838339532c7a95a0615b645Brian Paul#include "util/u_framebuffer.h"
4128486880ca3ec39419ccee0cb1a3bedc9ef7117cJosé Fonseca#include "util/u_inlines.h"
4208f89988c8738029c60e89c61c9da0522bd53087Michal Krol#include "util/u_math.h"
434f25420bdd834e81a3e22733304efc5261c2998aBrian Paul#include "util/u_memory.h"
44e0773da1e897164ed7597437070e32b867734ee5Marek Olšák#include "util/u_vbuf.h"
45c208a2c791fa24c7c5887fc496738cbddbfafc72José Fonseca#include "tgsi/tgsi_parse.h"
46d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
47d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell#include "cso_cache/cso_context.h"
48d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell#include "cso_cache/cso_cache.h"
49d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell#include "cso_cache/cso_hash.h"
5089d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin#include "cso_context.h"
51d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
52d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
53c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul/**
54c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul * Info related to samplers and sampler views.
55c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul * We have one of these for fragment samplers and another for vertex samplers.
56c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul */
57c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulstruct sampler_info
58c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul{
59d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   struct {
60c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      void *samplers[PIPE_MAX_SAMPLERS];
61c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      unsigned nr_samplers;
62d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   } hw;
63d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
64c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   void *samplers[PIPE_MAX_SAMPLERS];
65c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   unsigned nr_samplers;
66d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
67c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   void *samplers_saved[PIPE_MAX_SAMPLERS];
68c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   unsigned nr_samplers_saved;
69fd4aa4f32365a5f054e7fc36b558680dcac66d1bMichal Krol
70c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   struct pipe_sampler_view *views[PIPE_MAX_SAMPLERS];
71c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   unsigned nr_views;
727d95efde0a0e13e13c59444703bc47eb13926385Brian
73c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   struct pipe_sampler_view *views_saved[PIPE_MAX_SAMPLERS];
74c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   unsigned nr_views_saved;
75c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul};
76fd4aa4f32365a5f054e7fc36b558680dcac66d1bMichal Krol
777d95efde0a0e13e13c59444703bc47eb13926385Brian
78fd4aa4f32365a5f054e7fc36b558680dcac66d1bMichal Krol
79c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulstruct cso_context {
80c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   struct pipe_context *pipe;
81c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   struct cso_cache *cache;
82e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   struct u_vbuf *vbuf;
837d95efde0a0e13e13c59444703bc47eb13926385Brian
84d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák   boolean has_geometry_shader;
85c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   boolean has_streamout;
86d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák
87ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   struct sampler_info samplers[PIPE_SHADER_TYPES];
88fd4aa4f32365a5f054e7fc36b558680dcac66d1bMichal Krol
89d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   uint nr_vertex_buffers;
90d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   struct pipe_vertex_buffer vertex_buffers[PIPE_MAX_ATTRIBS];
91d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák
92d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   uint nr_vertex_buffers_saved;
93d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   struct pipe_vertex_buffer vertex_buffers_saved[PIPE_MAX_ATTRIBS];
94d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák
95c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   unsigned nr_so_targets;
96c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   struct pipe_stream_output_target *so_targets[PIPE_MAX_SO_BUFFERS];
97c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
98c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   unsigned nr_so_targets_saved;
99c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   struct pipe_stream_output_target *so_targets_saved[PIPE_MAX_SO_BUFFERS];
100c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1017d95efde0a0e13e13c59444703bc47eb13926385Brian   /** Current and saved state.
1027d95efde0a0e13e13c59444703bc47eb13926385Brian    * The saved state is used as a 1-deep stack.
1037d95efde0a0e13e13c59444703bc47eb13926385Brian    */
1047d95efde0a0e13e13c59444703bc47eb13926385Brian   void *blend, *blend_saved;
1057d95efde0a0e13e13c59444703bc47eb13926385Brian   void *depth_stencil, *depth_stencil_saved;
1067d95efde0a0e13e13c59444703bc47eb13926385Brian   void *rasterizer, *rasterizer_saved;
1073487b93cc4b8bea44f966a4f5cfd29b26dbbcff2Brian Paul   void *fragment_shader, *fragment_shader_saved;
1083487b93cc4b8bea44f966a4f5cfd29b26dbbcff2Brian Paul   void *vertex_shader, *vertex_shader_saved;
1093487b93cc4b8bea44f966a4f5cfd29b26dbbcff2Brian Paul   void *geometry_shader, *geometry_shader_saved;
11051d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   void *velements, *velements_saved;
1117d95efde0a0e13e13c59444703bc47eb13926385Brian
112227ae7b968c1351921babdbf6f052239766ffce4Michal Krol   struct pipe_clip_state clip;
113227ae7b968c1351921babdbf6f052239766ffce4Michal Krol   struct pipe_clip_state clip_saved;
1147d95efde0a0e13e13c59444703bc47eb13926385Brian
1157d95efde0a0e13e13c59444703bc47eb13926385Brian   struct pipe_framebuffer_state fb, fb_saved;
1167d95efde0a0e13e13c59444703bc47eb13926385Brian   struct pipe_viewport_state vp, vp_saved;
1177d95efde0a0e13e13c59444703bc47eb13926385Brian   struct pipe_blend_color blend_color;
118e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšák   unsigned sample_mask, sample_mask_saved;
119b75adabc89e34083ac302ac9bafc8d80b62ac14cRoland Scheidegger   struct pipe_stencil_ref stencil_ref, stencil_ref_saved;
120d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell};
121d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
122d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
123026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusinstatic boolean delete_blend_state(struct cso_context *ctx, void *state)
124026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin{
125026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   struct cso_blend *cso = (struct cso_blend *)state;
126026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
1271dfb3d4729ce4cd71a593c14dbb2907cd987f8abMichel Dänzer   if (ctx->blend == cso->data)
128026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      return FALSE;
129026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
130026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   if (cso->delete_state)
131026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      cso->delete_state(cso->context, cso->data);
132026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   FREE(state);
133026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   return TRUE;
134026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin}
135026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
136026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusinstatic boolean delete_depth_stencil_state(struct cso_context *ctx, void *state)
137026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin{
138076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul   struct cso_depth_stencil_alpha *cso =
139076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      (struct cso_depth_stencil_alpha *)state;
140026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
141026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   if (ctx->depth_stencil == cso->data)
142026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      return FALSE;
143026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
144026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   if (cso->delete_state)
145026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      cso->delete_state(cso->context, cso->data);
146026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   FREE(state);
147026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
148026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   return TRUE;
149026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin}
150026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
151026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusinstatic boolean delete_sampler_state(struct cso_context *ctx, void *state)
152026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin{
153026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   struct cso_sampler *cso = (struct cso_sampler *)state;
154026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   if (cso->delete_state)
155026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      cso->delete_state(cso->context, cso->data);
156026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   FREE(state);
157026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   return TRUE;
158026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin}
159026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
160026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusinstatic boolean delete_rasterizer_state(struct cso_context *ctx, void *state)
161026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin{
162026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
163026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
164026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   if (ctx->rasterizer == cso->data)
165026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      return FALSE;
166026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   if (cso->delete_state)
167026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      cso->delete_state(cso->context, cso->data);
168026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   FREE(state);
169026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   return TRUE;
170026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin}
171026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
17251d139f03898e5e46af6363c6bba131455738cc4Roland Scheideggerstatic boolean delete_vertex_elements(struct cso_context *ctx,
17351d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger                                      void *state)
17451d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger{
17551d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   struct cso_velements *cso = (struct cso_velements *)state;
17651d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger
17751d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   if (ctx->velements == cso->data)
17851d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      return FALSE;
17951d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger
18051d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   if (cso->delete_state)
18151d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      cso->delete_state(cso->context, cso->data);
18251d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   FREE(state);
18351d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   return TRUE;
18451d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger}
18551d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger
186026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
187026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusinstatic INLINE boolean delete_cso(struct cso_context *ctx,
188026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin                                 void *state, enum cso_cache_type type)
189026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin{
190026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   switch (type) {
191026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   case CSO_BLEND:
192026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      return delete_blend_state(ctx, state);
193026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   case CSO_SAMPLER:
194026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      return delete_sampler_state(ctx, state);
195026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   case CSO_DEPTH_STENCIL_ALPHA:
196026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      return delete_depth_stencil_state(ctx, state);
197026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   case CSO_RASTERIZER:
198026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      return delete_rasterizer_state(ctx, state);
19951d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   case CSO_VELEMENTS:
20051d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      return delete_vertex_elements(ctx, state);
201026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   default:
202026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      assert(0);
203026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      FREE(state);
204026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   }
205026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   return FALSE;
206026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin}
207026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
208076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paulstatic INLINE void
209076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paulsanitize_hash(struct cso_hash *hash, enum cso_cache_type type,
210076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul              int max_size, void *user_data)
211026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin{
212026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   struct cso_context *ctx = (struct cso_context *)user_data;
213026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   /* if we're approach the maximum size, remove fourth of the entries
214026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin    * otherwise every subsequent call will go through the same */
215026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   int hash_size = cso_hash_size(hash);
216026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   int max_entries = (max_size > hash_size) ? max_size : hash_size;
217026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   int to_remove =  (max_size < max_entries) * max_entries/4;
218026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   struct cso_hash_iter iter = cso_hash_first_node(hash);
219026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   if (hash_size > max_size)
220026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      to_remove += hash_size - max_size;
221026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   while (to_remove) {
222026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      /*remove elements until we're good */
223026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      /*fixme: currently we pick the nodes to remove at random*/
224026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      void *cso = cso_hash_iter_data(iter);
225026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      if (delete_cso(ctx, cso, type)) {
226026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin         iter = cso_hash_erase(hash, iter);
227026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin         --to_remove;
228026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin      } else
229026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin         iter = cso_hash_iter_next(iter);
230026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   }
231026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin}
232026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
23346fe17930ec71be3489fbb844de2bf16d877437eMarek Olšákstatic void cso_init_vbuf(struct cso_context *cso)
23446fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák{
23546fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák   struct u_vbuf_caps caps;
23646fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák
23746fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák   u_vbuf_get_caps(cso->pipe->screen, &caps);
23846fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák
23946fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák   /* Install u_vbuf if there is anything unsupported. */
24046fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák   if (!caps.buffer_offset_unaligned ||
24146fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák       !caps.buffer_stride_unaligned ||
24246fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák       !caps.velem_src_offset_unaligned ||
24346fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák       !caps.format_fixed32 ||
24446fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák       !caps.format_float16 ||
24546fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák       !caps.format_float64 ||
24646fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák       !caps.format_norm32 ||
24746fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák       !caps.format_scaled32 ||
24846fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák       !caps.user_vertex_buffers) {
24946fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák      cso->vbuf = u_vbuf_create(cso->pipe, &caps);
25046fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák   }
25146fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák}
252026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin
253d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwellstruct cso_context *cso_create_context( struct pipe_context *pipe )
254d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell{
255d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   struct cso_context *ctx = CALLOC_STRUCT(cso_context);
256d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   if (ctx == NULL)
257d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      goto out;
258d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
259d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   ctx->cache = cso_cache_create();
260d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   if (ctx->cache == NULL)
261d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      goto out;
262026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin   cso_cache_set_sanitize_callback(ctx->cache,
263026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin                                   sanitize_hash,
264026e31a068981724fb0c98f6d1fc87d086fd2da6Zack Rusin                                   ctx);
265d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
266d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   ctx->pipe = pipe;
267e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšák   ctx->sample_mask_saved = ~0;
268d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
26946fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák   cso_init_vbuf(ctx);
27046fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák
271b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell   /* Enable for testing: */
272b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell   if (0) cso_set_maximum_cache_size( ctx->cache, 4 );
273b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell
274d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák   if (pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
275d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák                                PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0) {
276d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák      ctx->has_geometry_shader = TRUE;
277d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák   }
278c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   if (pipe->screen->get_param(pipe->screen,
279c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák                               PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0) {
280c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      ctx->has_streamout = TRUE;
281c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
282d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák
283d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   return ctx;
284d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
285d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwellout:
286076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul   cso_destroy_context( ctx );
287d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   return NULL;
288d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell}
289d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
290a41804909d5799cddfbf48a46524f78c736408d4Brian Paul/**
291a41804909d5799cddfbf48a46524f78c736408d4Brian Paul * Prior to context destruction, this function unbinds all state objects.
292a41804909d5799cddfbf48a46524f78c736408d4Brian Paul */
293386102c62a3315182ffbc6319351cb883234511aBrian Paulvoid cso_release_all( struct cso_context *ctx )
294b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell{
295ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   unsigned i, shader;
296c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
297b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell   if (ctx->pipe) {
298b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell      ctx->pipe->bind_blend_state( ctx->pipe, NULL );
299b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell      ctx->pipe->bind_rasterizer_state( ctx->pipe, NULL );
300fd4aa4f32365a5f054e7fc36b558680dcac66d1bMichal Krol      ctx->pipe->bind_fragment_sampler_states( ctx->pipe, 0, NULL );
301debc0b6fa8ce110eb4febc376d8327336259742cBrian Paul      if (ctx->pipe->bind_vertex_sampler_states)
302debc0b6fa8ce110eb4febc376d8327336259742cBrian Paul         ctx->pipe->bind_vertex_sampler_states(ctx->pipe, 0, NULL);
303b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell      ctx->pipe->bind_depth_stencil_alpha_state( ctx->pipe, NULL );
304b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell      ctx->pipe->bind_fs_state( ctx->pipe, NULL );
305b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell      ctx->pipe->bind_vs_state( ctx->pipe, NULL );
3060d6b0b0d9d5257cc8fb95786b6cd77d088bdb35eRoland Scheidegger      ctx->pipe->bind_vertex_elements_state( ctx->pipe, NULL );
3071d298a3764cef6a7119524fdc8f3c0d2589d6070Brian Paul      ctx->pipe->set_fragment_sampler_views(ctx->pipe, 0, NULL);
308e845765f0f8791a0e6c2e54b91ebf9f0e831d19fBrian Paul      if (ctx->pipe->set_vertex_sampler_views)
309e845765f0f8791a0e6c2e54b91ebf9f0e831d19fBrian Paul         ctx->pipe->set_vertex_sampler_views(ctx->pipe, 0, NULL);
310cf5948380829ff44aafb12c911b0fc064360d352Fredrik Höglund      if (ctx->pipe->set_stream_output_targets)
311cf5948380829ff44aafb12c911b0fc064360d352Fredrik Höglund         ctx->pipe->set_stream_output_targets(ctx->pipe, 0, NULL, 0);
312b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell   }
313b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell
314c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   /* free fragment samplers, views */
315ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   for (shader = 0; shader < Elements(ctx->samplers); shader++) {
316ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      struct sampler_info *info = &ctx->samplers[shader];
317ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
318ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul         pipe_sampler_view_reference(&info->views[i], NULL);
319ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul         pipe_sampler_view_reference(&info->views_saved[i], NULL);
320ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      }
321fd4aa4f32365a5f054e7fc36b558680dcac66d1bMichal Krol   }
322fd4aa4f32365a5f054e7fc36b558680dcac66d1bMichal Krol
3239b02f41cac36286d6838339532c7a95a0615b645Brian Paul   util_unreference_framebuffer_state(&ctx->fb);
3249b02f41cac36286d6838339532c7a95a0615b645Brian Paul   util_unreference_framebuffer_state(&ctx->fb_saved);
325f68d2a0febca38bc7b31f9ab9718e944935b48bcBrian Paul
326d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   util_copy_vertex_buffers(ctx->vertex_buffers,
327d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            &ctx->nr_vertex_buffers,
328d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            NULL, 0);
329d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   util_copy_vertex_buffers(ctx->vertex_buffers_saved,
330d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            &ctx->nr_vertex_buffers_saved,
331d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            NULL, 0);
332d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák
333c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   for (i = 0; i < PIPE_MAX_SO_BUFFERS; i++) {
334c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      pipe_so_target_reference(&ctx->so_targets[i], NULL);
335c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      pipe_so_target_reference(&ctx->so_targets_saved[i], NULL);
336c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
337c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
338b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell   if (ctx->cache) {
339b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell      cso_cache_delete( ctx->cache );
340b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell      ctx->cache = NULL;
341b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell   }
342b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell}
343b041dbe9019ff8cb16ff15d0baaa803c7dc654dbKeith Whitwell
344d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
3454ccee747257192ef584d26d8854f8bc17cc57284Brian Paul/**
3464ccee747257192ef584d26d8854f8bc17cc57284Brian Paul * Free the CSO context.  NOTE: the state tracker should have previously called
3474ccee747257192ef584d26d8854f8bc17cc57284Brian Paul * cso_release_all().
3484ccee747257192ef584d26d8854f8bc17cc57284Brian Paul */
349d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwellvoid cso_destroy_context( struct cso_context *ctx )
350d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell{
351a41804909d5799cddfbf48a46524f78c736408d4Brian Paul   if (ctx) {
35246fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák      if (ctx->vbuf)
35346fe17930ec71be3489fbb844de2bf16d877437eMarek Olšák         u_vbuf_destroy(ctx->vbuf);
354a41804909d5799cddfbf48a46524f78c736408d4Brian Paul      FREE( ctx );
355a41804909d5799cddfbf48a46524f78c736408d4Brian Paul   }
356d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell}
357d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
358d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
359d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell/* Those function will either find the state of the given template
360d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * in the cache or they will create a new state from the given
361d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * template, insert it in the cache and return it.
362d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell */
363d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
364d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell/*
365d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * If the driver returns 0 from the create method then they will assign
366d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell * the data member of the cso to be the template itself.
367d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell */
368d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
3699fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusinenum pipe_error cso_set_blend(struct cso_context *ctx,
3709fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin                              const struct pipe_blend_state *templ)
371d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell{
372bef610f693266c338b99511b4d1eea5d5b97644eRoland Scheidegger   unsigned key_size, hash_key;
373bef610f693266c338b99511b4d1eea5d5b97644eRoland Scheidegger   struct cso_hash_iter iter;
374d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   void *handle;
375d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
376076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul   key_size = templ->independent_blend_enable ?
377076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      sizeof(struct pipe_blend_state) :
378076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      (char *)&(templ->rt[1]) - (char *)templ;
379bef610f693266c338b99511b4d1eea5d5b97644eRoland Scheidegger   hash_key = cso_construct_key((void*)templ, key_size);
380076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul   iter = cso_find_state_template(ctx->cache, hash_key, CSO_BLEND,
381076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                                  (void*)templ, key_size);
382bef610f693266c338b99511b4d1eea5d5b97644eRoland Scheidegger
383d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   if (cso_hash_iter_is_null(iter)) {
384d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      struct cso_blend *cso = MALLOC(sizeof(struct cso_blend));
3859fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin      if (!cso)
3869fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin         return PIPE_ERROR_OUT_OF_MEMORY;
387d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
38853499c64b212c68527f84f5e672cdfc95935f643José Fonseca      memset(&cso->state, 0, sizeof cso->state);
389bef610f693266c338b99511b4d1eea5d5b97644eRoland Scheidegger      memcpy(&cso->state, templ, key_size);
390d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      cso->data = ctx->pipe->create_blend_state(ctx->pipe, &cso->state);
391d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      cso->delete_state = (cso_state_callback)ctx->pipe->delete_blend_state;
392d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      cso->context = ctx->pipe;
393d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
394d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      iter = cso_insert_state(ctx->cache, hash_key, CSO_BLEND, cso);
3950879237725eca893318137b795d4234300a37e9aZack Rusin      if (cso_hash_iter_is_null(iter)) {
3960879237725eca893318137b795d4234300a37e9aZack Rusin         FREE(cso);
3970879237725eca893318137b795d4234300a37e9aZack Rusin         return PIPE_ERROR_OUT_OF_MEMORY;
3980879237725eca893318137b795d4234300a37e9aZack Rusin      }
3990879237725eca893318137b795d4234300a37e9aZack Rusin
400d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      handle = cso->data;
401d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   }
402d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   else {
403d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      handle = ((struct cso_blend *)cso_hash_iter_data(iter))->data;
404d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   }
405d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
406d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   if (ctx->blend != handle) {
407d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      ctx->blend = handle;
408d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      ctx->pipe->bind_blend_state(ctx->pipe, handle);
409d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   }
4109fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin   return PIPE_OK;
411d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell}
412d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
4137d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_save_blend(struct cso_context *ctx)
4147d95efde0a0e13e13c59444703bc47eb13926385Brian{
4157d95efde0a0e13e13c59444703bc47eb13926385Brian   assert(!ctx->blend_saved);
4167d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->blend_saved = ctx->blend;
4177d95efde0a0e13e13c59444703bc47eb13926385Brian}
4187d95efde0a0e13e13c59444703bc47eb13926385Brian
4197d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_restore_blend(struct cso_context *ctx)
420e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian{
4217d95efde0a0e13e13c59444703bc47eb13926385Brian   if (ctx->blend != ctx->blend_saved) {
4227d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->blend = ctx->blend_saved;
4237d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend_saved);
4247d95efde0a0e13e13c59444703bc47eb13926385Brian   }
4257d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->blend_saved = NULL;
426e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian}
427e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian
428e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian
4297d95efde0a0e13e13c59444703bc47eb13926385Brian
430076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paulenum pipe_error
431076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paulcso_set_depth_stencil_alpha(struct cso_context *ctx,
432076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                            const struct pipe_depth_stencil_alpha_state *templ)
433d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell{
434847ac8ec5ff683076dff17d8e0426a64b4ad65e7Roland Scheidegger   unsigned key_size = sizeof(struct pipe_depth_stencil_alpha_state);
435847ac8ec5ff683076dff17d8e0426a64b4ad65e7Roland Scheidegger   unsigned hash_key = cso_construct_key((void*)templ, key_size);
436d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
437076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                                                       hash_key,
438847ac8ec5ff683076dff17d8e0426a64b4ad65e7Roland Scheidegger                                                       CSO_DEPTH_STENCIL_ALPHA,
439847ac8ec5ff683076dff17d8e0426a64b4ad65e7Roland Scheidegger                                                       (void*)templ, key_size);
440d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   void *handle;
441d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
442d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   if (cso_hash_iter_is_null(iter)) {
443076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      struct cso_depth_stencil_alpha *cso =
444076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul         MALLOC(sizeof(struct cso_depth_stencil_alpha));
4459fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin      if (!cso)
4469fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin         return PIPE_ERROR_OUT_OF_MEMORY;
447d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
448359058e7b77ddbac5eec7e8d1c77232bcbb1adbfKeith Whitwell      memcpy(&cso->state, templ, sizeof(*templ));
449076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      cso->data = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe,
450076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                                                              &cso->state);
451076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      cso->delete_state =
452076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul         (cso_state_callback)ctx->pipe->delete_depth_stencil_alpha_state;
453d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      cso->context = ctx->pipe;
454d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
455076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      iter = cso_insert_state(ctx->cache, hash_key,
456076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                              CSO_DEPTH_STENCIL_ALPHA, cso);
4570879237725eca893318137b795d4234300a37e9aZack Rusin      if (cso_hash_iter_is_null(iter)) {
4580879237725eca893318137b795d4234300a37e9aZack Rusin         FREE(cso);
4590879237725eca893318137b795d4234300a37e9aZack Rusin         return PIPE_ERROR_OUT_OF_MEMORY;
4600879237725eca893318137b795d4234300a37e9aZack Rusin      }
4610879237725eca893318137b795d4234300a37e9aZack Rusin
462d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      handle = cso->data;
463d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   }
464d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   else {
465076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      handle = ((struct cso_depth_stencil_alpha *)
466076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                cso_hash_iter_data(iter))->data;
467d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   }
468d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
469d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   if (ctx->depth_stencil != handle) {
470d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      ctx->depth_stencil = handle;
471d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, handle);
472d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   }
4739fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin   return PIPE_OK;
474d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell}
475d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
4767d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_save_depth_stencil_alpha(struct cso_context *ctx)
477e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian{
4787d95efde0a0e13e13c59444703bc47eb13926385Brian   assert(!ctx->depth_stencil_saved);
4797d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->depth_stencil_saved = ctx->depth_stencil;
4807d95efde0a0e13e13c59444703bc47eb13926385Brian}
4817d95efde0a0e13e13c59444703bc47eb13926385Brian
4827d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_restore_depth_stencil_alpha(struct cso_context *ctx)
4837d95efde0a0e13e13c59444703bc47eb13926385Brian{
4847d95efde0a0e13e13c59444703bc47eb13926385Brian   if (ctx->depth_stencil != ctx->depth_stencil_saved) {
4857d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->depth_stencil = ctx->depth_stencil_saved;
486076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe,
487076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                                                ctx->depth_stencil_saved);
4887d95efde0a0e13e13c59444703bc47eb13926385Brian   }
4897d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->depth_stencil_saved = NULL;
490e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian}
491e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian
492d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
493d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
4949fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusinenum pipe_error cso_set_rasterizer(struct cso_context *ctx,
4959fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin                                   const struct pipe_rasterizer_state *templ)
496d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell{
497847ac8ec5ff683076dff17d8e0426a64b4ad65e7Roland Scheidegger   unsigned key_size = sizeof(struct pipe_rasterizer_state);
498847ac8ec5ff683076dff17d8e0426a64b4ad65e7Roland Scheidegger   unsigned hash_key = cso_construct_key((void*)templ, key_size);
499d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
500076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                                                       hash_key,
501076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                                                       CSO_RASTERIZER,
502847ac8ec5ff683076dff17d8e0426a64b4ad65e7Roland Scheidegger                                                       (void*)templ, key_size);
503d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   void *handle = NULL;
504d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
505d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   if (cso_hash_iter_is_null(iter)) {
506d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      struct cso_rasterizer *cso = MALLOC(sizeof(struct cso_rasterizer));
5079fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin      if (!cso)
5089fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin         return PIPE_ERROR_OUT_OF_MEMORY;
509d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
510359058e7b77ddbac5eec7e8d1c77232bcbb1adbfKeith Whitwell      memcpy(&cso->state, templ, sizeof(*templ));
511d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      cso->data = ctx->pipe->create_rasterizer_state(ctx->pipe, &cso->state);
512076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      cso->delete_state =
513076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul         (cso_state_callback)ctx->pipe->delete_rasterizer_state;
514d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      cso->context = ctx->pipe;
515d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
5160879237725eca893318137b795d4234300a37e9aZack Rusin      iter = cso_insert_state(ctx->cache, hash_key, CSO_RASTERIZER, cso);
5170879237725eca893318137b795d4234300a37e9aZack Rusin      if (cso_hash_iter_is_null(iter)) {
5180879237725eca893318137b795d4234300a37e9aZack Rusin         FREE(cso);
5190879237725eca893318137b795d4234300a37e9aZack Rusin         return PIPE_ERROR_OUT_OF_MEMORY;
5200879237725eca893318137b795d4234300a37e9aZack Rusin      }
5210879237725eca893318137b795d4234300a37e9aZack Rusin
522d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      handle = cso->data;
523d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   }
524d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   else {
525d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      handle = ((struct cso_rasterizer *)cso_hash_iter_data(iter))->data;
526d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   }
527d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
528d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   if (ctx->rasterizer != handle) {
529d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      ctx->rasterizer = handle;
530d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell      ctx->pipe->bind_rasterizer_state(ctx->pipe, handle);
531d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell   }
5329fe63929011cd9c4d86ab6525555a3e53423c854Zack Rusin   return PIPE_OK;
533d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell}
534d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
5357d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_save_rasterizer(struct cso_context *ctx)
536e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian{
5377d95efde0a0e13e13c59444703bc47eb13926385Brian   assert(!ctx->rasterizer_saved);
5387d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->rasterizer_saved = ctx->rasterizer;
539e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian}
540d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
5417d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_restore_rasterizer(struct cso_context *ctx)
5427d95efde0a0e13e13c59444703bc47eb13926385Brian{
5437d95efde0a0e13e13c59444703bc47eb13926385Brian   if (ctx->rasterizer != ctx->rasterizer_saved) {
5447d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->rasterizer = ctx->rasterizer_saved;
5457d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rasterizer_saved);
5467d95efde0a0e13e13c59444703bc47eb13926385Brian   }
5477d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->rasterizer_saved = NULL;
5487d95efde0a0e13e13c59444703bc47eb13926385Brian}
549d8d6569e288fe3324473fb19ade798502dfbba8eKeith Whitwell
550f2c31257167f85df276322be1b8523064e8b66a9Brian Paul
551f2c31257167f85df276322be1b8523064e8b66a9Brian Paul
552ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusinenum pipe_error cso_set_fragment_shader_handle(struct cso_context *ctx,
553ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusin                                               void *handle )
5541dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell{
5551dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell   if (ctx->fragment_shader != handle) {
5561dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell      ctx->fragment_shader = handle;
5571dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell      ctx->pipe->bind_fs_state(ctx->pipe, handle);
5581dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell   }
559ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusin   return PIPE_OK;
5601dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell}
5611dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell
562f2c31257167f85df276322be1b8523064e8b66a9Brian Paulvoid cso_delete_fragment_shader(struct cso_context *ctx, void *handle )
563f2c31257167f85df276322be1b8523064e8b66a9Brian Paul{
564ac79532a15a7109bf0fbd0e40a1ba8e65ed8c435Brian Paul   if (handle == ctx->fragment_shader) {
565ac79532a15a7109bf0fbd0e40a1ba8e65ed8c435Brian Paul      /* unbind before deleting */
566f2c31257167f85df276322be1b8523064e8b66a9Brian Paul      ctx->pipe->bind_fs_state(ctx->pipe, NULL);
567ac79532a15a7109bf0fbd0e40a1ba8e65ed8c435Brian Paul      ctx->fragment_shader = NULL;
568ac79532a15a7109bf0fbd0e40a1ba8e65ed8c435Brian Paul   }
569f2c31257167f85df276322be1b8523064e8b66a9Brian Paul   ctx->pipe->delete_fs_state(ctx->pipe, handle);
570f2c31257167f85df276322be1b8523064e8b66a9Brian Paul}
5711dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell
5727d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_save_fragment_shader(struct cso_context *ctx)
573e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian{
5747d95efde0a0e13e13c59444703bc47eb13926385Brian   assert(!ctx->fragment_shader_saved);
5757d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->fragment_shader_saved = ctx->fragment_shader;
576e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian}
577e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian
5787d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_restore_fragment_shader(struct cso_context *ctx)
5797d95efde0a0e13e13c59444703bc47eb13926385Brian{
5807d95efde0a0e13e13c59444703bc47eb13926385Brian   if (ctx->fragment_shader_saved != ctx->fragment_shader) {
5817d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->pipe->bind_fs_state(ctx->pipe, ctx->fragment_shader_saved);
5827d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->fragment_shader = ctx->fragment_shader_saved;
5837d95efde0a0e13e13c59444703bc47eb13926385Brian   }
5847d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->fragment_shader_saved = NULL;
5857d95efde0a0e13e13c59444703bc47eb13926385Brian}
5867d95efde0a0e13e13c59444703bc47eb13926385Brian
5877d95efde0a0e13e13c59444703bc47eb13926385Brian
588ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusinenum pipe_error cso_set_vertex_shader_handle(struct cso_context *ctx,
589ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusin                                             void *handle )
5901dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell{
5911dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell   if (ctx->vertex_shader != handle) {
5921dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell      ctx->vertex_shader = handle;
5931dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell      ctx->pipe->bind_vs_state(ctx->pipe, handle);
5941dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell   }
595ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusin   return PIPE_OK;
5961dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell}
5971dc5e56f3e48b629daa18c2d8631c96bda638eb6Keith Whitwell
598f2c31257167f85df276322be1b8523064e8b66a9Brian Paulvoid cso_delete_vertex_shader(struct cso_context *ctx, void *handle )
599f2c31257167f85df276322be1b8523064e8b66a9Brian Paul{
600ac79532a15a7109bf0fbd0e40a1ba8e65ed8c435Brian Paul   if (handle == ctx->vertex_shader) {
601ac79532a15a7109bf0fbd0e40a1ba8e65ed8c435Brian Paul      /* unbind before deleting */
602f2c31257167f85df276322be1b8523064e8b66a9Brian Paul      ctx->pipe->bind_vs_state(ctx->pipe, NULL);
603ac79532a15a7109bf0fbd0e40a1ba8e65ed8c435Brian Paul      ctx->vertex_shader = NULL;
604ac79532a15a7109bf0fbd0e40a1ba8e65ed8c435Brian Paul   }
605f2c31257167f85df276322be1b8523064e8b66a9Brian Paul   ctx->pipe->delete_vs_state(ctx->pipe, handle);
606f2c31257167f85df276322be1b8523064e8b66a9Brian Paul}
607f2c31257167f85df276322be1b8523064e8b66a9Brian Paul
6087d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_save_vertex_shader(struct cso_context *ctx)
609e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian{
6107d95efde0a0e13e13c59444703bc47eb13926385Brian   assert(!ctx->vertex_shader_saved);
6117d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->vertex_shader_saved = ctx->vertex_shader;
6127d95efde0a0e13e13c59444703bc47eb13926385Brian}
6137d95efde0a0e13e13c59444703bc47eb13926385Brian
6147d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_restore_vertex_shader(struct cso_context *ctx)
6157d95efde0a0e13e13c59444703bc47eb13926385Brian{
6167d95efde0a0e13e13c59444703bc47eb13926385Brian   if (ctx->vertex_shader_saved != ctx->vertex_shader) {
61788f8eed3c4430505b1436b6c5b0114d34c33f822Keith Whitwell      ctx->pipe->bind_vs_state(ctx->pipe, ctx->vertex_shader_saved);
6187d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->vertex_shader = ctx->vertex_shader_saved;
6197d95efde0a0e13e13c59444703bc47eb13926385Brian   }
6207d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->vertex_shader_saved = NULL;
6217d95efde0a0e13e13c59444703bc47eb13926385Brian}
6227d95efde0a0e13e13c59444703bc47eb13926385Brian
6237d95efde0a0e13e13c59444703bc47eb13926385Brian
624ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusinenum pipe_error cso_set_framebuffer(struct cso_context *ctx,
625ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusin                                    const struct pipe_framebuffer_state *fb)
6267d95efde0a0e13e13c59444703bc47eb13926385Brian{
627251db95945c6b484a093336e7bf12aed6091de54Keith Whitwell   if (memcmp(&ctx->fb, fb, sizeof(*fb)) != 0) {
6289b02f41cac36286d6838339532c7a95a0615b645Brian Paul      util_copy_framebuffer_state(&ctx->fb, fb);
6297d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->pipe->set_framebuffer_state(ctx->pipe, fb);
6307d95efde0a0e13e13c59444703bc47eb13926385Brian   }
631ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusin   return PIPE_OK;
6327d95efde0a0e13e13c59444703bc47eb13926385Brian}
6337d95efde0a0e13e13c59444703bc47eb13926385Brian
6347d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_save_framebuffer(struct cso_context *ctx)
6357d95efde0a0e13e13c59444703bc47eb13926385Brian{
6369b02f41cac36286d6838339532c7a95a0615b645Brian Paul   util_copy_framebuffer_state(&ctx->fb_saved, &ctx->fb);
6377d95efde0a0e13e13c59444703bc47eb13926385Brian}
6387d95efde0a0e13e13c59444703bc47eb13926385Brian
6397d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_restore_framebuffer(struct cso_context *ctx)
6407d95efde0a0e13e13c59444703bc47eb13926385Brian{
6417d95efde0a0e13e13c59444703bc47eb13926385Brian   if (memcmp(&ctx->fb, &ctx->fb_saved, sizeof(ctx->fb))) {
6429b02f41cac36286d6838339532c7a95a0615b645Brian Paul      util_copy_framebuffer_state(&ctx->fb, &ctx->fb_saved);
6437d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->pipe->set_framebuffer_state(ctx->pipe, &ctx->fb);
6449b02f41cac36286d6838339532c7a95a0615b645Brian Paul      util_unreference_framebuffer_state(&ctx->fb_saved);
6457d95efde0a0e13e13c59444703bc47eb13926385Brian   }
6467d95efde0a0e13e13c59444703bc47eb13926385Brian}
6477d95efde0a0e13e13c59444703bc47eb13926385Brian
6487d95efde0a0e13e13c59444703bc47eb13926385Brian
649ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusinenum pipe_error cso_set_viewport(struct cso_context *ctx,
650ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusin                                 const struct pipe_viewport_state *vp)
6517d95efde0a0e13e13c59444703bc47eb13926385Brian{
6527d95efde0a0e13e13c59444703bc47eb13926385Brian   if (memcmp(&ctx->vp, vp, sizeof(*vp))) {
6537d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->vp = *vp;
6547d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->pipe->set_viewport_state(ctx->pipe, vp);
6557d95efde0a0e13e13c59444703bc47eb13926385Brian   }
656ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusin   return PIPE_OK;
6577d95efde0a0e13e13c59444703bc47eb13926385Brian}
6587d95efde0a0e13e13c59444703bc47eb13926385Brian
6597d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_save_viewport(struct cso_context *ctx)
6607d95efde0a0e13e13c59444703bc47eb13926385Brian{
6617d95efde0a0e13e13c59444703bc47eb13926385Brian   ctx->vp_saved = ctx->vp;
6627d95efde0a0e13e13c59444703bc47eb13926385Brian}
6637d95efde0a0e13e13c59444703bc47eb13926385Brian
6647d95efde0a0e13e13c59444703bc47eb13926385Brian
6657d95efde0a0e13e13c59444703bc47eb13926385Brianvoid cso_restore_viewport(struct cso_context *ctx)
6667d95efde0a0e13e13c59444703bc47eb13926385Brian{
6677d95efde0a0e13e13c59444703bc47eb13926385Brian   if (memcmp(&ctx->vp, &ctx->vp_saved, sizeof(ctx->vp))) {
6687d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->vp = ctx->vp_saved;
6697d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->pipe->set_viewport_state(ctx->pipe, &ctx->vp);
6707d95efde0a0e13e13c59444703bc47eb13926385Brian   }
6717d95efde0a0e13e13c59444703bc47eb13926385Brian}
6727d95efde0a0e13e13c59444703bc47eb13926385Brian
6737d95efde0a0e13e13c59444703bc47eb13926385Brian
674ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusinenum pipe_error cso_set_blend_color(struct cso_context *ctx,
675ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusin                                    const struct pipe_blend_color *bc)
6767d95efde0a0e13e13c59444703bc47eb13926385Brian{
6777d95efde0a0e13e13c59444703bc47eb13926385Brian   if (memcmp(&ctx->blend_color, bc, sizeof(ctx->blend_color))) {
6787d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->blend_color = *bc;
6797d95efde0a0e13e13c59444703bc47eb13926385Brian      ctx->pipe->set_blend_color(ctx->pipe, bc);
6807d95efde0a0e13e13c59444703bc47eb13926385Brian   }
681ed187d39a6e0fd921b2a45a143d88ac4b66eee91Zack Rusin   return PIPE_OK;
682e5b19a0f833b5a3d5ffcf50d25a620d00bd8914bBrian}
68389d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin
684aac2cccccfd701ae8d7ce0813c28c64498d4a076Roland Scheideggerenum pipe_error cso_set_sample_mask(struct cso_context *ctx,
685aac2cccccfd701ae8d7ce0813c28c64498d4a076Roland Scheidegger                                    unsigned sample_mask)
686aac2cccccfd701ae8d7ce0813c28c64498d4a076Roland Scheidegger{
687aac2cccccfd701ae8d7ce0813c28c64498d4a076Roland Scheidegger   if (ctx->sample_mask != sample_mask) {
688aac2cccccfd701ae8d7ce0813c28c64498d4a076Roland Scheidegger      ctx->sample_mask = sample_mask;
689aac2cccccfd701ae8d7ce0813c28c64498d4a076Roland Scheidegger      ctx->pipe->set_sample_mask(ctx->pipe, sample_mask);
690aac2cccccfd701ae8d7ce0813c28c64498d4a076Roland Scheidegger   }
691aac2cccccfd701ae8d7ce0813c28c64498d4a076Roland Scheidegger   return PIPE_OK;
692aac2cccccfd701ae8d7ce0813c28c64498d4a076Roland Scheidegger}
693aac2cccccfd701ae8d7ce0813c28c64498d4a076Roland Scheidegger
694e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšákvoid cso_save_sample_mask(struct cso_context *ctx)
695e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšák{
696e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšák   ctx->sample_mask_saved = ctx->sample_mask;
697e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšák}
698e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšák
699e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšákvoid cso_restore_sample_mask(struct cso_context *ctx)
700e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšák{
701e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšák   cso_set_sample_mask(ctx, ctx->sample_mask_saved);
702e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšák}
703e7689303a8e4790c38cc69ae7a197712f98e8f5bMarek Olšák
7041a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheideggerenum pipe_error cso_set_stencil_ref(struct cso_context *ctx,
7051a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger                                    const struct pipe_stencil_ref *sr)
7061a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger{
7071a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger   if (memcmp(&ctx->stencil_ref, sr, sizeof(ctx->stencil_ref))) {
7081a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger      ctx->stencil_ref = *sr;
7091a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger      ctx->pipe->set_stencil_ref(ctx->pipe, sr);
7101a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger   }
7111a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger   return PIPE_OK;
7121a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger}
7131a859ecf4a4728cb321b1f68d51491cd285a3c03Roland Scheidegger
714263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheideggervoid cso_save_stencil_ref(struct cso_context *ctx)
715263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger{
716263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger   ctx->stencil_ref_saved = ctx->stencil_ref;
717263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger}
718263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger
719263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger
720263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheideggervoid cso_restore_stencil_ref(struct cso_context *ctx)
721263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger{
722076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul   if (memcmp(&ctx->stencil_ref, &ctx->stencil_ref_saved,
723076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul              sizeof(ctx->stencil_ref))) {
724263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger      ctx->stencil_ref = ctx->stencil_ref_saved;
725263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger      ctx->pipe->set_stencil_ref(ctx->pipe, &ctx->stencil_ref);
726263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger   }
727263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger}
728263406addb90ca7599ddfb210944ab0dd63b973cRoland Scheidegger
72989d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusinenum pipe_error cso_set_geometry_shader_handle(struct cso_context *ctx,
73089d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin                                               void *handle)
73189d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin{
732d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák   assert(ctx->has_geometry_shader || !handle);
733d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák
734d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák   if (ctx->has_geometry_shader && ctx->geometry_shader != handle) {
73589d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin      ctx->geometry_shader = handle;
73689d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin      ctx->pipe->bind_gs_state(ctx->pipe, handle);
73789d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin   }
73889d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin   return PIPE_OK;
73989d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin}
74089d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin
74189d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusinvoid cso_delete_geometry_shader(struct cso_context *ctx, void *handle)
74289d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin{
74389d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin    if (handle == ctx->geometry_shader) {
74489d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin      /* unbind before deleting */
74589d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin      ctx->pipe->bind_gs_state(ctx->pipe, NULL);
74689d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin      ctx->geometry_shader = NULL;
74789d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin   }
74889d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin   ctx->pipe->delete_gs_state(ctx->pipe, handle);
74989d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin}
75089d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin
75189d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusinvoid cso_save_geometry_shader(struct cso_context *ctx)
75289d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin{
753d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák   if (!ctx->has_geometry_shader) {
754d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák      return;
755d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák   }
756d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák
75789d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin   assert(!ctx->geometry_shader_saved);
75889d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin   ctx->geometry_shader_saved = ctx->geometry_shader;
75989d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin}
76089d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin
76189d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusinvoid cso_restore_geometry_shader(struct cso_context *ctx)
76289d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin{
763d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák   if (!ctx->has_geometry_shader) {
764d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák      return;
765d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák   }
766d2633af696f2e4ff98f669061e4e222e8643312cMarek Olšák
76789d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin   if (ctx->geometry_shader_saved != ctx->geometry_shader) {
76889d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin      ctx->pipe->bind_gs_state(ctx->pipe, ctx->geometry_shader_saved);
76989d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin      ctx->geometry_shader = ctx->geometry_shader_saved;
77089d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin   }
77189d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin   ctx->geometry_shader_saved = NULL;
77289d8577fb3036547ef0b47498cc8dc5c77f886e0Zack Rusin}
773227ae7b968c1351921babdbf6f052239766ffce4Michal Krol
774227ae7b968c1351921babdbf6f052239766ffce4Michal Krol/* clip state */
775227ae7b968c1351921babdbf6f052239766ffce4Michal Krol
776227ae7b968c1351921babdbf6f052239766ffce4Michal Krolstatic INLINE void
777227ae7b968c1351921babdbf6f052239766ffce4Michal Krolclip_state_cpy(struct pipe_clip_state *dst,
778227ae7b968c1351921babdbf6f052239766ffce4Michal Krol               const struct pipe_clip_state *src)
779227ae7b968c1351921babdbf6f052239766ffce4Michal Krol{
780dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   memcpy(dst->ucp, src->ucp, sizeof(dst->ucp));
781227ae7b968c1351921babdbf6f052239766ffce4Michal Krol}
782227ae7b968c1351921babdbf6f052239766ffce4Michal Krol
783227ae7b968c1351921babdbf6f052239766ffce4Michal Krolstatic INLINE int
784227ae7b968c1351921babdbf6f052239766ffce4Michal Krolclip_state_cmp(const struct pipe_clip_state *a,
785227ae7b968c1351921babdbf6f052239766ffce4Michal Krol               const struct pipe_clip_state *b)
786227ae7b968c1351921babdbf6f052239766ffce4Michal Krol{
787dc4c821f0817a3db716f965692fb701079f66340Marek Olšák   return memcmp(a->ucp, b->ucp, sizeof(a->ucp));
788227ae7b968c1351921babdbf6f052239766ffce4Michal Krol}
789227ae7b968c1351921babdbf6f052239766ffce4Michal Krol
790227ae7b968c1351921babdbf6f052239766ffce4Michal Krolvoid
791227ae7b968c1351921babdbf6f052239766ffce4Michal Krolcso_set_clip(struct cso_context *ctx,
792227ae7b968c1351921babdbf6f052239766ffce4Michal Krol             const struct pipe_clip_state *clip)
793227ae7b968c1351921babdbf6f052239766ffce4Michal Krol{
794227ae7b968c1351921babdbf6f052239766ffce4Michal Krol   if (clip_state_cmp(&ctx->clip, clip)) {
795227ae7b968c1351921babdbf6f052239766ffce4Michal Krol      clip_state_cpy(&ctx->clip, clip);
796227ae7b968c1351921babdbf6f052239766ffce4Michal Krol      ctx->pipe->set_clip_state(ctx->pipe, clip);
797227ae7b968c1351921babdbf6f052239766ffce4Michal Krol   }
798227ae7b968c1351921babdbf6f052239766ffce4Michal Krol}
799227ae7b968c1351921babdbf6f052239766ffce4Michal Krol
800227ae7b968c1351921babdbf6f052239766ffce4Michal Krolvoid
801227ae7b968c1351921babdbf6f052239766ffce4Michal Krolcso_save_clip(struct cso_context *ctx)
802227ae7b968c1351921babdbf6f052239766ffce4Michal Krol{
803227ae7b968c1351921babdbf6f052239766ffce4Michal Krol   clip_state_cpy(&ctx->clip_saved, &ctx->clip);
804227ae7b968c1351921babdbf6f052239766ffce4Michal Krol}
805227ae7b968c1351921babdbf6f052239766ffce4Michal Krol
806227ae7b968c1351921babdbf6f052239766ffce4Michal Krolvoid
807227ae7b968c1351921babdbf6f052239766ffce4Michal Krolcso_restore_clip(struct cso_context *ctx)
808227ae7b968c1351921babdbf6f052239766ffce4Michal Krol{
809227ae7b968c1351921babdbf6f052239766ffce4Michal Krol   if (clip_state_cmp(&ctx->clip, &ctx->clip_saved)) {
810227ae7b968c1351921babdbf6f052239766ffce4Michal Krol      clip_state_cpy(&ctx->clip, &ctx->clip_saved);
811227ae7b968c1351921babdbf6f052239766ffce4Michal Krol      ctx->pipe->set_clip_state(ctx->pipe, &ctx->clip_saved);
812227ae7b968c1351921babdbf6f052239766ffce4Michal Krol   }
813227ae7b968c1351921babdbf6f052239766ffce4Michal Krol}
814685340a53e1dcb2a888e634ee71ba24356befe69Roland Scheidegger
815076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paulenum pipe_error
816076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paulcso_set_vertex_elements(struct cso_context *ctx,
817076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                        unsigned count,
818076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                        const struct pipe_vertex_element *states)
81951d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger{
820e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   struct u_vbuf *vbuf = ctx->vbuf;
82151d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   unsigned key_size, hash_key;
82251d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   struct cso_hash_iter iter;
82351d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   void *handle;
824fe9f8536f1b1e7a3a2ac10afd8078e8f4d327578Roland Scheidegger   struct cso_velems_state velems_state;
825fe9f8536f1b1e7a3a2ac10afd8078e8f4d327578Roland Scheidegger
826e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   if (vbuf) {
827e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      u_vbuf_set_vertex_elements(vbuf, count, states);
828e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      return PIPE_OK;
829e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   }
830e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
831076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul   /* Need to include the count into the stored state data too.
832076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul    * Otherwise first few count pipe_vertex_elements could be identical
833076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul    * even if count is different, and there's no guarantee the hash would
834076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul    * be different in that case neither.
835076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul    */
836fe9f8536f1b1e7a3a2ac10afd8078e8f4d327578Roland Scheidegger   key_size = sizeof(struct pipe_vertex_element) * count + sizeof(unsigned);
837fe9f8536f1b1e7a3a2ac10afd8078e8f4d327578Roland Scheidegger   velems_state.count = count;
838076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul   memcpy(velems_state.velems, states,
839076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul          sizeof(struct pipe_vertex_element) * count);
840fe9f8536f1b1e7a3a2ac10afd8078e8f4d327578Roland Scheidegger   hash_key = cso_construct_key((void*)&velems_state, key_size);
841076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul   iter = cso_find_state_template(ctx->cache, hash_key, CSO_VELEMENTS,
842076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                                  (void*)&velems_state, key_size);
84351d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger
84451d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   if (cso_hash_iter_is_null(iter)) {
84551d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      struct cso_velements *cso = MALLOC(sizeof(struct cso_velements));
84651d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      if (!cso)
84751d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger         return PIPE_ERROR_OUT_OF_MEMORY;
84851d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger
849fe9f8536f1b1e7a3a2ac10afd8078e8f4d327578Roland Scheidegger      memcpy(&cso->state, &velems_state, key_size);
850076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      cso->data = ctx->pipe->create_vertex_elements_state(ctx->pipe, count,
851076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul                                                      &cso->state.velems[0]);
852076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul      cso->delete_state =
853076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul         (cso_state_callback) ctx->pipe->delete_vertex_elements_state;
85451d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      cso->context = ctx->pipe;
85551d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger
85651d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      iter = cso_insert_state(ctx->cache, hash_key, CSO_VELEMENTS, cso);
85751d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      if (cso_hash_iter_is_null(iter)) {
85851d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger         FREE(cso);
85951d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger         return PIPE_ERROR_OUT_OF_MEMORY;
86051d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      }
86151d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger
86251d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      handle = cso->data;
86351d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   }
86451d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   else {
86551d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      handle = ((struct cso_velements *)cso_hash_iter_data(iter))->data;
86651d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   }
86751d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger
86851d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   if (ctx->velements != handle) {
86951d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      ctx->velements = handle;
87051d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      ctx->pipe->bind_vertex_elements_state(ctx->pipe, handle);
87151d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   }
87251d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   return PIPE_OK;
87351d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger}
87451d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger
87551d139f03898e5e46af6363c6bba131455738cc4Roland Scheideggervoid cso_save_vertex_elements(struct cso_context *ctx)
87651d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger{
877e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   struct u_vbuf *vbuf = ctx->vbuf;
878e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
879e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   if (vbuf) {
880e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      u_vbuf_save_vertex_elements(vbuf);
881e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      return;
882e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   }
883e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
884a73fd447d4bb3d509fedf52b18a50fccab618298Corbin Simpson   assert(!ctx->velements_saved);
88551d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   ctx->velements_saved = ctx->velements;
88651d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger}
88751d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger
88851d139f03898e5e46af6363c6bba131455738cc4Roland Scheideggervoid cso_restore_vertex_elements(struct cso_context *ctx)
88951d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger{
890e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   struct u_vbuf *vbuf = ctx->vbuf;
891e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
892e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   if (vbuf) {
893e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      u_vbuf_restore_vertex_elements(vbuf);
894e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      return;
895e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   }
896e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
89751d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   if (ctx->velements != ctx->velements_saved) {
89851d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      ctx->velements = ctx->velements_saved;
89951d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger      ctx->pipe->bind_vertex_elements_state(ctx->pipe, ctx->velements_saved);
90051d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   }
90151d139f03898e5e46af6363c6bba131455738cc4Roland Scheidegger   ctx->velements_saved = NULL;
902685340a53e1dcb2a888e634ee71ba24356befe69Roland Scheidegger}
90308f89988c8738029c60e89c61c9da0522bd53087Michal Krol
904d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák/* vertex buffers */
905d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák
906d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšákvoid cso_set_vertex_buffers(struct cso_context *ctx,
907d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            unsigned count,
908d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            const struct pipe_vertex_buffer *buffers)
909d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák{
910e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   struct u_vbuf *vbuf = ctx->vbuf;
911e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
912e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   if (vbuf) {
913e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      u_vbuf_set_vertex_buffers(vbuf, count, buffers);
914e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      return;
915e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   }
916e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
917d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   if (count != ctx->nr_vertex_buffers ||
918d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák       memcmp(buffers, ctx->vertex_buffers,
919d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák              sizeof(struct pipe_vertex_buffer) * count) != 0) {
920d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák      util_copy_vertex_buffers(ctx->vertex_buffers, &ctx->nr_vertex_buffers,
921d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                               buffers, count);
922d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák      ctx->pipe->set_vertex_buffers(ctx->pipe, count, buffers);
923d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   }
924d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák}
925d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák
926d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšákvoid cso_save_vertex_buffers(struct cso_context *ctx)
927d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák{
928e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   struct u_vbuf *vbuf = ctx->vbuf;
929e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
930e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   if (vbuf) {
931e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      u_vbuf_save_vertex_buffers(vbuf);
932e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      return;
933e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   }
934e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
935d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   util_copy_vertex_buffers(ctx->vertex_buffers_saved,
936d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            &ctx->nr_vertex_buffers_saved,
937d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            ctx->vertex_buffers,
938d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            ctx->nr_vertex_buffers);
939d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák}
940d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák
941d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšákvoid cso_restore_vertex_buffers(struct cso_context *ctx)
942d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák{
9436f03692775ed49035802d660516b7d7464c7a1adMarek Olšák   unsigned i;
944e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   struct u_vbuf *vbuf = ctx->vbuf;
945e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
946e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   if (vbuf) {
947e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      u_vbuf_restore_vertex_buffers(vbuf);
948e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      return;
949e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   }
9506f03692775ed49035802d660516b7d7464c7a1adMarek Olšák
951d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   util_copy_vertex_buffers(ctx->vertex_buffers,
952d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            &ctx->nr_vertex_buffers,
953d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            ctx->vertex_buffers_saved,
954d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                            ctx->nr_vertex_buffers_saved);
9556f03692775ed49035802d660516b7d7464c7a1adMarek Olšák
9566f03692775ed49035802d660516b7d7464c7a1adMarek Olšák   for (i = 0; i < ctx->nr_vertex_buffers_saved; i++) {
9576f03692775ed49035802d660516b7d7464c7a1adMarek Olšák      pipe_resource_reference(&ctx->vertex_buffers_saved[i].buffer, NULL);
9586f03692775ed49035802d660516b7d7464c7a1adMarek Olšák   }
9596f03692775ed49035802d660516b7d7464c7a1adMarek Olšák   ctx->nr_vertex_buffers_saved = 0;
9606f03692775ed49035802d660516b7d7464c7a1adMarek Olšák
961d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák   ctx->pipe->set_vertex_buffers(ctx->pipe, ctx->nr_vertex_buffers,
962d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák                                 ctx->vertex_buffers);
963d5062fb3a315c46d77d5c954a3e3c14be1907d33Marek Olšák}
96408f89988c8738029c60e89c61c9da0522bd53087Michal Krol
965c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
966c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul/**************** fragment/vertex sampler view state *************************/
967c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
968c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulstatic enum pipe_error
969c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulsingle_sampler(struct cso_context *ctx,
970c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul               struct sampler_info *info,
971c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul               unsigned idx,
972c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul               const struct pipe_sampler_state *templ)
97308f89988c8738029c60e89c61c9da0522bd53087Michal Krol{
974c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   void *handle = NULL;
97508f89988c8738029c60e89c61c9da0522bd53087Michal Krol
976c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   if (templ != NULL) {
977c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      unsigned key_size = sizeof(struct pipe_sampler_state);
978c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      unsigned hash_key = cso_construct_key((void*)templ, key_size);
979c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      struct cso_hash_iter iter =
980c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         cso_find_state_template(ctx->cache,
981c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul                                 hash_key, CSO_SAMPLER,
982c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul                                 (void *) templ, key_size);
983c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
984c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      if (cso_hash_iter_is_null(iter)) {
985c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         struct cso_sampler *cso = MALLOC(sizeof(struct cso_sampler));
986c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         if (!cso)
987c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul            return PIPE_ERROR_OUT_OF_MEMORY;
988c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
989c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         memcpy(&cso->state, templ, sizeof(*templ));
990c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         cso->data = ctx->pipe->create_sampler_state(ctx->pipe, &cso->state);
991076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul         cso->delete_state =
992076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul            (cso_state_callback) ctx->pipe->delete_sampler_state;
993c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         cso->context = ctx->pipe;
994c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
995c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         iter = cso_insert_state(ctx->cache, hash_key, CSO_SAMPLER, cso);
996c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         if (cso_hash_iter_is_null(iter)) {
997c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul            FREE(cso);
998c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul            return PIPE_ERROR_OUT_OF_MEMORY;
999c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         }
1000c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1001c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         handle = cso->data;
1002c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      }
1003c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      else {
1004c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         handle = ((struct cso_sampler *)cso_hash_iter_data(iter))->data;
1005c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      }
100608f89988c8738029c60e89c61c9da0522bd53087Michal Krol   }
100708f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1008c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   info->samplers[idx] = handle;
100908f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1010c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   return PIPE_OK;
101108f89988c8738029c60e89c61c9da0522bd53087Michal Krol}
101208f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1013c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulenum pipe_error
1014c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulcso_single_sampler(struct cso_context *ctx,
1015ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul                   unsigned shader_stage,
1016c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul                   unsigned idx,
1017c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul                   const struct pipe_sampler_state *templ)
101808f89988c8738029c60e89c61c9da0522bd53087Michal Krol{
1019ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   return single_sampler(ctx, &ctx->samplers[shader_stage], idx, templ);
1020c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul}
102108f89988c8738029c60e89c61c9da0522bd53087Michal Krol
102208f89988c8738029c60e89c61c9da0522bd53087Michal Krol
102308f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1024c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulstatic void
1025ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paulsingle_sampler_done(struct cso_context *ctx, unsigned shader_stage)
1026c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul{
1027ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   struct sampler_info *info = &ctx->samplers[shader_stage];
1028c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   unsigned i;
1029c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1030c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   /* find highest non-null sampler */
1031c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   for (i = PIPE_MAX_SAMPLERS; i > 0; i--) {
1032c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      if (info->samplers[i - 1] != NULL)
1033c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         break;
1034c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   }
1035c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1036c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   info->nr_samplers = i;
1037c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1038c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   if (info->hw.nr_samplers != info->nr_samplers ||
1039c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul       memcmp(info->hw.samplers,
1040c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul              info->samplers,
1041076e5eacf1e5653acd1e99f0e7fdfe7499da5665Brian Paul              info->nr_samplers * sizeof(void *)) != 0)
1042c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   {
1043c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      memcpy(info->hw.samplers,
1044c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul             info->samplers,
1045c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul             info->nr_samplers * sizeof(void *));
1046c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      info->hw.nr_samplers = info->nr_samplers;
1047c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1048ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      switch (shader_stage) {
1049ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      case PIPE_SHADER_FRAGMENT:
1050c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         ctx->pipe->bind_fragment_sampler_states(ctx->pipe,
1051c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul                                                 info->nr_samplers,
1052c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul                                                 info->samplers);
1053ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul         break;
1054ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      case PIPE_SHADER_VERTEX:
1055c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         ctx->pipe->bind_vertex_sampler_states(ctx->pipe,
1056c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul                                               info->nr_samplers,
1057c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul                                               info->samplers);
1058ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul         break;
1059ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      case PIPE_SHADER_GEOMETRY:
1060ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul         ctx->pipe->bind_geometry_sampler_states(ctx->pipe,
1061ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul                                               info->nr_samplers,
1062ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul                                               info->samplers);
1063ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul         break;
1064ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      default:
1065ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul         assert(!"bad shader type in single_sampler_done()");
1066c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      }
106708f89988c8738029c60e89c61c9da0522bd53087Michal Krol   }
106808f89988c8738029c60e89c61c9da0522bd53087Michal Krol}
106908f89988c8738029c60e89c61c9da0522bd53087Michal Krol
107008f89988c8738029c60e89c61c9da0522bd53087Michal Krolvoid
1071ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paulcso_single_sampler_done(struct cso_context *ctx, unsigned shader_stage)
1072c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul{
1073ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   single_sampler_done(ctx, shader_stage);
1074c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul}
1075c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1076c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1077c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul/*
1078c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul * If the function encouters any errors it will return the
1079c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul * last one. Done to always try to set as many samplers
1080c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul * as possible.
1081c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul */
1082ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paulenum pipe_error
1083ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paulcso_set_samplers(struct cso_context *ctx,
1084ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul                 unsigned shader_stage,
1085ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul                 unsigned nr,
1086ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul                 const struct pipe_sampler_state **templates)
1087c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul{
1088ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   struct sampler_info *info = &ctx->samplers[shader_stage];
1089c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   unsigned i;
1090c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   enum pipe_error temp, error = PIPE_OK;
109108f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1092c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   /* TODO: fastpath
1093c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul    */
1094c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1095c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   for (i = 0; i < nr; i++) {
1096c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      temp = single_sampler(ctx, info, i, templates[i]);
1097c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      if (temp != PIPE_OK)
1098c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         error = temp;
109908f89988c8738029c60e89c61c9da0522bd53087Michal Krol   }
1100c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1101c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   for ( ; i < info->nr_samplers; i++) {
1102c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      temp = single_sampler(ctx, info, i, NULL);
1103c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      if (temp != PIPE_OK)
1104c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul         error = temp;
110508f89988c8738029c60e89c61c9da0522bd53087Michal Krol   }
110608f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1107ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   single_sampler_done(ctx, shader_stage);
1108c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1109c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   return error;
1110c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul}
1111c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1112ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paulvoid
1113ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paulcso_save_samplers(struct cso_context *ctx, unsigned shader_stage)
1114c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul{
1115ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   struct sampler_info *info = &ctx->samplers[shader_stage];
1116c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   info->nr_samplers_saved = info->nr_samplers;
1117c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   memcpy(info->samplers_saved, info->samplers, sizeof(info->samplers));
1118c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul}
111908f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1120c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1121c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulvoid
1122ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paulcso_restore_samplers(struct cso_context *ctx, unsigned shader_stage)
1123c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul{
1124ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   struct sampler_info *info = &ctx->samplers[shader_stage];
1125c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   info->nr_samplers = info->nr_samplers_saved;
1126c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   memcpy(info->samplers, info->samplers_saved, sizeof(info->samplers));
1127ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   single_sampler_done(ctx, shader_stage);
1128c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul}
1129c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1130c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1131c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulvoid
1132ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paulcso_set_sampler_views(struct cso_context *ctx,
1133ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul                      unsigned shader_stage,
1134ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul                      unsigned count,
1135ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul                      struct pipe_sampler_view **views)
1136c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul{
1137ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   struct sampler_info *info = &ctx->samplers[shader_stage];
1138ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   unsigned i;
113908f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1140c5fb2c60bfe114d993da6c416a39c7873ab9cb3dBrian Paul   /* reference new views */
114108f89988c8738029c60e89c61c9da0522bd53087Michal Krol   for (i = 0; i < count; i++) {
1142c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      pipe_sampler_view_reference(&info->views[i], views[i]);
114308f89988c8738029c60e89c61c9da0522bd53087Michal Krol   }
1144c5fb2c60bfe114d993da6c416a39c7873ab9cb3dBrian Paul   /* unref extra old views, if any */
1145c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   for (; i < info->nr_views; i++) {
1146c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      pipe_sampler_view_reference(&info->views[i], NULL);
114708f89988c8738029c60e89c61c9da0522bd53087Michal Krol   }
114808f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1149c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   info->nr_views = count;
1150c5fb2c60bfe114d993da6c416a39c7873ab9cb3dBrian Paul
1151c5fb2c60bfe114d993da6c416a39c7873ab9cb3dBrian Paul   /* bind the new sampler views */
1152ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   switch (shader_stage) {
1153ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   case PIPE_SHADER_FRAGMENT:
1154ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      ctx->pipe->set_fragment_sampler_views(ctx->pipe, count, info->views);
1155ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      break;
1156ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   case PIPE_SHADER_VERTEX:
1157ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      ctx->pipe->set_vertex_sampler_views(ctx->pipe, count, info->views);
1158ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      break;
1159ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   case PIPE_SHADER_GEOMETRY:
1160ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      ctx->pipe->set_geometry_sampler_views(ctx->pipe, count, info->views);
1161ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      break;
1162ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   default:
1163ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      assert(!"bad shader type in cso_set_sampler_views()");
1164ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   }
116508f89988c8738029c60e89c61c9da0522bd53087Michal Krol}
116608f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1167c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1168c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulvoid
1169ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paulcso_save_sampler_views(struct cso_context *ctx, unsigned shader_stage)
1170c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul{
1171ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   struct sampler_info *info = &ctx->samplers[shader_stage];
1172ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   unsigned i;
1173c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1174c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   info->nr_views_saved = info->nr_views;
1175c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1176c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   for (i = 0; i < info->nr_views; i++) {
1177c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      assert(!info->views_saved[i]);
1178c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      pipe_sampler_view_reference(&info->views_saved[i], info->views[i]);
117908f89988c8738029c60e89c61c9da0522bd53087Michal Krol   }
118008f89988c8738029c60e89c61c9da0522bd53087Michal Krol}
118108f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1182c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1183c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paulvoid
1184ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paulcso_restore_sampler_views(struct cso_context *ctx, unsigned shader_stage)
1185c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul{
1186ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   struct sampler_info *info = &ctx->samplers[shader_stage];
1187ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   unsigned i, nr_saved = info->nr_views_saved;
1188c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1189ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   for (i = 0; i < nr_saved; i++) {
1190c5e48025ed8dd44de63144ee3621c6aa4b5add81Marek Olšák      pipe_sampler_view_reference(&info->views[i], NULL);
1191c5e48025ed8dd44de63144ee3621c6aa4b5add81Marek Olšák      /* move the reference from one pointer to another */
1192c5e48025ed8dd44de63144ee3621c6aa4b5add81Marek Olšák      info->views[i] = info->views_saved[i];
1193c5e48025ed8dd44de63144ee3621c6aa4b5add81Marek Olšák      info->views_saved[i] = NULL;
119408f89988c8738029c60e89c61c9da0522bd53087Michal Krol   }
1195c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   for (; i < info->nr_views; i++) {
1196c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul      pipe_sampler_view_reference(&info->views[i], NULL);
119708f89988c8738029c60e89c61c9da0522bd53087Michal Krol   }
119808f89988c8738029c60e89c61c9da0522bd53087Michal Krol
1199c5fb2c60bfe114d993da6c416a39c7873ab9cb3dBrian Paul   /* bind the old/saved sampler views */
1200ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   switch (shader_stage) {
1201ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   case PIPE_SHADER_FRAGMENT:
1202ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      ctx->pipe->set_fragment_sampler_views(ctx->pipe, nr_saved, info->views);
1203ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      break;
1204ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   case PIPE_SHADER_VERTEX:
1205ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      ctx->pipe->set_vertex_sampler_views(ctx->pipe, nr_saved, info->views);
1206ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      break;
1207ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   case PIPE_SHADER_GEOMETRY:
1208ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      ctx->pipe->set_geometry_sampler_views(ctx->pipe, nr_saved, info->views);
1209ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      break;
1210ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   default:
1211ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul      assert(!"bad shader type in cso_restore_sampler_views()");
1212ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   }
1213c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1214ea6f035ae90895bd4ee3247408eb179dfdf96d22Brian Paul   info->nr_views = nr_saved;
1215c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul   info->nr_views_saved = 0;
1216c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul}
1217c06fa98c86abbff730a098ffbe980347b3c7d0e6Brian Paul
1218c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1219c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšákvoid
1220c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšákcso_set_stream_outputs(struct cso_context *ctx,
1221c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák                       unsigned num_targets,
1222c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák                       struct pipe_stream_output_target **targets,
1223c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák                       unsigned append_bitmask)
1224c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák{
1225c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   struct pipe_context *pipe = ctx->pipe;
1226c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   uint i;
1227c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1228c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   if (!ctx->has_streamout) {
1229c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      assert(num_targets == 0);
1230c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      return;
1231c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
1232c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1233c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   if (ctx->nr_so_targets == 0 && num_targets == 0) {
1234c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      /* Nothing to do. */
1235c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      return;
1236c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
1237c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1238c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   /* reference new targets */
1239c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   for (i = 0; i < num_targets; i++) {
1240c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      pipe_so_target_reference(&ctx->so_targets[i], targets[i]);
1241c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
1242c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   /* unref extra old targets, if any */
1243c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   for (; i < ctx->nr_so_targets; i++) {
1244c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      pipe_so_target_reference(&ctx->so_targets[i], NULL);
1245c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
1246c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1247c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   pipe->set_stream_output_targets(pipe, num_targets, targets,
1248c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák                                   append_bitmask);
1249c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   ctx->nr_so_targets = num_targets;
1250c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák}
1251c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1252c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšákvoid
1253c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšákcso_save_stream_outputs(struct cso_context *ctx)
1254c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák{
1255c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   uint i;
1256c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1257c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   if (!ctx->has_streamout) {
1258c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      return;
1259c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
1260c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1261c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   ctx->nr_so_targets_saved = ctx->nr_so_targets;
1262c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1263c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   for (i = 0; i < ctx->nr_so_targets; i++) {
1264c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      assert(!ctx->so_targets_saved[i]);
1265c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      pipe_so_target_reference(&ctx->so_targets_saved[i], ctx->so_targets[i]);
1266c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
1267c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák}
1268c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1269c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšákvoid
1270c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšákcso_restore_stream_outputs(struct cso_context *ctx)
1271c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák{
1272c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   struct pipe_context *pipe = ctx->pipe;
1273c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   uint i;
1274c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1275c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   if (!ctx->has_streamout) {
1276c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      return;
1277c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
1278c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1279c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   if (ctx->nr_so_targets == 0 && ctx->nr_so_targets_saved == 0) {
1280c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      /* Nothing to do. */
1281c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      return;
1282c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
1283c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1284c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   for (i = 0; i < ctx->nr_so_targets_saved; i++) {
1285c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      pipe_so_target_reference(&ctx->so_targets[i], NULL);
1286c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      /* move the reference from one pointer to another */
1287c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      ctx->so_targets[i] = ctx->so_targets_saved[i];
1288c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      ctx->so_targets_saved[i] = NULL;
1289c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
1290c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   for (; i < ctx->nr_so_targets; i++) {
1291c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák      pipe_so_target_reference(&ctx->so_targets[i], NULL);
1292c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   }
1293c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1294c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   /* ~0 means append */
1295c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   pipe->set_stream_output_targets(pipe, ctx->nr_so_targets_saved,
1296c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák                                   ctx->so_targets, ~0);
1297c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák
1298c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   ctx->nr_so_targets = ctx->nr_so_targets_saved;
1299c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák   ctx->nr_so_targets_saved = 0;
1300c05fafa4a0fd93d4264c46578e23a83ecf2b481eMarek Olšák}
130176eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák
130276eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák/* drawing */
130376eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák
130476eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšákvoid
130576eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšákcso_set_index_buffer(struct cso_context *cso,
130676eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák                     const struct pipe_index_buffer *ib)
130776eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák{
1308e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   struct u_vbuf *vbuf = cso->vbuf;
1309e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
1310e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   if (vbuf) {
1311e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      u_vbuf_set_index_buffer(vbuf, ib);
1312e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   } else {
1313e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      struct pipe_context *pipe = cso->pipe;
1314e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      pipe->set_index_buffer(pipe, ib);
1315e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   }
131676eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák}
131776eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák
131876eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšákvoid
131976eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšákcso_draw_vbo(struct cso_context *cso,
132076eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák             const struct pipe_draw_info *info)
132176eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák{
1322e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   struct u_vbuf *vbuf = cso->vbuf;
1323e0773da1e897164ed7597437070e32b867734ee5Marek Olšák
1324e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   if (vbuf) {
1325e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      u_vbuf_draw_vbo(vbuf, info);
1326e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   } else {
1327e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      struct pipe_context *pipe = cso->pipe;
1328e0773da1e897164ed7597437070e32b867734ee5Marek Olšák      pipe->draw_vbo(pipe, info);
1329e0773da1e897164ed7597437070e32b867734ee5Marek Olšák   }
133076eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák}
133176eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák
133276eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšákvoid
133376eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšákcso_draw_arrays(struct cso_context *cso, uint mode, uint start, uint count)
133476eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák{
133576eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák   struct pipe_draw_info info;
133676eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák
133776eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák   util_draw_init_info(&info);
133876eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák
133976eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák   info.mode = mode;
134076eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák   info.start = start;
134176eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák   info.count = count;
134276eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák   info.min_index = start;
134376eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák   info.max_index = start + count - 1;
134476eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák
134576eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák   cso_draw_vbo(cso, &info);
134676eefcc70cc62db7d226591de3f918ff102f6de3Marek Olšák}
1347