OSDN Git Service

d567bcb4a0bdc2030aa628db11358b74078c5256
[android-x86/external-mesa.git] / src / gallium / state_trackers / nine / pixelshader9.c
1 /*
2  * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "nine_helpers.h"
24 #include "nine_shader.h"
25
26 #include "pixelshader9.h"
27
28 #include "device9.h"
29 #include "pipe/p_context.h"
30
31 #define DBG_CHANNEL DBG_PIXELSHADER
32
33 HRESULT
34 NinePixelShader9_ctor( struct NinePixelShader9 *This,
35                        struct NineUnknownParams *pParams,
36                        const DWORD *pFunction, void *cso )
37 {
38     struct NineDevice9 *device;
39     struct nine_shader_info info;
40     HRESULT hr;
41
42     DBG("This=%p pParams=%p pFunction=%p cso=%p\n", This, pParams, pFunction, cso);
43
44     hr = NineUnknown_ctor(&This->base, pParams);
45     if (FAILED(hr))
46         return hr;
47
48     if (cso) {
49         This->variant.cso = cso;
50         return D3D_OK;
51     }
52     device = This->base.device;
53
54     info.type = PIPE_SHADER_FRAGMENT;
55     info.byte_code = pFunction;
56     info.const_i_base = NINE_CONST_I_BASE(device->max_ps_const_f) / 16;
57     info.const_b_base = NINE_CONST_B_BASE(device->max_ps_const_f) / 16;
58     info.sampler_mask_shadow = 0x0;
59     info.sampler_ps1xtypes = 0x0;
60
61     hr = nine_translate_shader(device, &info);
62     if (FAILED(hr))
63         return hr;
64     This->byte_code.version = info.version;
65
66     This->byte_code.tokens = mem_dup(pFunction, info.byte_size);
67     if (!This->byte_code.tokens)
68         return E_OUTOFMEMORY;
69     This->byte_code.size = info.byte_size;
70
71     This->variant.cso = info.cso;
72     This->sampler_mask = info.sampler_mask;
73     This->rt_mask = info.rt_mask;
74     This->const_used_size = info.const_used_size;
75     if (info.const_used_size == ~0)
76         This->const_used_size = NINE_CONSTBUF_SIZE(device->max_ps_const_f);
77     This->lconstf = info.lconstf;
78
79     return D3D_OK;
80 }
81
82 void
83 NinePixelShader9_dtor( struct NinePixelShader9 *This )
84 {
85     DBG("This=%p cso=%p\n", This, This->variant.cso);
86
87     if (This->base.device) {
88         struct pipe_context *pipe = This->base.device->pipe;
89         struct nine_shader_variant *var = &This->variant;
90         do {
91             if (var->cso) {
92                 if (This->base.device->state.cso.ps == var->cso)
93                     pipe->bind_fs_state(pipe, NULL);
94                 pipe->delete_fs_state(pipe, var->cso);
95             }
96             var = var->next;
97         } while (var);
98     }
99     nine_shader_variants_free(&This->variant);
100
101     if (This->byte_code.tokens)
102         FREE((void *)This->byte_code.tokens); /* const_cast */
103
104     FREE(This->lconstf.data);
105     FREE(This->lconstf.ranges);
106
107     NineUnknown_dtor(&This->base);
108 }
109
110 HRESULT WINAPI
111 NinePixelShader9_GetFunction( struct NinePixelShader9 *This,
112                               void *pData,
113                               UINT *pSizeOfData )
114 {
115     DBG("This=%p pData=%p pSizeOfData=%p\n", This, pData, pSizeOfData);
116
117     user_assert(pSizeOfData, D3DERR_INVALIDCALL);
118
119     if (!pData) {
120         *pSizeOfData = This->byte_code.size;
121         return D3D_OK;
122     }
123     user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL);
124
125     memcpy(pData, This->byte_code.tokens, This->byte_code.size);
126
127     return D3D_OK;
128 }
129
130 void *
131 NinePixelShader9_GetVariant( struct NinePixelShader9 *This,
132                              uint32_t key )
133 {
134     void *cso = nine_shader_variant_get(&This->variant, key);
135     if (!cso) {
136         struct NineDevice9 *device = This->base.device;
137         struct nine_shader_info info;
138         HRESULT hr;
139
140         info.type = PIPE_SHADER_FRAGMENT;
141         info.const_i_base = NINE_CONST_I_BASE(device->max_ps_const_f) / 16;
142         info.const_b_base = NINE_CONST_B_BASE(device->max_ps_const_f) / 16;
143         info.byte_code = This->byte_code.tokens;
144         info.sampler_mask_shadow = key & 0xffff;
145         info.sampler_ps1xtypes = key;
146
147         hr = nine_translate_shader(This->base.device, &info);
148         if (FAILED(hr))
149             return NULL;
150         nine_shader_variant_add(&This->variant, key, info.cso);
151         cso = info.cso;
152     }
153     return cso;
154 }
155
156 IDirect3DPixelShader9Vtbl NinePixelShader9_vtable = {
157     (void *)NineUnknown_QueryInterface,
158     (void *)NineUnknown_AddRef,
159     (void *)NineUnknown_Release,
160     (void *)NineUnknown_GetDevice,
161     (void *)NinePixelShader9_GetFunction
162 };
163
164 static const GUID *NinePixelShader9_IIDs[] = {
165     &IID_IDirect3DPixelShader9,
166     &IID_IUnknown,
167     NULL
168 };
169
170 HRESULT
171 NinePixelShader9_new( struct NineDevice9 *pDevice,
172                       struct NinePixelShader9 **ppOut,
173                       const DWORD *pFunction, void *cso )
174 {
175     NINE_DEVICE_CHILD_NEW(PixelShader9, ppOut, pDevice, pFunction, cso);
176 }