1b8e80941Smrg/*
2b8e80941Smrg * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8b8e80941Smrg * license, and/or sell copies of the Software, and to permit persons to whom
9b8e80941Smrg * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19b8e80941Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20b8e80941Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21b8e80941Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22b8e80941Smrg
23b8e80941Smrg#ifndef _NINE_PIXELSHADER9_H_
24b8e80941Smrg#define _NINE_PIXELSHADER9_H_
25b8e80941Smrg
26b8e80941Smrg#include "iunknown.h"
27b8e80941Smrg#include "nine_shader.h"
28b8e80941Smrg#include "nine_state.h"
29b8e80941Smrg#include "basetexture9.h"
30b8e80941Smrg#include "nine_ff.h"
31b8e80941Smrg#include "surface9.h"
32b8e80941Smrg
33b8e80941Smrgstruct nine_lconstf;
34b8e80941Smrg
35b8e80941Smrgstruct NinePixelShader9
36b8e80941Smrg{
37b8e80941Smrg    struct NineUnknown base;
38b8e80941Smrg    struct nine_shader_variant variant;
39b8e80941Smrg
40b8e80941Smrg    struct {
41b8e80941Smrg        const DWORD *tokens;
42b8e80941Smrg        DWORD size;
43b8e80941Smrg        uint8_t version; /* (major << 4) | minor */
44b8e80941Smrg    } byte_code;
45b8e80941Smrg
46b8e80941Smrg    uint8_t bumpenvmat_needed;
47b8e80941Smrg    uint16_t sampler_mask;
48b8e80941Smrg    uint8_t rt_mask;
49b8e80941Smrg
50b8e80941Smrg    boolean int_slots_used[NINE_MAX_CONST_I];
51b8e80941Smrg    boolean bool_slots_used[NINE_MAX_CONST_B];
52b8e80941Smrg
53b8e80941Smrg    unsigned const_int_slots;
54b8e80941Smrg    unsigned const_bool_slots;
55b8e80941Smrg
56b8e80941Smrg    struct nine_shader_constant_combination *c_combinations;
57b8e80941Smrg
58b8e80941Smrg    uint64_t ff_key[6];
59b8e80941Smrg    void *ff_cso;
60b8e80941Smrg
61b8e80941Smrg    uint64_t last_key;
62b8e80941Smrg    void *last_cso;
63b8e80941Smrg    unsigned *last_const_ranges;
64b8e80941Smrg    unsigned last_const_used_size; /* in bytes */
65b8e80941Smrg
66b8e80941Smrg    uint64_t next_key;
67b8e80941Smrg};
68b8e80941Smrgstatic inline struct NinePixelShader9 *
69b8e80941SmrgNinePixelShader9( void *data )
70b8e80941Smrg{
71b8e80941Smrg    return (struct NinePixelShader9 *)data;
72b8e80941Smrg}
73b8e80941Smrg
74b8e80941Smrgstatic inline BOOL
75b8e80941SmrgNinePixelShader9_UpdateKey( struct NinePixelShader9 *ps,
76b8e80941Smrg                            struct nine_context *context )
77b8e80941Smrg{
78b8e80941Smrg    uint16_t samplers_shadow;
79b8e80941Smrg    uint16_t samplers_ps1_types;
80b8e80941Smrg    uint8_t projected;
81b8e80941Smrg    uint64_t key;
82b8e80941Smrg    BOOL res;
83b8e80941Smrg
84b8e80941Smrg    samplers_shadow = (uint16_t)((context->samplers_shadow & NINE_PS_SAMPLERS_MASK) >> NINE_SAMPLER_PS(0));
85b8e80941Smrg    key = samplers_shadow & ps->sampler_mask;
86b8e80941Smrg
87b8e80941Smrg    if (unlikely(ps->byte_code.version < 0x20)) {
88b8e80941Smrg        /* variable targets */
89b8e80941Smrg        uint32_t m = ps->sampler_mask;
90b8e80941Smrg        samplers_ps1_types = 0;
91b8e80941Smrg        while (m) {
92b8e80941Smrg            int s = ffs(m) - 1;
93b8e80941Smrg            m &= ~(1 << s);
94b8e80941Smrg            samplers_ps1_types |= (context->texture[s].enabled ? context->texture[s].pstype : 1) << (s * 2);
95b8e80941Smrg        }
96b8e80941Smrg        /* Note: For ps 1.X, only samplers 0 1 2 and 3 are available (except 1.4 where 4 and 5 are available).
97b8e80941Smrg         * ps < 1.4: samplers_shadow 4b, samplers_ps1_types 8b, projected 8b
98b8e80941Smrg         * ps 1.4: samplers_shadow 6b, samplers_ps1_types 12b
99b8e80941Smrg         * Tot ps X.X samplers_shadow + extra: 20b */
100b8e80941Smrg        assert((ps->byte_code.version < 0x14 && !(ps->sampler_mask & 0xFFF0)) || !(ps->sampler_mask & 0xFFC0));
101b8e80941Smrg
102b8e80941Smrg        if (unlikely(ps->byte_code.version < 0x14)) {
103b8e80941Smrg            key |= samplers_ps1_types << 4;
104b8e80941Smrg            projected = nine_ff_get_projected_key_programmable(context);
105b8e80941Smrg            key |= ((uint64_t) projected) << 12;
106b8e80941Smrg        } else {
107b8e80941Smrg            key |= samplers_ps1_types << 6;
108b8e80941Smrg        }
109b8e80941Smrg    }
110b8e80941Smrg
111b8e80941Smrg    if (ps->byte_code.version < 0x30) {
112b8e80941Smrg        key |= ((uint64_t)context->rs[D3DRS_FOGENABLE]) << 20;
113b8e80941Smrg        key |= ((uint64_t)context->rs[D3DRS_FOGTABLEMODE]) << 21;
114b8e80941Smrg    }
115b8e80941Smrg
116b8e80941Smrg    /* centroid interpolation automatically used for color ps inputs */
117b8e80941Smrg    if (context->rt[0]->base.info.nr_samples)
118b8e80941Smrg        key |= ((uint64_t)1) << 22;
119b8e80941Smrg
120b8e80941Smrg    if ((ps->const_int_slots > 0 || ps->const_bool_slots > 0) && context->inline_constants)
121b8e80941Smrg        key |= ((uint64_t)nine_shader_constant_combination_key(&ps->c_combinations,
122b8e80941Smrg                                                               ps->int_slots_used,
123b8e80941Smrg                                                               ps->bool_slots_used,
124b8e80941Smrg                                                               (void *)context->ps_const_i,
125b8e80941Smrg                                                               context->ps_const_b)) << 24;
126b8e80941Smrg
127b8e80941Smrg    res = ps->last_key != key;
128b8e80941Smrg    if (res)
129b8e80941Smrg        ps->next_key = key;
130b8e80941Smrg    return res;
131b8e80941Smrg}
132b8e80941Smrg
133b8e80941Smrgvoid *
134b8e80941SmrgNinePixelShader9_GetVariant( struct NinePixelShader9 *ps,
135b8e80941Smrg                             unsigned **const_ranges,
136b8e80941Smrg                             unsigned *const_used_size );
137b8e80941Smrg
138b8e80941Smrg/*** public ***/
139b8e80941Smrg
140b8e80941SmrgHRESULT
141b8e80941SmrgNinePixelShader9_new( struct NineDevice9 *pDevice,
142b8e80941Smrg                      struct NinePixelShader9 **ppOut,
143b8e80941Smrg                      const DWORD *pFunction, void *cso );
144b8e80941Smrg
145b8e80941SmrgHRESULT
146b8e80941SmrgNinePixelShader9_ctor( struct NinePixelShader9 *,
147b8e80941Smrg                       struct NineUnknownParams *pParams,
148b8e80941Smrg                       const DWORD *pFunction, void *cso );
149b8e80941Smrg
150b8e80941Smrgvoid
151b8e80941SmrgNinePixelShader9_dtor( struct NinePixelShader9 * );
152b8e80941Smrg
153b8e80941SmrgHRESULT NINE_WINAPI
154b8e80941SmrgNinePixelShader9_GetFunction( struct NinePixelShader9 *This,
155b8e80941Smrg                              void *pData,
156b8e80941Smrg                              UINT *pSizeOfData );
157b8e80941Smrg
158b8e80941Smrg#endif /* _NINE_PIXELSHADER9_H_ */
159