I am trying to use spirv with OpenGL. Before in my shaders I had gl_VertexID to calculate the uv of a rectangle and now I replaced it with gl_VertexIndex.
If I use gl_VertexID the code works, if I use gl_VertexIndex in spirv with vulkan the code works, but if I use gl_VertexIndex with OpenGL the gl_VertexIndex is always 0.
Here is a test draw command I'm using:
glDrawArraysInstancedBaseInstance(GL_TRIANGLES, 0, 6, 1, 2);
Shouldn't the gl_VertexIndex go from 0 to 5?
There is no gl_VertexIndex in OpenGL, regardless of the shading language you use. When compiling GLSL into SPIR-V for OpenGL, you ought to have gotten a compile error.
That being said, there's no difference between the values of gl_VertexIndex and gl_VertexID (unlike gl_InstanceIndex vs. gl_InstanceID).
If using vulkan 1.2 :
Command Line:
$ ./glslangValidator.exe my_input.vert.glsl -S vert --target-env vulkan1.2 -o my_output.vert.spv
my_input.vert.glsl
#version 450
//: -------------------------------------------------------- ://
//: How to compile: ://
//: $ ./glslangValidator.exe MCV.vert.glsl ://
//: -S vert ://
//: --target-env vulkan1.2 ://
//: -o mcv.vert.spv ://
//: ://
//: Formatted for readability. Command should all be on ://
//: one line when given to console. ://
//: -------------------------------------------------------- ://
//: ://
//: Got Help From Vulkan Discord: ://
//: ://
//: Rodrigo#1643 : ://
//: ://
//: gl_VertexIndex is gl_VertexID ://
//: gl_BaseVertex in other words,gl_VertexID ://
//: does not include the base vertex,which was ://
//: confusing for some people,specially because ://
//: D3D always included the base vertex ://
//: ://
//: ://
//: ://
//: Implodee#0345 : ://
//: ://
//: In Vulkan dialect glsl there's no gl_VertexID ://
//: and gl_InstanceID they are replaced by ://
//: gl_VertexIndex and gl_InstanceIndex as seen here ://
//: SC[ IMPLODE_GL_VERTEX_INDEX_SEEN_HERE_PDF ] ://
//: SC[ IMPLODE_GL_VERTEX_INDEX_SEEN_HERE_TXT ] ://
//: https://raw.githubusercontent.com/KhronosGroup/GLSL ://
//: /master/extensions/khr/GL_KHR_vulkan_glsl.txt ://
//: ://
//: ://
//: Me : ://
//: ://
//: $ ./glslangValidator MCV.vert.glsl -S vert ://
//: MCV.vert.glsl ://
//: ERROR: 0:45: 'gl_InstanceIndex' : undeclared identifier ://
//: ERROR: 0:45: '[]' : scalar integer expression required ://
//: ERROR: 0:45: '' : compilation terminated ://
//: ERROR: 3 compilation errors. No code generated. ://
//: ://
//: ://
//: Implodee#0345 : ://
//: ://
//: call it with --target-env vulkan1.2 ://
//: (or your used vulkan version rather) ://
//: see if that works ://
//: ://
//: ://
//: #define V_I ( gl_VertexID + gl_BaseVertex ) //:WRONG
//: #define V_I ( gl_VertexIndex + gl_InstanceIndex ) //:1.2
//: -------------------------------------------------------- ://
#define V_I gl_VertexIndex
layout( location = 0 ) out vec3 fragColor ;
vec2 positions[3] = vec2[](
vec2( 0.0 , -0.5 )
, vec2( 0.5 , 0.5 )
, vec2(-0.5 , 0.5 )
);
vec3 colors[3] = vec3[](
vec3( 1.0 , 0.0 , 0.0 )
, vec3( 0.0 , 1.0 , 0.0 )
, vec3( 0.0 , 0.0 , 1.0 )
);
void main(){
gl_Position = vec4(
positions[ V_I ] //: XY ://
, 0.0 //: Z ://
, 1.0 //: W ://
);;
fragColor = colors[ V_I ];
}
#undef V_I
Original Source Code From:
https://vulkan-tutorial.com/Drawing_a_triangle/Graphics_pipeline_basics/Shader_modules
Related
I am trying to match all content between parentheses, including parentheses in a non-greedy way. There should be a space before and after the opening parentheses (or the start of a line before the opening parentheses) and a space before and after the closing parentheses. Take the following text:
( )
( This is a comment )
1 2 +
\ a
: square dup * ;
( foo bar
baz )
(quux)
( ( )
(
( )
The first line should be matched, the second line including its content should be matched, the second last line should not be matched (or raise an error) and the last line should be matched. The two lines foo bar baz should be matched, but (quux) should not as it doesn't contain a space before and after the parentheses. The line with the extra opening parentheses inside should be matched.
I tried a few conventional regexes for matching content between parentheses but without much success. The regex engine is that of Go's.
re := regexp.MustCompile(`(?s)\(( | .*? )\)`)
s = re.ReplaceAllString(s, "")
Playground: https://play.golang.org/p/t93tc_hWAG
Regular expressions "can't count" (that's over-simplified, but bear with me), so you can't match on an unbounded amount of parenthesis nesting. I guess you're mostly concerned about matching only a single level in this case, so you would need to use something like:
foo := regexp.MustCompile(`^ *\( ([^ ]| [^)]*? \)$`)
This does require the comment to be the very last thing on a line, so it may be better to add "match zero or more spaces" there. This does NOT match the string "( ( ) )" or try to cater for arbitrary nesting, as that's well outside the counting that regular expressions can do.
What they can do in terms of counting is "count a specific number of times", they can't "count how many blah, then make sure there's the same number of floobs" (that requires going from a regular expression to a context-free grammar).
Playground
Here is a way to match all the 3 lines in question:
(?m)^[\t\p{Zs}]*\([\pZs}\t](?:[^()\n]*[\pZs}\t])?\)[\pZs}\t]*$
See the Go regex demo at the new regex101.com
Details:
(?m) - multiline mode on
^ - due to the above, the start of a line
[\t\p{Zs}]* - 0+ horizontal whitespaces
\( - a (
[\pZs}\t] - exactly 1 horizontal whitespace
(?:[^()\n]*[\pZs}\t])? - an optional sequence matching:
[^()\n]* - a negated character class matching 0+ characters other than (, ) and a newline
[\pZs}\t] - horizontal whitespace
\) - a literal )
[\pZs}\t]* - 0+ horizontal whitespaces
$ - due to (?m), the end of a line.
Go playground demo:
package main
import (
"regexp"
"fmt"
)
func main() {
var re = regexp.MustCompile(`(?m)^[\t\p{Zs}]*\([\pZs}\t](?:[^()\n]*[\pZs}\t])?\)[\pZs}\t]*$`)
var str = ` ( )
( This is a comment )
1 2 +
\ a
: square dup * ;
( foo bar
baz )
(quux)
( ( )
(
( )`
for i, match := range re.FindAllString(str, -1) {
fmt.Println("'", match, "' (found at index", i, ")")
}
}
I am trying to create a regex for [lon,lat] coordinates.
The code first checks if the input starts with '['.
If it does we check the validity of the coordinates via a regex
/([\[][-+]?(180(\.0{1,15})?|((1[0-7]\d)|([1-9]?\d))(\.\d{1,15})?),[-+]?([1-8]?\d(\.\d{1,15})?|90(\.0{1,15})?)[\]][\;]?)+/gm
The regex tests for [lon,lat] with 15 decimals [+- 180degrees, +-90degrees]
it should match :
single coordinates :
[120,80];
[120,80]
multiple coordinates
[180,90];[180,67];
[180,90];[180,67]
with newlines
[123,34];[-32,21];
[12,-67]
it should not match:
semicolon separator missing - single
[25,67][76,23];
semicolon separator missing - multiple
[25,67]
[76,23][12,90];
I currently have problems with the ; between coordinates (see 4 & 5)
jsfiddle equivalent here : http://regex101.com/r/vQ4fE0/4
You can try with this (human readable) pattern:
$pattern = <<<'EOD'
~
(?(DEFINE)
(?<lon> [+-]?
(?:
180 (?:\.0{1,15})?
|
(?: 1(?:[0-7][0-9]?)? | [2-9][0-9]? | 0 )
(?:\.[0-9]{1,15})?
)
)
(?<lat> [+-]?
(?:
90 (?:\.0{1,15})?
|
(?: [1-8][0-9]? | 9)
(?:\.[0-9]{1,15})?
)
)
)
\A
\[ \g<lon> , \g<lat> ] (?: ; \n? \[ \g<lon> , \g<lat> ] )* ;?
\z
~x
EOD;
explanations:
When you have to deal with a long pattern inside which you have to repeat several time the same subpatterns, you can use several features to make it more readable.
The most well know is to use the free-spacing mode (the x modifier) that allows to indent has you want the pattern (all spaces are ignored) and eventually to add comments.
The second consists to define subpatterns in a definition section (?(DEFINE)...) in which you can define named subpatterns to be used later in the main pattern.
Since I don't want to repeat the large subpatterns that describes the longitude number and the latitude number, I have created in the definition section two named pattern "lon" and "lat". To use them in the main pattern, I only need to write \g<lon> and \g<lat>.
javascript version:
var lon_sp = '(?:[+-]?(?:180(?:\\.0{1,15})?|(?:1(?:[0-7][0-9]?)?|[2-9][0-9]?|0)(?:\\.[0-9]{1,15})?))';
var lat_sp = '(?:[+-]?(?:90(?:\\.0{1,15})?|(?:[1-8][0-9]?|9)(?:\\.[0-9]{1,15})?))';
var coo_sp = '\\[' + lon_sp + ',' + lat_sp + '\\]';
var regex = new RegExp('^' + coo_sp + '(?:;\\n?' + coo_sp + ')*;?$');
var coordinates = new Array('[120,80];',
'[120,80]',
'[180,90];[180,67];',
'[123,34];[-32,21];\n[12,-67]',
'[25,67][76,23];',
'[25,67]\n[76,23]');
for (var i = 0; i<coordinates.length; i++) {
console.log("\ntest "+(i+1)+": " + regex.test(coordinates[i]));
}
fiddle
Try this out:
^(\[([+-]?(?!(180\.|18[1-9]|19\d{1}))\d{1,3}(\.\d{1,15})?,[+-]?(?!(90\.|9[1-9]))\d{1,2}(\.\d{1,15})?(\];$|\]$|\];\[)){1,})
Demo: http://regex101.com/r/vQ4fE0/7
Explanation
^(\[
Must start with a bracket
[+-]?
May or may not contain +- in front of the number
(?!(180\.|18[1-9]|19\d{1}))
Should not contain 180., 181-189 nor 19x
\d{1,3}(\.\d{1,15})?
Otherwise, any number containing 1 or 3 digits, with or without decimals (up to 15) are allowed
(?!(90\.|9[1-9]))
The 90 check is similar put here we are not allowing 90. nor 91-99
\d{1,2}(\.\d{1,15})?
Otherwise, any number containing 1 or 2 digits, with or without decimals (up to 15) are allowed
(\];$|\]$|\];\[)
The ending of a bracket body must have a ; separating two bracket bodies, otherwise it must be the end of the line.
{1,}
The brackets can exist 1 or multiple times
Hope this was helpful.
This might work. Note that you have a lot of capture groups, none of which
will give you good information because of recursive quantifiers.
# /^(\[[-+]?(180(\.0{1,15})?|((1[0-7]\d)|([1-9]?\d))(\.\d{1,15})?),[-+]?([1-8]?\d(\.\d{1,15})?|90(\.0{1,15})?)\](?:;\n?|$))+$/
^
( # (1 start)
\[
[-+]?
( # (2 start)
180
( \. 0{1,15} )? # (3)
|
( # (4 start)
( 1 [0-7] \d ) # (5)
|
( [1-9]? \d ) # (6)
) # (4 end)
( \. \d{1,15} )? # (7)
) # (2 end)
,
[-+]?
( # (8 start)
[1-8]? \d
( \. \d{1,15} )? # (9)
|
90
( \. 0{1,15} )? # (10)
) # (8 end)
\]
(?: ; \n? | $ )
)+ # (1 end)
$
Try a function approach, where the function can do some of the splitting for you, as well as delegating the number comparisons away from the regex. I tested it here: http://repl.it/YyG/3
//represents regex necessary to capture one coordinate, which
// looks like 123 or 123.13532
// the decimal part is a non-capture group ?:
var oneCoord = '(-?\\d+(?:\\.\\d+)?)';
//console.log("oneCoord is: "+oneCoord+"\n");
//one coordinate pair is represented by [x,x]
// check start/end with ^, $
var coordPair = '^\\['+oneCoord+','+oneCoord+'\\]$';
//console.log("coordPair is: "+coordPair+"\n");
//the full regex string consists of one or more coordinate pairs,
// but we'll do the splitting in the function
var myRegex = new RegExp(coordPair);
//console.log("my regex is: "+myRegex+"\n");
function isPlusMinus180(x)
{
return -180.0<=x && x<=180.0;
}
function isPlusMinus90(y)
{
return -90.0<=y && y<=90.0;
}
function isValid(s)
{
//if there's a trailing semicolon, remove it
if(s.slice(-1)==';')
{
s = s.slice(0,-1);
}
//remove all newlines and split by semicolon
var all = s.replace(/\n/g,'').split(';');
//console.log(all);
for(var k=0; k<all.length; ++k)
{
var match = myRegex.exec(all[k]);
if(match===null)
return false;
console.log(" match[1]: "+match[1]);
console.log(" match[2]: "+match[2]);
//break out if one pair is bad
if(! (isPlusMinus180(match[1]) && isPlusMinus90(match[2])) )
{
console.log(" one of matches out of bounds");
return false;
}
}
return true;
}
var coords = new Array('[120,80];',
'[120.33,80]',
'[180,90];[180,67];',
'[123,34];[-32,21];\n[12,-67]',
'[25,67][76,23];',
'[25,67]\n[76,23]',
'[190,33.33]',
'[180.33,33]',
'[179.87,90]',
'[179.87,91]');
var s;
for (var i = 0; i<coords.length; i++) {
s = coords[i];
console.log((i+1)+". ==== testing "+s+" ====");
console.log(" isValid? => " + isValid(s));
}
Here is regular expression in urls.py
url(r'^company_data/(?:[A-Za-z]+)/((?:0?[1-9]|[12][0-9]|3[01])(?:0?[1-9]|1[012])(?:20)?[0-9]{2})*/((?:0?[1-9]|[12][0-9]|3[01])(?:0?[1-9]|1[012])(?:20)?[0-9]{2})*$', 'stats.views.second', name='home'),
my views.py
def second(request,comp_name,offset_min,offset_max=None):
I am calling in this way from browser /company_data/hello/24092014/25092014
Expecting in the below way
comp_name= "hello", offset_min="24092014",offset_max="25092014"
In reality it is
comp_name="24092014",offset_max="25092014"
What wrong did I do here??
Thanks in advance!!
enter code here
You're missing a capture group 1.
Edit: Also note that groups 2 and 3 should be done like below, unless I'm reading you
wrong and you intend to retrieve the last part of particular number groups.
# '^/?company_data/([A-Za-z]+)/((?:(?:0?[1-9]|[12][0-9]|3[01])(?:0?[1-9]|1[012])(?:20)?[0-9]{2})*)/((?:(?:0?[1-9]|[12][0-9]|3[01])(?:0?[1-9]|1[012])(?:20)?[0-9]{2})*)$'
^
/? company_data /
( [A-Za-z]+ ) # (1)
/
( # (2 start)
(?:
(?: 0? [1-9] | [12] [0-9] | 3 [01] )
(?: 0? [1-9] | 1 [012] )
(?: 20 )?
[0-9]{2}
)*
) # (2 end)
/
( # (3 start)
(?:
(?: 0? [1-9] | [12] [0-9] | 3 [01] )
(?: 0? [1-9] | 1 [012] )
(?: 20 )?
[0-9]{2}
)*
) # (3 end)
$
Output:
** Grp 0 - ( pos 0 , len 37 )
/company_data/hello/24092014/25092014
** Grp 1 - ( pos 14 , len 5 )
hello
** Grp 2 - ( pos 20 , len 8 )
24092014
** Grp 3 - ( pos 29 , len 8 )
25092014
Here is the regex:
ws(s)?://([0-9\.a-zA-Z\-_]+):([\d]+)([/([0-9\.a-zA-Z\-_]+)?
Here is a test pattern:
wss://beta5.max.com:18989/abcde.html
softlion.com likes it:
Test results
Match count: 1
Global matches:
wss://beta5.max.com:18989/abcde.html
Value of each capturing group:
0 1 2 3 4
wss://beta5.max.com:18989/abcde.html s beta5.max.com 18989 /abcde.html
scala does not:
val regex = """ws(s)?://([0-9\.a-zA-Z\-_]+):([\d]+)([/([0-9\.a-zA-Z\-_]+)?""".r
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 58
ws(s)?://([0-9\.a-zA-Z\-_]+):([\d]+)([/([0-9\.a-zA-Z\-_]+)?
My bad, I had an extra [ at the front of the last capturing group.
([/([0-9.a-zA-Z-_]+)?
Java allows intersections and all that, so error ..
ws
( s )?
://
( [0-9\.a-zA-Z\-_]+ )
:
( [\d]+ )
= ( <-- Unbalanced '('
= [ <-- Unbalanced '['
/
( [0-9\.a-zA-Z\-_]+ )?
With everybody else its no problem:
ws
( s )? # (1)
://
( [0-9\.a-zA-Z\-_]+ ) # (2)
:
( [\d]+ ) # (3)
( [/([0-9\.a-zA-Z\-_]+ )? # (4)
So, its good to see (know) the original regex is not what you thought it was.
I've got some problem to find the "options" in a jquery ui widget file with python regex. I read file with "os" class and put it in a var.
Problem is (I think) tabulation, space and endline caracter.
I try something like:
resp = re.findall( r'options\s?:\s?\{.×\n\t×},', myfile, flags=re.MULTILINE|re.DOTALL )
(the × symbol for multiplicator symbol)
to find the options{
kA: vA,
kB : vB,
...etc....
}
object in the widget.
But it doesn't work. It always put the rest of file at the end of result or find nothing (if i try to change the regex). If I put the last word of the object, it work!
But any other test fail.
Someone have an idea?!
thanks and, have a good new year!
This works:
/^\s*options{[^}]*}/mg
# explanation:
^ assert position at start of a line
\s* match any white space character [\r\n\t\f ]
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
options{ matches the characters options{ literally (case sensitive)
[^}]* match a single character not present in the list below
Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
} the literal character }
} matches the character } literally
Demo:
import re
txt='''\
nothing(blah)
options{ kA: vA, kB : vB, ...etc.... }
options{ kA: vA, kB : vB, ...etc.... }
blah blah
options{ kA: vA, kB : vB, ...etc.... } # tab'''
print(re.findall(r'^\s*(options{[^}]*})',txt, re.S | re.M))
# ['options{ kA: vA, kB : vB, ...etc.... }', 'options{ kA: vA, kB : vB, ...etc.... }', 'options{ kA: vA, kB : vB, ...etc.... }']
The more robust solution is to actually parse the file. The regex can be combined with something like pyparsing for a better solution:
import re
import pyparsing as pp
txt='''\
nothing(blah)
options{ kA: vA, kB : vB}
options{ kA: vA, kB : vB}
blah blah
options{ kA: vA, kB : vB } # tab
options{
kA: vA,
kB: vB,
kC : vC
}
'''
ident = pp.Word(pp.alphas+"_", pp.alphanums+"_")
comma = pp.Suppress(',')
pair=ident+pp.Suppress(':')+ident
pair_list=pp.OneOrMore(pair)+pp.ZeroOrMore(comma+pair)
options=(pp.Suppress('{')+
pair_list+
pp.Suppress('}'))
for om in (m.group(1) for m in re.finditer(r'^\s*options({[^}]*})',txt, re.S | re.M)):
res=options.parseString(om)
data=dict(res[i:i+2] for i in range(0,len(res),2))
print('"{}"=>{}==>{}'.format(om,res,data))
Prints:
"{ kA: vA, kB : vB}"=>['kA', 'vA', 'kB', 'vB']==>{'kB': 'vB', 'kA': 'vA'}
"{ kA: vA, kB : vB}"=>['kA', 'vA', 'kB', 'vB']==>{'kB': 'vB', 'kA': 'vA'}
"{ kA: vA, kB : vB }"=>['kA', 'vA', 'kB', 'vB']==>{'kB': 'vB', 'kA': 'vA'}
"{
kA: vA,
kB: vB,
kC : vC
}"=>['kA', 'vA', 'kB', 'vB', 'kC', 'vC']==>{'kC': 'vC', 'kB': 'vB', 'kA': 'vA'}
Properly parsing takes care of all the whitespace for you and validates in one step.
Based on your sample "options{ kA: vA, kB : vB, ...etc.... }", a rough regex is
# r'options\s*\{(?:[^:]*:[^,]*(?:,[^:]*:[^,]*)*[,\s]*)?\}'
options \s*
\{
(?:
[^:]* : [^,]*
(?: , [^:]* : [^,]* )*
[,\s]*
)?
\}