<div dir="ltr"><div>Hello,</div><div>I am trying to implement a 3D engine from scratch with XCB. To do this, I have to set the color of every individual pixel before drawing it. <br></div><div><br></div><div>I noticed that calling xcb_alloc_color and xcb_alloc_color_reply are bottlenecking the program and making it slow to draw individual triangles.</div><div><br></div><div>Below is how I am setting the color before drawing each pixel, straight from this <a href="https://www.x.org/releases/X11R7.7/doc/libxcb/tutorial/index.html#usecolor">tutorial</a>:</div><div><br></div><div>
<pre style="color:rgb(0,0,0);background:rgb(255,255,255) none repeat scroll 0% 0%"><span style="color:rgb(128,0,0);font-weight:bold">void</span> set_drawing_color<span style="color:rgb(128,128,48)">(</span>xcb_connection_t <span style="color:rgb(128,128,48)">*</span>c<span style="color:rgb(128,128,48)">,</span> xcb_colormap_t colormap<span style="color:rgb(128,128,48)">,</span> xcb_gcontext_t gcontext<span style="color:rgb(128,128,48)">,</span> <span style="color:rgb(128,0,0);font-weight:bold">int</span> r<span style="color:rgb(128,128,48)">,</span> <span style="color:rgb(128,0,0);font-weight:bold">int</span> g<span style="color:rgb(128,128,48)">,</span> <span style="color:rgb(128,0,0);font-weight:bold">int</span> b<span style="color:rgb(128,128,48)">)</span> <span style="color:rgb(128,0,128)">{</span>
xcb_alloc_color_reply_t <span style="color:rgb(128,128,48)">*</span>color_info<span style="color:rgb(128,0,128)">;</span>
uint32_t mask<span style="color:rgb(128,0,128)">;</span>
uint32_t value<span style="color:rgb(128,128,48)">[</span><span style="color:rgb(0,140,0)">3</span><span style="color:rgb(128,128,48)">]</span><span style="color:rgb(128,0,128)">;</span>
color_info <span style="color:rgb(128,128,48)">=</span> xcb_alloc_color_reply<span style="color:rgb(128,128,48)">(</span>c<span style="color:rgb(128,128,48)">,</span> xcb_alloc_color<span style="color:rgb(128,128,48)">(</span>c<span style="color:rgb(128,128,48)">,</span> colormap<span style="color:rgb(128,128,48)">,</span> r<span style="color:rgb(128,128,48)">,</span> g<span style="color:rgb(128,128,48)">,</span> b<span style="color:rgb(128,128,48)">)</span><span style="color:rgb(128,128,48)">,</span> <span style="color:rgb(125,0,69)">NULL</span><span style="color:rgb(128,128,48)">)</span><span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(128,0,0);font-weight:bold">if</span> <span style="color:rgb(128,128,48)">(</span><span style="color:rgb(128,128,48)">!</span>color_info<span style="color:rgb(128,128,48)">)</span>
<span style="color:rgb(128,0,0);font-weight:bold">return</span> <span style="color:rgb(0,140,0)">0</span><span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(96,48,0)">free</span><span style="color:rgb(128,128,48)">(</span>color_info<span style="color:rgb(128,128,48)">)</span><span style="color:rgb(128,0,128)">;</span>
mask <span style="color:rgb(128,128,48)">=</span> XCB_GC_FOREGROUND<span style="color:rgb(128,0,128)">;</span>
value<span style="color:rgb(128,128,48)">[</span><span style="color:rgb(0,140,0)">0</span><span style="color:rgb(128,128,48)">]</span> <span style="color:rgb(128,128,48)">=</span> color_info<span style="color:rgb(128,128,48)">-</span><span style="color:rgb(128,128,48)">></span>pixel<span style="color:rgb(128,0,128)">;</span>
xcb_change_gc<span style="color:rgb(128,128,48)">(</span>c<span style="color:rgb(128,128,48)">,</span> gcontext<span style="color:rgb(128,128,48)">,</span> mask<span style="color:rgb(128,128,48)">,</span> value<span style="color:rgb(128,128,48)">)</span><span style="color:rgb(128,0,128)">;</span>
<span style="color:rgb(128,0,128)">}</span><br><br></pre><pre style="color:rgb(0,0,0);background:rgb(255,255,255) none repeat scroll 0% 0%"><font face="arial,sans-serif">Is this an appropriate way to set the color? Is there a more efficient way to do so?<br><br></font></pre><pre style="color:rgb(0,0,0);background:rgb(255,255,255) none repeat scroll 0% 0%"><font face="arial,sans-serif">Slightly unrelated question: as someone completely new to Xorg and Linux graphics in general, <br>how do you deal with the lack of documentation for XCB?<br><br></font></pre><pre style="color:rgb(0,0,0);background:rgb(255,255,255) none repeat scroll 0% 0%"><font face="arial,sans-serif">Thanks,<br></font></pre><pre style="color:rgb(0,0,0);background:rgb(255,255,255) none repeat scroll 0% 0%"><font face="arial,sans-serif">Mark<br></font></pre>
</div><div><br></div><div><br></div></div>