1 /*
2  * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
3  * Copyright © 2018 Google, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Authors:
25  *    Rob Clark <robclark@freedesktop.org>
26  */
27 
28 #ifndef FD6_BLEND_H_
29 #define FD6_BLEND_H_
30 
31 #include "pipe/p_state.h"
32 #include "pipe/p_context.h"
33 
34 #include "freedreno_context.h"
35 #include "freedreno_util.h"
36 
37 /**
38  * Since the sample-mask is part of the hw blend state, we need to have state
39  * variants per sample-mask value.  But we don't expect the sample-mask state
40  * to change frequently.
41  */
42 struct fd6_blend_variant {
43 	unsigned sample_mask;
44 	struct fd_ringbuffer *stateobj;
45 };
46 
47 struct fd6_blend_stateobj {
48 	struct pipe_blend_state base;
49 
50 	struct fd_context *ctx;
51 	bool reads_dest;
52 	struct util_dynarray variants;
53 };
54 
55 static inline struct fd6_blend_stateobj *
fd6_blend_stateobj(struct pipe_blend_state * blend)56 fd6_blend_stateobj(struct pipe_blend_state *blend)
57 {
58 	return (struct fd6_blend_stateobj *)blend;
59 }
60 
61 struct fd6_blend_variant * __fd6_setup_blend_variant(
62 		struct fd6_blend_stateobj *blend, unsigned sample_mask);
63 
64 static inline struct fd6_blend_variant *
fd6_blend_variant(struct pipe_blend_state * cso,unsigned nr_samples,unsigned sample_mask)65 fd6_blend_variant(struct pipe_blend_state *cso,
66 		unsigned nr_samples, unsigned sample_mask)
67 {
68 	struct fd6_blend_stateobj *blend = fd6_blend_stateobj(cso);
69 	unsigned mask = BITFIELD_MASK(nr_samples);
70 
71 	util_dynarray_foreach(&blend->variants, struct fd6_blend_variant *, vp) {
72 		struct fd6_blend_variant *v = *vp;
73 
74 		/* mask out sample-mask bits that we don't care about to avoid
75 		 * creating unnecessary variants
76 		 */
77 		if ((mask & v->sample_mask) == (mask & sample_mask)) {
78 			return v;
79 		}
80 	}
81 
82 	return __fd6_setup_blend_variant(blend, sample_mask);
83 }
84 
85 void * fd6_blend_state_create(struct pipe_context *pctx,
86 		const struct pipe_blend_state *cso);
87 void fd6_blend_state_delete(struct pipe_context *, void *hwcso);
88 
89 #endif /* FD6_BLEND_H_ */
90