Difference between angled brackets and parentheses in HLSL texture samplers - directx

Is there any important difference between using parentheses and angled brackets for texture sampler parameters? I have used them interchangeably before without any different effect.
For instance
Sampler TexSceneSampler {
Texture = <TexScene>;
}
Versus
Sampler TexSceneSampler {
Texture = (TexScene);
}

The angular brackets is the correct format according to the docs. However, AFAIK, there are no differences between the 2. I'd stick to the angular though in case the compiler gets changed. Mind its a DX9 only thing so you'll probably be ok either way.

Related

AVX2 permute control bit

The permute command from AVX2 instructions needs a parameter from type imm8. This parameter controls how the permutation is performed. Unfortunately I do not understand how this imm8 parameter is "created". What value do I have to set or how can I determine what value I have to set for a specific permuation?
Example:
_mm256_permute_pd(vec2, 0x5);
Here the parameter 0x5 permutes the first and second double in vec2 and the third and fourth double in vec2. But how do I know that 0x5 does that?
It's 4x 1-bit indices that select one of the two elements from the corresponding lane of the source vector, for each destination element. Read the Operation section of the docs for the asm instruction: http://felixcloutier.com/x86/VPERMILPD.html.
Or look it up in Intel's intrinsics guide, which has similar pseudo-code that shows exactly how each bit selects the source for an element of the result.
It's not lane-crossing vpermpd, so it's not like the 2-bit indices that _MM_SHUFFLE is a helper macro for, so it's not quite like Convert _mm_shuffle_epi32 to C expression for the permutation?.

Is it possible to use Metal data types in Objective-C?

I want to use some of the vector data types defined in Metal (such as uint2, docs) in Objective-C for my iOS app. However, I haven't been able to find any information about whether or not this is possible.
Those types are in <simd/SIMD.h> for C and C++ (and by extension, for Objective-C and Objective-C++).
They're actually the same types, with the same data layout, and the same associated functions, as those that you use from a Metal shader. So using them in CPU-side code where you expect to interface with Metal is a great idea. For example, you can define your own struct for vertex shader input in a C++ header file, then import that header and use the same struct definition in both your CPU code and the shader.
Note that the names differ a bit: e.g. uint2 is vector_uint2 in C, but simd::uint2 in C++.

Strange way of declaring variables in hlsl

I found this example of implementing Phong lightning in hlsl. It is first snippet where I see that strange syntax where you declare variables in hlsl like here:
float3 materialEmissive : EMISSIVE;
float3 materialAmbient : AMBIENT;
In usual instead of EMISSIVE or AMBIENT I used to declare position in register like:
float3 materialEmissive : register(c0);
float3 materialAmbient : register(c1);
Why would I declare variables like in example from link? I checked DirectX documentation, but didn't find whether EMMISIVE or AMBIENT are some key words in hlsl.
In this case, EMISSIVE and AMBIENT are so called semantics. They describe what that variable should contain (and not where it is stored).
The shader compiler can access these semantics to create a handle for the variable. E.g. the Effect Framework and older versions of DirectX let you specify global variables by their semantic name. This decouples the actual shader implementation (i.e. the variable name) from its interface to the outside world (i.e. the semantics).

How do I read half precision floating point numbers from a Metal texture with Swift?

My Metal compute kernel writes to a texture with format MTLPixelFormat.RG16Float, half precision floating point numbers. This is because Metal does not support writing to 32 bits floating point textures.
I need to read these half precision numbers in my swift program? I have moved the texture into a Swift UInt8 array, but I cannot figure out how to convert the half precision floats into Swift floats.
Actually #Muzza's answer is not correct. You could have read them from a float16_t pointer and cast them to a normal float32_t. No need to use external libraries. Just import the arm_neon header.
EDIT: the below answer was written for Swift v1. Later versions of Swift added support via the float16_t type. Manual conversion may still be useful in some cases.
There is no built in method to interpret halfs as floats, you have to convert it yourself.
The half datatype is a floating point format following IEEE 754. You can read the values in your swift program using UInt16 and then convert from this to a float value.
I don't know of any conversion routines written in Swift, but here are two libraries written in C++ that could be converted easily enough:
http://mrob.com/pub/math/s10e5.h.txt
http://half.sourceforge.net/
In the first library it is the function operator float() that you need to convert, that takes the UInt16 member variable _h and outputs a float.

Matching two images using VideoGrabber with OpenCV

I' trying to match one image from VideoGrabber and another image that is on disk.
I'm following this tutorial.
The problem is that it uses the function imread, which takes a string (path of the file), incompatible with VideGrabber type.
How can I convert a VideoGrabber from openframeworks data type to opencv mat type?
Is this what you want?
ofVideoGrabber vidGrabber;
...
cv::Mat frame(vidGrabber.getHeight(), vidGrabber.getWidth(), CV_8UC3, vidGrabber.getPixels());
I am not sure what format uses to pack pixels, OpenCV uses BGR interleaved channels, you may have to swap pixels around.
Ok, it seems to be a specific question about openframworks. You can use the wonderful addon ofxCv fro Kyle, it is specifically for an alternative use of opencv library inside openframeworks.
in ofxCv you can find methods such as toCv for converting openframeworks types to opencv, and toOf, for the inverse process. Have a look, it is well documented, with a lot of examples, and well designed.
Not exactly sure what difficulty you have with string versus array char[]. Array of chars can be converted to string like this:
char myarray[ ] = "my_file_name";
string str(myarray);

Resources