st/nine: Align stack for entry points
[mesa.git] / src / gallium / state_trackers / nine / vertexshader9.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 "vertexshader9.h"
27
28 #include "device9.h"
29 #include "pipe/p_context.h"
30
31 #define DBG_CHANNEL DBG_VERTEXSHADER
32
33 HRESULT
34 NineVertexShader9_ctor( struct NineVertexShader9 *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 unsigned i;
42
43 DBG("This=%p pParams=%p pFunction=%p cso=%p\n",
44 This, pParams, pFunction, cso);
45
46 hr = NineUnknown_ctor(&This->base, pParams);
47 if (FAILED(hr))
48 return hr;
49
50 if (cso) {
51 This->ff_cso = cso;
52 return D3D_OK;
53 }
54
55 device = This->base.device;
56
57 info.type = PIPE_SHADER_VERTEX;
58 info.byte_code = pFunction;
59 info.const_i_base = NINE_CONST_I_BASE(device->max_vs_const_f) / 16;
60 info.const_b_base = NINE_CONST_B_BASE(device->max_vs_const_f) / 16;
61 info.sampler_mask_shadow = 0x0;
62 info.sampler_ps1xtypes = 0x0;
63 info.fog_enable = 0;
64
65 hr = nine_translate_shader(device, &info);
66 if (FAILED(hr))
67 return hr;
68 This->byte_code.version = info.version;
69
70 This->byte_code.tokens = mem_dup(pFunction, info.byte_size);
71 if (!This->byte_code.tokens)
72 return E_OUTOFMEMORY;
73 This->byte_code.size = info.byte_size;
74
75 This->variant.cso = info.cso;
76 This->last_cso = info.cso;
77 This->last_key = 0;
78
79 This->const_used_size = info.const_used_size;
80 This->lconstf = info.lconstf;
81 This->sampler_mask = info.sampler_mask;
82 This->position_t = info.position_t;
83 This->point_size = info.point_size;
84
85 for (i = 0; i < info.num_inputs && i < Elements(This->input_map); ++i)
86 This->input_map[i].ndecl = info.input_map[i];
87 This->num_inputs = i;
88
89 return D3D_OK;
90 }
91
92 void
93 NineVertexShader9_dtor( struct NineVertexShader9 *This )
94 {
95 DBG("This=%p\n", This);
96
97 if (This->base.device) {
98 struct pipe_context *pipe = This->base.device->pipe;
99 struct nine_shader_variant *var = &This->variant;
100
101 do {
102 if (var->cso) {
103 if (This->base.device->state.cso.vs == var->cso)
104 pipe->bind_vs_state(pipe, NULL);
105 pipe->delete_vs_state(pipe, var->cso);
106 }
107 var = var->next;
108 } while (var);
109
110 if (This->ff_cso) {
111 if (This->ff_cso == This->base.device->state.cso.vs)
112 pipe->bind_vs_state(pipe, NULL);
113 pipe->delete_vs_state(pipe, This->ff_cso);
114 }
115 }
116 nine_shader_variants_free(&This->variant);
117
118 FREE((void *)This->byte_code.tokens); /* const_cast */
119
120 FREE(This->lconstf.data);
121 FREE(This->lconstf.ranges);
122
123 NineUnknown_dtor(&This->base);
124 }
125
126 HRESULT NINE_WINAPI
127 NineVertexShader9_GetFunction( struct NineVertexShader9 *This,
128 void *pData,
129 UINT *pSizeOfData )
130 {
131 user_assert(pSizeOfData, D3DERR_INVALIDCALL);
132
133 if (!pData) {
134 *pSizeOfData = This->byte_code.size;
135 return D3D_OK;
136 }
137 user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL);
138
139 memcpy(pData, This->byte_code.tokens, This->byte_code.size);
140
141 return D3D_OK;
142 }
143
144 void *
145 NineVertexShader9_GetVariant( struct NineVertexShader9 *This )
146 {
147 void *cso;
148 uint32_t key;
149
150 key = This->next_key;
151 if (key == This->last_key)
152 return This->last_cso;
153
154 cso = nine_shader_variant_get(&This->variant, key);
155 if (!cso) {
156 struct NineDevice9 *device = This->base.device;
157 struct nine_shader_info info;
158 HRESULT hr;
159
160 info.type = PIPE_SHADER_VERTEX;
161 info.const_i_base = NINE_CONST_I_BASE(device->max_vs_const_f) / 16;
162 info.const_b_base = NINE_CONST_B_BASE(device->max_vs_const_f) / 16;
163 info.byte_code = This->byte_code.tokens;
164 info.sampler_mask_shadow = key & 0xf;
165 info.fog_enable = device->state.rs[D3DRS_FOGENABLE];
166
167 hr = nine_translate_shader(This->base.device, &info);
168 if (FAILED(hr))
169 return NULL;
170 nine_shader_variant_add(&This->variant, key, info.cso);
171 cso = info.cso;
172 }
173
174 This->last_key = key;
175 This->last_cso = cso;
176
177 return cso;
178 }
179
180 IDirect3DVertexShader9Vtbl NineVertexShader9_vtable = {
181 (void *)NineUnknown_QueryInterface,
182 (void *)NineUnknown_AddRef,
183 (void *)NineUnknown_Release,
184 (void *)NineUnknown_GetDevice,
185 (void *)NineVertexShader9_GetFunction
186 };
187
188 static const GUID *NineVertexShader9_IIDs[] = {
189 &IID_IDirect3DVertexShader9,
190 &IID_IUnknown,
191 NULL
192 };
193
194 HRESULT
195 NineVertexShader9_new( struct NineDevice9 *pDevice,
196 struct NineVertexShader9 **ppOut,
197 const DWORD *pFunction, void *cso )
198 {
199 NINE_DEVICE_CHILD_NEW(VertexShader9, ppOut, pDevice, pFunction, cso);
200 }