1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2012 Red Hat Inc.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
12bf215546Sopenharmony_ci * all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
21bf215546Sopenharmony_ci *
22bf215546Sopenharmony_ci * Authors: Ben Skeggs
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/format/u_format.h"
27bf215546Sopenharmony_ci#include "util/u_helpers.h"
28bf215546Sopenharmony_ci#include "util/u_inlines.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "nouveau_gldefs.h"
31bf215546Sopenharmony_ci#include "nv_object.xml.h"
32bf215546Sopenharmony_ci#include "nv30/nv30-40_3d.xml.h"
33bf215546Sopenharmony_ci#include "nv30/nv30_context.h"
34bf215546Sopenharmony_ci#include "nv30/nv30_winsys.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci#define NV40_3D_MRT_BLEND_ENABLE 0x0000036c
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_cistatic void *
39bf215546Sopenharmony_cinv30_blend_state_create(struct pipe_context *pipe,
40bf215546Sopenharmony_ci                        const struct pipe_blend_state *cso)
41bf215546Sopenharmony_ci{
42bf215546Sopenharmony_ci   struct nouveau_object *eng3d = nv30_context(pipe)->screen->eng3d;
43bf215546Sopenharmony_ci   struct nv30_blend_stateobj *so;
44bf215546Sopenharmony_ci   uint32_t blend[2], cmask[2];
45bf215546Sopenharmony_ci   int i;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   so = CALLOC_STRUCT(nv30_blend_stateobj);
48bf215546Sopenharmony_ci   if (!so)
49bf215546Sopenharmony_ci      return NULL;
50bf215546Sopenharmony_ci   so->pipe = *cso;
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci   if (cso->logicop_enable) {
53bf215546Sopenharmony_ci      SB_MTHD30(so, COLOR_LOGIC_OP_ENABLE, 2);
54bf215546Sopenharmony_ci      SB_DATA  (so, 1);
55bf215546Sopenharmony_ci      SB_DATA  (so, nvgl_logicop_func(cso->logicop_func));
56bf215546Sopenharmony_ci   } else {
57bf215546Sopenharmony_ci      SB_MTHD30(so, COLOR_LOGIC_OP_ENABLE, 1);
58bf215546Sopenharmony_ci      SB_DATA  (so, 0);
59bf215546Sopenharmony_ci   }
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   SB_MTHD30(so, DITHER_ENABLE, 1);
62bf215546Sopenharmony_ci   SB_DATA  (so, cso->dither);
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   blend[0] = cso->rt[0].blend_enable;
65bf215546Sopenharmony_ci   cmask[0] = !!(cso->rt[0].colormask & PIPE_MASK_A) << 24 |
66bf215546Sopenharmony_ci              !!(cso->rt[0].colormask & PIPE_MASK_R) << 16 |
67bf215546Sopenharmony_ci              !!(cso->rt[0].colormask & PIPE_MASK_G) <<  8 |
68bf215546Sopenharmony_ci              !!(cso->rt[0].colormask & PIPE_MASK_B);
69bf215546Sopenharmony_ci   if (cso->independent_blend_enable) {
70bf215546Sopenharmony_ci      blend[1] = 0;
71bf215546Sopenharmony_ci      cmask[1] = 0;
72bf215546Sopenharmony_ci      for (i = 1; i < 4; i++) {
73bf215546Sopenharmony_ci         blend[1] |= cso->rt[i].blend_enable << i;
74bf215546Sopenharmony_ci         cmask[1] |= !!(cso->rt[i].colormask & PIPE_MASK_A) << (0 + (i * 4)) |
75bf215546Sopenharmony_ci                     !!(cso->rt[i].colormask & PIPE_MASK_R) << (1 + (i * 4)) |
76bf215546Sopenharmony_ci                     !!(cso->rt[i].colormask & PIPE_MASK_G) << (2 + (i * 4)) |
77bf215546Sopenharmony_ci                     !!(cso->rt[i].colormask & PIPE_MASK_B) << (3 + (i * 4));
78bf215546Sopenharmony_ci      }
79bf215546Sopenharmony_ci   } else {
80bf215546Sopenharmony_ci      blend[1]  = 0x0000000e *   (blend[0] & 0x00000001);
81bf215546Sopenharmony_ci      cmask[1]  = 0x00001110 * !!(cmask[0] & 0x01000000);
82bf215546Sopenharmony_ci      cmask[1] |= 0x00002220 * !!(cmask[0] & 0x00010000);
83bf215546Sopenharmony_ci      cmask[1] |= 0x00004440 * !!(cmask[0] & 0x00000100);
84bf215546Sopenharmony_ci      cmask[1] |= 0x00008880 * !!(cmask[0] & 0x00000001);
85bf215546Sopenharmony_ci   }
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   if (eng3d->oclass >= NV40_3D_CLASS) {
88bf215546Sopenharmony_ci      SB_MTHD40(so, MRT_BLEND_ENABLE, 2);
89bf215546Sopenharmony_ci      SB_DATA  (so, blend[1]);
90bf215546Sopenharmony_ci      SB_DATA  (so, cmask[1]);
91bf215546Sopenharmony_ci   }
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   if (blend[0] || blend[1]) {
94bf215546Sopenharmony_ci      SB_MTHD30(so, BLEND_FUNC_ENABLE, 3);
95bf215546Sopenharmony_ci      SB_DATA  (so, blend[0]);
96bf215546Sopenharmony_ci      SB_DATA  (so, (nvgl_blend_func(cso->rt[0].alpha_src_factor) << 16) |
97bf215546Sopenharmony_ci                     nvgl_blend_func(cso->rt[0].rgb_src_factor));
98bf215546Sopenharmony_ci      SB_DATA  (so, (nvgl_blend_func(cso->rt[0].alpha_dst_factor) << 16) |
99bf215546Sopenharmony_ci                     nvgl_blend_func(cso->rt[0].rgb_dst_factor));
100bf215546Sopenharmony_ci      if (eng3d->oclass < NV40_3D_CLASS) {
101bf215546Sopenharmony_ci         SB_MTHD30(so, BLEND_EQUATION, 1);
102bf215546Sopenharmony_ci         SB_DATA  (so, nvgl_blend_eqn(cso->rt[0].rgb_func));
103bf215546Sopenharmony_ci      } else {
104bf215546Sopenharmony_ci         SB_MTHD40(so, BLEND_EQUATION, 1);
105bf215546Sopenharmony_ci         SB_DATA  (so, (nvgl_blend_eqn(cso->rt[0].alpha_func) << 16) |
106bf215546Sopenharmony_ci                        nvgl_blend_eqn(cso->rt[0].rgb_func));
107bf215546Sopenharmony_ci      }
108bf215546Sopenharmony_ci   } else {
109bf215546Sopenharmony_ci      SB_MTHD30(so, BLEND_FUNC_ENABLE, 1);
110bf215546Sopenharmony_ci      SB_DATA  (so, blend[0]);
111bf215546Sopenharmony_ci   }
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   SB_MTHD30(so, COLOR_MASK, 1);
114bf215546Sopenharmony_ci   SB_DATA  (so, cmask[0]);
115bf215546Sopenharmony_ci   return so;
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_cistatic void
119bf215546Sopenharmony_cinv30_blend_state_bind(struct pipe_context *pipe, void *hwcso)
120bf215546Sopenharmony_ci{
121bf215546Sopenharmony_ci   struct nv30_context *nv30 = nv30_context(pipe);
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_ci   nv30->blend = hwcso;
124bf215546Sopenharmony_ci   nv30->dirty |= NV30_NEW_BLEND;
125bf215546Sopenharmony_ci}
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_cistatic void
128bf215546Sopenharmony_cinv30_blend_state_delete(struct pipe_context *pipe, void *hwcso)
129bf215546Sopenharmony_ci{
130bf215546Sopenharmony_ci   FREE(hwcso);
131bf215546Sopenharmony_ci}
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_cistatic void *
134bf215546Sopenharmony_cinv30_rasterizer_state_create(struct pipe_context *pipe,
135bf215546Sopenharmony_ci                             const struct pipe_rasterizer_state *cso)
136bf215546Sopenharmony_ci{
137bf215546Sopenharmony_ci   struct nv30_rasterizer_stateobj *so;
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci   so = CALLOC_STRUCT(nv30_rasterizer_stateobj);
140bf215546Sopenharmony_ci   if (!so)
141bf215546Sopenharmony_ci      return NULL;
142bf215546Sopenharmony_ci   so->pipe = *cso;
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci   SB_MTHD30(so, SHADE_MODEL, 1);
145bf215546Sopenharmony_ci   SB_DATA  (so, cso->flatshade ? NV30_3D_SHADE_MODEL_FLAT :
146bf215546Sopenharmony_ci                                  NV30_3D_SHADE_MODEL_SMOOTH);
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   SB_MTHD30(so, POLYGON_MODE_FRONT, 6);
149bf215546Sopenharmony_ci   SB_DATA  (so, nvgl_polygon_mode(cso->fill_front));
150bf215546Sopenharmony_ci   SB_DATA  (so, nvgl_polygon_mode(cso->fill_back));
151bf215546Sopenharmony_ci   if (cso->cull_face == PIPE_FACE_FRONT_AND_BACK)
152bf215546Sopenharmony_ci      SB_DATA  (so, NV30_3D_CULL_FACE_FRONT_AND_BACK);
153bf215546Sopenharmony_ci   else
154bf215546Sopenharmony_ci   if (cso->cull_face == PIPE_FACE_FRONT)
155bf215546Sopenharmony_ci      SB_DATA  (so, NV30_3D_CULL_FACE_FRONT);
156bf215546Sopenharmony_ci   else
157bf215546Sopenharmony_ci      SB_DATA  (so, NV30_3D_CULL_FACE_BACK);
158bf215546Sopenharmony_ci   SB_DATA  (so, cso->front_ccw ? NV30_3D_FRONT_FACE_CCW :
159bf215546Sopenharmony_ci                                  NV30_3D_FRONT_FACE_CW);
160bf215546Sopenharmony_ci   SB_DATA  (so, cso->poly_smooth);
161bf215546Sopenharmony_ci   SB_DATA  (so, cso->cull_face != PIPE_FACE_NONE);
162bf215546Sopenharmony_ci
163bf215546Sopenharmony_ci   SB_MTHD30(so, POLYGON_OFFSET_POINT_ENABLE, 3);
164bf215546Sopenharmony_ci   SB_DATA  (so, cso->offset_point);
165bf215546Sopenharmony_ci   SB_DATA  (so, cso->offset_line);
166bf215546Sopenharmony_ci   SB_DATA  (so, cso->offset_tri);
167bf215546Sopenharmony_ci   if (cso->offset_point || cso->offset_line || cso->offset_tri) {
168bf215546Sopenharmony_ci      SB_MTHD30(so, POLYGON_OFFSET_FACTOR, 2);
169bf215546Sopenharmony_ci      SB_DATA  (so, fui(cso->offset_scale));
170bf215546Sopenharmony_ci      SB_DATA  (so, fui(cso->offset_units * 2.0));
171bf215546Sopenharmony_ci   }
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   SB_MTHD30(so, LINE_WIDTH, 2);
174bf215546Sopenharmony_ci   SB_DATA  (so, (unsigned char)(cso->line_width * 8.0) & 0xff);
175bf215546Sopenharmony_ci   SB_DATA  (so, cso->line_smooth);
176bf215546Sopenharmony_ci   SB_MTHD30(so, LINE_STIPPLE_ENABLE, 2);
177bf215546Sopenharmony_ci   SB_DATA  (so, cso->line_stipple_enable);
178bf215546Sopenharmony_ci   SB_DATA  (so, (cso->line_stipple_pattern << 16) |
179bf215546Sopenharmony_ci                  cso->line_stipple_factor);
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci   SB_MTHD30(so, VERTEX_TWO_SIDE_ENABLE, 1);
182bf215546Sopenharmony_ci   SB_DATA  (so, cso->light_twoside);
183bf215546Sopenharmony_ci   SB_MTHD30(so, POLYGON_STIPPLE_ENABLE, 1);
184bf215546Sopenharmony_ci   SB_DATA  (so, cso->poly_stipple_enable);
185bf215546Sopenharmony_ci   SB_MTHD30(so, POINT_SIZE, 1);
186bf215546Sopenharmony_ci   SB_DATA  (so, fui(cso->point_size));
187bf215546Sopenharmony_ci   SB_MTHD30(so, FLATSHADE_FIRST, 1);
188bf215546Sopenharmony_ci   SB_DATA  (so, cso->flatshade_first);
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci   SB_MTHD30(so, DEPTH_CONTROL, 1);
191bf215546Sopenharmony_ci   SB_DATA  (so, cso->depth_clip_near ? 0x00000001 : 0x00000010);
192bf215546Sopenharmony_ci   return so;
193bf215546Sopenharmony_ci}
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_cistatic void
196bf215546Sopenharmony_cinv30_rasterizer_state_bind(struct pipe_context *pipe, void *hwcso)
197bf215546Sopenharmony_ci{
198bf215546Sopenharmony_ci   struct nv30_context *nv30 = nv30_context(pipe);
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_ci   nv30->rast = hwcso;
201bf215546Sopenharmony_ci   nv30->dirty |= NV30_NEW_RASTERIZER;
202bf215546Sopenharmony_ci}
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_cistatic void
205bf215546Sopenharmony_cinv30_rasterizer_state_delete(struct pipe_context *pipe, void *hwcso)
206bf215546Sopenharmony_ci{
207bf215546Sopenharmony_ci   FREE(hwcso);
208bf215546Sopenharmony_ci}
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_cistatic void *
211bf215546Sopenharmony_cinv30_zsa_state_create(struct pipe_context *pipe,
212bf215546Sopenharmony_ci                      const struct pipe_depth_stencil_alpha_state *cso)
213bf215546Sopenharmony_ci{
214bf215546Sopenharmony_ci   struct nouveau_object *eng3d = nv30_context(pipe)->screen->eng3d;
215bf215546Sopenharmony_ci   struct nv30_zsa_stateobj *so;
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci   so = CALLOC_STRUCT(nv30_zsa_stateobj);
218bf215546Sopenharmony_ci   if (!so)
219bf215546Sopenharmony_ci      return NULL;
220bf215546Sopenharmony_ci   so->pipe = *cso;
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci   SB_MTHD30(so, DEPTH_FUNC, 3);
223bf215546Sopenharmony_ci   SB_DATA  (so, nvgl_comparison_op(cso->depth_func));
224bf215546Sopenharmony_ci   SB_DATA  (so, cso->depth_writemask);
225bf215546Sopenharmony_ci   SB_DATA  (so, cso->depth_enabled);
226bf215546Sopenharmony_ci
227bf215546Sopenharmony_ci   if (eng3d->oclass == NV35_3D_CLASS || eng3d->oclass >= NV40_3D_CLASS) {
228bf215546Sopenharmony_ci      SB_MTHD35(so, DEPTH_BOUNDS_TEST_ENABLE, 3);
229bf215546Sopenharmony_ci      SB_DATA  (so, cso->depth_bounds_test);
230bf215546Sopenharmony_ci      SB_DATA  (so, fui(cso->depth_bounds_min));
231bf215546Sopenharmony_ci      SB_DATA  (so, fui(cso->depth_bounds_max));
232bf215546Sopenharmony_ci   }
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci   if (cso->stencil[0].enabled) {
235bf215546Sopenharmony_ci      SB_MTHD30(so, STENCIL_ENABLE(0), 3);
236bf215546Sopenharmony_ci      SB_DATA  (so, 1);
237bf215546Sopenharmony_ci      SB_DATA  (so, cso->stencil[0].writemask);
238bf215546Sopenharmony_ci      SB_DATA  (so, nvgl_comparison_op(cso->stencil[0].func));
239bf215546Sopenharmony_ci      SB_MTHD30(so, STENCIL_FUNC_MASK(0), 4);
240bf215546Sopenharmony_ci      SB_DATA  (so, cso->stencil[0].valuemask);
241bf215546Sopenharmony_ci      SB_DATA  (so, nvgl_stencil_op(cso->stencil[0].fail_op));
242bf215546Sopenharmony_ci      SB_DATA  (so, nvgl_stencil_op(cso->stencil[0].zfail_op));
243bf215546Sopenharmony_ci      SB_DATA  (so, nvgl_stencil_op(cso->stencil[0].zpass_op));
244bf215546Sopenharmony_ci   } else {
245bf215546Sopenharmony_ci      SB_MTHD30(so, STENCIL_ENABLE(0), 2);
246bf215546Sopenharmony_ci      SB_DATA  (so, 0);
247bf215546Sopenharmony_ci      SB_DATA  (so, 0x000000ff);
248bf215546Sopenharmony_ci   }
249bf215546Sopenharmony_ci
250bf215546Sopenharmony_ci   if (cso->stencil[1].enabled) {
251bf215546Sopenharmony_ci      SB_MTHD30(so, STENCIL_ENABLE(1), 3);
252bf215546Sopenharmony_ci      SB_DATA  (so, 1);
253bf215546Sopenharmony_ci      SB_DATA  (so, cso->stencil[1].writemask);
254bf215546Sopenharmony_ci      SB_DATA  (so, nvgl_comparison_op(cso->stencil[1].func));
255bf215546Sopenharmony_ci      SB_MTHD30(so, STENCIL_FUNC_MASK(1), 4);
256bf215546Sopenharmony_ci      SB_DATA  (so, cso->stencil[1].valuemask);
257bf215546Sopenharmony_ci      SB_DATA  (so, nvgl_stencil_op(cso->stencil[1].fail_op));
258bf215546Sopenharmony_ci      SB_DATA  (so, nvgl_stencil_op(cso->stencil[1].zfail_op));
259bf215546Sopenharmony_ci      SB_DATA  (so, nvgl_stencil_op(cso->stencil[1].zpass_op));
260bf215546Sopenharmony_ci   } else {
261bf215546Sopenharmony_ci      SB_MTHD30(so, STENCIL_ENABLE(1), 1);
262bf215546Sopenharmony_ci      SB_DATA  (so, 0);
263bf215546Sopenharmony_ci   }
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci   SB_MTHD30(so, ALPHA_FUNC_ENABLE, 3);
266bf215546Sopenharmony_ci   SB_DATA  (so, cso->alpha_enabled ? 1 : 0);
267bf215546Sopenharmony_ci   SB_DATA  (so, nvgl_comparison_op(cso->alpha_func));
268bf215546Sopenharmony_ci   SB_DATA  (so, float_to_ubyte(cso->alpha_ref_value));
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci   return so;
271bf215546Sopenharmony_ci}
272bf215546Sopenharmony_ci
273bf215546Sopenharmony_cistatic void
274bf215546Sopenharmony_cinv30_zsa_state_bind(struct pipe_context *pipe, void *hwcso)
275bf215546Sopenharmony_ci{
276bf215546Sopenharmony_ci   struct nv30_context *nv30 = nv30_context(pipe);
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci   nv30->zsa = hwcso;
279bf215546Sopenharmony_ci   nv30->dirty |= NV30_NEW_ZSA;
280bf215546Sopenharmony_ci}
281bf215546Sopenharmony_ci
282bf215546Sopenharmony_cistatic void
283bf215546Sopenharmony_cinv30_zsa_state_delete(struct pipe_context *pipe, void *hwcso)
284bf215546Sopenharmony_ci{
285bf215546Sopenharmony_ci   FREE(hwcso);
286bf215546Sopenharmony_ci}
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_cistatic void
289bf215546Sopenharmony_cinv30_set_blend_color(struct pipe_context *pipe,
290bf215546Sopenharmony_ci                     const struct pipe_blend_color *bcol)
291bf215546Sopenharmony_ci{
292bf215546Sopenharmony_ci    struct nv30_context *nv30 = nv30_context(pipe);
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ci    nv30->blend_colour = *bcol;
295bf215546Sopenharmony_ci    nv30->dirty |= NV30_NEW_BLEND_COLOUR;
296bf215546Sopenharmony_ci}
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_cistatic void
299bf215546Sopenharmony_cinv30_set_stencil_ref(struct pipe_context *pipe,
300bf215546Sopenharmony_ci                     const struct pipe_stencil_ref sr)
301bf215546Sopenharmony_ci{
302bf215546Sopenharmony_ci    struct nv30_context *nv30 = nv30_context(pipe);
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_ci    nv30->stencil_ref = sr;
305bf215546Sopenharmony_ci    nv30->dirty |= NV30_NEW_STENCIL_REF;
306bf215546Sopenharmony_ci}
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_cistatic void
309bf215546Sopenharmony_cinv30_set_clip_state(struct pipe_context *pipe,
310bf215546Sopenharmony_ci                    const struct pipe_clip_state *clip)
311bf215546Sopenharmony_ci{
312bf215546Sopenharmony_ci    struct nv30_context *nv30 = nv30_context(pipe);
313bf215546Sopenharmony_ci
314bf215546Sopenharmony_ci    memcpy(nv30->clip.ucp, clip->ucp, sizeof(clip->ucp));
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_ci    nv30->dirty |= NV30_NEW_CLIP;
317bf215546Sopenharmony_ci}
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_cistatic void
320bf215546Sopenharmony_cinv30_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)
321bf215546Sopenharmony_ci{
322bf215546Sopenharmony_ci    struct nv30_context *nv30 = nv30_context(pipe);
323bf215546Sopenharmony_ci
324bf215546Sopenharmony_ci    nv30->sample_mask = sample_mask;
325bf215546Sopenharmony_ci    nv30->dirty |= NV30_NEW_SAMPLE_MASK;
326bf215546Sopenharmony_ci}
327bf215546Sopenharmony_ci
328bf215546Sopenharmony_cistatic void
329bf215546Sopenharmony_cinv30_set_constant_buffer(struct pipe_context *pipe,
330bf215546Sopenharmony_ci                         enum pipe_shader_type shader, uint index,
331bf215546Sopenharmony_ci                         bool pass_reference,
332bf215546Sopenharmony_ci                         const struct pipe_constant_buffer *cb)
333bf215546Sopenharmony_ci{
334bf215546Sopenharmony_ci   struct nv30_context *nv30 = nv30_context(pipe);
335bf215546Sopenharmony_ci   struct pipe_resource *buf = cb ? cb->buffer : NULL;
336bf215546Sopenharmony_ci   unsigned size;
337bf215546Sopenharmony_ci
338bf215546Sopenharmony_ci   if (cb && cb->user_buffer) {
339bf215546Sopenharmony_ci      buf = nouveau_user_buffer_create(pipe->screen, (void*)cb->user_buffer,
340bf215546Sopenharmony_ci                                       cb->buffer_size,
341bf215546Sopenharmony_ci                                       PIPE_BIND_CONSTANT_BUFFER);
342bf215546Sopenharmony_ci   }
343bf215546Sopenharmony_ci
344bf215546Sopenharmony_ci   size = 0;
345bf215546Sopenharmony_ci   if (buf)
346bf215546Sopenharmony_ci      size = buf->width0 / (4 * sizeof(float));
347bf215546Sopenharmony_ci
348bf215546Sopenharmony_ci   if (shader == PIPE_SHADER_VERTEX) {
349bf215546Sopenharmony_ci      if (pass_reference) {
350bf215546Sopenharmony_ci         pipe_resource_reference(&nv30->vertprog.constbuf, NULL);
351bf215546Sopenharmony_ci         nv30->vertprog.constbuf = buf;
352bf215546Sopenharmony_ci      } else {
353bf215546Sopenharmony_ci         pipe_resource_reference(&nv30->vertprog.constbuf, buf);
354bf215546Sopenharmony_ci      }
355bf215546Sopenharmony_ci      nv30->vertprog.constbuf_nr = size;
356bf215546Sopenharmony_ci      nv30->dirty |= NV30_NEW_VERTCONST;
357bf215546Sopenharmony_ci   } else
358bf215546Sopenharmony_ci   if (shader == PIPE_SHADER_FRAGMENT) {
359bf215546Sopenharmony_ci      if (pass_reference) {
360bf215546Sopenharmony_ci         pipe_resource_reference(&nv30->fragprog.constbuf, NULL);
361bf215546Sopenharmony_ci         nv30->fragprog.constbuf = buf;
362bf215546Sopenharmony_ci      } else {
363bf215546Sopenharmony_ci         pipe_resource_reference(&nv30->fragprog.constbuf, buf);
364bf215546Sopenharmony_ci      }
365bf215546Sopenharmony_ci      nv30->fragprog.constbuf_nr = size;
366bf215546Sopenharmony_ci      nv30->dirty |= NV30_NEW_FRAGCONST;
367bf215546Sopenharmony_ci   }
368bf215546Sopenharmony_ci
369bf215546Sopenharmony_ci   if (cb && cb->user_buffer) {
370bf215546Sopenharmony_ci      pipe_resource_reference(&buf, NULL);
371bf215546Sopenharmony_ci   }
372bf215546Sopenharmony_ci}
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_cistatic void
375bf215546Sopenharmony_cinv30_set_framebuffer_state(struct pipe_context *pipe,
376bf215546Sopenharmony_ci                           const struct pipe_framebuffer_state *fb)
377bf215546Sopenharmony_ci{
378bf215546Sopenharmony_ci    struct nv30_context *nv30 = nv30_context(pipe);
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci    nouveau_bufctx_reset(nv30->bufctx, BUFCTX_FB);
381bf215546Sopenharmony_ci
382bf215546Sopenharmony_ci    nv30->framebuffer = *fb;
383bf215546Sopenharmony_ci    nv30->dirty |= NV30_NEW_FRAMEBUFFER;
384bf215546Sopenharmony_ci
385bf215546Sopenharmony_ci   /* Hardware can't handle different swizzled-ness or different blocksizes
386bf215546Sopenharmony_ci    * for zs and cbufs. If both are supplied and something doesn't match,
387bf215546Sopenharmony_ci    * blank out the zs for now so that at least *some* rendering can occur.
388bf215546Sopenharmony_ci    */
389bf215546Sopenharmony_ci    if (fb->nr_cbufs > 0 && fb->zsbuf) {
390bf215546Sopenharmony_ci       struct nv30_miptree *color_mt = nv30_miptree(fb->cbufs[0]->texture);
391bf215546Sopenharmony_ci       struct nv30_miptree *zeta_mt = nv30_miptree(fb->zsbuf->texture);
392bf215546Sopenharmony_ci
393bf215546Sopenharmony_ci       if (color_mt->swizzled != zeta_mt->swizzled ||
394bf215546Sopenharmony_ci           (color_mt->swizzled &&
395bf215546Sopenharmony_ci            (util_format_get_blocksize(fb->zsbuf->format) > 2) !=
396bf215546Sopenharmony_ci            (util_format_get_blocksize(fb->cbufs[0]->format) > 2))) {
397bf215546Sopenharmony_ci          nv30->framebuffer.zsbuf = NULL;
398bf215546Sopenharmony_ci          debug_printf("Mismatched color and zeta formats, ignoring zeta.\n");
399bf215546Sopenharmony_ci       }
400bf215546Sopenharmony_ci    }
401bf215546Sopenharmony_ci}
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_cistatic void
404bf215546Sopenharmony_cinv30_set_polygon_stipple(struct pipe_context *pipe,
405bf215546Sopenharmony_ci                         const struct pipe_poly_stipple *stipple)
406bf215546Sopenharmony_ci{
407bf215546Sopenharmony_ci    struct nv30_context *nv30 = nv30_context(pipe);
408bf215546Sopenharmony_ci
409bf215546Sopenharmony_ci    nv30->stipple = *stipple;
410bf215546Sopenharmony_ci    nv30->dirty |= NV30_NEW_STIPPLE;
411bf215546Sopenharmony_ci}
412bf215546Sopenharmony_ci
413bf215546Sopenharmony_cistatic void
414bf215546Sopenharmony_cinv30_set_scissor_states(struct pipe_context *pipe,
415bf215546Sopenharmony_ci                        unsigned start_slot,
416bf215546Sopenharmony_ci                        unsigned num_viewports,
417bf215546Sopenharmony_ci                        const struct pipe_scissor_state *scissor)
418bf215546Sopenharmony_ci{
419bf215546Sopenharmony_ci    struct nv30_context *nv30 = nv30_context(pipe);
420bf215546Sopenharmony_ci
421bf215546Sopenharmony_ci    nv30->scissor = *scissor;
422bf215546Sopenharmony_ci    nv30->dirty |= NV30_NEW_SCISSOR;
423bf215546Sopenharmony_ci}
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_cistatic void
426bf215546Sopenharmony_cinv30_set_viewport_states(struct pipe_context *pipe,
427bf215546Sopenharmony_ci                         unsigned start_slot,
428bf215546Sopenharmony_ci                         unsigned num_viewports,
429bf215546Sopenharmony_ci                         const struct pipe_viewport_state *vpt)
430bf215546Sopenharmony_ci{
431bf215546Sopenharmony_ci    struct nv30_context *nv30 = nv30_context(pipe);
432bf215546Sopenharmony_ci
433bf215546Sopenharmony_ci    nv30->viewport = *vpt;
434bf215546Sopenharmony_ci    nv30->dirty |= NV30_NEW_VIEWPORT;
435bf215546Sopenharmony_ci}
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_cistatic void
438bf215546Sopenharmony_cinv30_set_vertex_buffers(struct pipe_context *pipe,
439bf215546Sopenharmony_ci                        unsigned start_slot, unsigned count,
440bf215546Sopenharmony_ci                        unsigned unbind_num_trailing_slots,
441bf215546Sopenharmony_ci                        bool take_ownership,
442bf215546Sopenharmony_ci                        const struct pipe_vertex_buffer *vb)
443bf215546Sopenharmony_ci{
444bf215546Sopenharmony_ci    struct nv30_context *nv30 = nv30_context(pipe);
445bf215546Sopenharmony_ci
446bf215546Sopenharmony_ci    nouveau_bufctx_reset(nv30->bufctx, BUFCTX_VTXBUF);
447bf215546Sopenharmony_ci
448bf215546Sopenharmony_ci    util_set_vertex_buffers_count(nv30->vtxbuf, &nv30->num_vtxbufs,
449bf215546Sopenharmony_ci                                  vb, start_slot, count,
450bf215546Sopenharmony_ci                                  unbind_num_trailing_slots,
451bf215546Sopenharmony_ci                                  take_ownership);
452bf215546Sopenharmony_ci
453bf215546Sopenharmony_ci    nv30->dirty |= NV30_NEW_ARRAYS;
454bf215546Sopenharmony_ci}
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_civoid
457bf215546Sopenharmony_cinv30_state_init(struct pipe_context *pipe)
458bf215546Sopenharmony_ci{
459bf215546Sopenharmony_ci   pipe->create_blend_state = nv30_blend_state_create;
460bf215546Sopenharmony_ci   pipe->bind_blend_state = nv30_blend_state_bind;
461bf215546Sopenharmony_ci   pipe->delete_blend_state = nv30_blend_state_delete;
462bf215546Sopenharmony_ci
463bf215546Sopenharmony_ci   pipe->create_rasterizer_state = nv30_rasterizer_state_create;
464bf215546Sopenharmony_ci   pipe->bind_rasterizer_state = nv30_rasterizer_state_bind;
465bf215546Sopenharmony_ci   pipe->delete_rasterizer_state = nv30_rasterizer_state_delete;
466bf215546Sopenharmony_ci
467bf215546Sopenharmony_ci   pipe->create_depth_stencil_alpha_state = nv30_zsa_state_create;
468bf215546Sopenharmony_ci   pipe->bind_depth_stencil_alpha_state = nv30_zsa_state_bind;
469bf215546Sopenharmony_ci   pipe->delete_depth_stencil_alpha_state = nv30_zsa_state_delete;
470bf215546Sopenharmony_ci
471bf215546Sopenharmony_ci   pipe->set_blend_color = nv30_set_blend_color;
472bf215546Sopenharmony_ci   pipe->set_stencil_ref = nv30_set_stencil_ref;
473bf215546Sopenharmony_ci   pipe->set_clip_state = nv30_set_clip_state;
474bf215546Sopenharmony_ci   pipe->set_sample_mask = nv30_set_sample_mask;
475bf215546Sopenharmony_ci   pipe->set_constant_buffer = nv30_set_constant_buffer;
476bf215546Sopenharmony_ci   pipe->set_framebuffer_state = nv30_set_framebuffer_state;
477bf215546Sopenharmony_ci   pipe->set_polygon_stipple = nv30_set_polygon_stipple;
478bf215546Sopenharmony_ci   pipe->set_scissor_states = nv30_set_scissor_states;
479bf215546Sopenharmony_ci   pipe->set_viewport_states = nv30_set_viewport_states;
480bf215546Sopenharmony_ci
481bf215546Sopenharmony_ci   pipe->set_vertex_buffers = nv30_set_vertex_buffers;
482bf215546Sopenharmony_ci}
483