   Swirling data...
  I'm actually writing some routines for "swirling" data from one format to another and i'm trying to optimize the speed of the functions. (cause it will be used 24/25 times a second :D).I have two particular situation i'm trying to optimize1) I have an RGB24 image and i need to create an RGBA32 image (A will always be 255 aka FF)2) I have an RGB24 image and i need to create an BGRA32 image (A will always be 255 aka FF)Actually i'm working this way :1) Copy byte byte with pointers and add the alpha each 3 bytes.2) Copy byte byte with pointers moving the pointer to read the correct byte for each write.They work, but they are not very performant cause i'm not using the whole bus width. Someone has a better idea? (the language is not important, i just need the concepts)Thanks!!!   
Quote:Someone has a better idea? (the language is not important, i just need the concepts.If language is not important, then simply do it with shaders, or SIMD.;  The language is not important, but i need to be compatible with the whole world, using SIMD or Shaders is out of question cause this way I put limitations over who can use these routines.   ;Quote:Original post by feal87The language is not important, but i need to be compatible with the whole world, using SIMD or Shaders is out of question cause this way I put limitations over who can use these routines.To get maximum coverage then, the language will be C. It doesn't cover the whole world, but reasonable part of it.Quote:1) I have an RGB24 image and i need to create an RGBA32 image (A will always be 255 aka FF)void rgb2rgba(unsigned char dst[], unsigned char src[], int n) {  int i;  for (i = 0; i < n; i++) {    dst[4*i+0] = src[3*i+0];    dst[4*i+1] = src[3*i+1];    dst[4*i+2] = src[3*i+2];    dst[4*i+3] = 0xff;  }};Quote:2) I have an RGB24 image and i need to create an BGRA32 image (A will always be 255 aka FF)void rgb2bgra(unsigned char dst[], unsigned char src[], int n) {  int i;  for (i = 0; i < n; i++) {    dst[4*i+0] = src[3*i+2];    dst[4*i+1] = src[3*i+1];    dst[4*i+2] = src[3*i+0];    dst[4*i+3] = 0xff;  }};Those two algorithms are optimal in general case.Any other optimizations (sometimes by several factors) can be achieved by taking into consideration the exact compiler and exact target architecture, such as memory layout or CPU.On some architectures it may be beneficial to instead operate on 4 or 8-byte ints, or perhaps even larger types. On some architectures it may also be beneficial to unroll the loop. But this is all completely and entirely platform-specific. This is why such algorithms are usually implemented in reference form as above, and then optimized algorithms are provided for specific cases.Depending on compiler, parameter notation may matter, so may prefetching, additional compiler options such as _restrict may provide dramatic run-time decrease. It really is impossible to optimize without knowing exact details of compiler and platform, sometimes even revision of a CPU.And as always: profile, profile, profile.   ;  So basically the same as i've already done. (except that i need to read backward cause windows (and directshow) store the images with the rows inversed (first row is last row...) :|).For the moment I increased 4 times (from 4.5 ms to 1,2 ms for a 720x400 image (on a rather old pc)) the speed of the first case by changing the source from RGB24 to RGB32 (last 8 bytes always 255 as standard) and then using 32 bit int as vector of exchange. (this way you use a wider bus and the speed generally increase a lot. At least on Intel-Compatible CPUs :D)For the second case I think i'll remain as it is unless i find a way to tell DirectShow to send me images in BGR32/BGRA32 format. :( (if someone knows how (or if it is possible to do it, please tell :D))Thanks anyway for your support Antheus.   ;  Are you doing this at runtime, or load time?I would recommend doing the conversion on loading. You would have to do the conversion once, and it's not as time critical. Or, if space is an issue, cache the results, removing any unused conversions.The only time I can think that runtime conversion would be necessary is for video, but most video codecs output a 'normal' 16 or 24/32 bit format.   ;  i'm doing this at runtime, and yes its video playing.DirectShow allows RGB24, RGB32, RGBA32 etc...this goes fine with DirectX9 that supports all these formats, but DirectX10 allows only ARGB (basically BGRA cause they are inversed) that i don't find into the uuid list of DirectShow. There is a way to tell DirectShow to reformat the stream as ARGB? o.o   ;Quote:Original post by feal87I'm actually writing some routines for "swirling" data from one format to another and i'm trying to optimize the speed of the functions. (cause it will be used 24/25 times a second :D).The problem is that you want to do this every frame. It's a lot of work.If you're re-doing it for the same image, you've missed the point: you are supposed to do all the necessary work when the image is loaded, and keep the loaded image around.If you're doing it for dynamically generated content, then you still have a problem: you should already be generating the content in the desired output format.;Quote:Original post by ZahlmanQuote:Original post by feal87I'm actually writing some routines for "swirling" data from one format to another and i'm trying to optimize the speed of the functions. (cause it will be used 24/25 times a second :D).The problem is that you want to do this every frame. It's a lot of work.If you're re-doing it for the same image, you've missed the point: you are supposed to do all the necessary work when the image is loaded, and keep the loaded image around.If you're doing it for dynamically generated content, then you still have a problem: you should already be generating the content in the desired output format.if you read the other posts, you'll know the problem. I don't know if DirectShow can work with ARGB (BGRA inversed) cause inside the uuids I only found RGBA / RGB formats...While the swirling work fine, i would like to optimize reducing the work on the cpu....someone who knows directshow?   ;  Oh. I suppose you could try asking in DirectX and XNA, then. :)   