st/nine: Refactor how user constbufs sizes are calculated
[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->variant.cso = cso;
52 return D3D_OK;
53 }
54 device = This->base.device;
55
56 info.type = PIPE_SHADER_VERTEX;
57 info.byte_code = pFunction;
58 info.const_i_base = NINE_CONST_I_BASE(device->max_vs_const_f) / 16;
59 info.const_b_base = NINE_CONST_B_BASE(device->max_vs_const_f) / 16;
60 info.sampler_mask_shadow = 0x0;
61 info.sampler_ps1xtypes = 0x0;
62
63 hr = nine_translate_shader(device, &info);
64 if (FAILED(hr))
65 return hr;
66 This->byte_code.version = info.version;
67
68 This->byte_code.tokens = mem_dup(pFunction, info.byte_size);
69 if (!This->byte_code.tokens)
70 return E_OUTOFMEMORY;
71 This->byte_code.size = info.byte_size;
72
73 This->variant.cso = info.cso;
74 This->const_used_size = info.const_used_size;
75 This->lconstf = info.lconstf;
76 This->sampler_mask = info.sampler_mask;
77 This->position_t = info.position_t;
78 This->point_size = info.point_size;
79
80 for (i = 0; i < info.num_inputs && i < Elements(This->input_map); ++i)
81 This->input_map[i].ndecl = info.input_map[i];
82 This->num_inputs = i;
83
84 return D3D_OK;
85 }
86
87 void
88 NineVertexShader9_dtor( struct NineVertexShader9 *This )
89 {
90 DBG("This=%p cso=%p\n", This, This->variant.cso);
91
92 if (This->base.device) {
93 struct pipe_context *pipe = This->base.device->pipe;
94 struct nine_shader_variant *var = &This->variant;
95 do {
96 if (var->cso) {
97 if (This->base.device->state.cso.vs == var->cso)
98 pipe->bind_vs_state(pipe, NULL);
99 pipe->delete_vs_state(pipe, var->cso);
100 }
101 var = var->next;
102 } while (var);
103 }
104 nine_shader_variants_free(&This->variant);
105
106 FREE((void *)This->byte_code.tokens); /* const_cast */
107
108 FREE(This->lconstf.data);
109 FREE(This->lconstf.ranges);
110
111 NineUnknown_dtor(&This->base);
112 }
113
114 HRESULT WINAPI
115 NineVertexShader9_GetFunction( struct NineVertexShader9 *This,
116 void *pData,
117 UINT *pSizeOfData )
118 {
119 user_assert(pSizeOfData, D3DERR_INVALIDCALL);
120
121 if (!pData) {
122 *pSizeOfData = This->byte_code.size;
123 return D3D_OK;
124 }
125 user_assert(*pSizeOfData >= This->byte_code.size, D3DERR_INVALIDCALL);
126
127 memcpy(pData, This->byte_code.tokens, This->byte_code.size);
128
129 return D3D_OK;
130 }
131
132 void *
133 NineVertexShader9_GetVariant( struct NineVertexShader9 *This,
134 uint32_t key )
135 {
136 void *cso = nine_shader_variant_get(&This->variant, key);
137 if (!cso) {
138 struct NineDevice9 *device = This->base.device;
139 struct nine_shader_info info;
140 HRESULT hr;
141
142 info.type = PIPE_SHADER_VERTEX;
143 info.const_i_base = NINE_CONST_I_BASE(device->max_vs_const_f) / 16;
144 info.const_b_base = NINE_CONST_B_BASE(device->max_vs_const_f) / 16;
145 info.byte_code = This->byte_code.tokens;
146 info.sampler_mask_shadow = key & 0xf;
147
148 hr = nine_translate_shader(This->base.device, &info);
149 if (FAILED(hr))
150 return NULL;
151 nine_shader_variant_add(&This->variant, key, info.cso);
152 cso = info.cso;
153 }
154 return cso;
155 }
156
157 IDirect3DVertexShader9Vtbl NineVertexShader9_vtable = {
158 (void *)NineUnknown_QueryInterface,
159 (void *)NineUnknown_AddRef,
160 (void *)NineUnknown_Release,
161 (void *)NineUnknown_GetDevice,
162 (void *)NineVertexShader9_GetFunction
163 };
164
165 static const GUID *NineVertexShader9_IIDs[] = {
166 &IID_IDirect3DVertexShader9,
167 &IID_IUnknown,
168 NULL
169 };
170
171 HRESULT
172 NineVertexShader9_new( struct NineDevice9 *pDevice,
173 struct NineVertexShader9 **ppOut,
174 const DWORD *pFunction, void *cso )
175 {
176 NINE_DEVICE_CHILD_NEW(VertexShader9, ppOut, pDevice, pFunction, cso);
177 }