2 * Copyright 2011 Joakim Sindholt <opensource@zhasha.com>
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:
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
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. */
23 #include "vertexdeclaration9.h"
24 #include "vertexbuffer9.h"
26 #include "nine_helpers.h"
28 #include "pipe/p_format.h"
29 #include "pipe/p_context.h"
30 #include "util/u_math.h"
31 #include "util/u_format.h"
32 #include "util/u_box.h"
33 #include "translate/translate.h"
35 #define DBG_CHANNEL DBG_VERTEXDECLARATION
37 static INLINE
enum pipe_format
decltype_format(BYTE type
)
40 case D3DDECLTYPE_FLOAT1
: return PIPE_FORMAT_R32_FLOAT
;
41 case D3DDECLTYPE_FLOAT2
: return PIPE_FORMAT_R32G32_FLOAT
;
42 case D3DDECLTYPE_FLOAT3
: return PIPE_FORMAT_R32G32B32_FLOAT
;
43 case D3DDECLTYPE_FLOAT4
: return PIPE_FORMAT_R32G32B32A32_FLOAT
;
44 case D3DDECLTYPE_D3DCOLOR
: return PIPE_FORMAT_B8G8R8A8_UNORM
;
45 case D3DDECLTYPE_UBYTE4
: return PIPE_FORMAT_R8G8B8A8_USCALED
;
46 case D3DDECLTYPE_SHORT2
: return PIPE_FORMAT_R16G16_SSCALED
;
47 case D3DDECLTYPE_SHORT4
: return PIPE_FORMAT_R16G16B16A16_SSCALED
;
48 case D3DDECLTYPE_UBYTE4N
: return PIPE_FORMAT_R8G8B8A8_UNORM
;
49 case D3DDECLTYPE_SHORT2N
: return PIPE_FORMAT_R16G16_SNORM
;
50 case D3DDECLTYPE_SHORT4N
: return PIPE_FORMAT_R16G16B16A16_SNORM
;
51 case D3DDECLTYPE_USHORT2N
: return PIPE_FORMAT_R16G16_UNORM
;
52 case D3DDECLTYPE_USHORT4N
: return PIPE_FORMAT_R16G16B16A16_UNORM
;
53 case D3DDECLTYPE_UDEC3
: return PIPE_FORMAT_R10G10B10X2_USCALED
;
54 case D3DDECLTYPE_DEC3N
: return PIPE_FORMAT_R10G10B10X2_SNORM
;
55 case D3DDECLTYPE_FLOAT16_2
: return PIPE_FORMAT_R16G16_FLOAT
;
56 case D3DDECLTYPE_FLOAT16_4
: return PIPE_FORMAT_R16G16B16A16_FLOAT
;
58 assert(!"Implementation error !");
60 return PIPE_FORMAT_NONE
;
63 static INLINE
unsigned decltype_size(BYTE type
)
66 case D3DDECLTYPE_FLOAT1
: return 1 * sizeof(float);
67 case D3DDECLTYPE_FLOAT2
: return 2 * sizeof(float);
68 case D3DDECLTYPE_FLOAT3
: return 3 * sizeof(float);
69 case D3DDECLTYPE_FLOAT4
: return 4 * sizeof(float);
70 case D3DDECLTYPE_D3DCOLOR
: return 1 * sizeof(DWORD
);
71 case D3DDECLTYPE_UBYTE4
: return 4 * sizeof(BYTE
);
72 case D3DDECLTYPE_SHORT2
: return 2 * sizeof(short);
73 case D3DDECLTYPE_SHORT4
: return 4 * sizeof(short);
74 case D3DDECLTYPE_UBYTE4N
: return 4 * sizeof(BYTE
);
75 case D3DDECLTYPE_SHORT2N
: return 2 * sizeof(short);
76 case D3DDECLTYPE_SHORT4N
: return 4 * sizeof(short);
77 case D3DDECLTYPE_USHORT2N
: return 2 * sizeof(short);
78 case D3DDECLTYPE_USHORT4N
: return 4 * sizeof(short);
79 case D3DDECLTYPE_UDEC3
: return 4;
80 case D3DDECLTYPE_DEC3N
: return 4;
81 case D3DDECLTYPE_FLOAT16_2
: return 2 * 2;
82 case D3DDECLTYPE_FLOAT16_4
: return 4 * 2;
84 assert(!"Implementation error !");
89 /* Actually, arbitrary usage index values are permitted, but a
90 * simple lookup table won't work in that case. Let's just wait
91 * with making this more generic until we need it.
94 nine_d3ddeclusage_check(unsigned usage
, unsigned usage_idx
)
97 case D3DDECLUSAGE_POSITIONT
:
98 case D3DDECLUSAGE_TESSFACTOR
:
99 case D3DDECLUSAGE_DEPTH
:
100 case D3DDECLUSAGE_NORMAL
:
101 case D3DDECLUSAGE_TANGENT
:
102 case D3DDECLUSAGE_BINORMAL
:
103 case D3DDECLUSAGE_POSITION
:
104 case D3DDECLUSAGE_BLENDWEIGHT
:
105 case D3DDECLUSAGE_BLENDINDICES
:
106 case D3DDECLUSAGE_COLOR
:
108 case D3DDECLUSAGE_PSIZE
:
109 case D3DDECLUSAGE_FOG
:
110 case D3DDECLUSAGE_SAMPLE
:
111 return usage_idx
<= 0;
112 case D3DDECLUSAGE_TEXCOORD
:
113 return usage_idx
<= 15;
119 #define NINE_DECLUSAGE_CASE0(n) case D3DDECLUSAGE_##n: return NINE_DECLUSAGE_##n
120 #define NINE_DECLUSAGE_CASEi(n) case D3DDECLUSAGE_##n: return NINE_DECLUSAGE_i(n, usage_idx)
122 nine_d3d9_to_nine_declusage(unsigned usage
, unsigned usage_idx
)
124 if (!nine_d3ddeclusage_check(usage
, usage_idx
))
125 ERR("D3DDECLUSAGE_%u[%u]\n",usage
,usage_idx
);
126 assert(nine_d3ddeclusage_check(usage
, usage_idx
));
128 NINE_DECLUSAGE_CASEi(POSITION
);
129 NINE_DECLUSAGE_CASEi(BLENDWEIGHT
);
130 NINE_DECLUSAGE_CASEi(BLENDINDICES
);
131 NINE_DECLUSAGE_CASEi(NORMAL
);
132 NINE_DECLUSAGE_CASE0(PSIZE
);
133 NINE_DECLUSAGE_CASEi(TEXCOORD
);
134 NINE_DECLUSAGE_CASEi(TANGENT
);
135 NINE_DECLUSAGE_CASEi(BINORMAL
);
136 NINE_DECLUSAGE_CASE0(TESSFACTOR
);
137 NINE_DECLUSAGE_CASEi(POSITIONT
);
138 NINE_DECLUSAGE_CASEi(COLOR
);
139 NINE_DECLUSAGE_CASE0(DEPTH
);
140 NINE_DECLUSAGE_CASE0(FOG
);
141 NINE_DECLUSAGE_CASE0(SAMPLE
);
143 assert(!"Invalid DECLUSAGE.");
144 return NINE_DECLUSAGE_NONE
;
148 static const char *nine_declusage_names
[] =
150 [NINE_DECLUSAGE_POSITION
] = "POSITION",
151 [NINE_DECLUSAGE_BLENDWEIGHT
] = "BLENDWEIGHT",
152 [NINE_DECLUSAGE_BLENDINDICES
] = "BLENDINDICES",
153 [NINE_DECLUSAGE_NORMAL
] = "NORMAL",
154 [NINE_DECLUSAGE_PSIZE
] = "PSIZE",
155 [NINE_DECLUSAGE_TEXCOORD
] = "TEXCOORD",
156 [NINE_DECLUSAGE_TANGENT
] = "TANGENT",
157 [NINE_DECLUSAGE_BINORMAL
] = "BINORMAL",
158 [NINE_DECLUSAGE_TESSFACTOR
] = "TESSFACTOR",
159 [NINE_DECLUSAGE_POSITIONT
] = "POSITIONT",
160 [NINE_DECLUSAGE_COLOR
] = "DIFFUSE",
161 [NINE_DECLUSAGE_DEPTH
] = "DEPTH",
162 [NINE_DECLUSAGE_FOG
] = "FOG",
163 [NINE_DECLUSAGE_NONE
] = "(NONE)",
165 static INLINE
const char *
166 nine_declusage_name(unsigned ndcl
)
168 return nine_declusage_names
[ndcl
% NINE_DECLUSAGE_COUNT
];
172 NineVertexDeclaration9_ctor( struct NineVertexDeclaration9
*This
,
173 struct NineUnknownParams
*pParams
,
174 const D3DVERTEXELEMENT9
*pElements
)
176 const D3DCAPS9
*caps
;
179 DBG("This=%p pParams=%p pElements=%p\n", This
, pParams
, pElements
);
181 HRESULT hr
= NineUnknown_ctor(&This
->base
, pParams
);
182 if (FAILED(hr
)) { return hr
; }
185 for (This
->nelems
= 0;
186 pElements
[This
->nelems
].Stream
!= 0xFF;
188 user_assert(pElements
[This
->nelems
].Type
!= D3DDECLTYPE_UNUSED
, E_FAIL
);
190 caps
= NineDevice9_GetCaps(This
->base
.device
);
191 user_assert(This
->nelems
<= caps
->MaxStreams
, D3DERR_INVALIDCALL
);
193 This
->decls
= CALLOC(This
->nelems
+1, sizeof(D3DVERTEXELEMENT9
));
194 This
->elems
= CALLOC(This
->nelems
, sizeof(struct pipe_vertex_element
));
195 This
->usage_map
= CALLOC(This
->nelems
, sizeof(uint16_t));
196 if (!This
->decls
|| !This
->elems
|| !This
->usage_map
) { return E_OUTOFMEMORY
; }
197 memcpy(This
->decls
, pElements
, sizeof(D3DVERTEXELEMENT9
)*(This
->nelems
+1));
199 for (i
= 0; i
< This
->nelems
; ++i
) {
200 uint16_t usage
= nine_d3d9_to_nine_declusage(This
->decls
[i
].Usage
,
201 This
->decls
[i
].UsageIndex
);
202 This
->usage_map
[i
] = usage
;
204 This
->elems
[i
].src_offset
= This
->decls
[i
].Offset
;
205 This
->elems
[i
].instance_divisor
= 0;
206 This
->elems
[i
].vertex_buffer_index
= This
->decls
[i
].Stream
;
207 This
->elems
[i
].src_format
= decltype_format(This
->decls
[i
].Type
);
208 /* XXX Remember Method (tesselation), Usage, UsageIndex */
210 DBG("VERTEXELEMENT[%u]: Stream=%u Offset=%u Type=%s DeclUsage=%s%d\n", i
,
211 This
->decls
[i
].Stream
,
212 This
->decls
[i
].Offset
,
213 util_format_name(This
->elems
[i
].src_format
),
214 nine_declusage_name(usage
),
215 usage
/ NINE_DECLUSAGE_COUNT
);
222 NineVertexDeclaration9_dtor( struct NineVertexDeclaration9
*This
)
226 FREE(This
->usage_map
);
228 NineUnknown_dtor(&This
->base
);
232 NineVertexDeclaration9_GetDeclaration( struct NineVertexDeclaration9
*This
,
233 D3DVERTEXELEMENT9
*pElement
,
237 user_assert(pNumElements
, D3DERR_INVALIDCALL
);
238 *pNumElements
= This
->nelems
+1;
241 if (pNumElements
) { *pNumElements
= This
->nelems
+1; }
242 memcpy(pElement
, This
->decls
, sizeof(D3DVERTEXELEMENT9
)*(This
->nelems
+1));
246 IDirect3DVertexDeclaration9Vtbl NineVertexDeclaration9_vtable
= {
247 (void *)NineUnknown_QueryInterface
,
248 (void *)NineUnknown_AddRef
,
249 (void *)NineUnknown_Release
,
250 (void *)NineUnknown_GetDevice
, /* actually part of VertexDecl9 iface */
251 (void *)NineVertexDeclaration9_GetDeclaration
254 static const GUID
*NineVertexDeclaration9_IIDs
[] = {
255 &IID_IDirect3DVertexDeclaration9
,
261 NineVertexDeclaration9_new( struct NineDevice9
*pDevice
,
262 const D3DVERTEXELEMENT9
*pElements
,
263 struct NineVertexDeclaration9
**ppOut
)
265 NINE_DEVICE_CHILD_NEW(VertexDeclaration9
, ppOut
, /* args */ pDevice
, pElements
);
269 NineVertexDeclaration9_new_from_fvf( struct NineDevice9
*pDevice
,
271 struct NineVertexDeclaration9
**ppOut
)
273 D3DVERTEXELEMENT9 elems
[16], decl_end
= D3DDECL_END();
274 unsigned texcount
, i
, betas
, nelems
= 0;
275 BYTE beta_index
= 0xFF;
277 switch (FVF
& D3DFVF_POSITION_MASK
) {
278 case D3DFVF_XYZ
: /* simple XYZ */
283 case D3DFVF_XYZB5
: /* XYZ with beta values */
284 elems
[nelems
].Type
= D3DDECLTYPE_FLOAT3
;
285 elems
[nelems
].Usage
= D3DDECLUSAGE_POSITION
;
286 elems
[nelems
].UsageIndex
= 0;
288 /* simple XYZ has no beta values. break. */
289 if ((FVF
& D3DFVF_POSITION_MASK
) == D3DFVF_XYZ
) { break; }
291 betas
= (((FVF
& D3DFVF_XYZB5
)-D3DFVF_XYZB1
)>>1)+1;
292 if (FVF
& D3DFVF_LASTBETA_D3DCOLOR
) {
293 beta_index
= D3DDECLTYPE_D3DCOLOR
;
294 } else if (FVF
& D3DFVF_LASTBETA_UBYTE4
) {
295 beta_index
= D3DDECLTYPE_UBYTE4
;
296 } else if ((FVF
& D3DFVF_XYZB5
) == D3DFVF_XYZB5
) {
297 beta_index
= D3DDECLTYPE_FLOAT1
;
299 if (beta_index
!= 0xFF) { --betas
; }
303 case 1: elems
[nelems
].Type
= D3DDECLTYPE_FLOAT1
; break;
304 case 2: elems
[nelems
].Type
= D3DDECLTYPE_FLOAT2
; break;
305 case 3: elems
[nelems
].Type
= D3DDECLTYPE_FLOAT3
; break;
306 case 4: elems
[nelems
].Type
= D3DDECLTYPE_FLOAT4
; break;
308 assert(!"Implementation error!");
310 elems
[nelems
].Usage
= D3DDECLUSAGE_BLENDWEIGHT
;
311 elems
[nelems
].UsageIndex
= 0;
315 if (beta_index
!= 0xFF) {
316 elems
[nelems
].Type
= beta_index
;
317 elems
[nelems
].Usage
= D3DDECLUSAGE_BLENDINDICES
;
318 elems
[nelems
].UsageIndex
= 0;
323 case D3DFVF_XYZW
: /* simple XYZW */
324 case D3DFVF_XYZRHW
: /* pretransformed XYZW */
325 elems
[nelems
].Type
= D3DDECLTYPE_FLOAT4
;
326 elems
[nelems
].Usage
=
327 ((FVF
& D3DFVF_POSITION_MASK
) == D3DFVF_XYZW
) ?
328 D3DDECLUSAGE_POSITION
: D3DDECLUSAGE_POSITIONT
;
329 elems
[nelems
].UsageIndex
= 0;
334 (void)user_error(!"Position doesn't match any known combination");
337 /* normals, psize and colors */
338 if (FVF
& D3DFVF_NORMAL
) {
339 elems
[nelems
].Type
= D3DDECLTYPE_FLOAT3
;
340 elems
[nelems
].Usage
= D3DDECLUSAGE_NORMAL
;
341 elems
[nelems
].UsageIndex
= 0;
344 if (FVF
& D3DFVF_PSIZE
) {
345 elems
[nelems
].Type
= D3DDECLTYPE_FLOAT1
;
346 elems
[nelems
].Usage
= D3DDECLUSAGE_PSIZE
;
347 elems
[nelems
].UsageIndex
= 0;
350 if (FVF
& D3DFVF_DIFFUSE
) {
351 elems
[nelems
].Type
= D3DDECLTYPE_D3DCOLOR
;
352 elems
[nelems
].Usage
= D3DDECLUSAGE_COLOR
;
353 elems
[nelems
].UsageIndex
= 0;
356 if (FVF
& D3DFVF_SPECULAR
) {
357 elems
[nelems
].Type
= D3DDECLTYPE_D3DCOLOR
;
358 elems
[nelems
].Usage
= D3DDECLUSAGE_COLOR
;
359 elems
[nelems
].UsageIndex
= 1;
364 texcount
= (FVF
& D3DFVF_TEXCOUNT_MASK
) >> D3DFVF_TEXCOUNT_SHIFT
;
365 if (user_error(texcount
<= 8)) { texcount
= 8; }
367 for (i
= 0; i
< texcount
; ++i
) {
368 switch ((FVF
>> (16+i
*2)) & 0x3) {
369 case D3DFVF_TEXTUREFORMAT1
:
370 elems
[nelems
].Type
= D3DDECLTYPE_FLOAT1
;
373 case D3DFVF_TEXTUREFORMAT2
:
374 elems
[nelems
].Type
= D3DDECLTYPE_FLOAT2
;
377 case D3DFVF_TEXTUREFORMAT3
:
378 elems
[nelems
].Type
= D3DDECLTYPE_FLOAT3
;
381 case D3DFVF_TEXTUREFORMAT4
:
382 elems
[nelems
].Type
= D3DDECLTYPE_FLOAT4
;
386 assert(!"Implementation error!");
388 elems
[nelems
].Usage
= D3DDECLUSAGE_TEXCOORD
;
389 elems
[nelems
].UsageIndex
= i
;
393 /* fill out remaining data */
394 for (i
= 0; i
< nelems
; ++i
) {
396 elems
[i
].Offset
= (i
== 0) ? 0 : (elems
[i
-1].Offset
+
397 decltype_size(elems
[i
-1].Type
));
398 elems
[i
].Method
= D3DDECLMETHOD_DEFAULT
;
400 elems
[nelems
++] = decl_end
;
402 NINE_DEVICE_CHILD_NEW(VertexDeclaration9
, ppOut
, /* args */ pDevice
, elems
);
405 /* ProcessVertices runs stream output into a temporary buffer to capture
407 * Now we have to convert them to the format and order set by the vertex
408 * declaration, for which we use u_translate.
409 * This is necessary if the vertex declaration contains elements using a
410 * non float32 format, because stream output only supports f32/u32/s32.
413 NineVertexDeclaration9_ConvertStreamOutput(
414 struct NineVertexDeclaration9
*This
,
415 struct NineVertexBuffer9
*pDstBuf
,
418 struct pipe_resource
*pSrcBuf
,
419 const struct pipe_stream_output_info
*so
)
421 struct pipe_context
*pipe
= This
->base
.device
->pipe
;
422 struct pipe_transfer
*transfer
= NULL
;
423 struct translate
*translate
;
424 struct translate_key transkey
;
431 DBG("This=%p pDstBuf=%p DestIndex=%u VertexCount=%u pSrcBuf=%p so=%p\n",
432 This
, pDstBuf
, DestIndex
, VertexCount
, pSrcBuf
, so
);
434 transkey
.output_stride
= 0;
435 for (i
= 0; i
< This
->nelems
; ++i
) {
436 enum pipe_format format
;
438 switch (so
->output
[i
].num_components
) {
439 case 1: format
= PIPE_FORMAT_R32_FLOAT
; break;
440 case 2: format
= PIPE_FORMAT_R32G32_FLOAT
; break;
441 case 3: format
= PIPE_FORMAT_R32G32B32_FLOAT
; break;
443 assert(so
->output
[i
].num_components
== 4);
444 format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
447 transkey
.element
[i
].type
= TRANSLATE_ELEMENT_NORMAL
;
448 transkey
.element
[i
].input_format
= format
;
449 transkey
.element
[i
].input_buffer
= 0;
450 transkey
.element
[i
].input_offset
= so
->output
[i
].dst_offset
* 4;
451 transkey
.element
[i
].instance_divisor
= 0;
453 transkey
.element
[i
].output_format
= This
->elems
[i
].src_format
;
454 transkey
.element
[i
].output_offset
= This
->elems
[i
].src_offset
;
455 transkey
.output_stride
+=
456 util_format_get_blocksize(This
->elems
[i
].src_format
);
458 assert(!(transkey
.output_stride
& 3));
460 transkey
.nr_elements
= This
->nelems
;
462 translate
= translate_create(&transkey
);
464 return E_OUTOFMEMORY
;
466 hr
= NineVertexBuffer9_Lock(pDstBuf
,
467 transkey
.output_stride
* DestIndex
,
468 transkey
.output_stride
* VertexCount
,
469 &dst_map
, D3DLOCK_DISCARD
);
473 src_map
= pipe
->transfer_map(pipe
, pSrcBuf
, 0, PIPE_TRANSFER_READ
, &box
,
476 hr
= D3DERR_DRIVERINTERNALERROR
;
479 translate
->set_buffer(translate
, 0, src_map
, so
->stride
[0], ~0);
481 translate
->run(translate
, 0, VertexCount
, 0, 0, dst_map
);
483 NineVertexBuffer9_Unlock(pDstBuf
);
486 pipe
->transfer_unmap(pipe
, transfer
);
487 translate
->release(translate
); /* TODO: cache these */