1b8e80941Smrg/****************************************************************************
2b8e80941Smrg * Copyright (C) 2015 Intel Corporation.   All Rights Reserved.
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21b8e80941Smrg * IN THE SOFTWARE.
22b8e80941Smrg ***************************************************************************/
23b8e80941Smrg
24b8e80941Smrg#ifndef SWR_CONTEXT_H
25b8e80941Smrg#define SWR_CONTEXT_H
26b8e80941Smrg
27b8e80941Smrg#include "common/os.h"
28b8e80941Smrg
29b8e80941Smrg#include "pipe/p_context.h"
30b8e80941Smrg#include "pipe/p_state.h"
31b8e80941Smrg#include "util/u_blitter.h"
32b8e80941Smrg#include "jit_api.h"
33b8e80941Smrg#include "swr_state.h"
34b8e80941Smrg#include <unordered_map>
35b8e80941Smrg
36b8e80941Smrg#define SWR_NEW_BLEND (1 << 0)
37b8e80941Smrg#define SWR_NEW_RASTERIZER (1 << 1)
38b8e80941Smrg#define SWR_NEW_DEPTH_STENCIL_ALPHA (1 << 2)
39b8e80941Smrg#define SWR_NEW_SAMPLER (1 << 3)
40b8e80941Smrg#define SWR_NEW_SAMPLER_VIEW (1 << 4)
41b8e80941Smrg#define SWR_NEW_VS (1 << 5)
42b8e80941Smrg#define SWR_NEW_FS (1 << 6)
43b8e80941Smrg#define SWR_NEW_GS (1 << 7)
44b8e80941Smrg#define SWR_NEW_VSCONSTANTS (1 << 8)
45b8e80941Smrg#define SWR_NEW_FSCONSTANTS (1 << 9)
46b8e80941Smrg#define SWR_NEW_GSCONSTANTS (1 << 10)
47b8e80941Smrg#define SWR_NEW_VERTEX (1 << 11)
48b8e80941Smrg#define SWR_NEW_STIPPLE (1 << 12)
49b8e80941Smrg#define SWR_NEW_SCISSOR (1 << 13)
50b8e80941Smrg#define SWR_NEW_VIEWPORT (1 << 14)
51b8e80941Smrg#define SWR_NEW_FRAMEBUFFER (1 << 15)
52b8e80941Smrg#define SWR_NEW_CLIP (1 << 16)
53b8e80941Smrg#define SWR_NEW_SO (1 << 17)
54b8e80941Smrg#define SWR_LARGE_CLIENT_DRAW (1<<18) // Indicates client draw will block
55b8e80941Smrg
56b8e80941Smrgnamespace std
57b8e80941Smrg{
58b8e80941Smrgtemplate <> struct hash<BLEND_COMPILE_STATE> {
59b8e80941Smrg   std::size_t operator()(const BLEND_COMPILE_STATE &k) const
60b8e80941Smrg   {
61b8e80941Smrg      return util_hash_crc32(&k, sizeof(k));
62b8e80941Smrg   }
63b8e80941Smrg};
64b8e80941Smrg};
65b8e80941Smrg
66b8e80941Smrgstruct swr_jit_texture {
67b8e80941Smrg   uint32_t width; // same as number of elements
68b8e80941Smrg   uint32_t height;
69b8e80941Smrg   uint32_t depth; // doubles as array size
70b8e80941Smrg   uint32_t first_level;
71b8e80941Smrg   uint32_t last_level;
72b8e80941Smrg   const uint8_t *base_ptr;
73b8e80941Smrg   uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS];
74b8e80941Smrg   uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS];
75b8e80941Smrg   uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];
76b8e80941Smrg};
77b8e80941Smrg
78b8e80941Smrgstruct swr_jit_sampler {
79b8e80941Smrg   float min_lod;
80b8e80941Smrg   float max_lod;
81b8e80941Smrg   float lod_bias;
82b8e80941Smrg   float border_color[4];
83b8e80941Smrg};
84b8e80941Smrg
85b8e80941Smrgstruct swr_draw_context {
86b8e80941Smrg   const float *constantVS[PIPE_MAX_CONSTANT_BUFFERS];
87b8e80941Smrg   uint32_t num_constantsVS[PIPE_MAX_CONSTANT_BUFFERS];
88b8e80941Smrg   const float *constantFS[PIPE_MAX_CONSTANT_BUFFERS];
89b8e80941Smrg   uint32_t num_constantsFS[PIPE_MAX_CONSTANT_BUFFERS];
90b8e80941Smrg   const float *constantGS[PIPE_MAX_CONSTANT_BUFFERS];
91b8e80941Smrg   uint32_t num_constantsGS[PIPE_MAX_CONSTANT_BUFFERS];
92b8e80941Smrg
93b8e80941Smrg   swr_jit_texture texturesVS[PIPE_MAX_SHADER_SAMPLER_VIEWS];
94b8e80941Smrg   swr_jit_sampler samplersVS[PIPE_MAX_SAMPLERS];
95b8e80941Smrg   swr_jit_texture texturesFS[PIPE_MAX_SHADER_SAMPLER_VIEWS];
96b8e80941Smrg   swr_jit_sampler samplersFS[PIPE_MAX_SAMPLERS];
97b8e80941Smrg   swr_jit_texture texturesGS[PIPE_MAX_SHADER_SAMPLER_VIEWS];
98b8e80941Smrg   swr_jit_sampler samplersGS[PIPE_MAX_SAMPLERS];
99b8e80941Smrg
100b8e80941Smrg   float userClipPlanes[PIPE_MAX_CLIP_PLANES][4];
101b8e80941Smrg
102b8e80941Smrg   uint32_t polyStipple[32];
103b8e80941Smrg
104b8e80941Smrg   SWR_SURFACE_STATE renderTargets[SWR_NUM_ATTACHMENTS];
105b8e80941Smrg   struct swr_query_result *pStats; // @llvm_struct
106b8e80941Smrg   SWR_INTERFACE *pAPI; // @llvm_struct - Needed for the swr_memory callbacks
107b8e80941Smrg};
108b8e80941Smrg
109b8e80941Smrg/* gen_llvm_types FINI */
110b8e80941Smrg
111b8e80941Smrgstruct swr_context {
112b8e80941Smrg   struct pipe_context pipe; /**< base class */
113b8e80941Smrg
114b8e80941Smrg   HANDLE swrContext;
115b8e80941Smrg
116b8e80941Smrg   /** Constant state objects */
117b8e80941Smrg   struct swr_blend_state *blend;
118b8e80941Smrg   struct pipe_sampler_state *samplers[PIPE_SHADER_TYPES][PIPE_MAX_SAMPLERS];
119b8e80941Smrg   struct pipe_depth_stencil_alpha_state *depth_stencil;
120b8e80941Smrg   struct pipe_rasterizer_state *rasterizer;
121b8e80941Smrg
122b8e80941Smrg   struct swr_vertex_shader *vs;
123b8e80941Smrg   struct swr_fragment_shader *fs;
124b8e80941Smrg   struct swr_geometry_shader *gs;
125b8e80941Smrg   struct swr_vertex_element_state *velems;
126b8e80941Smrg
127b8e80941Smrg   /** Other rendering state */
128b8e80941Smrg   struct pipe_blend_color blend_color;
129b8e80941Smrg   struct pipe_stencil_ref stencil_ref;
130b8e80941Smrg   struct pipe_clip_state clip;
131b8e80941Smrg   struct pipe_constant_buffer
132b8e80941Smrg      constants[PIPE_SHADER_TYPES][PIPE_MAX_CONSTANT_BUFFERS];
133b8e80941Smrg   struct pipe_framebuffer_state framebuffer;
134b8e80941Smrg   struct swr_poly_stipple poly_stipple;
135b8e80941Smrg   struct pipe_scissor_state scissor;
136b8e80941Smrg   SWR_RECT swr_scissor;
137b8e80941Smrg   struct pipe_sampler_view *
138b8e80941Smrg      sampler_views[PIPE_SHADER_TYPES][PIPE_MAX_SHADER_SAMPLER_VIEWS];
139b8e80941Smrg
140b8e80941Smrg   struct pipe_viewport_state viewport;
141b8e80941Smrg   struct pipe_vertex_buffer vertex_buffer[PIPE_MAX_ATTRIBS];
142b8e80941Smrg
143b8e80941Smrg   struct blitter_context *blitter;
144b8e80941Smrg
145b8e80941Smrg   /** Conditional query object and mode */
146b8e80941Smrg   struct pipe_query *render_cond_query;
147b8e80941Smrg   enum pipe_render_cond_flag render_cond_mode;
148b8e80941Smrg   boolean render_cond_cond;
149b8e80941Smrg   unsigned active_queries;
150b8e80941Smrg
151b8e80941Smrg   unsigned num_vertex_buffers;
152b8e80941Smrg   unsigned num_samplers[PIPE_SHADER_TYPES];
153b8e80941Smrg   unsigned num_sampler_views[PIPE_SHADER_TYPES];
154b8e80941Smrg
155b8e80941Smrg   unsigned sample_mask;
156b8e80941Smrg
157b8e80941Smrg   // streamout
158b8e80941Smrg   pipe_stream_output_target *so_targets[MAX_SO_STREAMS];
159b8e80941Smrg   uint32_t num_so_targets;
160b8e80941Smrg
161b8e80941Smrg   /* Temp storage for user_buffer constants */
162b8e80941Smrg   struct swr_scratch_buffers *scratch;
163b8e80941Smrg
164b8e80941Smrg   // blend jit functions
165b8e80941Smrg   std::unordered_map<BLEND_COMPILE_STATE, PFN_BLEND_JIT_FUNC> *blendJIT;
166b8e80941Smrg
167b8e80941Smrg   /* Derived SWR API DrawState */
168b8e80941Smrg   struct swr_derived_state derived;
169b8e80941Smrg
170b8e80941Smrg   /* SWR private state - draw context */
171b8e80941Smrg   struct swr_draw_context swrDC;
172b8e80941Smrg
173b8e80941Smrg   unsigned dirty; /**< Mask of SWR_NEW_x flags */
174b8e80941Smrg
175b8e80941Smrg   SWR_INTERFACE api;
176b8e80941Smrg
177b8e80941Smrg   uint32_t max_draws_in_flight;
178b8e80941Smrg};
179b8e80941Smrg
180b8e80941Smrgstatic INLINE struct swr_context *
181b8e80941Smrgswr_context(struct pipe_context *pipe)
182b8e80941Smrg{
183b8e80941Smrg   return (struct swr_context *)pipe;
184b8e80941Smrg}
185b8e80941Smrg
186b8e80941Smrgstatic INLINE void
187b8e80941Smrgswr_update_draw_context(struct swr_context *ctx,
188b8e80941Smrg      struct swr_query_result *pqr = nullptr)
189b8e80941Smrg{
190b8e80941Smrg   swr_draw_context *pDC =
191b8e80941Smrg      (swr_draw_context *)ctx->api.pfnSwrGetPrivateContextState(ctx->swrContext);
192b8e80941Smrg   if (pqr)
193b8e80941Smrg      ctx->swrDC.pStats = pqr;
194b8e80941Smrg   memcpy(pDC, &ctx->swrDC, sizeof(swr_draw_context));
195b8e80941Smrg}
196b8e80941Smrg
197b8e80941Smrgstruct pipe_context *swr_create_context(struct pipe_screen *, void *priv, unsigned flags);
198b8e80941Smrg
199b8e80941Smrgvoid swr_state_init(struct pipe_context *pipe);
200b8e80941Smrg
201b8e80941Smrgvoid swr_clear_init(struct pipe_context *pipe);
202b8e80941Smrg
203b8e80941Smrgvoid swr_draw_init(struct pipe_context *pipe);
204b8e80941Smrg
205b8e80941Smrgvoid swr_finish(struct pipe_context *pipe);
206b8e80941Smrg
207b8e80941Smrgvoid swr_do_msaa_resolve(struct pipe_resource *src_resource,
208b8e80941Smrg                         struct pipe_resource *dst_resource);
209b8e80941Smrg#endif
210