Last active
August 29, 2015 14:22
-
-
Save lukaszmargielewski/6b64d06d2d106d110126 to your computer and use it in GitHub Desktop.
iOS Metal shader searching text naive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <metal_stdlib> | |
using namespace metal; | |
// Every kernel processes 1 row of the char table: | |
kernel void lmCharSearch( | |
const device char *charTable [[ buffer(0) ]], | |
constant uint &charsInRow [[ buffer(1) ]], | |
constant uint &rowCount [[ buffer(2) ]], | |
const device char *searchPhrase [[ buffer(3) ]], | |
constant uint &searchPhraseLenght [[ buffer(4) ]], | |
device bool *outMatchMasks [[ buffer(5) ]], | |
uint row [[ thread_position_in_grid ]]) | |
{ | |
if(row >= rowCount)return; | |
bool found = false; | |
char tableCharacter; | |
char searchedPhraseCharacter; | |
uint phraseMatchCount = 0; | |
uint indexRowStart = row * charsInRow; | |
uint indexRowEnd = indexRowStart + charsInRow; | |
// 48.3 % of the time spent in this line: !!! (even if it is outside the loop) | |
searchedPhraseCharacter = searchPhrase[phraseMatchCount]; | |
uint firstMatchIndex = 0; | |
for (uint i = indexRowStart; i < indexRowEnd; i++) { | |
// 42.3 % of the time spent in this line: !!! | |
tableCharacter = charTable[i]; | |
if (searchedPhraseCharacter == tableCharacter) { | |
if(phraseMatchCount == 0)firstMatchIndex = i; | |
phraseMatchCount++; | |
searchedPhraseCharacter = searchPhrase[phraseMatchCount]; | |
if (phraseMatchCount == searchPhraseLenght) { | |
found = true; | |
break; | |
} | |
}else{ | |
if(phraseMatchCount > 0){ | |
i = firstMatchIndex + 1; | |
phraseMatchCount = 0; | |
searchedPhraseCharacter = searchPhrase[phraseMatchCount]; | |
} | |
} | |
} | |
outMatchMasks[row] = found; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment