I have a compiled pixel shader 4.0 (I don’t have source code for that), with the following in the input signature:
// Name Index Mask Register
// TEXCOORD 4 xyz 4
// TEXCOORD 8 w 4
There’re are other input slots, I’ve only pasted the interesting lines of the signature.
As you see, the signature says both TEXCOORD4 and TEXCOORD8 input values go to v4 input register, the former one to xyz fields, the latter one to w field.
MSDN says the type of TEXCOORD[n] input is float4.
Which component of TEXCOORD8 input value goes to the v4.w field, x or w?
I don't think that mapping is specific to a particular component - the HLSL compiler is just attempting to map the inputs to a more efficient layout. I'm fairly certain the input signature for the shader is going to look something like:
float4 myPixelShader(float3 input_f3 : TEXCOORD4, float input_scalar : TEXCOORD8)
{
// ...
}
so if you're looking to create a vertex shader that outputs to this particular shader, you could just mirror that layout in your output, so something like:
struct MyInterpolants
{
float3 something : TEXCOORD4;
float something_scalar : TEXCOORD8;
}
MyInterpolants MyVertexShader(...)
{
MyInterpolants o;
float3 calc;
float4 some_var;
// Some code here that does something and fills elements of some_var and calc
o.something = calc;
o.something_scalar = some_var.z;
return o;
}
should map just fine. It doesn't really matter which element of a local variable is mapped to that output, as long as you're passing a scalar and the signatures between the pixel shader input and vertex shader outputs match. So you could pass x, y, z or w to that scalar output and it'll work out the same.
Related
I'm setting a render target that has two outputs, one of type rgba and other of type r8. I would like to know, inside the filament material definition file, how do I specify the outputs for each target? in opengl gl it would look like :
out vec4 accum;
out float reveal;
void material(inout MaterialInputs material) {
prepareMaterial(material);
// prepare material inputs
accum = material.baseColor;
reveal = material.baseColor.a;
}
What does float4 position [[position]]; do in the following snippet?
#include <metal_stdlib>
using namespace metal;
struct Vertex
{
float4 position [[position]];
float4 color;
};
vertex Vertex vertex_main(device Vertex *vertices [[buffer(0)]], uint vid [[vertex_id]])
{
return vertices[vid];
}
I am confused by the [[position]] part and similar usage in the function definition especially.
The Metal Shading Language is documented at https://developer.apple.com/metal/metal-shading-language-specification.pdf
In particular look at "Table 9" on page 68 of that document. There [[position]] is identified as an attribute qualifier for the return type of a Vertex Function. I assume that means that when your vertex shader returns the caller will use the values in that part of the struct to determine the positions of the vertices the shader would like to modify.
I don't have enough reputation to respond to your comment regarding the name of the brackets, but the [[]] brackets are attribute syntax taken from C++11.
Metal is based on C++ and the this is just the syntax of attributes in C++11. See this for more details about the grammar.
I used a map<CString, vector<double>> structure to store the mapping of file name to its HSV color histogram.And there are 100 elements in this map as a image DB.If now comes a image,and I have get the input image's histogram,how can I do the compare?
I know a method called "quadratic distance", but I do not understand it.
One simple method would be using a distance calculator like this:
double dist(vector<double> *histogram1, vector<double> *histogram2) {
double result = 0.0;
for (vector<double>::iterator val1=histogram1->begin(), val2=histogram2->begin();
val1<histogram1->end();
val1++, val2++) {
result += (*val1 - *val2) * (*val1 - *val2);
}
result = sqrt(result);
return result;
}
And then determine which histogram has the smallest distance. Please note that this is for
demonstration purposes only, you must add vector size checks etc.
From section 5.8 of The OpenGL® ES Shading Language (v1.00, r17) [PDF] (emphasis mine):
The assignment operator stores the value of the rvalue-expression into the l-value and returns an r-value with the type and precision of the lvalue-expression. The lvalue-expression and rvalue-expression must have the same type. All desired type-conversions must be specified explicitly via a constructor.
So it sounds like doing something like this would not be legal:
vec3 my_vec3 = vec3(1, 2, 3);
vec4 my_vec4 = my_vec3;
And to make it legal the second line would have to be something like:
vec4 my_vec4 = vec4(my_vec3, 1); // add 4th component
I assumed that glVertexAttribPointer had similar requirements. That is, if you were assigning to a vec4 that the size parameter would have to be equal to 4.
Then I came across the GLES20TriangleRenderer sample for Android. Some relevant snippets:
attribute vec4 aPosition;
maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false,
TRIANGLE_VERTICES_DATA_STRIDE_BYTES, mTriangleVertices);
So aPosition is a vec4, but the call to glVertexAttribPointer that's used to set it has a size of 3. Is this code correct, is GLES20TriangleRenderer relying on unspecified behavior, or is there something else I'm missing?
The size of the attribute data passed to the shader does not have to match the size of the attribute in that shader. You can pass 2 values (from glVertexAttribPointer) to an attribute defined as a vec4; the leftover two values are zero, except for the W component which is 1. And similarly, you can pass 4 values to a vec2 attribute; the extra values are discarded.
So you can mix and match vertex attributes with uploaded values all you want.
I am doing work on Opengl-es 2.0 in ubuntu 10.10 with pvrsdk .
In my code i am taking the vertices of triangle to render but when i have to use the Attribute parameter in vertex shader . how does it change
I see that in examples they are using myVertex . what is meant by that.
like this:
const char* pszVertShader = "\
attribute highp vec4 myVertex;\
uniform mediump mat4 projmatrix;\
invariant gl_Position;\
void main(void)\
{\
gl_Position = projmatrix * myVertex;\
}";
===============================render======================
glBindAttribLocation(uiProgramObject, VERTEX_ARRAY, "myVertex");
So i just wanna know that I am taking vertices from text file will it affect the myVertex attribute.
if you need another information I can provide you and I have already posted my whole code in previous question here
OpenGL 3/OpenGL-ES 2 are abandoned the concept of predefined vertex attributes "position", "normal", "texcoord", …, that are supplied through glVertexPointer, glNormalPointer, glTexCoordPointer, …
Instead in your shaders you introduce custom varyings/in attribute identifiers. Those identifiers are referred to by a so called attribute location, a numeric index. glBindAttribLocation allows one to assign attribute identifiers to specific locations. In the case of above code fragment there seems to exists a global constant VERTEX_ARRAY – introduced by the program code(!), i.e. not predefined by OpenGL or something like that – that is universally used for the myVertex varying/in attribute in the shader.
So that particular vertex attribute can be supplied with data by glVertexAttribPointer through a common token VERTEX_ARRAY in a statement similar to this:
glVertexAttribPointer(VERTEX_ARRAY, 3, GL_FLOAT, false, 0, isVBO ? (char*)vertex.offset : (char*)vertex.data + vertex.offset);
Of course the exact semantics of the glVertexAttribPointer calls depends on the particular program that does them.
Instead of a (global) constant VERTEX_ARRAY you could as well use a variable shader.attrib.vertex, which you set per shader using
shader.attrib.vertex = glGetAttribLocation(shader.program_object, "myVertex");
and use that variable in the calls to glVertexAttribPointer
glVertexAttribPointer(shader.attrib.vertex, …)