Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://www2.units.it/~csia/calcolointensivo/tartaglia/intel/cce/intref_cls.pdf" rel="noreferrer">Use SSE</a> - start around page 131. </p> <p>The basic workflow</p> <ol> <li><p>Load 4 pixels from src (16 1 byte numbers) RGBA RGBA RGBA RGBA (streaming load)</p></li> <li><p>Load 4 more which you want to blend with srcbytetop RGBx RGBx RGBx RGBx </p></li> <li><p>Do some swizzling so that the A term in 1 fills every slot I.e </p> <p>xxxA xxxB xxxC xxxD -> AAAA BBBB CCCC DDDD </p> <p>In my solution below I opted instead to re-use your existing "maskcurrent" array but having alpha integrated into the "A" field of 1 will require less loads from memory and thus be faster. Swizzling in this case would probably be: And with mask to select A, B, C, D. Shift right 8, Or with origional, shift right 16, or again.</p></li> <li><p>Add the above to a vector that is all -255 in every slot </p></li> <li><p>Multiply 1 * 4 (source with 255-alpha) and 2 * 3 (result with alpha). </p> <p>You should be able to use the "multiply and discard bottom 8 bits" SSE2 instruction for this.</p></li> <li><p>add those two (4 and 5) together </p></li> <li><p>Store those somewhere else (if possible) or on top of your destination (if you must)</p></li> </ol> <p>Here is a starting point for you:</p> <pre><code> //Define your image with __declspec(align(16)) i.e char __declspec(align(16)) image[640*480] // so the first byte is aligned correctly for SIMD. // Stride must be a multiple of 16. for (int y = top ; y &lt; bottom; ++y) { BYTE* resultByte = GET_BYTE(resultBits, left, y, stride, bytepp); BYTE* srcByte = GET_BYTE(srcBits, left, y, stride, bytepp); BYTE* srcByteTop = GET_BYTE(srcBitsTop, left, y, stride, bytepp); BYTE* maskCurrent = GET_GREY(maskSrc, left, y, width); for (int x = left; x &lt; right; x += 4) { //If you can't align, use _mm_loadu_si128() // Step 1 __mm128i src = _mm_load_si128(reinterpret_cast&lt;__mm128i*&gt;(srcByte)) // Step 2 __mm128i srcTop = _mm_load_si128(reinterpret_cast&lt;__mm128i*&gt;(srcByteTop)) // Step 3 // Fill the 4 positions for the first pixel with maskCurrent[0], etc // Could do better with shifts and so on, but this is clear __mm128i mask = _mm_set_epi8(maskCurrent[0],maskCurrent[0],maskCurrent[0],maskCurrent[0], maskCurrent[1],maskCurrent[1],maskCurrent[1],maskCurrent[1], maskCurrent[2],maskCurrent[2],maskCurrent[2],maskCurrent[2], maskCurrent[3],maskCurrent[3],maskCurrent[3],maskCurrent[3], ) // step 4 __mm128i maskInv = _mm_subs_epu8(_mm_set1_epu8(255), mask) //Todo : Multiply, with saturate - find correct instructions for 4..6 //note you can use Multiply and add _mm_madd_epi16 alpha = *maskCurrent; red = (srcByteTop[R] * alpha + srcByte[R] * (255 - alpha)) / 255; green = (srcByteTop[G] * alpha + srcByte[G] * (255 - alpha)) / 255; blue = (srcByteTop[B] * alpha + srcByte[B] * (255 - alpha)) / 255; CLAMPTOBYTE(red); CLAMPTOBYTE(green); CLAMPTOBYTE(blue); resultByte[R] = red; resultByte[G] = green; resultByte[B] = blue; //---- // Step 7 - store result. //Store aligned if output is aligned on 16 byte boundrary _mm_store_si128(reinterpret_cast&lt;__mm128i*&gt;(resultByte), result) //Slow version if you can't guarantee alignment //_mm_storeu_si128(reinterpret_cast&lt;__mm128i*&gt;(resultByte), result) //Move pointers forward 4 places srcByte += bytepp * 4; srcByteTop += bytepp * 4; resultByte += bytepp * 4; maskCurrent += 4; } } </code></pre> <p>To find out which AMD processors will run this code (currently it is using SSE2 instructions) see <a href="http://en.wikipedia.org/wiki/List_of_AMD_Turion_microprocessors" rel="noreferrer">Wikipedia's List of AMD Turion microprocessors</a>. You could also look at other lists of processors on Wikipedia but my research shows that AMD cpus from around 4 years ago all support at least SSE2.</p> <p>You should expect a good SSE2 implimentation to run around 8-16 times faster than your current code. That is because we eliminate branches in the loop, process 4 pixels (or 12 channels) at once and improve cache performance by using streaming instructions. As an alternative to SSE, you could probably make your existing code run much faster by eliminating the if checks you are using for saturation. Beyond that I would need to run a profiler on your workload.</p> <p>Of course, the best solution is to use hardware support (i.e code your problem up in DirectX) and have it done on the video card.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload